\pset pager off
\pset tuples_only on
\pset fieldsep '\t'
-- 昨日 ops_error_logs 总量与 stream/error sending 相关量
SELECT 'ops_total', count(*) FROM ops_error_logs
WHERE created_at >= TIMESTAMPTZ '2026-05-11 00:00:00+08' AND created_at < TIMESTAMPTZ '2026-05-12 00:00:00+08';
SELECT 'stream_any_message', count(*) FROM ops_error_logs
WHERE created_at >= TIMESTAMPTZ '2026-05-11 00:00:00+08' AND created_at < TIMESTAMPTZ '2026-05-12 00:00:00+08'
  AND (error_message ILIKE '%stream disconnected%' OR upstream_error_message ILIKE '%stream disconnected%' OR error_body ILIKE '%stream disconnected%' OR upstream_error_detail ILIKE '%stream disconnected%');
SELECT 'sending_request_any', count(*) FROM ops_error_logs
WHERE created_at >= TIMESTAMPTZ '2026-05-11 00:00:00+08' AND created_at < TIMESTAMPTZ '2026-05-12 00:00:00+08'
  AND (error_message ILIKE '%error sending request for url%' OR upstream_error_message ILIKE '%error sending request for url%' OR error_body ILIKE '%error sending request for url%' OR upstream_error_detail ILIKE '%error sending request for url%');
SELECT 'before_completion_any', count(*) FROM ops_error_logs
WHERE created_at >= TIMESTAMPTZ '2026-05-11 00:00:00+08' AND created_at < TIMESTAMPTZ '2026-05-12 00:00:00+08'
  AND (error_message ILIKE '%before completion%' OR upstream_error_message ILIKE '%before completion%' OR error_body ILIKE '%before completion%' OR upstream_error_detail ILIKE '%before completion%');
-- 按错误文本聚合 Top
SELECT 'TOP_MSG', count(*), coalesce(error_phase,''), coalesce(error_owner,''), coalesce(status_code,0), coalesce(upstream_status_code,0), left(coalesce(error_message,''),180)
FROM ops_error_logs
WHERE created_at >= TIMESTAMPTZ '2026-05-11 00:00:00+08' AND created_at < TIMESTAMPTZ '2026-05-12 00:00:00+08'
GROUP BY 3,4,5,6,7 ORDER BY count(*) DESC LIMIT 30;
