forked from all_user/stat_center
375 lines
9.4 KiB
Go
375 lines
9.4 KiB
Go
package advertisejob
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"stat/common/config"
|
|
"stat/common/log"
|
|
"stat/common/redis"
|
|
"stat/common/stdLog"
|
|
"stat/common/utils"
|
|
"stat/models/advertiseLocMod"
|
|
"stat/models/advertiseMod"
|
|
"stat/models/advertisestat"
|
|
"stat/models/appInfoMod"
|
|
"stat/models/applicationmod"
|
|
"stat/statg"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type TAdsHourCountMap map[int64]advertisestat.THourStat
|
|
|
|
// AdvertiseClickDayHourStat not include endHour self
|
|
func AdvertiseClickDayHourStat(set *redis.Hash, sumDate time.Time, endHour int) error {
|
|
data, err := set.NameValues()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
// app dict
|
|
filter := bson.M{}
|
|
appDbDict, err := appInfoMod.GetAllAppIdMap(context.Background(), filter)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
source := make(TAdsHourCountMap)
|
|
appDict := make(map[int64]int32)
|
|
for name, value := range data {
|
|
ss := strings.Split(name, redis.AdvertiseClickDayHashIdHourSep)
|
|
if len(ss) != 3 {
|
|
continue
|
|
}
|
|
appId, err := strconv.ParseInt(ss[0], 10, 32)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if appId <= 0 {
|
|
continue
|
|
}
|
|
if _, exist := appDbDict[int32(appId)]; !exist {
|
|
continue
|
|
}
|
|
adsId, err := strconv.ParseInt(ss[1], 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if adsId <= 0 {
|
|
continue
|
|
}
|
|
hour, err := strconv.Atoi(ss[2])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if hour >= endHour {
|
|
continue
|
|
}
|
|
appDict[adsId] = int32(appId)
|
|
stat := source[adsId]
|
|
if stat == nil {
|
|
stat = make(advertisestat.THourStat)
|
|
source[adsId] = 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 adsId, stat := range source {
|
|
now := time.Now().Local()
|
|
total := stat.Total()
|
|
update := &advertisestat.Update{
|
|
Total: &total,
|
|
HourData: &stat,
|
|
UpdatedAt: &now,
|
|
}
|
|
// 尝试更新文档
|
|
err = advertisestat.UpdateOneAdvertiseStat(ctx, bson.M{
|
|
advertisestat.FN.AdvertiseStat.AdsId: adsId,
|
|
advertisestat.FN.AdvertiseStat.SumData: sumDate,
|
|
}, bson.M{"$set": update})
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
// 不存在则插入
|
|
doc := &advertisestat.AdvertiseStat{
|
|
Id: primitive.NewObjectID(),
|
|
AdsId: adsId,
|
|
AppId: appDict[adsId],
|
|
SumDate: sumDate,
|
|
Total: stat.Total(),
|
|
HourData: stat,
|
|
UpdatedAt: now,
|
|
CreatedAt: now,
|
|
}
|
|
err = advertisestat.InsertOneAdvertiseStat(ctx, doc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StatAdvertiseHourStat() error {
|
|
now := time.Now().Local()
|
|
// 给今天的Redis集合设置过期时间
|
|
setToday := redis.GetAdvertiseClickDayHash(now)
|
|
if err := redis.StatRds.Expire(setToday.Key(), 4*24*time.Hour); err != nil {
|
|
log.Error("set redis set expire time failed", log.Any(setToday.Key(), err))
|
|
return err
|
|
}
|
|
// 今日统计
|
|
err := AdvertiseClickDayHourStat(setToday, utils.BeginWithTime(now), now.Hour())
|
|
if err != nil {
|
|
log.Error("AdvertiseClickDayHourStat Today Failed", log.Any(setToday.Key(), err))
|
|
return err
|
|
}
|
|
// 昨日统计
|
|
yesterday := now.Add(-24 * time.Hour)
|
|
setYesterday := redis.GetAdvertiseClickDayHash(yesterday)
|
|
if exists, err := redis.StatRds.Exists(setYesterday.Key()); err == nil && exists {
|
|
err = AdvertiseClickDayHourStat(setYesterday, utils.BeginWithTime(yesterday), 24)
|
|
if err != nil {
|
|
log.Error("AdvertiseClickDayHourStat Yesterday Failed", log.Any(setToday.Key(), err))
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type AdvertiseTemp struct {
|
|
Advertise advertiseMod.Advertise `bson:"advertise"` // 应用id
|
|
Loc advertiseLocMod.AdvertiseLocation `bson:"loc"` // 应用id
|
|
}
|
|
type AdvertiseMap struct {
|
|
AppId int32 `bson:"appId"` // 应用id
|
|
Name string `bson:"name"` // 应用name
|
|
List []AdvertiseTemp `bson:"list"` // 广告list
|
|
}
|
|
type ApplicationMap struct {
|
|
AppId int32 `bson:"appId"` // 所属产品appIdid
|
|
Name string `bson:"name"` // 所属产品name
|
|
List []ApplicationTemp `bson:"list"` // list
|
|
}
|
|
|
|
type ApplicationTemp struct {
|
|
Application applicationmod.Application `bson:"advertise"` // 应用信息
|
|
}
|
|
|
|
func AdvertiseExpireSend() error {
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
tomorrow := now.AddDate(0, 0, 1)
|
|
|
|
sort := bson.D{{Key: "appId", Value: 1}}
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
filter := bson.M{"isActive": true,
|
|
"endTime": bson.M{"$lte": tomorrow}}
|
|
list, serr := advertiseMod.FindAdvertiselist(ctx, filter, opts)
|
|
if serr != nil && serr.Code != stdLog.DataEmpty {
|
|
log.Error("AdvertiseExpireSend err", log.E(serr))
|
|
return errors.New(serr.Error())
|
|
}
|
|
|
|
if len(list) <= 0 {
|
|
return nil
|
|
}
|
|
|
|
// 获取app信息
|
|
appIds := make([]int32, 0)
|
|
for _, v := range list {
|
|
appIds = append(appIds, v.AppId)
|
|
}
|
|
|
|
appList, err := appInfoMod.GetAppInfosByAppIds(ctx, appIds)
|
|
if err != nil {
|
|
log.Error("AdvertiseExpireSend GetAppInfosByAppIds err", log.E(err))
|
|
return err
|
|
}
|
|
appMap := make(map[int32]appInfoMod.AppInfo)
|
|
for _, v := range appList {
|
|
appMap[v.AppId] = v
|
|
}
|
|
|
|
adsmap := make(map[int32]*AdvertiseMap)
|
|
for _, v := range list {
|
|
// 获取位置信息
|
|
adsLocInfo, err := advertiseLocMod.FindLocationByAppIdAndLocId(ctx, v.AppId, v.LocId)
|
|
if err != nil {
|
|
log.Error("AdvertiseExpireSend FindLocationByAppIdAndLocId err", log.E(err),
|
|
log.Any("appid", v.AppId), log.Any("locid", v.LocId))
|
|
continue
|
|
}
|
|
if info, ok := adsmap[v.AppId]; ok {
|
|
temp := AdvertiseTemp{
|
|
Advertise: v,
|
|
Loc: adsLocInfo,
|
|
}
|
|
info.List = append(info.List, temp)
|
|
} else {
|
|
ads := &AdvertiseMap{}
|
|
ads.List = make([]AdvertiseTemp, 0)
|
|
ads.AppId = v.AppId
|
|
|
|
if appInfo, ok := appMap[v.AppId]; ok {
|
|
ads.Name = appInfo.Name
|
|
}
|
|
|
|
temp := AdvertiseTemp{
|
|
Advertise: v,
|
|
Loc: adsLocInfo,
|
|
}
|
|
ads.List = append(ads.List, temp)
|
|
|
|
adsmap[ads.AppId] = ads
|
|
}
|
|
}
|
|
|
|
if statg.Bot != nil {
|
|
text := ""
|
|
for _, info := range adsmap {
|
|
text += "=======================================\n"
|
|
text += fmt.Sprintf("【%s】有广告已过期\n", info.Name)
|
|
for _, value := range info.List {
|
|
if value.Advertise.EndTime.After(now) {
|
|
text += fmt.Sprintf(" 【标题: %s】【位置: %s】将过期 【%s】 \n",
|
|
value.Advertise.Title, value.Loc.Name, value.Advertise.EndTime.Format("2006.01.02"))
|
|
} else {
|
|
text += fmt.Sprintf(" 【标题: %s】【位置: %s】已过期 【%s】 \n",
|
|
value.Advertise.Title, value.Loc.Name, value.Advertise.EndTime.Format("2006.01.02"))
|
|
}
|
|
}
|
|
}
|
|
|
|
if text != "" {
|
|
text += statg.Tgbotsend
|
|
statg.Bot.SendAdvsSendMessage(text)
|
|
}
|
|
} else {
|
|
log.Error("AdvertiseExpireSend statg.Bot nil")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 过期应用检测告警
|
|
func ApplicationExpireSend() error {
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
tomorrow := now.AddDate(0, 0, 1)
|
|
|
|
sort := bson.D{{Key: "endTime", Value: 1}}
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
filter := bson.M{"status": true,
|
|
"endTime": bson.M{"$lte": tomorrow}}
|
|
list, serr := applicationmod.FindManyApplication(ctx, filter, opts)
|
|
if serr != nil && serr.Code != stdLog.DataEmpty {
|
|
log.Error("AdvertiseExpireSend err", log.E(serr))
|
|
return errors.New(serr.Error())
|
|
}
|
|
|
|
if len(list) <= 0 {
|
|
return nil
|
|
}
|
|
|
|
// 获取app信息
|
|
appIds := make([]int32, 0)
|
|
for _, v := range list {
|
|
appIds = append(appIds, int32(v.ParentAppId))
|
|
}
|
|
|
|
appList, err := appInfoMod.GetAppInfosByAppIds(ctx, appIds)
|
|
if err != nil {
|
|
log.Error("AdvertiseExpireSend GetAppInfosByAppIds err", log.E(err))
|
|
return err
|
|
}
|
|
appMap := make(map[int32]appInfoMod.AppInfo)
|
|
for _, v := range appList {
|
|
appMap[v.AppId] = v
|
|
}
|
|
|
|
appsmap := make(map[int32]*ApplicationMap)
|
|
for _, v := range list {
|
|
if info, ok := appsmap[int32(v.ParentAppId)]; ok {
|
|
temp := ApplicationTemp{
|
|
Application: *v,
|
|
}
|
|
info.List = append(info.List, temp)
|
|
} else {
|
|
apps := &ApplicationMap{}
|
|
apps.List = make([]ApplicationTemp, 0)
|
|
apps.AppId = int32(v.ParentAppId)
|
|
|
|
if appInfo, ok := appMap[int32(v.ParentAppId)]; ok {
|
|
apps.Name = appInfo.Name
|
|
}
|
|
|
|
temp := ApplicationTemp{
|
|
Application: *v,
|
|
}
|
|
apps.List = append(apps.List, temp)
|
|
|
|
appsmap[apps.AppId] = apps
|
|
}
|
|
}
|
|
|
|
if statg.Bot != nil {
|
|
text := ""
|
|
for _, info := range appsmap {
|
|
text += "=======================================\n"
|
|
text += fmt.Sprintf("【%s】有应用过期告警\n", info.Name)
|
|
for _, value := range info.List {
|
|
if value.Application.EndTime.After(now) {
|
|
text += fmt.Sprintf(" 【应用名称: %s】【ID: %s】将过期 【%s】 \n",
|
|
value.Application.Name, value.Application.Id.Hex(), value.Application.EndTime.Format("2006.01.02"))
|
|
} else {
|
|
text += fmt.Sprintf(" 【应用名称: %s】【ID: %s】已过期 【%s】 \n",
|
|
value.Application.Name, value.Application.Id.Hex(), value.Application.EndTime.Format("2006.01.02"))
|
|
}
|
|
}
|
|
}
|
|
|
|
if text != "" {
|
|
text += statg.Tgbotsend
|
|
statg.Bot.SendAdvsSendMessage(text)
|
|
}
|
|
} else {
|
|
log.Error("ApplicationExpireSend statg.Bot nil")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func LoadTgbotsend() error {
|
|
conf := statg.GlobalConfig{}
|
|
err := config.LoadJSON("conf/server.json", &conf)
|
|
if err != nil {
|
|
log.Fatal("load配置失败", log.E(err))
|
|
return err
|
|
}
|
|
statg.Tgbotsend = conf.TgbotSend
|
|
fmt.Println(statg.Tgbotsend)
|
|
return nil
|
|
}
|