#!/bin/bash
export LC_ALL=C
echo "===== ERRORS BY NODE ACROSS LAST 40 EXECUTIONS ====="
python3 - <<'PY'
import sqlite3, json
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 eid, status, started, raw in rows:
    try:
        doc=json.loads(raw)[0]
    except Exception:
        continue
    rd=doc.get('resultData') or {}
    err=rd.get('error')
    node_errs=[]
    if isinstance(err, dict):
        n=err.get('node'); n=n.get('name') if isinstance(n,dict) else n
        node_errs.append((n, str(err.get('message'))[:120]))
    elif err:
        node_errs.append(('?', str(err)[:120]))
    for n, runs in (rd.get('runData') or {}).items():
        for r in runs or []:
            e=(r or {}).get('error')
            if e:
                node_errs.append((n, str(e.get('message'))[:120]))
    if node_errs:
        print(f"\n  exec {eid} [{status}] {started}")
        for n,m in node_errs[:4]:
            print(f"    {n}: {m}")
PY

echo
echo "===== QDRANT / OLLAMA NODE HEALTH ====="
python3 - <<'PY'
import sqlite3, json, re
c=sqlite3.connect('/tmp/n8n_exec.db')
rows=c.execute("""
  select e.id, d.data from execution_entity e
  join execution_data d on d.executionId=e.id
  where e.status='success' order by e.id desc limit 3""").fetchall()
for eid, raw in rows:
    doc=json.loads(raw)[0]
    rd=doc.get('resultData') or {}
    nodes=list((rd.get('runData') or {}).keys())
    interesting=[n for n in nodes if re.search(r'qdr|ollam|embed|vector|Sari', n, re.I)]
    print(f"  exec {eid}: {len(nodes)} nodes, vector/embed nodes = {interesting}")
PY
