Fix SQL INTERVAL syntax bug in metrics API

Changed INTERVAL '$1 hours' to INTERVAL '1 hour' * $1 in all queries.
The previous syntax didn't properly parameterize the hours value, causing
PostgreSQL to treat $1 as a literal string instead of substituting the
actual parameter value.

This fix ensures the time range filter works correctly for all metric queries.
This commit is contained in:
Claude
2025-11-23 04:21:03 +00:00
parent 94155233e5
commit 2ccedc2a0e

View File

@@ -30,7 +30,7 @@ export async function GET(request: NextRequest) {
COUNT(DISTINCT model) as unique_models,
COUNT(DISTINCT client_ip) as unique_clients
FROM ${validatedTableName}
WHERE timestamp >= NOW() - INTERVAL '$1 hours'
WHERE timestamp >= NOW() - INTERVAL '1 hour' * $1
`;
const summaryResult = await client.query(summaryQuery, [hours]);
const summary = summaryResult.rows[0];
@@ -50,7 +50,7 @@ export async function GET(request: NextRequest) {
client_ip,
stream
FROM ${validatedTableName}
WHERE timestamp >= NOW() - INTERVAL '$1 hours'
WHERE timestamp >= NOW() - INTERVAL '1 hour' * $1
ORDER BY timestamp DESC
LIMIT $2
`;
@@ -66,7 +66,7 @@ export async function GET(request: NextRequest) {
SUM(total_cost) as total_cost,
AVG(response_time) as avg_response_time
FROM ${validatedTableName}
WHERE timestamp >= NOW() - INTERVAL '$1 hours'
WHERE timestamp >= NOW() - INTERVAL '1 hour' * $1
GROUP BY model
ORDER BY request_count DESC
`;
@@ -82,7 +82,7 @@ export async function GET(request: NextRequest) {
SUM(total_cost) as cost,
AVG(response_time) as avg_response_time
FROM ${validatedTableName}
WHERE timestamp >= NOW() - INTERVAL '$1 hours'
WHERE timestamp >= NOW() - INTERVAL '1 hour' * $1
GROUP BY hour
ORDER BY hour ASC
`;