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:
Dean Sheather
2025-08-30 03:39:37 +10:00
committed by GitHub
parent 433f9c4a38
commit 39bf3ba628
18 changed files with 488 additions and 116 deletions
+8
View File
@@ -3778,6 +3778,14 @@ type UsageEvent struct {
FailureMessage sql.NullString `db:"failure_message" json:"failure_message"`
}
// usage_events_daily is a daily rollup of usage events. It stores the total usage for each event type by day.
type UsageEventsDaily struct {
// The date of the summed usage events, always in UTC.
Day time.Time `db:"day" json:"day"`
EventType string `db:"event_type" json:"event_type"`
UsageData json.RawMessage `db:"usage_data" json:"usage_data"`
}
type User struct {
ID uuid.UUID `db:"id" json:"id"`
Email string `db:"email" json:"email"`