merge: 合并main分支最新改动
解决冲突: - backend/internal/config/config.go: 合并Ops和Dashboard配置 - backend/internal/server/api_contract_test.go: 合并handler初始化 - backend/internal/service/openai_gateway_service.go: 保留Ops错误追踪逻辑 - backend/internal/service/wire.go: 合并Ops和APIKeyAuth provider 主要合并内容: - Dashboard缓存和预聚合功能 - API Key认证缓存优化 - Codex转换支持 - 使用日志分区表
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
-- Usage dashboard aggregation tables (hourly/daily) + active-user dedup + watermark.
|
||||
-- These tables support Admin Dashboard statistics without full-table scans on usage_logs.
|
||||
|
||||
-- Hourly aggregates (UTC buckets).
|
||||
CREATE TABLE IF NOT EXISTS usage_dashboard_hourly (
|
||||
bucket_start TIMESTAMPTZ PRIMARY KEY,
|
||||
total_requests BIGINT NOT NULL DEFAULT 0,
|
||||
input_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
output_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
cache_read_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
total_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
|
||||
actual_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
|
||||
total_duration_ms BIGINT NOT NULL DEFAULT 0,
|
||||
active_users BIGINT NOT NULL DEFAULT 0,
|
||||
computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_usage_dashboard_hourly_bucket_start
|
||||
ON usage_dashboard_hourly (bucket_start DESC);
|
||||
|
||||
COMMENT ON TABLE usage_dashboard_hourly IS 'Pre-aggregated hourly usage metrics for admin dashboard (UTC buckets).';
|
||||
COMMENT ON COLUMN usage_dashboard_hourly.bucket_start IS 'UTC start timestamp of the hour bucket.';
|
||||
COMMENT ON COLUMN usage_dashboard_hourly.computed_at IS 'When the hourly row was last computed/refreshed.';
|
||||
|
||||
-- Daily aggregates (UTC dates).
|
||||
CREATE TABLE IF NOT EXISTS usage_dashboard_daily (
|
||||
bucket_date DATE PRIMARY KEY,
|
||||
total_requests BIGINT NOT NULL DEFAULT 0,
|
||||
input_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
output_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
cache_creation_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
cache_read_tokens BIGINT NOT NULL DEFAULT 0,
|
||||
total_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
|
||||
actual_cost DECIMAL(20, 10) NOT NULL DEFAULT 0,
|
||||
total_duration_ms BIGINT NOT NULL DEFAULT 0,
|
||||
active_users BIGINT NOT NULL DEFAULT 0,
|
||||
computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_usage_dashboard_daily_bucket_date
|
||||
ON usage_dashboard_daily (bucket_date DESC);
|
||||
|
||||
COMMENT ON TABLE usage_dashboard_daily IS 'Pre-aggregated daily usage metrics for admin dashboard (UTC dates).';
|
||||
COMMENT ON COLUMN usage_dashboard_daily.bucket_date IS 'UTC date of the day bucket.';
|
||||
COMMENT ON COLUMN usage_dashboard_daily.computed_at IS 'When the daily row was last computed/refreshed.';
|
||||
|
||||
-- Hourly active user dedup table.
|
||||
CREATE TABLE IF NOT EXISTS usage_dashboard_hourly_users (
|
||||
bucket_start TIMESTAMPTZ NOT NULL,
|
||||
user_id BIGINT NOT NULL,
|
||||
PRIMARY KEY (bucket_start, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_usage_dashboard_hourly_users_bucket_start
|
||||
ON usage_dashboard_hourly_users (bucket_start);
|
||||
|
||||
-- Daily active user dedup table.
|
||||
CREATE TABLE IF NOT EXISTS usage_dashboard_daily_users (
|
||||
bucket_date DATE NOT NULL,
|
||||
user_id BIGINT NOT NULL,
|
||||
PRIMARY KEY (bucket_date, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_usage_dashboard_daily_users_bucket_date
|
||||
ON usage_dashboard_daily_users (bucket_date);
|
||||
|
||||
-- Aggregation watermark table (single row).
|
||||
CREATE TABLE IF NOT EXISTS usage_dashboard_aggregation_watermark (
|
||||
id INT PRIMARY KEY,
|
||||
last_aggregated_at TIMESTAMPTZ NOT NULL DEFAULT TIMESTAMPTZ '1970-01-01 00:00:00+00',
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
INSERT INTO usage_dashboard_aggregation_watermark (id)
|
||||
VALUES (1)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
54
backend/migrations/035_usage_logs_partitioning.sql
Normal file
54
backend/migrations/035_usage_logs_partitioning.sql
Normal file
@@ -0,0 +1,54 @@
|
||||
-- usage_logs monthly partition bootstrap.
|
||||
-- Only creates partitions when usage_logs is already partitioned.
|
||||
-- Converting usage_logs to a partitioned table requires a manual migration plan.
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
is_partitioned BOOLEAN := FALSE;
|
||||
has_data BOOLEAN := FALSE;
|
||||
month_start DATE;
|
||||
prev_month DATE;
|
||||
next_month DATE;
|
||||
BEGIN
|
||||
SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM pg_partitioned_table pt
|
||||
JOIN pg_class c ON c.oid = pt.partrelid
|
||||
WHERE c.relname = 'usage_logs'
|
||||
) INTO is_partitioned;
|
||||
|
||||
IF NOT is_partitioned THEN
|
||||
SELECT EXISTS(SELECT 1 FROM usage_logs LIMIT 1) INTO has_data;
|
||||
IF NOT has_data THEN
|
||||
-- Automatic conversion is intentionally skipped; see manual migration plan.
|
||||
RAISE NOTICE 'usage_logs is not partitioned; skip automatic partitioning';
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
IF is_partitioned THEN
|
||||
month_start := date_trunc('month', now() AT TIME ZONE 'UTC')::date;
|
||||
prev_month := (month_start - INTERVAL '1 month')::date;
|
||||
next_month := (month_start + INTERVAL '1 month')::date;
|
||||
|
||||
EXECUTE format(
|
||||
'CREATE TABLE IF NOT EXISTS usage_logs_%s PARTITION OF usage_logs FOR VALUES FROM (%L) TO (%L)',
|
||||
to_char(prev_month, 'YYYYMM'),
|
||||
prev_month,
|
||||
month_start
|
||||
);
|
||||
|
||||
EXECUTE format(
|
||||
'CREATE TABLE IF NOT EXISTS usage_logs_%s PARTITION OF usage_logs FOR VALUES FROM (%L) TO (%L)',
|
||||
to_char(month_start, 'YYYYMM'),
|
||||
month_start,
|
||||
next_month
|
||||
);
|
||||
|
||||
EXECUTE format(
|
||||
'CREATE TABLE IF NOT EXISTS usage_logs_%s PARTITION OF usage_logs FOR VALUES FROM (%L) TO (%L)',
|
||||
to_char(next_month, 'YYYYMM'),
|
||||
next_month,
|
||||
(next_month + INTERVAL '1 month')::date
|
||||
);
|
||||
END IF;
|
||||
END $$;
|
||||
Reference in New Issue
Block a user