034_usage_dashboard_aggregation_tables.sql 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. -- Usage dashboard aggregation tables (hourly/daily) + active-user dedup + watermark.
  2. -- These tables support Admin Dashboard statistics without full-table scans on usage_logs.
  3. -- Hourly aggregates (UTC buckets).
  4. CREATE TABLE IF NOT EXISTS usage_dashboard_hourly (
  5. bucket_start TIMESTAMPTZ PRIMARY KEY,
  6. total_requests BIGINT NOT NULL DEFAULT 0,
  7. input_tokens BIGINT NOT NULL DEFAULT 0,
  8. output_tokens BIGINT NOT NULL DEFAULT 0,
  9. cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
  10. cache_read_tokens BIGINT NOT NULL DEFAULT 0,
  11. total_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
  12. actual_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
  13. total_duration_ms BIGINT NOT NULL DEFAULT 0,
  14. active_users BIGINT NOT NULL DEFAULT 0,
  15. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  16. );
  17. CREATE INDEX IF NOT EXISTS idx_usage_dashboard_hourly_bucket_start
  18. ON usage_dashboard_hourly (bucket_start DESC);
  19. COMMENT ON TABLE usage_dashboard_hourly IS 'Pre-aggregated hourly usage metrics for admin dashboard (UTC buckets).';
  20. COMMENT ON COLUMN usage_dashboard_hourly.bucket_start IS 'UTC start timestamp of the hour bucket.';
  21. COMMENT ON COLUMN usage_dashboard_hourly.computed_at IS 'When the hourly row was last computed/refreshed.';
  22. -- Daily aggregates (UTC dates).
  23. CREATE TABLE IF NOT EXISTS usage_dashboard_daily (
  24. bucket_date DATE PRIMARY KEY,
  25. total_requests BIGINT NOT NULL DEFAULT 0,
  26. input_tokens BIGINT NOT NULL DEFAULT 0,
  27. output_tokens BIGINT NOT NULL DEFAULT 0,
  28. cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
  29. cache_read_tokens BIGINT NOT NULL DEFAULT 0,
  30. total_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
  31. actual_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
  32. total_duration_ms BIGINT NOT NULL DEFAULT 0,
  33. active_users BIGINT NOT NULL DEFAULT 0,
  34. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  35. );
  36. CREATE INDEX IF NOT EXISTS idx_usage_dashboard_daily_bucket_date
  37. ON usage_dashboard_daily (bucket_date DESC);
  38. COMMENT ON TABLE usage_dashboard_daily IS 'Pre-aggregated daily usage metrics for admin dashboard (UTC dates).';
  39. COMMENT ON COLUMN usage_dashboard_daily.bucket_date IS 'UTC date of the day bucket.';
  40. COMMENT ON COLUMN usage_dashboard_daily.computed_at IS 'When the daily row was last computed/refreshed.';
  41. -- Hourly active user dedup table.
  42. CREATE TABLE IF NOT EXISTS usage_dashboard_hourly_users (
  43. bucket_start TIMESTAMPTZ NOT NULL,
  44. user_id BIGINT NOT NULL,
  45. PRIMARY KEY (bucket_start, user_id)
  46. );
  47. CREATE INDEX IF NOT EXISTS idx_usage_dashboard_hourly_users_bucket_start
  48. ON usage_dashboard_hourly_users (bucket_start);
  49. -- Daily active user dedup table.
  50. CREATE TABLE IF NOT EXISTS usage_dashboard_daily_users (
  51. bucket_date DATE NOT NULL,
  52. user_id BIGINT NOT NULL,
  53. PRIMARY KEY (bucket_date, user_id)
  54. );
  55. CREATE INDEX IF NOT EXISTS idx_usage_dashboard_daily_users_bucket_date
  56. ON usage_dashboard_daily_users (bucket_date);
  57. -- Aggregation watermark table (single row).
  58. CREATE TABLE IF NOT EXISTS usage_dashboard_aggregation_watermark (
  59. id INT PRIMARY KEY,
  60. last_aggregated_at TIMESTAMPTZ NOT NULL DEFAULT TIMESTAMPTZ '1970-01-01 00:00:00+00',
  61. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  62. );
  63. INSERT INTO usage_dashboard_aggregation_watermark (id)
  64. VALUES (1)
  65. ON CONFLICT (id) DO NOTHING;