-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add hourly hb_agent_runtime_v1 usage events for Coder Agent runtime #27312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
35f980a
f8882fc
34c7fac
b8ae639
ee89c16
15acaf5
0fcc777
4d357de
f8e7819
9d1ee50
4c20d3f
dfc293d
5a7641f
42fed81
58f73db
d7fe770
95f7beb
71eca18
206c738
4a2c001
a02ddde
8c3e9b4
f1d30c9
b3eacc7
f444daa
446d77c
7ad3f84
8cc70e6
abd74ba
2d99562
4678b3c
18e82e9
cf943ca
2dc61ef
206f034
c07c96c
b72d53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reminder: check migration number before merge |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| COMMENT ON COLUMN usage_events.created_at IS NULL; | ||
|
|
||
| DROP INDEX IF EXISTS idx_usage_events_agent_runtime; | ||
|
|
||
| -- Remove hb_agent_runtime_v1 rows so the previous constraint can be restored. | ||
| DELETE FROM usage_events WHERE event_type = 'hb_agent_runtime_v1'; | ||
| DELETE FROM usage_events_daily WHERE event_type = 'hb_agent_runtime_v1'; | ||
|
|
||
| ALTER TABLE usage_events | ||
| DROP CONSTRAINT usage_event_type_check, | ||
| ADD CONSTRAINT usage_event_type_check CHECK (event_type IN ('dc_managed_agents_v1', 'hb_ai_seats_v1')); | ||
|
|
||
| -- Restores the 000444 version of the function. | ||
| CREATE OR REPLACE FUNCTION aggregate_usage_event() | ||
| 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', 'hb_ai_seats_v1') THEN | ||
| RAISE EXCEPTION 'Unhandled usage event type in aggregate_usage_event: %', NEW.event_type; | ||
| END IF; | ||
|
|
||
| INSERT INTO usage_events_daily (day, event_type, usage_data) | ||
| VALUES ( | ||
| date_trunc('day', NEW.created_at AT TIME ZONE 'UTC')::date, | ||
| NEW.event_type, | ||
| NEW.event_data | ||
| ) | ||
| ON CONFLICT (day, event_type) DO UPDATE SET | ||
| usage_data = CASE | ||
| -- Handle simple counter events by summing the count. | ||
| WHEN NEW.event_type IN ('dc_managed_agents_v1') THEN | ||
| jsonb_build_object( | ||
| 'count', | ||
| COALESCE((usage_events_daily.usage_data->>'count')::bigint, 0) + | ||
| COALESCE((NEW.event_data->>'count')::bigint, 0) | ||
| ) | ||
| -- Heartbeat events: keep the max value seen that day | ||
| WHEN NEW.event_type IN ('hb_ai_seats_v1') THEN | ||
| jsonb_build_object( | ||
| 'count', | ||
| GREATEST( | ||
| COALESCE((usage_events_daily.usage_data->>'count')::bigint, 0), | ||
| COALESCE((NEW.event_data->>'count')::bigint, 0) | ||
| ) | ||
| ) | ||
| END; | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| $$ LANGUAGE plpgsql; |
Uh oh!
There was an error while loading. Please reload this page.