Skip to content

Conversation

deansheather
Copy link
Member

@deansheather deansheather commented Aug 29, 2025

  • 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 coder/internal#943

- 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`

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.
@deansheather deansheather added the cherry-pick/v2.26 Needs to be cherry-picked to the 2.26 release branch label Aug 29, 2025
@deansheather deansheather requested a review from johnstcn August 29, 2025 14:13
RETURNS TRIGGER AS $$
BEGIN
-- Check for supported event types and throw error for unknown types
IF NEW.event_type NOT IN ('dc_managed_agents_v1') THEN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main worry with this is that if some bad code spams insertion of unknown usage events we could end up creating some serious DB load. IIRC you make it fairly difficult to even do that though, so I guess this is OK?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine it would generate a lot of load anyway if something like that was happening. This upsert should be fairly quick since it's using the primary key.

I also don't know how else I'd handle this other than a cronjob, which would also generate a lot of load if there was a lot of rows, and would require a new Go package to handle doing it every once in a while.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder to check migration number before merge

@deansheather deansheather merged commit 39bf3ba into main Aug 29, 2025
31 checks passed
@deansheather deansheather deleted the dean/remove-get-managed-agent-count-query branch August 29, 2025 17:39
@github-actions github-actions bot locked and limited conversation to collaborators Aug 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cherry-pick/v2.26 Needs to be cherry-picked to the 2.26 release branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: GetManagedAgentCount is called > 100k times per day and creates lots of DB load
2 participants