#!/bin/bash
export LC_ALL=C
python3 - <<'PY'
import sqlite3, re
c=sqlite3.connect('/tmp/n8n_exec.db')
rows=c.execute("""
  select e.id, e.status, e.startedAt, d.data from execution_entity e
  join execution_data d on d.executionId=e.id order by e.id desc limit 40""").fetchall()

print(f"scanned {len(rows)} executions for error signatures\n")
pats = {
    'EAI_AGAIN': r'EAI_AGAIN',
    'fetch failed': r'fetch failed',
    'ECONNREFUSED': r'ECONNREFUSED',
    'ollama/11434': r'(11434|ollama)',
    'qdrant/6333': r'(6333|qdrant)',
    '503/502': r'\[(502|503)\]',
}
for eid, status, started, raw in rows:
    if not raw: continue
    text = raw if isinstance(raw, str) else str(raw)
    hits = {k: len(re.findall(v, text, re.I)) for k, v in pats.items()}
    interesting = {k: v for k, v in hits.items() if v and k not in ('ollama/11434', 'qdrant/6333')}
    if interesting or status == 'error':
        print(f"exec {eid} [{status}] {started}")
        print(f"   {hits}")

print("\n--- exact snippets mentioning a failure cause ---")
for eid, status, started, raw in rows:
    text = raw if isinstance(raw, str) else str(raw)
    for m in re.finditer(r'\[(?:502|503)\][^"]{0,160}', text):
        print(f"  exec {eid} [{status}]: {m.group(0)[:170]}")
        break
PY
