forked from all_user/stat_center
Initial commit
This commit is contained in:
+158
@@ -0,0 +1,158 @@
|
||||
package skd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"stat/common/enum/consts"
|
||||
"stat/common/log"
|
||||
"stat/common/statutils"
|
||||
"stat/skd/job/advertisejob"
|
||||
applicationjob "stat/skd/job/application"
|
||||
"stat/skd/job/blocstatdaily"
|
||||
"stat/skd/job/blocstatdistrict"
|
||||
"stat/skd/job/limitstat"
|
||||
"stat/skd/job/statdaily"
|
||||
"stat/skd/job/statdailyhour"
|
||||
"stat/skd/job/statdistrict"
|
||||
"stat/skd/job/stathour"
|
||||
"stat/skd/job/statlandpage"
|
||||
"stat/statg"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
skdCron *cron.Cron
|
||||
)
|
||||
|
||||
func init() {
|
||||
skdCron = cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(&log.Logger{}))) // 秒级定时任务
|
||||
}
|
||||
|
||||
// Start 定时器任务启动
|
||||
func Start() {
|
||||
log.Info("skd start...")
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
if statg.Conf.RunMod != consts.RunModRelease {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取默认统计开始日期
|
||||
defaultStatDate, err := getDefaultStatDate()
|
||||
if err != nil {
|
||||
log.Error("defaultStatDate invalid", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
if statg.Conf.RunMod == consts.RunModRelease {
|
||||
// IP限制统计
|
||||
_, _ = skdCron.AddFunc("0 5 */1 * * ?", func() {
|
||||
statutils.ServerMon.Run(limitstat.IpAddressLimitStat)
|
||||
})
|
||||
// 广告点击统计
|
||||
_, _ = skdCron.AddFunc("0 5 */1 * * ?", func() {
|
||||
statutils.ServerMon.Run(advertisejob.StatAdvertiseHourStat)
|
||||
})
|
||||
// 落地页小时统计
|
||||
_, _ = skdCron.AddFunc("0 28 */1 * * ?", func() {
|
||||
defaultTime, _ := time.Parse(time.RFC3339, "2023-08-01T00:00:00+08:00")
|
||||
statutils.ServerMon.Run1(statlandpage.RunStatHour, defaultTime)
|
||||
})
|
||||
// 落地页每日统计
|
||||
_, _ = skdCron.AddFunc("0 40 0 * * ?", func() {
|
||||
defaultTime, _ := time.Parse(time.RFC3339, "2023-08-01T00:00:00+08:00")
|
||||
statutils.ServerMon.Run1(statlandpage.RunStatDaily, defaultTime)
|
||||
})
|
||||
// 日活统计,统计上一天日活,校正
|
||||
_, _ = skdCron.AddFunc("0 35 0 * * ?", func() {
|
||||
statutils.ServerMon.Run1(statdaily.RunStat, defaultStatDate)
|
||||
|
||||
if statg.Conf.ServerIndex.IsMadou() {
|
||||
statutils.ServerMon.Run1(blocstatdaily.RunStat, defaultStatDate)
|
||||
statutils.ServerMon.Run1(blocstatdistrict.RunStat, defaultStatDate)
|
||||
}
|
||||
})
|
||||
|
||||
// 日活统计,统计上一天日活,校正
|
||||
if statg.Conf.ServerIndex.IsMadou() {
|
||||
_, _ = skdCron.AddFunc("0 35 2 * * ?", func() {
|
||||
statutils.ServerMon.Run1(statdistrict.RunStat, defaultStatDate)
|
||||
})
|
||||
}
|
||||
|
||||
// 日常统计数据、每小时数据
|
||||
_, _ = skdCron.AddFunc("0 0/5 * * * ?", func() {
|
||||
statutils.ServerMon.Run(statdaily.RunStatToday)
|
||||
statutils.ServerMon.Run1(statdailyhour.RunStat, defaultStatDate)
|
||||
})
|
||||
|
||||
// 统计首页曲线图
|
||||
_, _ = skdCron.AddFunc("0 0/5 * * * ?", func() {
|
||||
statutils.ServerMon.Run1(stathour.RunStat, defaultStatDate)
|
||||
})
|
||||
|
||||
if statg.Conf.ServerIndex.IsMadou() {
|
||||
// 渠道结算
|
||||
// _, _ = skdCron.AddFunc("0 0/5 * * * ?", func() {
|
||||
// statutils.ServerMon.Run1(statminute.RunStat, defaultStatDate)
|
||||
// })
|
||||
}
|
||||
}
|
||||
|
||||
// 应用点击统计
|
||||
id, err := skdCron.AddFunc("0 5 */1 * * ?", func() {
|
||||
statutils.ServerMon.Run(applicationjob.StatAppHourClick)
|
||||
})
|
||||
|
||||
// 不在发送数据
|
||||
// if statg.Conf.ServerIndex == statg.ServerIndexBili {
|
||||
// // 充值
|
||||
// _, _ = skdCron.AddFunc("0 40 0 * * ?", func() {
|
||||
// // statutils.ServerMon.Run(applicationjob.RechargeDayXlsl)
|
||||
// })
|
||||
// // 充值
|
||||
// _, _ = skdCron.AddFunc("0 50 0 * * ?", func() {
|
||||
// // statutils.ServerMon.Run(applicationjob.RechargeMonthXlsl)
|
||||
// })
|
||||
// }
|
||||
|
||||
// 过期广告
|
||||
_, _ = skdCron.AddFunc("0 0 10 * * ?", func() {
|
||||
statutils.ServerMon.Run(advertisejob.AdvertiseExpireSend)
|
||||
})
|
||||
|
||||
// 过期应用检测告警
|
||||
_, _ = skdCron.AddFunc("0 1 10 * * ?", func() {
|
||||
statutils.ServerMon.Run(advertisejob.ApplicationExpireSend)
|
||||
})
|
||||
|
||||
// 广告配置
|
||||
skdCron.AddFunc("0 0/5 * * * ?", func() {
|
||||
statutils.ServerMon.Run(advertisejob.LoadTgbotsend)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Error("defaultStatDate invalid", log.E(err), log.Any("id", id))
|
||||
return
|
||||
}
|
||||
skdCron.Start()
|
||||
}
|
||||
|
||||
// Stop 定时器任务停止
|
||||
func Stop() {
|
||||
ctx := skdCron.Stop()
|
||||
<-ctx.Done()
|
||||
err := ctx.Err()
|
||||
if err != nil && err != context.Canceled {
|
||||
log.Error("skd job stop error", log.E(err))
|
||||
return
|
||||
}
|
||||
log.Info("skd jobs end!", log.Any("end at", time.Now()))
|
||||
}
|
||||
|
||||
func getDefaultStatDate() (time.Time, error) {
|
||||
return time.ParseInLocation("2006-01-02", "2021-03-01", time.Local)
|
||||
}
|
||||
Reference in New Issue
Block a user