signal-bot/signal_bot/stats/nightly.py
Krzysztof kuhy Rudnicki 423db74788 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

72 lines
2.5 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Which days still owe the group a report, and posting them.
The event log already survives an outage: a message carries Signal's own send
timestamp, signal-cli keeps receiving while the bot is down, and the replay on
reconnect is absorbed by the primary key. So the *counts* come back on their
own, filed under the day they were actually sent.
The *report* did not. A bot that was down at 21:37 simply never posted that
day, and nothing ever went back for it -- the old counter merged the day into
the next one instead, which is the bug this release exists to remove. So the
last day reported is written down per group, and every day since then gets
posted when the bot comes back.
Bounded, because coming back from a fortnight offline should not open with a
fortnight of back-posts.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Final
from signal_bot.stats.periods import shift
if TYPE_CHECKING:
from signal_bot.store import Store
logger = logging.getLogger(__name__)
LAST_REPORTED_KEY: Final = "last_reported_day"
# At most a week of catch-up in one go. Long enough to cover a weekend
# outage nobody noticed, short enough that a month offline is not a wall.
CATCHUP_LIMIT: Final = 7
def days_to_report(store: Store, group_id: str, latest: str) -> list[str]:
"""Every day up to ``latest`` that has not been reported yet, oldest first.
Args:
store: The open database, which holds the per-group marker.
group_id: The group being reported on.
latest: The most recent day eligible for a report, normally yesterday.
Returns:
The days to post, oldest first. A group with no marker gets only
``latest``: the marker is absent on a brand-new group and on the first
run after this release, and neither should trigger a backlog.
"""
last = store.meta(group_id, LAST_REPORTED_KEY)
if last is None:
return [latest]
if last >= latest:
return []
days = []
day = shift(last, 1)
while day <= latest:
days.append(day)
day = shift(day, 1)
return days[-CATCHUP_LIMIT:]
def mark_reported(store: Store, group_id: str, day: str) -> None:
"""Record that a day no longer owes this group a report.
Called for a day that was posted *and* for a day nobody spoke on -- an
empty day is settled, not pending, and leaving it unmarked would make the
bot reconsider it every night forever.
"""
store.set_meta(group_id, LAST_REPORTED_KEY, day)