199_channel_monitor_v2_fixed_rollups.sql 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. -- Fixed-size Channel Monitor V2 rollups for the 24h / 7d / 30d views.
  2. -- Source of truth remains the 1-minute passive rollup; these tables cache the
  3. -- UI bucket sizes so historical buckets do not need to be re-aggregated on each
  4. -- filter change. The background aggregator overwrites affected buckets.
  5. CREATE TABLE IF NOT EXISTS channel_monitor_v2_metrics_rollup (
  6. bucket_start TIMESTAMPTZ NOT NULL,
  7. bucket_seconds INTEGER NOT NULL CHECK (bucket_seconds IN (300, 3600, 43200, 86400)),
  8. platform TEXT NOT NULL,
  9. group_id BIGINT NOT NULL DEFAULT 0,
  10. model TEXT NOT NULL,
  11. success_requests BIGINT NOT NULL DEFAULT 0,
  12. error_requests BIGINT NOT NULL DEFAULT 0,
  13. upstream_affected_requests BIGINT NOT NULL DEFAULT 0,
  14. upstream_attempt_count BIGINT NOT NULL DEFAULT 0,
  15. input_tokens BIGINT NOT NULL DEFAULT 0,
  16. output_tokens BIGINT NOT NULL DEFAULT 0,
  17. cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
  18. cache_read_tokens BIGINT NOT NULL DEFAULT 0,
  19. ttft_sum_ms BIGINT NOT NULL DEFAULT 0,
  20. ttft_count BIGINT NOT NULL DEFAULT 0,
  21. duration_sum_ms BIGINT NOT NULL DEFAULT 0,
  22. duration_count BIGINT NOT NULL DEFAULT 0,
  23. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  24. PRIMARY KEY (bucket_seconds, bucket_start, platform, group_id, model)
  25. );
  26. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_metrics_rollup_platform_time
  27. ON channel_monitor_v2_metrics_rollup (bucket_seconds, platform, bucket_start DESC);
  28. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_metrics_rollup_group_time
  29. ON channel_monitor_v2_metrics_rollup (bucket_seconds, group_id, bucket_start DESC);
  30. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_metrics_rollup_model_time
  31. ON channel_monitor_v2_metrics_rollup (bucket_seconds, model, bucket_start DESC);
  32. CREATE TABLE IF NOT EXISTS channel_monitor_v2_user_metrics_rollup (
  33. bucket_start TIMESTAMPTZ NOT NULL,
  34. bucket_seconds INTEGER NOT NULL CHECK (bucket_seconds IN (300, 3600, 43200, 86400)),
  35. platform TEXT NOT NULL,
  36. group_id BIGINT NOT NULL DEFAULT 0,
  37. model TEXT NOT NULL,
  38. user_id BIGINT NOT NULL,
  39. success_requests BIGINT NOT NULL DEFAULT 0,
  40. error_requests BIGINT NOT NULL DEFAULT 0,
  41. input_tokens BIGINT NOT NULL DEFAULT 0,
  42. output_tokens BIGINT NOT NULL DEFAULT 0,
  43. cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
  44. cache_read_tokens BIGINT NOT NULL DEFAULT 0,
  45. ttft_sum_ms BIGINT NOT NULL DEFAULT 0,
  46. ttft_count BIGINT NOT NULL DEFAULT 0,
  47. duration_sum_ms BIGINT NOT NULL DEFAULT 0,
  48. duration_count BIGINT NOT NULL DEFAULT 0,
  49. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  50. PRIMARY KEY (bucket_seconds, bucket_start, platform, group_id, model, user_id)
  51. );
  52. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_user_rollup_user_time
  53. ON channel_monitor_v2_user_metrics_rollup (bucket_seconds, user_id, bucket_start DESC);
  54. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_user_rollup_time
  55. ON channel_monitor_v2_user_metrics_rollup (bucket_seconds, bucket_start DESC);
  56. CREATE TABLE IF NOT EXISTS channel_monitor_v2_error_metrics_rollup (
  57. bucket_start TIMESTAMPTZ NOT NULL,
  58. bucket_seconds INTEGER NOT NULL CHECK (bucket_seconds IN (300, 3600, 43200, 86400)),
  59. platform TEXT NOT NULL,
  60. group_id BIGINT NOT NULL DEFAULT 0,
  61. model TEXT NOT NULL,
  62. error_category TEXT NOT NULL,
  63. taxonomy_version SMALLINT NOT NULL,
  64. error_requests BIGINT NOT NULL DEFAULT 0,
  65. PRIMARY KEY (bucket_seconds, bucket_start, platform, group_id, model, error_category, taxonomy_version)
  66. );
  67. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_errors_rollup_time
  68. ON channel_monitor_v2_error_metrics_rollup (bucket_seconds, bucket_start DESC);
  69. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_errors_rollup_category_time
  70. ON channel_monitor_v2_error_metrics_rollup (bucket_seconds, error_category, bucket_start DESC);
  71. CREATE TABLE IF NOT EXISTS channel_monitor_v2_latency_histograms_rollup (
  72. bucket_start TIMESTAMPTZ NOT NULL,
  73. bucket_seconds INTEGER NOT NULL CHECK (bucket_seconds IN (300, 3600, 43200, 86400)),
  74. platform TEXT NOT NULL,
  75. group_id BIGINT NOT NULL DEFAULT 0,
  76. model TEXT NOT NULL,
  77. user_id BIGINT NOT NULL DEFAULT 0,
  78. metric TEXT NOT NULL CHECK (metric IN ('ttft', 'duration')),
  79. upper_bound_ms INTEGER NOT NULL,
  80. sample_count BIGINT NOT NULL DEFAULT 0,
  81. PRIMARY KEY (bucket_seconds, bucket_start, platform, group_id, model, user_id, metric, upper_bound_ms)
  82. );
  83. CREATE INDEX IF NOT EXISTS idx_channel_monitor_v2_histograms_rollup_time
  84. ON channel_monitor_v2_latency_histograms_rollup (bucket_seconds, bucket_start DESC, metric);
  85. COMMENT ON TABLE channel_monitor_v2_metrics_rollup IS
  86. 'Precomputed fixed UI buckets derived from channel_monitor_v2_metrics_1m for 24h/7d/30d views.';