@@ -0,0 +1,46 @@
|
||||
package vidtotal
|
||||
|
||||
import (
|
||||
"91porn-server/common/timeutil/timerange"
|
||||
"91porn-server/models/s/statvidmod"
|
||||
"91porn-server/models/s/statvidtotalmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
type TimeRange = timerange.TimeRange
|
||||
|
||||
type VideoTotalIncDoc = statvidtotalmod.IncDoc
|
||||
|
||||
type VideoTotalIncDocMap = statvidtotalmod.IncDocMap
|
||||
|
||||
type RecordAtGTEAndLTMatch = statvidmod.RecordAtGTEAndLTMatch
|
||||
|
||||
func getVideoTotalIncDocMap(timeRange TimeRange) (VideoTotalIncDocMap, error) {
|
||||
recordAtGTEAndLTMatch := RecordAtGTEAndLTMatch{GTE: &timeRange.Head, LT: &timeRange.Tail}
|
||||
groupList, err := statvidmod.SumByRecordAt(recordAtGTEAndLTMatch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := toVideoTotalIncDocMap(groupList)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func toVideoTotalIncDocMap(groupList []statvidmod.SumGroup) VideoTotalIncDocMap {
|
||||
m := make(VideoTotalIncDocMap, len(groupList))
|
||||
for _, v := range groupList {
|
||||
_, ok := m[v.Vid]
|
||||
if !ok {
|
||||
m[v.Vid] = &VideoTotalIncDoc{}
|
||||
}
|
||||
doc := m[v.Vid]
|
||||
doc.Income = v.Income
|
||||
doc.PayCount = v.PayCount
|
||||
doc.PlayCount = v.PlayCount
|
||||
doc.PayPlayCount = v.PayPlayCount
|
||||
doc.Tax = v.Tax
|
||||
}
|
||||
return m
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package vidtotal
|
||||
|
||||
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/statvidtotalmod"
|
||||
"91porn-server/skd/skdg"
|
||||
)
|
||||
|
||||
func RunStat(defaultStatDate time.Time) {
|
||||
log.Info("video total stat running...")
|
||||
defer log.Info("video total stat finished...")
|
||||
now := time.Now()
|
||||
//从历史提交记录中获取上次记录时间
|
||||
lastStatAt, err := statrecordmod.LastRecordTime(statrecordmod.VideoTotalStatJob, defaultStatDate) //第一次从默认记录时间开始统计
|
||||
if err != nil {
|
||||
log.Error("video total 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 total 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.FiveMinuteScale * time.Minute)
|
||||
//防止时间超出范围
|
||||
if end.After(recentMinute) {
|
||||
end = recentMinute
|
||||
}
|
||||
subTimeRange := timerange.TimeRange{ //连续子切片
|
||||
Head: lastStatAt,
|
||||
Tail: end,
|
||||
}
|
||||
if err = statByTimeRange(subTimeRange); err != nil {
|
||||
log.Error("video total RunStat statByTimeRange faild!", log.E(err))
|
||||
return
|
||||
}
|
||||
lastStatAt = end
|
||||
}
|
||||
}
|
||||
|
||||
func statByTimeRange(timeRange timerange.TimeRange) error {
|
||||
//装配基础统计数据
|
||||
statDocMap, err := getVideoTotalIncDocMap(timeRange)
|
||||
if err != nil {
|
||||
log.Error("video total stat RunVideoStat fillStatDocMap faild!", log.E(err))
|
||||
return err
|
||||
}
|
||||
//提交视屏总计数据
|
||||
log.Info("videoTotalStat committing...")
|
||||
if err = commit(timeRange.Tail, statDocMap); err != nil {
|
||||
log.Error("video total stat RunVideoStat commit faild!", log.E(err))
|
||||
return err
|
||||
}
|
||||
log.Info("videoTotalStat committed.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// commit 提交统计数据
|
||||
func commit(recordTime time.Time, docMap VideoTotalIncDocMap) error {
|
||||
opt := (&db.TransOpts{}).SetReEntry(10)
|
||||
return skdg.StatDB.Trans(func(trans *db.MongoTool) error {
|
||||
if len(docMap) != 0 {
|
||||
if err := statvidtotalmod.ChangeStatTrans(trans, recordTime, docMap); err != nil {
|
||||
return fmt.Errorf("video total stat commit ChangeStatTrans faild!, err:%+v\n", err)
|
||||
}
|
||||
}
|
||||
//提交本次修改记录
|
||||
if err := statrecordmod.UpsertOneTrans(trans, statrecordmod.VideoTotalStatJob, recordTime); err != nil {
|
||||
return fmt.Errorf("video total stat commit UpsertOneTrans faild!, err:%+v\n", err)
|
||||
}
|
||||
return nil
|
||||
}, opt)
|
||||
}
|
||||
Reference in New Issue
Block a user