Files
llmgw/internal/store/migrations/postgres/0042_rollup_images.sql
T
Ilker ManapandClaude Opus 5.5 eaea671f23 The usage sums and exports count pictures
The five-minute, hourly and daily sums gain images (migration 0042), and
the CSV and JSON exports a column for them: once request rows were past
their retention, an export said what pictures cost but not how many there
were. The pictures made before are added to the sums from their rows.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-10 10:36:19 +02:00

24 lines
1.8 KiB
SQL

-- Ported from the SQLite migration of the same number.
-- How many pictures each slice's requests made, beside their tokens and
-- cost, so the sums and exports say it after the request rows are gone.
-- Pictures made before this were counted only in those rows: added from
-- them, for the slices since the first.
ALTER TABLE usage_5m ADD COLUMN images BIGINT NOT NULL DEFAULT 0;
ALTER TABLE usage_1h ADD COLUMN images BIGINT NOT NULL DEFAULT 0;
ALTER TABLE usage_1d ADD COLUMN images BIGINT NOT NULL DEFAULT 0;
UPDATE usage_5m SET images = COALESCE((SELECT SUM(u.images) FROM usage u
WHERE u.images > 0 AND u.ts >= usage_5m.bucket AND u.ts < usage_5m.bucket + 300000
AND u.key_id = usage_5m.key_id AND u.user_id = usage_5m.user_id AND u.project_id = usage_5m.project_id
AND u.model = usage_5m.model AND u.served_model = usage_5m.served_model), 0)
WHERE bucket >= (SELECT COALESCE(MIN(ts), 9000000000000000000) - 300000 FROM usage WHERE images > 0);
UPDATE usage_1h SET images = COALESCE((SELECT SUM(u.images) FROM usage u
WHERE u.images > 0 AND u.ts >= usage_1h.bucket AND u.ts < usage_1h.bucket + 3600000
AND u.key_id = usage_1h.key_id AND u.user_id = usage_1h.user_id AND u.project_id = usage_1h.project_id
AND u.model = usage_1h.model AND u.served_model = usage_1h.served_model), 0)
WHERE bucket >= (SELECT COALESCE(MIN(ts), 9000000000000000000) - 3600000 FROM usage WHERE images > 0);
UPDATE usage_1d SET images = COALESCE((SELECT SUM(u.images) FROM usage u
WHERE u.images > 0 AND u.ts >= usage_1d.bucket AND u.ts < usage_1d.bucket + 86400000
AND u.key_id = usage_1d.key_id AND u.user_id = usage_1d.user_id AND u.project_id = usage_1d.project_id
AND u.model = usage_1d.model AND u.served_model = usage_1d.served_model), 0)
WHERE bucket >= (SELECT COALESCE(MIN(ts), 9000000000000000000) - 86400000 FROM usage WHERE images > 0);