mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add ai_user_daily_spend table and queries (#26562)
## Description Adds the spend tracking table and queries needed by [AIGOV-427](https://linear.app/codercom/issue/AIGOV-427/add-post-response-spend-accumulation) (post-response accumulation) and [AIGOV-428](https://linear.app/codercom/issue/AIGOV-428/add-pre-request-budget-enforcement) (pre-request enforcement). ## Changes - Add `ai_user_daily_spend` table to aggregate per-user, per-effective-group AI spend by UTC day. - Add `UpsertUserAIDailySpend` and `GetUserAISpendSince` queries. Closes https://linear.app/codercom/issue/AIGOV-426/add-daily-spend-table-and-queries > [!NOTE] > Initially generated by Claude Opus 4.7, modified and reviewed by @ssncferreira
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS ai_user_daily_spend CASCADE;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Aggregates a user's AI spend within their effective group, one row per
|
||||
-- UTC day. Drives budget enforcement and reporting.
|
||||
CREATE TABLE ai_user_daily_spend (
|
||||
-- No FK to users. Spend records persist after user deletion.
|
||||
user_id UUID NOT NULL,
|
||||
-- No FK to groups. Spend records persist after group deletion.
|
||||
effective_group_id UUID NOT NULL,
|
||||
day DATE NOT NULL,
|
||||
spend_micros BIGINT NOT NULL CHECK (spend_micros >= 0),
|
||||
PRIMARY KEY (user_id, effective_group_id, day)
|
||||
);
|
||||
|
||||
COMMENT ON TABLE ai_user_daily_spend IS 'Daily AI spend per user and effective group.';
|
||||
COMMENT ON COLUMN ai_user_daily_spend.user_id IS 'The user who incurred the spend.';
|
||||
COMMENT ON COLUMN ai_user_daily_spend.effective_group_id IS 'The group this spend is attributed to for budget purposes.';
|
||||
COMMENT ON COLUMN ai_user_daily_spend.day IS 'UTC calendar day the spend was incurred.';
|
||||
COMMENT ON COLUMN ai_user_daily_spend.spend_micros IS 'Accumulated spend in micro-units (1 unit = 1,000,000).';
|
||||
|
||||
-- For queries filtering by effective_group_id alone.
|
||||
CREATE INDEX idx_ai_user_daily_spend_effective_group_id_day
|
||||
ON ai_user_daily_spend (effective_group_id, day);
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
INSERT INTO ai_user_daily_spend (
|
||||
user_id,
|
||||
effective_group_id,
|
||||
day,
|
||||
spend_micros
|
||||
) VALUES
|
||||
('30095c71-380b-457a-8995-97b8ee6e5307', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2024-06-15', 100000);
|
||||
Reference in New Issue
Block a user