signal-bot/tests/test_stats_backfill.py
Krzysztof kuhy Rudnicki 63f262bddd feat: survive an outage, and keep the counts that predate the event log
Two gaps kuhy pointed at, both about not losing messages the bot was not
awake for.

Missed *counts* already worked and now have a test saying so: an event
is keyed by Signal's own send timestamp, signal-cli keeps receiving in
json-rpc mode while the bot is down, and the replay on reconnect is
absorbed by the primary key. Messages sent during an outage come back
filed under the day they were actually sent.

Missed *reports* did not. A bot that was down at 21:37 never posted that
day and never went back for it. The last day reported is now written
down per group, so returning from an outage posts every day it owes,
oldest first, bounded at a week. A day nobody spoke on is marked settled
rather than reconsidered forever; a day whose send *failed* is
deliberately left unmarked so the next run retries it.

The pre-0.1.0 counter is carried over rather than discarded: 342
messages across four groups, 319 of them in the real group. What the
counter cannot say is when, so before assuming, four places were checked
for a real inbound timestamp -- journald (commands only), signal-cli's
account.db (19 tables, none inbound; message_send_log is the bot's own
outgoing resend buffer), Signal Desktop (unlinked, empty, and it does
not backfill on link), and signal-api/attachments (the one dated
artifact). The attachment mtimes put 12 files on 29 Aug against 79 on
30 Aug inside the counter's window, which is why the whole counter is
filed under 30 Aug rather than split.

That day is marked in group_meta, and a marked day claims no average
message length -- those messages have a count but no lengths, so
"średnio 0 znaków" would be a measurement nobody took. The mark is also
what will keep the day out of personal records: a merged 22-hour day
would otherwise hand somebody a record they never set.

render_day's options move into a DayView value object; the keyword list
had reached eight and this renderer is about to be shared by several
commands that each want a different subset.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 19:23:53 +02:00

115 lines
4.0 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Carrying the pre-0.1.0 counter into the event log."""
from __future__ import annotations
from typing import TYPE_CHECKING
from signal_bot.stats.backfill import (
BACKFILL_HOUR,
backfilled_day,
has_legacy_counter,
seed_from_counter,
)
from signal_bot.stats.queries_day import day_rows, day_totals
if TYPE_CHECKING:
from signal_bot.store import Store
GROUP = "group-a=="
OTHER = "group-b=="
DAY = "2026-08-30"
LEGACY = """
CREATE TABLE message_counts (
group_id TEXT NOT NULL,
uuid TEXT NOT NULL,
display_name TEXT NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (group_id, uuid)
);
"""
def give_legacy_counter(store: Store, rows: list[tuple[str, str, str, int]]) -> None:
"""Recreate the pre-0.1.0 table and fill it, as a real old database has."""
store.events.connection.executescript(LEGACY)
store.events.connection.executemany(
"INSERT INTO message_counts (group_id, uuid, display_name, count) VALUES (?, ?, ?, ?)",
rows,
)
store.events.connection.commit()
def test_a_fresh_database_has_no_legacy_counter(store: Store) -> None:
"""0.1.0 stopped declaring the table, so a new file never has one."""
assert has_legacy_counter(store) is False
def test_an_old_database_is_detected(store: Store) -> None:
give_legacy_counter(store, [(GROUP, "u1", "Ala", 3)])
assert has_legacy_counter(store) is True
def test_counts_become_events_on_the_named_day(store: Store) -> None:
give_legacy_counter(
store,
[(GROUP, "u1", "Ala", 3), (GROUP, "u2", "kuhy", 2), (OTHER, "u1", "Ala", 1)],
)
seeded = seed_from_counter(store, DAY)
assert seeded == {GROUP: 5, OTHER: 1}
rows = day_rows(store.events, GROUP, DAY)
assert [(r.display_name, r.messages) for r in rows] == [("Ala", 3), ("kuhy", 2)]
assert day_totals(rows).messages == 5
def test_the_seeded_day_is_marked_per_group(store: Store) -> None:
"""Marked, so records and hour statistics can leave it out."""
give_legacy_counter(store, [(GROUP, "u1", "Ala", 1), (OTHER, "u2", "kuhy", 1)])
seed_from_counter(store, DAY)
assert backfilled_day(store, GROUP) == DAY
assert backfilled_day(store, OTHER) == DAY
def test_an_unseeded_group_is_not_marked(store: Store) -> None:
give_legacy_counter(store, [(GROUP, "u1", "Ala", 1)])
seed_from_counter(store, DAY)
assert backfilled_day(store, OTHER) is None
def test_zero_counts_are_skipped(store: Store) -> None:
"""A row at zero is somebody who was reset, not somebody who spoke."""
give_legacy_counter(store, [(GROUP, "u1", "Ala", 0)])
assert seed_from_counter(store, DAY) == {}
assert day_rows(store.events, GROUP, DAY) == []
assert backfilled_day(store, GROUP) is None
def test_seeded_people_are_known_to_the_roster(store: Store) -> None:
"""Otherwise they would show as unnamed strangers in the first report."""
give_legacy_counter(store, [(GROUP, "u1", "Ala", 2)])
seed_from_counter(store, DAY)
assert [p.display_name for p in store.events.roster(GROUP, ["u1"])] == ["Ala"]
def test_seeded_events_carry_no_invented_detail(store: Store) -> None:
"""No hours, no lengths, no media -- the counter knew none of them."""
give_legacy_counter(store, [(GROUP, "u1", "Ala", 2)])
seed_from_counter(store, DAY)
rows = store.events.connection.execute(
"SELECT local_hour, char_len, media_count, is_command FROM message_events"
).fetchall()
assert len(rows) == 2
for row in rows:
assert row["local_hour"] == BACKFILL_HOUR
assert (row["char_len"], row["media_count"], row["is_command"]) == (0, 0, 0)
def test_seeding_twice_does_not_double_the_totals(store: Store) -> None:
"""The synthetic keys are deterministic, so a rerun is absorbed."""
give_legacy_counter(store, [(GROUP, "u1", "Ala", 3)])
seed_from_counter(store, DAY)
seed_from_counter(store, DAY)
assert day_totals(day_rows(store.events, GROUP, DAY)).messages == 3