123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package v1
- import (
- "context"
- "designs/config"
- "designs/global"
- "designs/model"
- "designs/response"
- "fmt"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/v2/bson"
- "strconv"
- "time"
- )
- type logData struct {
- LogTime time.Time `json:"logTime" gorm:"column:logTime;"`
- Type int `json:"Type"`
- }
- func calculateUserOnlineTime(logData []logData) int64 {
- var lastLog int64
- var isStart bool
- var onlineTimeTotal int64
- for k, v := range logData {
- logTime := v.LogTime.Unix()
- //如果跟上一次的记录隔的时间超过6分钟,就认为是之前断线了,属于无效数据
- if k > 0 && logData[k-1].LogTime.Unix()+60*6 < logTime {
- isStart = false
- continue
- }
- if v.Type == 1 && isStart == false {
- isStart = true
- lastLog = v.LogTime.Unix()
- continue
- }
- if v.Type == 1 && isStart == true {
- //logTime := v.LogTime.Unix()
- onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
- lastLog = logTime
- //如果下一次的心跳,间隔时间大于6分钟了,就可以认为,这次就算是结束了
- if k+1 == len(logData) || logData[k+1].LogTime.Unix() > logTime+60*6 {
- isStart = false
- }
- continue
- }
- if v.Type == 2 && isStart == true {
- //logTime := v.LogTime.Unix()
- onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
- isStart = false
- continue
- }
- }
- return onlineTimeTotal
- }
- func SetUserBehaviorBak(c *gin.Context) {
- //读取出来user数据
- var users []model.User
- global.App.DB.Table("user").
- Where("pf", "=", "tt").
- Where("gid", "=", "linkup").
- Where("createdAt", ">=", "2024-11-12").
- Where("createdAt", "<=", "2024-11-13").Scan(&users)
- collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
- for _, user := range users {
- //查询出reids中的openId
- userIdKeys := fmt.Sprintf("%s:%s:%s:%v", user.Gid, user.Pf, config.Get("app.user_table_id"), strconv.Itoa(user.UserId))
- userIdData, _ := global.App.Redis.HGetAll(context.Background(), userIdKeys).Result()
- openId := userIdData["openId"]
- //openId = strconv.Itoa(user.UserId)
- //查询出在线时长数据
- var LogData []logData
- global.App.DB.Table("user_online").
- Where("pf", user.Pf).
- Where("gid", user.Gid).
- Where("userId", user.UserId).Scan(&LogData)
- duration := calculateUserOnlineTime(LogData)
- //查询出看广告数据
- var req_count int64
- var exp_count int64
- global.App.DB.Table("user_see_ads").
- Where("pf", user.Pf).
- Where("gid", user.Gid).
- Where("userId", user.UserId).Count(&req_count)
- global.App.DB.Table("user_see_ads").
- Where("pf", user.Pf).
- Where("gid", user.Gid).
- Where("adState", 2).
- Where("userId", user.UserId).Count(&exp_count)
- //查询出启动次数
- var start_num int64
- global.App.DB.Table("user_login").
- Where("pf", user.Pf).
- Where("gid", user.Gid).
- Where("userId", user.UserId).Count(&start_num)
- //存入mongo
- adData := struct {
- Id string `bson:"_id" json:"_id"`
- UserId string `bson:"userId" json:"userId"`
- Aid int64 `json:"aid" bson:"aid"` //广告的推广计划id,即广告ID,广告平台配置落地页参数
- Cid string `json:"cid" bson:"cid"` //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
- Pid int64 `json:"pid" bson:"pid"` //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
- CreateTime int64 `bson:"create_time"` //当前计划的创建时间
- StartNum int `bson:"startNum" json:"startNum"` //启动次数
- Revenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
- Duration int64 `bson:"duration" json:"duration"` //当日在线时长
- ReqCount int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
- ExpCount int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
- }{
- UserId: user.Gid + "|" + user.Pf + "|" + openId,
- Id: user.Gid + "|" + user.Pf + "|" + openId + "|" + "7436301890621997000",
- Aid: 7436301890621997000,
- Pid: 0,
- Cid: "",
- Duration: duration,
- StartNum: int(start_num),
- Revenue: 0,
- ReqCount: int(req_count),
- ExpCount: int(exp_count),
- CreateTime: user.CreatedAt.Unix(),
- }
- collection.InsertOne(context.Background(), adData)
- fmt.Println(user.Gid+"|"+user.Pf+"|"+openId, "数据写入完成\n")
- }
- }
- func DeleteUserBehaviorBak(c *gin.Context) {
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- filter := bson.M{"create_time": bson.M{"$gt": 1}}
- collection.DeleteMany(context.Background(), filter)
- response.Success(c, gin.H{})
- }
|