-
Notifications
You must be signed in to change notification settings - Fork 980
chore: replace GetManagedAgentCount query with aggregate table #19636
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
Merged
+488
−116
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
coderd/database/migrations/000362_aggregate_usage_events.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TRIGGER IF EXISTS trigger_aggregate_usage_event ON usage_events; | ||
DROP FUNCTION IF EXISTS aggregate_usage_event(); | ||
DROP TABLE IF EXISTS usage_events_daily; |
65 changes: 65 additions & 0 deletions
65
coderd/database/migrations/000362_aggregate_usage_events.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
CREATE TABLE usage_events_daily ( | ||
day date NOT NULL, -- always grouped by day in UTC | ||
event_type text NOT NULL, | ||
usage_data jsonb NOT NULL, | ||
PRIMARY KEY (day, event_type) | ||
); | ||
|
||
COMMENT ON TABLE usage_events_daily IS 'usage_events_daily is a daily rollup of usage events. It stores the total usage for each event type by day.'; | ||
COMMENT ON COLUMN usage_events_daily.day IS 'The date of the summed usage events, always in UTC.'; | ||
|
||
-- Function to handle usage event aggregation | ||
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') 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 ( | ||
-- Extract the date from the created_at timestamp, always using UTC for | ||
-- consistency | ||
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) | ||
) | ||
END; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- Create trigger to automatically aggregate usage events | ||
CREATE TRIGGER trigger_aggregate_usage_event | ||
AFTER INSERT ON usage_events | ||
FOR EACH ROW | ||
EXECUTE FUNCTION aggregate_usage_event(); | ||
|
||
-- Populate usage_events_daily with existing data | ||
INSERT INTO | ||
usage_events_daily (day, event_type, usage_data) | ||
SELECT | ||
date_trunc('day', created_at AT TIME ZONE 'UTC')::date AS day, | ||
event_type, | ||
jsonb_build_object('count', SUM((event_data->>'count')::bigint)) AS usage_data | ||
FROM | ||
usage_events | ||
WHERE | ||
-- The only event type we currently support is dc_managed_agents_v1 | ||
event_type = 'dc_managed_agents_v1' | ||
GROUP BY | ||
date_trunc('day', created_at AT TIME ZONE 'UTC')::date, | ||
event_type | ||
ON CONFLICT (day, event_type) DO UPDATE SET | ||
usage_data = EXCLUDED.usage_data; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.