fix: parse PR-Agent analytics record-wrapped JSON format

Real PR-Agent analytics logs wrap fields under 'record': {...}. The parser
now unwraps that before extracting command/pr_url/message/level, so
/api/analytics and /api/metrics show real data (verified with actual format
from production logs).
This commit is contained in:
asepharyana
2026-08-04 11:43:13 +07:00
parent efb99b73b7
commit 407819b647
+11 -2
View File
@@ -64,7 +64,14 @@ app.include_router(pr_router)
# ── Analytics / Metrics ───────────────────────────────────────────────────── # ── Analytics / Metrics ─────────────────────────────────────────────────────
def _read_analytics_logs(max_files: int = 5) -> list: def _read_analytics_logs(max_files: int = 5) -> list:
"""Parse PR-Agent analytics JSON logs (analytics=True records).""" """Parse PR-Agent analytics JSON logs (analytics=True records).
Real log lines look like:
{"text": "...", "record": {"elapsed": {...}, "extra": {"command": "...", "pr_url": "..."},
"file": {...}, "function": "...", "level": {"name": "INFO", ...},
"message": "...", "module": "...", "process": {...}, "thread": {...},
"time": {"repr": "2026-08-04 ...", "timestamp": ...}}}
"""
records = [] records = []
files = sorted(glob.glob(os.path.join(ANALYTICS_DIR, "pr-agent.*.log"))) files = sorted(glob.glob(os.path.join(ANALYTICS_DIR, "pr-agent.*.log")))
for f in files[-max_files:]: for f in files[-max_files:]:
@@ -78,7 +85,9 @@ def _read_analytics_logs(max_files: int = 5) -> list:
rec = json.loads(line) rec = json.loads(line)
except json.JSONDecodeError: except json.JSONDecodeError:
continue continue
# Normalize structure: {message, extra{...}} # PR-Agent wraps under "record": {...}
if "record" in rec and isinstance(rec["record"], dict):
rec = rec["record"]
extra = rec.get("extra", {}) or {} extra = rec.get("extra", {}) or {}
if "artifact" in extra and isinstance(extra["artifact"], dict): if "artifact" in extra and isinstance(extra["artifact"], dict):
extra.update(extra.pop("artifact")) extra.update(extra.pop("artifact"))