From 2d658c35e654bf3a71edccb933d6196b8d1b39f2 Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 5 Dec 2024 06:51:16 +0800 Subject: [PATCH] fix(metrics): refine SQL query for recent metrics retrieval - Updated the SQL query in GetRecentMetrics to adjust the time filtering logic, ensuring that only metrics within the specified hours are retrieved. - This change enhances the accuracy of the metrics data by correctly bounding the time range, improving the reliability of performance monitoring. --- internal/models/metrics.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/models/metrics.go b/internal/models/metrics.go index 0e1a152..6563e21 100644 --- a/internal/models/metrics.go +++ b/internal/models/metrics.go @@ -312,7 +312,7 @@ func (db *MetricsDB) GetRecentMetrics(hours int) ([]HistoricalMetrics, error) { rows, err := db.DB.Query(` WITH time_series AS ( -- 生成时间序列 - SELECT datetime('now', '-' || ?2 || ' hours', 'localtime') + + SELECT datetime('now', 'localtime') - (CASE WHEN ?2 <= 24 THEN (300 * n) -- 5分钟间隔 WHEN ?2 <= 168 THEN (3600 * n) -- 1小时间隔 @@ -327,7 +327,8 @@ func (db *MetricsDB) GetRecentMetrics(hours int) ([]HistoricalMetrics, error) { ELSE ?2 / 24 -- 1天间隔 END) ) - WHERE time_point <= datetime('now', 'localtime') + WHERE time_point >= datetime('now', '-' || ?2 || ' hours', 'localtime') + AND time_point <= datetime('now', 'localtime') ), grouped_metrics AS ( SELECT