@@ -0,0 +1,130 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/timeutil/timerange"
|
||||
"91porn-server/models/l/payvidlgmod"
|
||||
"91porn-server/models/l/playlgmod"
|
||||
"91porn-server/models/s/statvidmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
type TimeRange = timerange.TimeRange
|
||||
|
||||
type VideoStatIncDoc = statvidmod.VideoStatIncDoc
|
||||
|
||||
type VideoStatSetDoc = statvidmod.VideoStatSetDoc
|
||||
|
||||
type VideoStatDoc = statvidmod.VideoStatDoc
|
||||
|
||||
type VideoStatDocMap = statvidmod.VideoStatDocMap //ObjectID->VideoStatDoc
|
||||
|
||||
// fillPayCoin
|
||||
func fillPayCoin(timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
incomeMap, err := payvidlgmod.PayCoinMapByTime(timeRange.Head, timeRange.Tail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docM := *docMap
|
||||
for vid, v := range incomeMap {
|
||||
if _, existed := docM[vid]; !existed {
|
||||
docM[vid] = &VideoStatDoc{}
|
||||
}
|
||||
val := v
|
||||
docM[vid].Income = &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fillPayCount
|
||||
func fillPayCount(timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
payCountMap, err := payvidlgmod.PayCountMapByTime(timeRange.Head, timeRange.Tail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docM := *docMap
|
||||
for vid, v := range payCountMap {
|
||||
if _, existed := docM[vid]; !existed {
|
||||
docM[vid] = &VideoStatDoc{}
|
||||
}
|
||||
val := v
|
||||
docM[vid].PayCount = &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fillPlayCount
|
||||
func fillPlayCount(timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
playCountMap, err := playlgmod.PlayCountMapByTime(timeRange.Head, timeRange.Tail, []int{0, 1, 2}) //0,免费 1付费 2.试看
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docM := *docMap
|
||||
for vid, v := range playCountMap {
|
||||
if _, existed := docM[vid]; !existed {
|
||||
docM[vid] = &VideoStatDoc{}
|
||||
}
|
||||
val := v
|
||||
docM[vid].PlayCount = &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fillPayPlayCount
|
||||
func fillPayPlayCount(timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
payPlayCountMap, err := playlgmod.PlayCountMapByTime(timeRange.Head, timeRange.Tail, []int{1}) //0,免费 1付费 2.试看
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docM := *docMap
|
||||
for vid, v := range payPlayCountMap {
|
||||
if _, existed := docM[vid]; !existed {
|
||||
docM[vid] = &VideoStatDoc{}
|
||||
}
|
||||
val := v
|
||||
docM[vid].PayPlayCount = &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fillTax(timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
payPlayCountMap, err := payvidlgmod.TaxAmountMapByTime(timeRange.Head, timeRange.Tail) //0,免费 1付费 2.试看
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docM := *docMap
|
||||
for vid, v := range payPlayCountMap {
|
||||
if _, existed := docM[vid]; !existed {
|
||||
docM[vid] = &VideoStatDoc{}
|
||||
}
|
||||
val := v
|
||||
docM[vid].Tax = &val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fillStatDocMap
|
||||
func fillStatDocMap(sumDate time.Time, timeRange TimeRange, docMap *VideoStatDocMap) error {
|
||||
//收入
|
||||
if err := fillPayCoin(timeRange, docMap); err != nil {
|
||||
return err
|
||||
}
|
||||
//购买次数
|
||||
if err := fillPayCount(timeRange, docMap); err != nil {
|
||||
return err
|
||||
}
|
||||
//视屏播放次数
|
||||
if err := fillPlayCount(timeRange, docMap); err != nil {
|
||||
return err
|
||||
}
|
||||
//付费视屏播放次数
|
||||
if err := fillPayPlayCount(timeRange, docMap); err != nil {
|
||||
return err
|
||||
}
|
||||
//税
|
||||
return fillTax(timeRange, docMap)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/timeutil/timerange"
|
||||
"91porn-server/models/s/statrecordmod"
|
||||
"91porn-server/models/s/statvidmod"
|
||||
"91porn-server/skd/skdg"
|
||||
)
|
||||
|
||||
func RunStat(defaultStatDate time.Time) {
|
||||
log.Info("video stat running...")
|
||||
defer log.Info("video stat finished...")
|
||||
now := time.Now()
|
||||
//从历史提交记录中获取上次记录时间
|
||||
lastStatAt, err := statrecordmod.LastRecordTime(statrecordmod.VideoStatJob, defaultStatDate) //第一次从默认记录时间开始统计
|
||||
if err != nil {
|
||||
log.Error("video stat RunVideoStat lastRecordTime faild!", log.E(err))
|
||||
return
|
||||
}
|
||||
//过去几天的数据按一日为单位统计,帮助数据恢复
|
||||
dayHead := timerange.LocDayRange(now).Head
|
||||
for dayHead.After(lastStatAt) {
|
||||
end := lastStatAt.AddDate(0, 0, 1)
|
||||
//防止时间超出范围
|
||||
if end.After(dayHead) {
|
||||
end = dayHead
|
||||
}
|
||||
subTimeRange := timerange.TimeRange{ //连续子切片
|
||||
Head: lastStatAt,
|
||||
Tail: end,
|
||||
}
|
||||
if err = statByTimeRange(subTimeRange); err != nil {
|
||||
log.Error("video RunStat statByTimeRange faild!", log.E(err))
|
||||
return
|
||||
}
|
||||
lastStatAt = end
|
||||
}
|
||||
//当天的数据按照5分钟统计
|
||||
recentMinute := timerange.RecentMinute(now, statrecordmod.FiveMinuteScale) //对齐本次统计时间 Minute % frequency == 0
|
||||
//拆分时间区间并依次提交,控制数据库读写压力
|
||||
for recentMinute.After(lastStatAt) {
|
||||
end := lastStatAt.Add(statrecordmod.OneMinuteScale * time.Minute)
|
||||
//防止时间超出范围
|
||||
if end.After(recentMinute) {
|
||||
end = recentMinute
|
||||
}
|
||||
subTimeRange := timerange.TimeRange{ //连续子切片
|
||||
Head: lastStatAt,
|
||||
Tail: end,
|
||||
}
|
||||
if err = statByTimeRange(subTimeRange); err != nil {
|
||||
log.Error("video RunStat statByTimeRange faild!", log.E(err))
|
||||
return
|
||||
}
|
||||
lastStatAt = end
|
||||
}
|
||||
}
|
||||
|
||||
func statByTimeRange(timeRange timerange.TimeRange) error {
|
||||
sumDate := timerange.LocDayRange(timeRange.Head).Head //获取此时的零日点
|
||||
//装配基础统计数据
|
||||
statDocMap := make(VideoStatDocMap)
|
||||
if err := fillStatDocMap(sumDate, timeRange, &statDocMap); err != nil {
|
||||
log.Error("video stat RunVideoStat fillStatDocMap faild!", log.E(err))
|
||||
return err
|
||||
}
|
||||
//提交渠道统计数据
|
||||
log.Info("videoStat committing...", log.Any("sumDate", sumDate), log.Any("location", sumDate.Location().String()))
|
||||
if err := commit(sumDate, timeRange.Tail, statDocMap); err != nil {
|
||||
log.Error("video stat RunVideoStat commit faild!", log.E(err))
|
||||
return err
|
||||
}
|
||||
log.Info("videoStat committed.", log.Any("sumDate", sumDate), log.Any("location", sumDate.Location().String()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// commit 提交统计数据
|
||||
func commit(sumDate time.Time, recordTime time.Time, docMap VideoStatDocMap) error {
|
||||
opt := (&db.TransOpts{}).SetReEntry(10)
|
||||
return skdg.StatDB.Trans(func(trans *db.MongoTool) error {
|
||||
if len(docMap) != 0 {
|
||||
if err := statvidmod.ChangeStatTrans(trans, sumDate, recordTime, docMap); err != nil {
|
||||
return fmt.Errorf("video stat commit ChangeStatTrans faild!, err:%+v\n", err)
|
||||
}
|
||||
}
|
||||
//提交本次修改记录
|
||||
if err := statrecordmod.UpsertOneTrans(trans, statrecordmod.VideoStatJob, recordTime); err != nil {
|
||||
return fmt.Errorf("video stat commit UpsertOneTrans faild!, err:%+v\n", err)
|
||||
}
|
||||
return nil
|
||||
}, opt)
|
||||
}
|
||||
Reference in New Issue
Block a user