askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_change_log_tail.py · 4.4 KB · 107 lines
| 1 | """Reading the end of a log without reading all of it. |
| 2 | |
| 3 | The activity page stopped finishing. Its own docstring had predicted why: |
| 4 | "reads each collection's whole change log... fine at current scale... |
| 5 | future work if/when logs grow large." Logs grew large -- 944MB for one |
| 6 | derived collection -- and the feed was parsing a gigabyte of JSON on a |
| 7 | single core to show fifty rows. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | |
| 12 | import object_activity |
| 13 | import object_record_changes |
| 14 | |
| 15 | |
| 16 | def write_log(tmp_path, collection, count, *, actor="dana"): |
| 17 | path = object_record_changes.record_changes_file(collection, base_dir=tmp_path) |
| 18 | path.parent.mkdir(parents=True, exist_ok=True) |
| 19 | with path.open("w", encoding="utf-8") as handle: |
| 20 | for i in range(count): |
| 21 | handle.write(json.dumps({ |
| 22 | "change_id": f"c{i}", "collection": collection, |
| 23 | "record_id": f"r{i}", "action": "update", "actor": actor, |
| 24 | "timestamp": f"2026-07-{(i % 28) + 1:02d}T00:00:{i % 60:02d}Z", |
| 25 | # Padding, so the file is comfortably larger than one read |
| 26 | # block and the tail path is actually exercised. |
| 27 | "snapshot": {"note": "x" * 400}, |
| 28 | })) |
| 29 | handle.write("\n") |
| 30 | return path |
| 31 | |
| 32 | |
| 33 | def test_the_tail_returns_the_newest_entries(tmp_path): |
| 34 | write_log(tmp_path, "notes", 2000) |
| 35 | payload = object_record_changes.list_record_changes( |
| 36 | "notes", base_dir=tmp_path, limit=10, tail_only=True) |
| 37 | |
| 38 | assert [c["record_id"] for c in payload["changes"]] == [ |
| 39 | f"r{i}" for i in range(1999, 1989, -1)] |
| 40 | |
| 41 | |
| 42 | def test_the_tail_agrees_with_the_full_read(tmp_path): |
| 43 | """The optimisation may be faster; it may not be different.""" |
| 44 | write_log(tmp_path, "notes", 2000) |
| 45 | full = object_record_changes.list_record_changes( |
| 46 | "notes", base_dir=tmp_path, limit=25) |
| 47 | tail = object_record_changes.list_record_changes( |
| 48 | "notes", base_dir=tmp_path, limit=25, tail_only=True) |
| 49 | |
| 50 | assert tail["changes"] == full["changes"] |
| 51 | |
| 52 | |
| 53 | def test_a_partial_first_line_is_never_parsed_as_a_record(tmp_path): |
| 54 | """Reading backwards lands mid-line; that fragment must be dropped, not |
| 55 | silently swallowed as a corrupt entry that shifts every row after it.""" |
| 56 | write_log(tmp_path, "notes", 3000) |
| 57 | payload = object_record_changes.list_record_changes( |
| 58 | "notes", base_dir=tmp_path, limit=100, tail_only=True) |
| 59 | |
| 60 | assert len(payload["changes"]) == 100 |
| 61 | assert all(c["record_id"].startswith("r") for c in payload["changes"]) |
| 62 | |
| 63 | |
| 64 | def test_a_small_log_still_reads_whole(tmp_path): |
| 65 | write_log(tmp_path, "notes", 3) |
| 66 | payload = object_record_changes.list_record_changes( |
| 67 | "notes", base_dir=tmp_path, limit=50, tail_only=True) |
| 68 | assert payload["total"] == 3 |
| 69 | |
| 70 | |
| 71 | def test_a_record_scoped_query_still_scans(tmp_path): |
| 72 | """One record's entries can be anywhere in the file. Answering "show me |
| 73 | this invoice's history" from the tail alone would lose the half that |
| 74 | matters, so tail_only deliberately does not apply.""" |
| 75 | write_log(tmp_path, "notes", 2000) |
| 76 | payload = object_record_changes.list_record_changes( |
| 77 | "notes", base_dir=tmp_path, record_id="r3", limit=10, tail_only=True) |
| 78 | assert [c["record_id"] for c in payload["changes"]] == ["r3"] |
| 79 | |
| 80 | |
| 81 | def test_the_default_is_still_a_full_read(tmp_path): |
| 82 | """tail_only is opt-in because it makes `total` a count of what was |
| 83 | READ. A feed does not care; an audit page showing "1 of 4,312" does.""" |
| 84 | write_log(tmp_path, "notes", 500) |
| 85 | assert object_record_changes.list_record_changes( |
| 86 | "notes", base_dir=tmp_path, limit=10)["total"] == 500 |
| 87 | |
| 88 | |
| 89 | # --- what belongs in a feed at all --------------------------------------------- |
| 90 | |
| 91 | def test_machine_churn_is_not_activity(tmp_path): |
| 92 | """A rollup rewriting its own output is not something a person did, and |
| 93 | a feed that is 99% machine churn is a feed nobody reads.""" |
| 94 | write_log(tmp_path, "notes", 5, actor="dana") |
| 95 | write_log(tmp_path, "analytics_top_paths", 400, actor="system:rollup") |
| 96 | |
| 97 | feed = object_activity.recent_activity(base_dir=tmp_path, limit=50) |
| 98 | assert {entry["collection"] for entry in feed} == {"notes"} |
| 99 | |
| 100 | |
| 101 | def test_a_persons_own_changes_still_arrive(tmp_path): |
| 102 | write_log(tmp_path, "notes", 5, actor="dana") |
| 103 | write_log(tmp_path, "invoices", 5, actor="sam") |
| 104 | |
| 105 | mine = object_activity.recent_activity(base_dir=tmp_path, actor="dana", limit=50) |
| 106 | assert {entry["collection"] for entry in mine} == {"notes"} |
| 107 | assert len(mine) == 5 |