refactor: 移除 Ops 监控模块

移除未完成的运维监控功能,简化系统架构:
- 删除 ops_handler, ops_service, ops_repo 等后端代码
- 删除 ops 相关数据库迁移文件
- 删除前端 OpsDashboard 页面和 API
This commit is contained in:
ianshaw
2026-01-03 06:18:44 -08:00
parent 45bd9ac705
commit df1ef3deb6
25 changed files with 0 additions and 6883 deletions

View File

@@ -1,48 +0,0 @@
-- Ops error logs and system metrics
CREATE TABLE IF NOT EXISTS ops_error_logs (
id BIGSERIAL PRIMARY KEY,
request_id VARCHAR(64),
user_id BIGINT,
api_key_id BIGINT,
account_id BIGINT,
group_id BIGINT,
client_ip INET,
error_phase VARCHAR(32) NOT NULL,
error_type VARCHAR(64) NOT NULL,
severity VARCHAR(4) NOT NULL,
status_code INT,
platform VARCHAR(32),
model VARCHAR(100),
request_path VARCHAR(256),
stream BOOLEAN NOT NULL DEFAULT FALSE,
error_message TEXT,
error_body TEXT,
provider_error_code VARCHAR(64),
provider_error_type VARCHAR(64),
is_retryable BOOLEAN NOT NULL DEFAULT FALSE,
is_user_actionable BOOLEAN NOT NULL DEFAULT FALSE,
retry_count INT NOT NULL DEFAULT 0,
completion_status VARCHAR(16),
duration_ms INT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ops_error_logs_created_at ON ops_error_logs (created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ops_error_logs_phase ON ops_error_logs (error_phase);
CREATE INDEX IF NOT EXISTS idx_ops_error_logs_platform ON ops_error_logs (platform);
CREATE INDEX IF NOT EXISTS idx_ops_error_logs_severity ON ops_error_logs (severity);
CREATE INDEX IF NOT EXISTS idx_ops_error_logs_phase_platform_time ON ops_error_logs (error_phase, platform, created_at DESC);
CREATE TABLE IF NOT EXISTS ops_system_metrics (
id BIGSERIAL PRIMARY KEY,
success_rate DOUBLE PRECISION,
error_rate DOUBLE PRECISION,
p95_latency_ms INT,
p99_latency_ms INT,
http2_errors INT,
active_alerts INT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ops_system_metrics_created_at ON ops_system_metrics (created_at DESC);

View File

@@ -1,14 +0,0 @@
-- Extend ops_system_metrics with windowed/system stats
ALTER TABLE ops_system_metrics
ADD COLUMN IF NOT EXISTS window_minutes INT NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS cpu_usage_percent DOUBLE PRECISION,
ADD COLUMN IF NOT EXISTS memory_used_mb BIGINT,
ADD COLUMN IF NOT EXISTS memory_total_mb BIGINT,
ADD COLUMN IF NOT EXISTS memory_usage_percent DOUBLE PRECISION,
ADD COLUMN IF NOT EXISTS heap_alloc_mb BIGINT,
ADD COLUMN IF NOT EXISTS gc_pause_ms DOUBLE PRECISION,
ADD COLUMN IF NOT EXISTS concurrency_queue_depth INT;
CREATE INDEX IF NOT EXISTS idx_ops_system_metrics_window_time
ON ops_system_metrics (window_minutes, created_at DESC);

View File

@@ -1,42 +0,0 @@
-- Ops alert rules and events
CREATE TABLE IF NOT EXISTS ops_alert_rules (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
description TEXT,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
metric_type VARCHAR(64) NOT NULL,
operator VARCHAR(8) NOT NULL,
threshold DOUBLE PRECISION NOT NULL,
window_minutes INT NOT NULL DEFAULT 1,
sustained_minutes INT NOT NULL DEFAULT 1,
severity VARCHAR(4) NOT NULL DEFAULT 'P1',
notify_email BOOLEAN NOT NULL DEFAULT FALSE,
notify_webhook BOOLEAN NOT NULL DEFAULT FALSE,
webhook_url TEXT,
cooldown_minutes INT NOT NULL DEFAULT 10,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ops_alert_rules_enabled ON ops_alert_rules (enabled);
CREATE INDEX IF NOT EXISTS idx_ops_alert_rules_metric ON ops_alert_rules (metric_type, window_minutes);
CREATE TABLE IF NOT EXISTS ops_alert_events (
id BIGSERIAL PRIMARY KEY,
rule_id BIGINT NOT NULL REFERENCES ops_alert_rules(id) ON DELETE CASCADE,
severity VARCHAR(4) NOT NULL,
status VARCHAR(16) NOT NULL DEFAULT 'firing',
title VARCHAR(200),
description TEXT,
metric_value DOUBLE PRECISION,
threshold_value DOUBLE PRECISION,
fired_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
resolved_at TIMESTAMPTZ,
email_sent BOOLEAN NOT NULL DEFAULT FALSE,
webhook_sent BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ops_alert_events_rule_status ON ops_alert_events (rule_id, status);
CREATE INDEX IF NOT EXISTS idx_ops_alert_events_fired_at ON ops_alert_events (fired_at DESC);

View File

@@ -1,32 +0,0 @@
-- Seed default ops alert rules (idempotent)
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'Global success rate < 99%',
'Trigger when the 1-minute success rate drops below 99% for 2 consecutive minutes.',
TRUE,
'success_rate',
'<',
99,
1,
2,
'P1',
TRUE,
FALSE,
NULL,
10
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules);

View File

@@ -1,205 +0,0 @@
-- Seed additional ops alert rules (idempotent)
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'Global error rate > 1%',
'Trigger when the 1-minute error rate exceeds 1% for 2 consecutive minutes.',
TRUE,
'error_rate',
'>',
1,
1,
2,
'P1',
TRUE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
10
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'Global error rate > 1%');
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'P99 latency > 2000ms',
'Trigger when the 5-minute P99 latency exceeds 2000ms for 2 consecutive samples.',
TRUE,
'p99_latency_ms',
'>',
2000,
5,
2,
'P1',
TRUE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
15
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'P99 latency > 2000ms');
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'HTTP/2 errors > 20',
'Trigger when HTTP/2 errors exceed 20 in the last minute for 2 consecutive minutes.',
TRUE,
'http2_errors',
'>',
20,
1,
2,
'P2',
FALSE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
10
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'HTTP/2 errors > 20');
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'CPU usage > 85%',
'Trigger when CPU usage exceeds 85% for 5 consecutive minutes.',
TRUE,
'cpu_usage_percent',
'>',
85,
1,
5,
'P2',
FALSE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
15
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'CPU usage > 85%');
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'Memory usage > 90%',
'Trigger when memory usage exceeds 90% for 5 consecutive minutes.',
TRUE,
'memory_usage_percent',
'>',
90,
1,
5,
'P2',
FALSE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
15
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'Memory usage > 90%');
INSERT INTO ops_alert_rules (
name,
description,
enabled,
metric_type,
operator,
threshold,
window_minutes,
sustained_minutes,
severity,
notify_email,
notify_webhook,
webhook_url,
cooldown_minutes
)
SELECT
'Queue depth > 50',
'Trigger when concurrency queue depth exceeds 50 for 2 consecutive minutes.',
TRUE,
'concurrency_queue_depth',
'>',
50,
1,
2,
'P2',
FALSE,
CASE
WHEN (SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1) IS NULL THEN FALSE
ELSE TRUE
END,
(SELECT webhook_url FROM ops_alert_rules WHERE webhook_url IS NOT NULL AND webhook_url <> '' LIMIT 1),
10
WHERE NOT EXISTS (SELECT 1 FROM ops_alert_rules WHERE name = 'Queue depth > 50');

View File

@@ -1,7 +0,0 @@
-- Enable webhook notifications for rules with webhook_url configured
UPDATE ops_alert_rules
SET notify_webhook = TRUE
WHERE webhook_url IS NOT NULL
AND webhook_url <> ''
AND notify_webhook IS DISTINCT FROM TRUE;

View File

@@ -1,6 +0,0 @@
-- Add request counts to ops_system_metrics so the UI/alerts can distinguish "no traffic" from "healthy".
ALTER TABLE ops_system_metrics
ADD COLUMN IF NOT EXISTS request_count BIGINT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS success_count BIGINT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS error_count BIGINT NOT NULL DEFAULT 0;

View File

@@ -1,272 +0,0 @@
-- 运维监控中心 2.0 - 数据库 Schema 增强
-- 创建时间: 2026-01-02
-- 说明: 扩展监控指标,支持多维度分析和告警管理
-- ============================================
-- 1. 扩展 ops_system_metrics 表
-- ============================================
-- 添加 RED 指标列
ALTER TABLE ops_system_metrics
ADD COLUMN IF NOT EXISTS qps DECIMAL(10,2) DEFAULT 0,
ADD COLUMN IF NOT EXISTS tps DECIMAL(10,2) DEFAULT 0,
-- 错误分类
ADD COLUMN IF NOT EXISTS error_4xx_count BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS error_5xx_count BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS error_timeout_count BIGINT DEFAULT 0,
-- 延迟指标扩展
ADD COLUMN IF NOT EXISTS latency_p50 DECIMAL(10,2),
ADD COLUMN IF NOT EXISTS latency_p999 DECIMAL(10,2),
ADD COLUMN IF NOT EXISTS latency_avg DECIMAL(10,2),
ADD COLUMN IF NOT EXISTS latency_max DECIMAL(10,2),
-- 上游延迟
ADD COLUMN IF NOT EXISTS upstream_latency_avg DECIMAL(10,2),
-- 资源指标
ADD COLUMN IF NOT EXISTS disk_used BIGINT,
ADD COLUMN IF NOT EXISTS disk_total BIGINT,
ADD COLUMN IF NOT EXISTS disk_iops BIGINT,
ADD COLUMN IF NOT EXISTS network_in_bytes BIGINT,
ADD COLUMN IF NOT EXISTS network_out_bytes BIGINT,
-- 饱和度指标
ADD COLUMN IF NOT EXISTS goroutine_count INT,
ADD COLUMN IF NOT EXISTS db_conn_active INT,
ADD COLUMN IF NOT EXISTS db_conn_idle INT,
ADD COLUMN IF NOT EXISTS db_conn_waiting INT,
-- 业务指标
ADD COLUMN IF NOT EXISTS token_consumed BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS token_rate DECIMAL(10,2) DEFAULT 0,
ADD COLUMN IF NOT EXISTS active_subscriptions INT DEFAULT 0,
-- 维度标签 (支持多维度分析)
ADD COLUMN IF NOT EXISTS tags JSONB;
-- 添加 JSONB 索引以加速标签查询
CREATE INDEX IF NOT EXISTS idx_ops_metrics_tags ON ops_system_metrics USING GIN(tags);
-- 添加注释
COMMENT ON COLUMN ops_system_metrics.qps IS '每秒查询数 (Queries Per Second)';
COMMENT ON COLUMN ops_system_metrics.tps IS '每秒事务数 (Transactions Per Second)';
COMMENT ON COLUMN ops_system_metrics.error_4xx_count IS '客户端错误数量 (4xx)';
COMMENT ON COLUMN ops_system_metrics.error_5xx_count IS '服务端错误数量 (5xx)';
COMMENT ON COLUMN ops_system_metrics.error_timeout_count IS '超时错误数量';
COMMENT ON COLUMN ops_system_metrics.upstream_latency_avg IS '上游 API 平均延迟 (ms)';
COMMENT ON COLUMN ops_system_metrics.goroutine_count IS 'Goroutine 数量 (检测泄露)';
COMMENT ON COLUMN ops_system_metrics.tags IS '维度标签 (JSON), 如: {"account_id": "123", "api_path": "/v1/chat"}';
-- ============================================
-- 2. 创建维度统计表
-- ============================================
CREATE TABLE IF NOT EXISTS ops_dimension_stats (
id BIGSERIAL PRIMARY KEY,
timestamp TIMESTAMPTZ NOT NULL,
-- 维度类型: account, api_path, provider, region
dimension_type VARCHAR(50) NOT NULL,
dimension_value VARCHAR(255) NOT NULL,
-- 统计指标
request_count BIGINT DEFAULT 0,
success_count BIGINT DEFAULT 0,
error_count BIGINT DEFAULT 0,
success_rate DECIMAL(5,2),
error_rate DECIMAL(5,2),
-- 性能指标
latency_p50 DECIMAL(10,2),
latency_p95 DECIMAL(10,2),
latency_p99 DECIMAL(10,2),
-- 业务指标
token_consumed BIGINT DEFAULT 0,
cost_usd DECIMAL(10,4) DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 创建复合索引以加速维度查询
CREATE INDEX IF NOT EXISTS idx_ops_dim_type_value_time
ON ops_dimension_stats(dimension_type, dimension_value, timestamp DESC);
-- 创建单独的时间索引用于范围查询
CREATE INDEX IF NOT EXISTS idx_ops_dim_timestamp
ON ops_dimension_stats(timestamp DESC);
-- 添加注释
COMMENT ON TABLE ops_dimension_stats IS '多维度统计表,支持按账户/API/Provider等维度下钻分析';
COMMENT ON COLUMN ops_dimension_stats.dimension_type IS '维度类型: account(账户), api_path(接口), provider(上游), region(地域)';
COMMENT ON COLUMN ops_dimension_stats.dimension_value IS '维度值,如: 账户ID, /v1/chat, openai, us-east-1';
-- ============================================
-- 3. 创建告警规则表
-- ============================================
ALTER TABLE ops_alert_rules
ADD COLUMN IF NOT EXISTS dimension_filters JSONB,
ADD COLUMN IF NOT EXISTS notify_channels JSONB,
ADD COLUMN IF NOT EXISTS notify_config JSONB,
ADD COLUMN IF NOT EXISTS created_by VARCHAR(100),
ADD COLUMN IF NOT EXISTS last_triggered_at TIMESTAMPTZ;
-- ============================================
-- 4. 告警历史表 (使用现有的 ops_alert_events)
-- ============================================
-- 注意: 后端代码使用 ops_alert_events 表,不创建新表
-- ============================================
-- 5. 创建数据清理配置表
-- ============================================
CREATE TABLE IF NOT EXISTS ops_data_retention_config (
id SERIAL PRIMARY KEY,
table_name VARCHAR(100) NOT NULL UNIQUE,
retention_days INT NOT NULL, -- 保留天数
enabled BOOLEAN DEFAULT true,
last_cleanup_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- 插入默认配置
INSERT INTO ops_data_retention_config (table_name, retention_days) VALUES
('ops_system_metrics', 30), -- 系统指标保留 30 天
('ops_dimension_stats', 30), -- 维度统计保留 30 天
('ops_error_logs', 30), -- 错误日志保留 30 天
('ops_alert_events', 90), -- 告警事件保留 90 天
('usage_logs', 90) -- 使用日志保留 90 天
ON CONFLICT (table_name) DO NOTHING;
COMMENT ON TABLE ops_data_retention_config IS '数据保留策略配置表';
COMMENT ON COLUMN ops_data_retention_config.retention_days IS '数据保留天数,超过此天数的数据将被自动清理';
-- ============================================
-- 6. 创建辅助函数
-- ============================================
-- 函数: 计算健康度评分
-- 权重: SLA(40%) + 错误率(30%) + 延迟(20%) + 资源(10%)
CREATE OR REPLACE FUNCTION calculate_health_score(
p_success_rate DECIMAL,
p_error_rate DECIMAL,
p_latency_p99 DECIMAL,
p_cpu_usage DECIMAL
) RETURNS INT AS $$
DECLARE
sla_score INT;
error_score INT;
latency_score INT;
resource_score INT;
BEGIN
-- SLA 评分 (40分)
sla_score := CASE
WHEN p_success_rate >= 99.9 THEN 40
WHEN p_success_rate >= 99.5 THEN 35
WHEN p_success_rate >= 99.0 THEN 30
WHEN p_success_rate >= 95.0 THEN 20
ELSE 10
END;
-- 错误率评分 (30分)
error_score := CASE
WHEN p_error_rate <= 0.1 THEN 30
WHEN p_error_rate <= 0.5 THEN 25
WHEN p_error_rate <= 1.0 THEN 20
WHEN p_error_rate <= 5.0 THEN 10
ELSE 5
END;
-- 延迟评分 (20分)
latency_score := CASE
WHEN p_latency_p99 <= 500 THEN 20
WHEN p_latency_p99 <= 1000 THEN 15
WHEN p_latency_p99 <= 3000 THEN 10
WHEN p_latency_p99 <= 5000 THEN 5
ELSE 0
END;
-- 资源评分 (10分)
resource_score := CASE
WHEN p_cpu_usage <= 50 THEN 10
WHEN p_cpu_usage <= 70 THEN 7
WHEN p_cpu_usage <= 85 THEN 5
ELSE 2
END;
RETURN sla_score + error_score + latency_score + resource_score;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
COMMENT ON FUNCTION calculate_health_score IS '计算系统健康度评分 (0-100),权重: SLA 40% + 错误率 30% + 延迟 20% + 资源 10%';
-- ============================================
-- 7. 创建视图: 最新指标快照
-- ============================================
CREATE OR REPLACE VIEW ops_latest_metrics AS
SELECT
m.*,
calculate_health_score(
m.success_rate::DECIMAL,
m.error_rate::DECIMAL,
m.p99_latency_ms::DECIMAL,
m.cpu_usage_percent::DECIMAL
) AS health_score
FROM ops_system_metrics m
WHERE m.window_minutes = 1
AND m.created_at = (SELECT MAX(created_at) FROM ops_system_metrics WHERE window_minutes = 1)
LIMIT 1;
COMMENT ON VIEW ops_latest_metrics IS '最新的系统指标快照,包含健康度评分';
-- ============================================
-- 8. 创建视图: 活跃告警列表
-- ============================================
CREATE OR REPLACE VIEW ops_active_alerts AS
SELECT
e.id,
e.rule_id,
r.name AS rule_name,
r.metric_type,
e.fired_at,
e.metric_value,
e.threshold_value,
r.severity,
EXTRACT(EPOCH FROM (NOW() - e.fired_at))::INT AS duration_seconds
FROM ops_alert_events e
JOIN ops_alert_rules r ON e.rule_id = r.id
WHERE e.status = 'firing'
ORDER BY e.fired_at DESC;
COMMENT ON VIEW ops_active_alerts IS '当前活跃的告警列表';
-- ============================================
-- 9. 权限设置 (可选)
-- ============================================
-- 如果有专门的 ops 用户,可以授权
-- GRANT SELECT, INSERT, UPDATE ON ops_system_metrics TO ops_user;
-- GRANT SELECT, INSERT ON ops_dimension_stats TO ops_user;
-- GRANT ALL ON ops_alert_rules TO ops_user;
-- GRANT ALL ON ops_alert_events TO ops_user;
-- ============================================
-- 10. 数据完整性检查
-- ============================================
-- 确保现有数据的兼容性
UPDATE ops_system_metrics
SET
qps = COALESCE(request_count / (window_minutes * 60.0), 0),
error_rate = COALESCE((error_count::DECIMAL / NULLIF(request_count, 0)) * 100, 0)
WHERE qps = 0 AND request_count > 0;
-- ============================================
-- 完成
-- ============================================