026_ops_metrics_aggregation_tables.sql 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- Ops monitoring: pre-aggregation tables for dashboard queries
  2. --
  3. -- Problem:
  4. -- The ops dashboard currently runs percentile_cont + GROUP BY queries over large raw tables
  5. -- (usage_logs, ops_error_logs). These will get slower as data grows.
  6. --
  7. -- This migration adds schema-only aggregation tables that can be populated by a future background job.
  8. -- No triggers/functions/jobs are created here (schema only).
  9. -- ============================================
  10. -- Hourly aggregates (per provider/platform)
  11. -- ============================================
  12. CREATE TABLE IF NOT EXISTS ops_metrics_hourly (
  13. -- Start of the hour bucket (recommended: UTC).
  14. bucket_start TIMESTAMPTZ NOT NULL,
  15. -- Provider/platform label (e.g. anthropic/openai/gemini). Mirrors ops_* queries that GROUP BY platform.
  16. platform VARCHAR(50) NOT NULL,
  17. -- Traffic counts (use these to compute rates reliably across ranges).
  18. request_count BIGINT NOT NULL DEFAULT 0,
  19. success_count BIGINT NOT NULL DEFAULT 0,
  20. error_count BIGINT NOT NULL DEFAULT 0,
  21. -- Error breakdown used by provider health UI.
  22. error_4xx_count BIGINT NOT NULL DEFAULT 0,
  23. error_5xx_count BIGINT NOT NULL DEFAULT 0,
  24. timeout_count BIGINT NOT NULL DEFAULT 0,
  25. -- Latency aggregates (ms).
  26. avg_latency_ms DOUBLE PRECISION,
  27. p99_latency_ms DOUBLE PRECISION,
  28. -- Convenience rate (percentage, 0-100). Still keep counts as source of truth.
  29. error_rate DOUBLE PRECISION NOT NULL DEFAULT 0,
  30. -- When this row was last (re)computed by the background job.
  31. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  32. PRIMARY KEY (bucket_start, platform)
  33. );
  34. CREATE INDEX IF NOT EXISTS idx_ops_metrics_hourly_platform_bucket_start
  35. ON ops_metrics_hourly (platform, bucket_start DESC);
  36. COMMENT ON TABLE ops_metrics_hourly IS 'Pre-aggregated hourly ops metrics by provider/platform to speed up dashboard queries.';
  37. COMMENT ON COLUMN ops_metrics_hourly.bucket_start IS 'Start timestamp of the hour bucket (recommended UTC).';
  38. COMMENT ON COLUMN ops_metrics_hourly.platform IS 'Provider/platform label (anthropic/openai/gemini, etc).';
  39. COMMENT ON COLUMN ops_metrics_hourly.error_rate IS 'Error rate percentage for the bucket (0-100). Counts remain the source of truth.';
  40. COMMENT ON COLUMN ops_metrics_hourly.computed_at IS 'When the row was last computed/refreshed.';
  41. -- ============================================
  42. -- Daily aggregates (per provider/platform)
  43. -- ============================================
  44. CREATE TABLE IF NOT EXISTS ops_metrics_daily (
  45. -- Day bucket (recommended: UTC date).
  46. bucket_date DATE NOT NULL,
  47. platform VARCHAR(50) NOT NULL,
  48. request_count BIGINT NOT NULL DEFAULT 0,
  49. success_count BIGINT NOT NULL DEFAULT 0,
  50. error_count BIGINT NOT NULL DEFAULT 0,
  51. error_4xx_count BIGINT NOT NULL DEFAULT 0,
  52. error_5xx_count BIGINT NOT NULL DEFAULT 0,
  53. timeout_count BIGINT NOT NULL DEFAULT 0,
  54. avg_latency_ms DOUBLE PRECISION,
  55. p99_latency_ms DOUBLE PRECISION,
  56. error_rate DOUBLE PRECISION NOT NULL DEFAULT 0,
  57. computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  58. PRIMARY KEY (bucket_date, platform)
  59. );
  60. CREATE INDEX IF NOT EXISTS idx_ops_metrics_daily_platform_bucket_date
  61. ON ops_metrics_daily (platform, bucket_date DESC);
  62. COMMENT ON TABLE ops_metrics_daily IS 'Pre-aggregated daily ops metrics by provider/platform for longer-term trends.';
  63. COMMENT ON COLUMN ops_metrics_daily.bucket_date IS 'UTC date of the day bucket (recommended).';
  64. -- ============================================
  65. -- Population strategy (future background job)
  66. -- ============================================
  67. --
  68. -- Suggested approach:
  69. -- 1) Compute hourly buckets from raw logs using UTC time-bucketing, then UPSERT into ops_metrics_hourly.
  70. -- 2) Compute daily buckets either directly from raw logs or by rolling up ops_metrics_hourly.
  71. --
  72. -- Notes:
  73. -- - Ensure the job uses a consistent timezone (recommended: SET TIME ZONE ''UTC'') to avoid bucket drift.
  74. -- - Derive the provider/platform similarly to existing dashboard queries:
  75. -- usage_logs: COALESCE(NULLIF(groups.platform, ''), accounts.platform, '')
  76. -- ops_error_logs: COALESCE(NULLIF(ops_error_logs.platform, ''), groups.platform, accounts.platform, '')
  77. -- - Keep request_count/success_count/error_count as the authoritative values; compute error_rate from counts.
  78. --
  79. -- Example (hourly) shape (pseudo-SQL):
  80. -- INSERT INTO ops_metrics_hourly (...)
  81. -- SELECT date_trunc('hour', created_at) AS bucket_start, platform, ...
  82. -- FROM (/* aggregate usage_logs + ops_error_logs */) s
  83. -- ON CONFLICT (bucket_start, platform) DO UPDATE SET ...;