\pset pager off
\x on
SELECT e.id,
       to_char(e.created_at at time zone 'Asia/Shanghai','YYYY-MM-DD HH24:MI:SS') AS created_at_shanghai,
       e.request_id, e.client_request_id,
       e.user_id, u.email AS user_email,
       e.api_key_id, k.name AS api_key_name, k.group_id AS api_key_group_id, k.status AS api_key_status,
       e.account_id, a.name AS account_name, a.platform AS account_platform, a.type AS account_type,
       a.status AS account_status_now, a.schedulable AS account_schedulable,
       a.extra->>'base_url' AS account_extra_base_url,
       a.credentials->>'base_url' AS account_credentials_base_url,
       e.group_id,
       e.platform, e.model, e.request_path, e.stream,
       e.error_phase, e.error_type, e.severity, e.status_code, e.error_source, e.error_owner,
       e.account_status AS account_status_at_error,
       e.upstream_status_code, e.provider_error_code, e.provider_error_type, e.network_error_type,
       e.is_retryable, e.retry_count, e.duration_ms, e.auth_latency_ms, e.routing_latency_ms, e.upstream_latency_ms,
       left(coalesce(e.error_message,''), 800) AS error_message_preview,
       left(coalesce(e.upstream_error_message,''), 800) AS upstream_error_message_preview,
       left(coalesce(e.upstream_error_detail,''), 800) AS upstream_error_detail_preview,
       left(coalesce(e.error_body::text,''), 1000) AS error_body_preview,
       left(coalesce(e.upstream_errors::text,''), 2000) AS upstream_errors_preview
FROM public.ops_error_logs e
LEFT JOIN public.users u ON u.id=e.user_id
LEFT JOIN public.api_keys k ON k.id=e.api_key_id
LEFT JOIN public.accounts a ON a.id=e.account_id
WHERE e.request_id = 'eeac27ca-421d-405a-ad62-41087aa491c7'
   OR e.client_request_id = 'eeac27ca-421d-405a-ad62-41087aa491c7'
ORDER BY e.created_at DESC
LIMIT 5;
\x off
SELECT 'nearby_errors' AS section,
       to_char(created_at at time zone 'Asia/Shanghai','YYYY-MM-DD HH24:MI:SS') AS t,
       request_id, user_id, api_key_id, account_id, group_id, model, request_path,
       error_phase, status_code, upstream_status_code, provider_error_code,
       left(coalesce(upstream_error_message,error_message,''), 180) AS msg
FROM public.ops_error_logs
WHERE created_at >= now() - interval '2 hours'
  AND request_path IN ('/v1/responses','/responses')
ORDER BY created_at DESC
LIMIT 20;
