From bb0688e3418f6c81849cda7dc981f6afcb820d7b Mon Sep 17 00:00:00 2001 From: hujiebin Date: Fri, 13 Sep 2024 15:36:44 +0800 Subject: [PATCH] 1 --- script/Makefile | 6 ++ script/analyze_hilo.go | 116 ++++++++++++++++++++++++++++ script/clear_redis.go | 12 +-- script/gift_time.go | 36 +++++++++ script/history_charge_with_agent.go | 104 +++++++++++++++++++++++++ script/user_wealth_high.go | 114 +++++++++++++++++++++++++++ 6 files changed, 382 insertions(+), 6 deletions(-) create mode 100644 script/analyze_hilo.go create mode 100644 script/gift_time.go create mode 100644 script/history_charge_with_agent.go create mode 100644 script/user_wealth_high.go diff --git a/script/Makefile b/script/Makefile index b383afa..6278eee 100644 --- a/script/Makefile +++ b/script/Makefile @@ -40,3 +40,9 @@ charge_history_sum: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o charge_history_sum charge_history_sum.go svip_history_top: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o svip_history_top svip_history_top.go +user_wealth_high: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o user_wealth_high user_wealth_high.go +history_charge_with_agent: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o history_charge_with_agent history_charge_with_agent.go +analyze_hilo: + go build -o analyze_hilo.exe analyze_hilo.go diff --git a/script/analyze_hilo.go b/script/analyze_hilo.go new file mode 100644 index 0000000..3a42f26 --- /dev/null +++ b/script/analyze_hilo.go @@ -0,0 +1,116 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "github.com/tealeg/xlsx" + "log" + "os" + "strings" +) + +// LogEntry 表示每行日志记录的结构 +type DataD struct { + File string `json:"file"` + Func string `json:"func"` + Level string `json:"level"` + Msg string `json:"msg"` + Time string `json:"time"` +} + +// LogEntry 表示日志记录的结构 +type LogEntry struct { + Content string `json:"__CONTENT__"` +} + +type RealData struct { + Uid string + Ip string + Lang string + Timezone string + Country string + CountryCode string + Imei string + Device string + Version string + Carrier string + Vpn string + Time string +} + +func main() { + // 打开文件 + file, err := os.Open("log.json") + if err != nil { + log.Fatalf("无法打开文件: %v", err) + } + defer file.Close() + var res []RealData + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + // 读取每一行 + line := scanner.Text() + + // 解析JSON + var entry LogEntry + err := json.Unmarshal([]byte(line), &entry) + if err != nil { + log.Printf("解析JSON失败: %v", err) + continue + } + var data DataD + json.Unmarshal([]byte(strings.Replace(entry.Content, "\\", "", -1)), &data) + // 打印解析后的结构 + realDatas := strings.Split(data.Msg, "--") + uid := strings.Split(realDatas[1], ":") + ip := strings.Split(realDatas[2], ":") + lang := strings.Split(realDatas[3], ":") + timezone := strings.Split(realDatas[4], ":") + country := strings.Split(realDatas[5], ":") + countryCode := strings.Split(realDatas[6], ":") + imei := strings.Split(realDatas[7], ":") + device := strings.Split(realDatas[8], ":") + version := strings.Split(realDatas[9], ":") + carrier := strings.Split(realDatas[10], ":") + vpn := strings.Split(realDatas[11], ":") + fmt.Printf("uid:%v,ip:%v,lang:%v,timezone:%v,country:%v,countryCode:%v,imei:%v,device:%v,version:%v,carrier:%v,vpn:%v,time:%v\n", + uid[1], ip[1], lang[1], timezone[1], country[1], countryCode[1], imei[1], device[1], version[1], carrier[1], vpn[1], data.Time) + res = append(res, RealData{ + Uid: uid[1], + Ip: ip[1], + Lang: lang[1], + Timezone: timezone[1], + Country: country[1], + CountryCode: countryCode[1], + Imei: imei[1], + Device: device[1], + Version: version[1], + Carrier: carrier[1], + Vpn: vpn[1], + Time: data.Time, + }) + } + + if err := scanner.Err(); err != nil { + log.Fatalf("读取文件时出错: %v", err) + } + excelFileName := fmt.Sprintf("./用户地区分析.xlsx") + xlFile := xlsx.NewFile() + sheet, err := xlFile.AddSheet("wealth") + if err != nil { + panic(err) + } + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value = + "uid", "ip", "lang", "timezone", "country", "countryCode", "imei", "device", "version", "carrier", "vpn", "time" + for _, d := range res { + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value = + d.Uid, d.Ip, d.Lang, d.Timezone, d.Country, d.CountryCode, d.Imei, d.Device, d.Version, d.Carrier, d.Vpn, d.Time + } + _ = xlFile.Save(excelFileName) +} diff --git a/script/clear_redis.go b/script/clear_redis.go index 87ada1c..2e32e5c 100644 --- a/script/clear_redis.go +++ b/script/clear_redis.go @@ -7,7 +7,7 @@ import ( "log" ) -const redisAddr = "172.28.16.31:6379" // 替换为你的Redis地址 +const redisAddr = "172.28.16.47:6379" // 替换为你的Redis地址 func main() { // 创建Redis客户端 @@ -25,17 +25,17 @@ func main() { }() // 获取所有以"user_qps_"为前缀的键 - keys, err := getKeysWithPrefix(client, "lobby:match:msg:") + keys, err := getKeysWithPrefix(client, "game:level:points:") if err != nil { log.Fatalf("Failed to get keys with prefix: %v", err) } println(len(keys)) // 谨慎打开-删除所有匹配的键 - err = deleteKeys(client, keys) - if err != nil { - log.Fatalf("Failed to delete keys: %v", err) - } + //err = deleteKeys(client, keys) + //if err != nil { + // log.Fatalf("Failed to delete keys: %v", err) + //} fmt.Println("Cleanup completed.") } diff --git a/script/gift_time.go b/script/gift_time.go new file mode 100644 index 0000000..96e29f1 --- /dev/null +++ b/script/gift_time.go @@ -0,0 +1,36 @@ +package main + +import ( + "git.hilo.cn/hilo-common/mylogrus" + "git.hilo.cn/hilo-common/script/mysql" + "time" +) + +type GiftOperate struct { + ResGiftId uint64 + GiftN uint64 + SendUserId uint64 + ReceiveUserId uint64 + SendUserDiamond uint64 + ReceiveUserDiamond uint64 + ReceiveUserBean uint64 + SceneUid string + BagId uint64 + ID uint64 `gorm:"primary_key"` + CreatedTime time.Time `gorm:"->"` + UpdatedTime time.Time `gorm:"->"` +} + +func main() { + var gift GiftOperate + if err := mysql.ProdReadOnlyDB.Table("gift_operate_2").Order("id ASC").First(&gift).Error; err != nil { + mylogrus.MyLog.Errorf("GiftOperateTable fail:%v", err) + panic("e") + } + println(time.Now().Sub(gift.CreatedTime).Hours()) + if !gift.CreatedTime.IsZero() && time.Now().Sub(gift.CreatedTime) >= time.Hour*24*90 && time.Now().Hour() == 10 { // newTable已经过去90天 + println(true) + } else { + println(false) + } +} diff --git a/script/history_charge_with_agent.go b/script/history_charge_with_agent.go new file mode 100644 index 0000000..11607de --- /dev/null +++ b/script/history_charge_with_agent.go @@ -0,0 +1,104 @@ +package main + +import ( + "fmt" + "git.hilo.cn/hilo-common/script/mysql" + "github.com/spf13/cast" + "github.com/tealeg/xlsx" + "gorm.io/gorm" + "time" +) + +type ChargeWithAgent struct { + UserId uint64 + Code string + Dollar int64 + Country string + Area string + LastDealerCode string +} + +func main() { + type ResCountry struct { + Name string + Area int + } + var area []ResCountry + if err := mysql.ProdReadOnlyDB.Table("res_country").Find(&area).Error; err != nil { + panic(err) + } + am := make(map[string]string) + for _, v := range area { + am[v.Name] = "非阿语" + if v.Area == 1 { + am[v.Name] = "阿语区" + } + } + sql := ` +select id as user_id,code ,SUM(dollar) as dollar ,country FROM ( +select u.id,u.code,SUM(price) as dollar,u.country FROM pay_order p, user u where u.id = p.user_id AND p.status = 2 AND type = 0 group by user_id UNION ALL +select u.id,u.code,SUM(dollar) as dollar,u.country FROM dealer_transfer_detail d, user u where u.id = d.receiver_id group by receiver_id UNION ALL +select u.id,u.code,SUM(dollar) as dollar,u.country FROM dealer_transfer_detail_pink d, user u where u.id = d.receiver_id group by receiver_id +) t +-- where country in (SELECT name FROM res_country where area = 1) +group by code order by dollar DESC; +` + var data []ChargeWithAgent + if err := mysql.ProdReadOnlyDB.Raw(sql).Scan(&data).Error; err != nil { + panic(err) + } + for i, d := range data { + data[i].LastDealerCode = getLastDealerCode(d.UserId) + data[i].Area = am[d.Country] + if len(data[i].LastDealerCode) > 0 { + } + println(i) + } + excelFileName := fmt.Sprintf("./历史累充带代理.xlsx") + xlFile := xlsx.NewFile() + sheet, err := xlFile.AddSheet("wealth") + if err != nil { + panic(err) + } + row := sheet.AddRow() + c1, c2, c3, c4, c5 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value = "靓号", "充值美分", "国家", "区域", "最近代理" + for _, d := range data { + row := sheet.AddRow() + c1, c2, c3, c4, c5 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value = d.Code, cast.ToString(d.Dollar), d.Country, d.Area, d.LastDealerCode + } + _ = xlFile.Save(excelFileName) +} + +func getLastDealerCode(userId uint64) string { + type Result struct { + Code string + CreateTime time.Time + } + sql := "SELECT u.code,dealer_id,receiver_id,dealer_transfer_detail.created_time FROM dealer_transfer_detail,user u where u.id = dealer_transfer_detail.dealer_id AND receiver_id = ? order by dealer_transfer_detail.id DESC limit 1" + var res Result + if err := mysql.ProdReadOnlyDB.Raw(sql, userId).Scan(&res).Error; err != nil { + if err != gorm.ErrRecordNotFound { + panic(err) + } + } + sql2 := "SELECT u.code,dealer_id,receiver_id,dealer_transfer_detail_pink.created_time FROM dealer_transfer_detail_pink,user u where u.id = dealer_transfer_detail_pink.dealer_id AND receiver_id = ? order by dealer_transfer_detail_pink.id DESC limit 1" + var res2 Result + if err := mysql.ProdReadOnlyDB.Raw(sql2, userId).Scan(&res2).Error; err != nil { + if err != gorm.ErrRecordNotFound { + panic(err) + } + } + if len(res.Code) > 0 && len(res2.Code) > 0 { + if res.CreateTime.After(res2.CreateTime) { + return res.Code + } else { + return res2.Code + } + } + if len(res.Code) > 0 { + return res.Code + } + return res2.Code +} diff --git a/script/user_wealth_high.go b/script/user_wealth_high.go new file mode 100644 index 0000000..99da4d8 --- /dev/null +++ b/script/user_wealth_high.go @@ -0,0 +1,114 @@ +package main + +import ( + "fmt" + "git.hilo.cn/hilo-common/script/mysql" + "github.com/spf13/cast" + "github.com/tealeg/xlsx" +) + +type UserWealthHigh struct { + UserId uint64 + Grade int + Code string + Country string + Area string + Charge int64 + SvipLevel int64 +} + +type ResArea struct { + Name string + Area int +} + +type UserSvip22 struct { + UserId uint64 + Level int64 +} + +func main() { + var users []UserWealthHigh + if err := mysql.ProdReadOnlyDB.Table("match_wealth_user_score").Joins("INNER JOIN user ON user.id = match_wealth_user_score.user_id").Where("grade >= 20"). + Select("user_id,grade,country,code").Find(&users).Error; err != nil { + panic(err) + } + var resAreas []ResArea + if err := mysql.ProdReadOnlyDB.Table("res_country").Find(&resAreas).Error; err != nil { + panic(err) + } + areaMap := make(map[string]string) + for _, v := range resAreas { + if v.Area == 1 { + areaMap[v.Name] = "阿语区" + } else { + areaMap[v.Name] = "非阿语" + } + } + var userIds []uint64 + for _, v := range users { + userIds = append(userIds, v.UserId) + } + var svips []UserSvip22 + if err := mysql.ProdReadOnlyDB.Table("user_svip").Where("user_id in ?", userIds).Find(&svips).Error; err != nil { + panic(err) + } + sm := make(map[uint64]int64) + for _, v := range svips { + sm[v.UserId] = v.Level + } + for i, u := range users { + users[i].Area = areaMap[u.Country] + users[i].SvipLevel = sm[u.UserId] + users[i].Charge = GetUserChargeMoneySum(u.UserId) + if i >= 2 { + //break + } + } + excelFileName := fmt.Sprintf("./财富等级用户.xlsx") + xlFile := xlsx.NewFile() + sheet, err := xlFile.AddSheet("wealth") + if err != nil { + panic(err) + } + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value = "id", "财富等级", "区域", "国家", "累计充值金额(美分)", "svip等级" + for _, d := range users { + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value = + cast.ToString(d.Code), cast.ToString(d.Grade), cast.ToString(d.Area), cast.ToString(d.Country), cast.ToString(d.Charge), cast.ToString(d.SvipLevel) + } + _ = xlFile.Save(excelFileName) +} + +// 获取用户累计充值 +func GetUserChargeMoneySum(userId uint64) int64 { + type R struct { + Money int64 + } + var money R + var total int64 + if err := mysql.ProdReadOnlyDB.Table("pay_order AS p"). + Where("p.status = 2 AND p.`type` = 0"). // type=0 就是用户给自己充值,status=2成功 + Where("p.user_id = ?", userId). + Select("SUM(p.price) as money").Scan(&money).Error; err != nil { + } + total += money.Money + var money2 R + if err := mysql.ProdReadOnlyDB.Table("dealer_transfer_detail AS t"). + Where("t.receiver_id = ?", userId). + Select("SUM(t.dollar) as money"). + Scan(&money2).Error; err != nil { + } + total += money2.Money + var money3 R + if err := mysql.ProdReadOnlyDB.Table("dealer_transfer_detail_pink AS t"). + Where("t.receiver_id = ?", userId). + Select("SUM(t.dollar) as money"). + Scan(&money3).Error; err != nil { + } + total += money3.Money + return total +} -- 2.22.0