fix: 统一 Agent 在线状态时区

This commit is contained in:
Logan.Li committed 2026-07-27 16:32:17 +08:00
1 parent e557782176
commit e831ec2eb1
1 file changed
+3 -3
+3 -3
View File
@@ -277,7 +277,7 @@ func (h *AgentHandler) writeHotMetric(c *gin.Context, key string, value float64,
// GET /api/v1/agent/latest
func (h *AgentHandler) GetLatestReports(c *gin.Context) {
var results []model.AgentLatestResponse
threeMinAgo := time.Now().Add(-3 * time.Minute)
threeMinAgo := time.Now().UTC().Add(-3 * time.Minute) // SQLite 上报时间统一以 UTC 持久化。 // Logan.Li
// 以 servers 表为主,按资产名或 Agent 自报 IP 关联最近 3 分钟内的上报。 // Logan.Li
// 未装 Agent 的主机也会显示(agent_status='not_installed'),打破"不显示→无法安装"的循环
h.db.Raw(`
@@ -348,7 +348,7 @@ func parseFlexibleTime(s string) (time.Time, error) {
}
for _, layout := range layouts {
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
return t, nil
return t.UTC(), nil // 历史报告使用 UTC 存储,避免自定义时间范围跨时区漏数。 // Logan.Li
}
}
return time.Time{}, fmt.Errorf("无法解析时间: %s", s)
@@ -356,7 +356,7 @@ func parseFlexibleTime(s string) (time.Time, error) {
// resolveHostHistoryRange 解析历史查询时间范围(minutes / hours / start+end) // Logan.Li
func resolveHostHistoryRange(c *gin.Context) (since, until time.Time, err error) {
until = time.Now()
until = time.Now().UTC() // 历史报告使用 UTC 存储,避免本地时区造成范围比较失效。 // Logan.Li
if startStr := strings.TrimSpace(c.Query("start")); startStr != "" {
endStr := strings.TrimSpace(c.Query("end"))
if endStr == "" {