Files
multica/server/pkg/db/queries/runtime_usage.sql
jayavibhavnk 67212b0bef fix(server): fix ListRuntimeUsage to filter by date range instead of row count
GetRuntimeUsage accepted a ?days=N query parameter intended to return N
days of history, but the underlying ListRuntimeUsage query used it as a
raw LIMIT on rows. When a runtime uses multiple models (e.g. claude-opus
and claude-sonnet), each day produces multiple rows, so LIMIT 90 would
only return ~30 days of data instead of 90, silently truncating history.

Fix: replace the LIMIT clause with AND date >= $2 in the SQL query,
update ListRuntimeUsageParams to hold a Since pgtype.Date instead of a
Limit int32, and compute the date cutoff in the handler using
time.Now().AddDate(0, 0, -days). The generated Go code and the handler
are updated consistently.

Closes #731

Made-with: Cursor
2026-04-12 22:42:56 +08:00

35 lines
1.2 KiB
SQL

-- name: UpsertRuntimeUsage :exec
INSERT INTO runtime_usage (runtime_id, date, provider, model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (runtime_id, date, provider, model)
DO UPDATE SET
input_tokens = EXCLUDED.input_tokens,
output_tokens = EXCLUDED.output_tokens,
cache_read_tokens = EXCLUDED.cache_read_tokens,
cache_write_tokens = EXCLUDED.cache_write_tokens,
updated_at = now();
-- name: ListRuntimeUsage :many
SELECT * FROM runtime_usage
WHERE runtime_id = $1
AND date >= $2
ORDER BY date DESC;
-- name: GetRuntimeUsageSummary :many
SELECT provider, model,
SUM(input_tokens)::bigint AS total_input_tokens,
SUM(output_tokens)::bigint AS total_output_tokens,
SUM(cache_read_tokens)::bigint AS total_cache_read_tokens,
SUM(cache_write_tokens)::bigint AS total_cache_write_tokens
FROM runtime_usage
WHERE runtime_id = $1
GROUP BY provider, model
ORDER BY provider, model;
-- name: GetRuntimeTaskHourlyActivity :many
SELECT EXTRACT(HOUR FROM started_at)::int AS hour, COUNT(*)::int AS count
FROM agent_task_queue
WHERE runtime_id = $1 AND started_at IS NOT NULL
GROUP BY hour
ORDER BY hour;