fix(api): use INTERVAL syntax to avoid ClickHouse Decimal overflow

The toDateTime64(nanoseconds, 9) caused Decimal overflow.
Switched to simpler `now() - INTERVAL X MINUTE` syntax.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-03-23 00:57:45 +08:00
parent 7f2adab78b
commit e4bd030882

View File

@@ -254,9 +254,7 @@ class SignOzClient:
# =====================================================================
# 使用 signoz_traces.distributed_signoz_index_v3 表
# statusCode: 0=Unset, 1=Ok, 2=Error
# 計算 Unix 納秒時間戳 (ClickHouse DateTime64(9) 格式)
start_ns = int(start_time.timestamp() * 1_000_000_000)
end_ns = int(end_time.timestamp() * 1_000_000_000)
# 使用 INTERVAL 語法避免 Decimal overflow
rps_query = f"""
SELECT
@@ -264,7 +262,7 @@ class SignOzClient:
countIf(statusCode = 2) as error_count
FROM signoz_traces.distributed_signoz_index_v3
WHERE
timestamp BETWEEN toDateTime64({start_ns}, 9) AND toDateTime64({end_ns}, 9)
timestamp > now() - INTERVAL {time_window_minutes} MINUTE
AND serviceName LIKE '%{service_name}%'
"""
@@ -290,7 +288,7 @@ class SignOzClient:
quantile(0.99)(durationNano / 1000000.0) as p99
FROM signoz_traces.distributed_signoz_index_v3
WHERE
timestamp BETWEEN toDateTime64({start_ns}, 9) AND toDateTime64({end_ns}, 9)
timestamp > now() - INTERVAL {time_window_minutes} MINUTE
AND serviceName LIKE '%{service_name}%'
"""
@@ -305,14 +303,11 @@ class SignOzClient:
# =====================================================================
# Query 3: Trend Analysis (對比前一時間窗)
# =====================================================================
prev_start_ns = int((start_time - timedelta(minutes=time_window_minutes)).timestamp() * 1_000_000_000)
prev_end_ns = start_ns
trend_query = f"""
SELECT count() as prev_requests
FROM signoz_traces.distributed_signoz_index_v3
WHERE
timestamp BETWEEN toDateTime64({prev_start_ns}, 9) AND toDateTime64({prev_end_ns}, 9)
timestamp BETWEEN now() - INTERVAL {time_window_minutes * 2} MINUTE AND now() - INTERVAL {time_window_minutes} MINUTE
AND serviceName LIKE '%{service_name}%'
"""