\pset pager off
WITH w AS (
  SELECT * FROM ops_error_logs
  WHERE created_at >= now() - interval '2 hours'
)
SELECT 'last2h_total' AS k, count(*)::text AS v FROM w;
WITH w AS (SELECT * FROM ops_error_logs WHERE created_at >= now() - interval '2 hours')
SELECT error_phase, error_owner, COALESCE(status_code,0) AS status_code, COALESCE(upstream_status_code,0) AS upstream_status_code, count(*)
FROM w GROUP BY 1,2,3,4 ORDER BY count(*) DESC LIMIT 25;
WITH w AS (SELECT * FROM ops_error_logs WHERE created_at >= now() - interval '2 hours')
SELECT COALESCE(group_id,0) AS group_id, COALESCE(account_id,0) AS account_id, model, error_phase, error_owner, COALESCE(status_code,0) AS status_code, COALESCE(upstream_status_code,0) AS upstream_status_code, left(error_message,160) AS msg, count(*)
FROM w
WHERE NOT (
  error_message ILIKE '%API key required%' OR
  error_message ILIKE '%Invalid API key%' OR
  error_message ILIKE '%API key disabled%' OR
  error_message ILIKE '%No active subscription%' OR
  error_message ILIKE '%Insufficient account balance%'
)
GROUP BY 1,2,3,4,5,6,7,8 ORDER BY count(*) DESC LIMIT 50;
WITH w AS (SELECT * FROM ops_error_logs WHERE created_at >= now() - interval '2 hours')
SELECT COALESCE(group_id,0) AS group_id, model, left(error_message,200) AS msg, count(*)
FROM w
WHERE error_message ILIKE '%no available OpenAI accounts%' OR error_message ILIKE '%context canceled%'
GROUP BY 1,2,3 ORDER BY count(*) DESC;
SELECT count(*) AS stream_disconnected_last24h FROM ops_error_logs WHERE created_at >= now() - interval '24 hours' AND error_message ILIKE '%stream disconnected before completion: error sending request for url%';
