增加玩家在线数据监控

This commit is contained in:
hahwu 2025-12-12 15:18:46 +08:00
parent 92cd035e76
commit 581a0cf61a
2 changed files with 61 additions and 4 deletions

View File

@ -61,12 +61,10 @@ func SendFeishuMsg(msg string) error {
func SendNoticeMsg(AppName, ServerName, notice string) error {
str := fmt.Sprintf(`
## **端口扫描异常**
## **游戏数据监控异常**
- 项目名称: %s
- 服务器名称: %s
- 监控项名称: %s
- 异常信息: %s
<at id="all"></at>
`, AppName, ServerName, notice)
// 创建请求体

59
monitor/Monitor.go Normal file
View File

@ -0,0 +1,59 @@
package monitor
import (
"backend/feishu"
"backend/util"
"fmt"
"log"
"time"
)
func UserAliveMonitor(AppId int) {
AppConfig, _ := util.GetAppConfig(AppId)
Db := util.MPool.GetTopicDB(AppConfig.Topic)
for {
//now := time.Now()
//next := now.Truncate(time.Hour).Add(time.Hour)
// time.Sleep(time.Until(next))
for {
// time window: the hour that just finished
endT := time.Now().Truncate(time.Hour)
startT := endT.Add(-time.Hour)
yStartT := startT.Add(-24 * time.Hour)
yEndT := endT.Add(-24 * time.Hour)
start := startT.Unix()
end := endT.Unix()
yStart := yStartT.Unix()
yEnd := yEndT.Unix()
var curCount, yCount int64
if err := Db.QueryRow("SELECT COUNT(DISTINCT Uid) FROM log_event WHERE Timestamp>=? AND Timestamp<?", start, end).Scan(&curCount); err != nil {
log.Printf("monitor current count query error: %v", err)
time.Sleep(time.Hour)
continue
}
if err := Db.QueryRow("SELECT COUNT(DISTINCT Uid) FROM log_event WHERE Timestamp>=? AND Timestamp<?", yStart, yEnd).Scan(&yCount); err != nil {
log.Printf("monitor yesterday count query error: %v", err)
time.Sleep(time.Hour)
continue
}
if yCount > 0 {
drop := float64(yCount-curCount) / float64(yCount)
if drop >= 0.3 && (yCount-curCount) >= int64(10) {
feishu.SendNoticeMsg("meowment", "用户存活监控",
fmt.Sprintf("监控时间段: %s ~ %s\n昨日活跃用户数: %d\n当前活跃用户数: %d\n用户流失率: %.2f%%",
startT.Format("2006-01-02 15:04:05"),
endT.Format("2006-01-02 15:04:05"),
yCount,
curCount,
drop*100))
}
}
time.Sleep(time.Until(time.Now().Truncate(time.Hour).Add(time.Hour)))
}
}
}