feat: Redis热数据+K线聚合+Dashboard MySQL指标修复

This commit is contained in:
attacker committed 2026-06-18 08:03:49 +00:00
1 parent b8b1a3d5f6
commit a843ab7b00
9 files changed
+311 -12

No files matched your search

+4 -4
View File
@@ -294,17 +294,17 @@ func (h *DashboardHandler) GetMetrics(c *gin.Context) {
}
}
// 从 agent_reports 表查最近 N 小时的真实数据 // Logan.Li
// 从 agent_reports 表查最近 N 小时的真实数据(MySQL 聚合)
since := time.Now().Add(-time.Duration(hours) * time.Hour)
type avgPoint struct {
Bucket string // strftime 返回字符串 "2006-01-02 15:00",不能直接扫入 time.Time(会得零值)
Bucket string
AvgCPU float64
AvgMem float64
}
var rows []avgPoint
h.db.Model(&models.AgentReport{}).
Select("strftime('%Y-%m-%d %H:00', created_at) as bucket, AVG(cpu_usage) as avg_cpu, AVG(mem_usage) as avg_mem").
Where("created_at >= ?", since).
Select("DATE_FORMAT(report_time, '%Y-%m-%d %H:00') as bucket, ROUND(AVG(cpu_usage),2) as avg_cpu, ROUND(AVG(mem_usage),2) as avg_mem").
Where("report_time >= ?", since).
Group("bucket").
Order("bucket ASC").
Scan(&rows)