forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
package applicationjob
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stat/common/log"
|
||||
"stat/common/redis"
|
||||
"stat/common/stdLog"
|
||||
"stat/common/utils"
|
||||
"stat/models/applicationmod"
|
||||
"stat/models/applicationstat"
|
||||
"stat/service/innerserverser"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type TAppHourCountMap map[string]applicationstat.THourStat
|
||||
|
||||
// AppClickDayHourStat not include endHour self
|
||||
func AppClickDayHourStat(set *redis.Hash, sumDate time.Time, endHour int) error {
|
||||
data, err := set.NameValues()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
source := make(TAppHourCountMap)
|
||||
for name, value := range data {
|
||||
ss := strings.Split(name, redis.AppClickDaySetIdHourSep)
|
||||
if len(ss) != 2 {
|
||||
continue
|
||||
}
|
||||
id := ss[0]
|
||||
if len(id) != 24 {
|
||||
continue
|
||||
}
|
||||
hour, err := strconv.Atoi(ss[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if hour >= endHour {
|
||||
continue
|
||||
}
|
||||
stat := source[id]
|
||||
if stat == nil {
|
||||
stat = make(applicationstat.THourStat)
|
||||
source[id] = stat
|
||||
for h := 0; h < endHour; h++ {
|
||||
stat[h] = 0
|
||||
}
|
||||
}
|
||||
if count, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
stat[hour] = count
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
for id, stat := range source {
|
||||
/*appidslice := strings.Split(id, "_")
|
||||
if len(appidslice) != 2 {
|
||||
log.Error("AppClickDayHourStat", log.Any(id, stat))
|
||||
continue
|
||||
}
|
||||
appId, err := strconv.Atoi(appidslice[0])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
parentAppId, err := strconv.Atoi(appidslice[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
*/
|
||||
appId, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
now := time.Now().Local()
|
||||
total := stat.Total()
|
||||
update := &applicationstat.Update{
|
||||
Total: &total,
|
||||
HourData: &stat,
|
||||
UpdatedAt: &now,
|
||||
}
|
||||
// 尝试更新文档
|
||||
sErr := applicationstat.UpdateOneApplicationStat(ctx, bson.M{
|
||||
"applicationDbId": appId,
|
||||
applicationstat.FN.ApplicationStat.SumData: sumDate,
|
||||
}, bson.M{"$set": update})
|
||||
if sErr != nil {
|
||||
if sErr.Code == stdLog.DataNotExist {
|
||||
// 不存在则插入
|
||||
doc := &applicationstat.ApplicationStat{
|
||||
Id: primitive.NewObjectID(),
|
||||
ApplicationDbId: appId,
|
||||
SumDate: sumDate,
|
||||
Total: stat.Total(),
|
||||
HourData: stat,
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
sErr = applicationstat.InsertOneApplicationStat(ctx, doc)
|
||||
if sErr != nil {
|
||||
return sErr
|
||||
}
|
||||
} else {
|
||||
return sErr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func StatAppHourClick() error {
|
||||
log.Info("configser:StatAppHourClick begin")
|
||||
|
||||
defer log.Info("configser:StatAppHourClick end")
|
||||
|
||||
now := time.Now().Local()
|
||||
// 给今天的Redis集合设置过期时间
|
||||
setToday := redis.GetAppClickDaySet(now)
|
||||
if err := redis.StatRds.Expire(setToday.Key(), 2*24*time.Hour); err != nil {
|
||||
log.Error("set redis set expire time failed", log.Any(setToday.Key(), err))
|
||||
return err
|
||||
}
|
||||
// 今日统计
|
||||
err := AppClickDayHourStat(setToday, utils.BeginWithTime(now), now.Hour())
|
||||
if err != nil {
|
||||
log.Error("AppClickDayHourStat Today Failed", log.Any(setToday.Key(), err))
|
||||
return err
|
||||
}
|
||||
// 昨日统计
|
||||
yesterday := now.Add(-24 * time.Hour)
|
||||
setYesterday := redis.GetAppClickDaySet(yesterday)
|
||||
if exists, err := redis.StatRds.Exists(setYesterday.Key()); err == nil && exists {
|
||||
err = AppClickDayHourStat(setYesterday, utils.BeginWithTime(yesterday), 24)
|
||||
if err != nil {
|
||||
log.Error("AppClickDayHourStat Yesterday Failed", log.Any(setToday.Key(), err))
|
||||
return err
|
||||
}
|
||||
// 删除昨日统计集合
|
||||
redis.StatRds.Del(setYesterday.Key())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RechargeDayXlsl() error {
|
||||
err := innerserverser.InnerServerDailyStat(context.Background())
|
||||
|
||||
if err != nil {
|
||||
log.Error("RechargeDayXlsl", log.Any("err", err))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RechargeMonthXlsl() error {
|
||||
err := innerserverser.InnerServerMonthStat2(context.Background(), nil, nil, nil)
|
||||
|
||||
if err != nil {
|
||||
log.Error("RechargeMonthXlsl", log.Any("err", err))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CloseExpiredApplications() error {
|
||||
log.Info("applicationjob:CloseExpiredApplications begin")
|
||||
|
||||
defer log.Info("applicationjob:CloseExpiredApplications end")
|
||||
|
||||
now := time.Now().Local()
|
||||
filter := bson.M{
|
||||
"status": true,
|
||||
"endTime": bson.M{"$lt": now},
|
||||
}
|
||||
|
||||
update := bson.M{"$set": bson.M{"status": false}}
|
||||
serr := applicationmod.UpdateManyApplication(context.Background(), filter, update)
|
||||
if serr != nil {
|
||||
log.Error("application FindAdsByAppId Find err", log.E(serr))
|
||||
return serr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user