From 581a0cf61aaa88b237b5d80cc62330851a7971b5 Mon Sep 17 00:00:00 2001 From: hahwu <31872165+hahwu@users.noreply.github.com> Date: Fri, 12 Dec 2025 15:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8E=A9=E5=AE=B6=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E6=95=B0=E6=8D=AE=E7=9B=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feishu/feishu.go | 6 ++--- monitor/Monitor.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 monitor/Monitor.go diff --git a/feishu/feishu.go b/feishu/feishu.go index 529b0ba..04a1190 100644 --- a/feishu/feishu.go +++ b/feishu/feishu.go @@ -61,12 +61,10 @@ func SendFeishuMsg(msg string) error { func SendNoticeMsg(AppName, ServerName, notice string) error { str := fmt.Sprintf(` -## **端口扫描异常** - +## **游戏数据监控异常** - 项目名称: %s - - 服务器名称: %s + - 监控项名称: %s - 异常信息: %s - `, AppName, ServerName, notice) // 创建请求体 diff --git a/monitor/Monitor.go b/monitor/Monitor.go new file mode 100644 index 0000000..34bb20e --- /dev/null +++ b/monitor/Monitor.go @@ -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=? AND Timestamp 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))) + } + } +}