Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/diagrams/analyze_bi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import json
import re
import nbformat
from collections import defaultdict
from pathlib import Path
from datetime import datetime
Expand Down Expand Up @@ -61,8 +60,10 @@ def parse_all():
with open(lf) as f:
for line in f:
if line.strip():
try: events.append(json.loads(line))
except: pass
try:
events.append(json.loads(line))
except json.JSONDecodeError:
pass
if not events:
continue

Expand Down Expand Up @@ -134,6 +135,8 @@ def parse_all():
# Build notebook
# ---------------------------------------------------------------------------
def build_notebook(records):
import nbformat # optional dependency, only needed for notebook generation

nb = nbformat.v4.new_notebook()
data_json = json.dumps(records, indent=2)

Expand Down
35 changes: 35 additions & 0 deletions tests/test_diagrams_analyze_bi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Regression tests for the BI analysis diagram script."""

import importlib.util
import json
import sys
from pathlib import Path


def _load_analyze_bi():
spec = importlib.util.spec_from_file_location(
"analyze_bi",
Path(__file__).parent.parent / "examples" / "diagrams" / "analyze_bi.py",
)
module = importlib.util.module_from_spec(spec)
sys.modules["analyze_bi"] = module
spec.loader.exec_module(module)
return module


def test_parse_all_skips_invalid_jsonl_lines(tmp_path, monkeypatch):
"""Malformed JSON lines should be ignored instead of crashing the parser."""
module = _load_analyze_bi()

log_file = tmp_path / "cat-issue-iter1.jsonl"
log_file.write_text(
json.dumps({"event": "start", "ts": 1.0, "model": "test"}) + "\n"
+ "this is not valid json\n"
+ json.dumps({"event": "end", "ts": 2.0}) + "\n"
)
monkeypatch.setattr(module, "LOG_DIR", tmp_path)

records = module.parse_all()

assert len(records) == 1
assert records[0]["model"] == "test"
Loading