\pset tuples_only on
\pset format unaligned
BEGIN READ ONLY;
select 'audit_model|' || coalesce(model,'') || '|' || count(*)
from public.request_audit_logs
where created_at >= now() - interval '6 hours'
  and path = '/v1/messages'
  and (model ilike '%deepseek%' or model ilike 'claude%')
group by model
order by count(*) desc, model;
select 'ops_deepseek|' || coalesce(request_path,'') || '|' || coalesce(model,'') || '|status=' || coalesce(status_code::text,'') || '|phase=' || coalesce(error_phase,'') || '|owner=' || coalesce(error_owner,'') || '|upstream=' || coalesce(upstream_status_code::text,'') || '|account=' || coalesce(account_id::text,'NULL') || '|group=' || coalesce(group_id::text,'NULL') || '|count=' || count(*) || '|msg=' || regexp_replace(left(coalesce(error_message,upstream_error_message,error_body,''),180),'[\n\r\t]+',' ','g')
from public.ops_error_logs
where created_at >= now() - interval '6 hours'
  and (model ilike '%deepseek-v4%' or error_message ilike '%deepseek-v4%' or error_body ilike '%deepseek-v4%')
group by request_path,model,status_code,error_phase,error_owner,upstream_status_code,account_id,group_id,regexp_replace(left(coalesce(error_message,upstream_error_message,error_body,''),180),'[\n\r\t]+',' ','g')
order by count(*) desc
limit 30;
select 'ops_latest|' || to_char(created_at at time zone 'Asia/Shanghai','YYYY-MM-DD HH24:MI:SS') || '|path=' || coalesce(request_path,'') || '|model=' || coalesce(model,'') || '|status=' || coalesce(status_code::text,'') || '|account=' || coalesce(account_id::text,'NULL') || '|group=' || coalesce(group_id::text,'NULL') || '|msg=' || regexp_replace(left(coalesce(error_message,upstream_error_message,error_body,''),220),'[\n\r\t]+',' ','g')
from public.ops_error_logs
where created_at >= now() - interval '6 hours'
  and (model ilike '%deepseek-v4%' or error_message ilike '%deepseek-v4%' or error_body ilike '%deepseek-v4%')
order by created_at desc
limit 10;
COMMIT;
