From 59358cbe8a499a27e83175b4d59f3012bad098d9 Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 5 Dec 2024 01:06:55 +0800 Subject: [PATCH] fix(metrics): update SQL query to use localtime for accurate metrics retrieval - Modified the SQL query in GetRecentMetrics to utilize the 'localtime' modifier for timestamp handling, ensuring that metrics are retrieved based on the local time zone. - This change enhances the accuracy of the metrics data by aligning the time calculations with local time, improving the reliability of performance monitoring. --- internal/models/metrics.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/models/metrics.go b/internal/models/metrics.go index c65a656..e56dff1 100644 --- a/internal/models/metrics.go +++ b/internal/models/metrics.go @@ -240,17 +240,18 @@ func (db *MetricsDB) GetRecentMetrics(hours int) ([]HistoricalMetrics, error) { interval = "%Y-%m-%d 00:00:00" } + // 修改查询,使用 localtime 修饰符 rows, err := db.DB.Query(` WITH grouped_metrics AS ( SELECT - strftime(?1, timestamp) as group_time, + strftime(?1, timestamp, 'localtime') as group_time, SUM(total_requests) as total_requests, SUM(total_errors) as total_errors, SUM(total_bytes) as total_bytes, CAST(AVG(CAST(avg_latency AS FLOAT)) AS FLOAT) as avg_latency FROM metrics_history - WHERE timestamp >= datetime('now', '-' || ?2 || ' hours') - GROUP BY group_time + WHERE timestamp >= datetime('now', '-' || ?2 || ' hours', 'localtime') + GROUP BY group_time ORDER BY group_time DESC ) SELECT * FROM grouped_metrics