mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
chore: replace GetManagedAgentCount query with aggregate table (#19636)
- Removes GetManagedAgentCount query - Adds new table `usage_events_daily` which stores aggregated usage events by the type and UTC day - Adds trigger to update the values in this table when a new row is inserted into `usage_events` - Adds a migration that adds `usage_events_daily` rows for existing data in `usage_events` - Adds tests for the trigger - Adds tests for the backfill query in the migration Since the `usage_events` table is unreleased currently, this migration will do nothing on real deployments and will only affect preview deployments such as dogfood. Closes https://github.com/coder/internal/issues/943
This commit is contained in:
@@ -39,7 +39,7 @@ WITH usage_events AS (
|
||||
-- than an hour ago. This is so we can retry publishing
|
||||
-- events where the replica exited or couldn't update the
|
||||
-- row.
|
||||
-- The parenthesis around @now::timestamptz are necessary to
|
||||
-- The parentheses around @now::timestamptz are necessary to
|
||||
-- avoid sqlc from generating an extra argument.
|
||||
OR potential_event.publish_started_at < (@now::timestamptz) - INTERVAL '1 hour'
|
||||
)
|
||||
@@ -47,7 +47,7 @@ WITH usage_events AS (
|
||||
-- always permanently reject these events anyways. This is to
|
||||
-- avoid duplicate events being billed to customers, as
|
||||
-- Metronome will only deduplicate events within 34 days.
|
||||
-- Also, the same parenthesis thing here as above.
|
||||
-- Also, the same parentheses thing here as above.
|
||||
AND potential_event.created_at > (@now::timestamptz) - INTERVAL '30 days'
|
||||
ORDER BY potential_event.created_at ASC
|
||||
FOR UPDATE SKIP LOCKED
|
||||
@@ -84,3 +84,24 @@ WHERE
|
||||
-- zero, so this is the best we can do.
|
||||
AND cardinality(@ids::text[]) = cardinality(@failure_messages::text[])
|
||||
AND cardinality(@ids::text[]) = cardinality(@set_published_ats::boolean[]);
|
||||
|
||||
-- name: GetTotalUsageDCManagedAgentsV1 :one
|
||||
-- Gets the total number of managed agents created between two dates. Uses the
|
||||
-- aggregate table to avoid large scans or a complex index on the usage_events
|
||||
-- table.
|
||||
--
|
||||
-- This has the trade off that we can't count accurately between two exact
|
||||
-- timestamps. The provided timestamps will be converted to UTC and truncated to
|
||||
-- the events that happened on and between the two dates. Both dates are
|
||||
-- inclusive.
|
||||
SELECT
|
||||
-- The first cast is necessary since you can't sum strings, and the second
|
||||
-- cast is necessary to make sqlc happy.
|
||||
COALESCE(SUM((usage_data->>'count')::bigint), 0)::bigint AS total_count
|
||||
FROM
|
||||
usage_events_daily
|
||||
WHERE
|
||||
event_type = 'dc_managed_agents_v1'
|
||||
-- Parentheses are necessary to avoid sqlc from generating an extra
|
||||
-- argument.
|
||||
AND day BETWEEN date_trunc('day', (@start_date::timestamptz) AT TIME ZONE 'UTC')::date AND date_trunc('day', (@end_date::timestamptz) AT TIME ZONE 'UTC')::date;
|
||||
|
||||
Reference in New Issue
Block a user