mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-11 13:57:32 +01:00
Around twenty things could be said about a day. Saying all of them is a wall nobody reads; saying whichever three are computed first is arbitrary. So each candidate declares a priority and a magnitude and the picker takes the top few, with two rules that make the result readable: one line per family, so "set a record", "had 60% of the day" and "kept the crown" do not become three sentences about the same person; and fully deterministic ordering, so a day renders identically twice. Nine candidates so far: personal records with what they beat, the group's busiest day ever, a photo record, crown streaks (naming everyone when it is shared), a silence broken, a monologue, one person dominating the day, an unusual showing for that weekday, and a night owl. Every candidate is (DayFacts) -> Highlight | None with the same signature. On day one of this release almost all of them return None, and a shape where "not yet" is the ordinary case is much easier to get right than one where every guard is bespoke. Two off-by-ones fixed by writing the sentence out and checking it: - "odezwał się po 3 dniach ciszy" was counting four. The day somebody last spoke is not silent, and neither is the day they came back on, so the gap excludes both ends. The function is now named for that. - A personal record on somebody's first ever day was being announced as a record. It is, trivially, and saying so every time is noise. Verified against the live database, read-only on a copy: the real group renders correctly at 324 messages, claims no average length for the backfilled day, and fires no highlights at all -- which is right, since there is one day of history for them to stand against. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Everything known about one day, gathered once.
|
|
|
|
The candidates in ``curiosities`` each need some slice of the same handful of
|
|
queries. Gathering them once and passing this around keeps the night's work to
|
|
a fixed number of queries however many candidates are added later, and gives
|
|
every candidate the same uniform signature -- which is what makes the "not
|
|
enough history yet" guards mechanical to write and to test.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING
|
|
|
|
from signal_bot.stats import records, streaks
|
|
from signal_bot.stats.periods import weekday_of
|
|
from signal_bot.stats.queries_day import DayTotals, day_totals
|
|
from signal_bot.stats.queries_period import every_day, weekday_buckets
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.stats.events import EventStore
|
|
from signal_bot.stats.queries_day import DayRow
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class DayFacts:
|
|
"""One day, plus the history it has to be interesting against."""
|
|
|
|
day: str
|
|
rows: list[DayRow]
|
|
totals: DayTotals
|
|
# Messages per recorded day across the whole history, the backfilled day
|
|
# already removed. Missing days are silences, not zeros.
|
|
history: dict[str, int]
|
|
winners: dict[str, list[tuple[str, str]]]
|
|
events: EventStore
|
|
group_id: str
|
|
exclude: str | None
|
|
|
|
@property
|
|
def leaders(self) -> list[tuple[str, str]]:
|
|
"""Whoever holds today's crown, as (uuid, display name). Ties included."""
|
|
return self.winners.get(self.day, [])
|
|
|
|
@property
|
|
def speakers(self) -> list[DayRow]:
|
|
"""Everyone who actually wrote today, most talkative first."""
|
|
return [row for row in self.rows if not row.is_silent]
|
|
|
|
@property
|
|
def weekday(self) -> int:
|
|
"""0 for Monday through 6 for Sunday."""
|
|
return weekday_of(self.day)
|
|
|
|
@property
|
|
def same_weekday_before(self) -> list[int]:
|
|
"""Every earlier day that fell on the same weekday.
|
|
|
|
Today is excluded, so "wtorki mają średnio 74" is a claim about the
|
|
other Tuesdays rather than one that quietly includes the day it is
|
|
being compared against.
|
|
"""
|
|
buckets = weekday_buckets({d: n for d, n in self.history.items() if d < self.day})
|
|
return buckets.get(self.weekday, [])
|
|
|
|
def person_days(self, uuid: str) -> dict[str, int]:
|
|
"""One person's messages per day, across the whole history."""
|
|
rows = self.events.connection.execute(
|
|
"""
|
|
SELECT day, COUNT(*) AS messages
|
|
FROM message_events
|
|
WHERE group_id = ? AND uuid = ? AND day <> ?
|
|
GROUP BY day
|
|
""",
|
|
(self.group_id, uuid, self.exclude or ""),
|
|
).fetchall()
|
|
return {r["day"]: r["messages"] for r in rows}
|
|
|
|
|
|
def gather(
|
|
events: EventStore,
|
|
group_id: str,
|
|
day: str,
|
|
rows: list[DayRow],
|
|
*,
|
|
exclude: str | None = None,
|
|
) -> DayFacts:
|
|
"""Run the shared queries once and bundle them up."""
|
|
return DayFacts(
|
|
day=day,
|
|
rows=rows,
|
|
totals=day_totals(rows),
|
|
history=every_day(events, group_id, exclude=exclude),
|
|
winners=streaks.daily_winners(events, group_id, exclude=exclude),
|
|
events=events,
|
|
group_id=group_id,
|
|
exclude=exclude,
|
|
)
|
|
|
|
|
|
def personal_record(facts: DayFacts, uuid: str) -> records.DayRecord | None:
|
|
"""Today's count, if it is that person's best ever, else None."""
|
|
best = records.personal_best(facts.events, facts.group_id, uuid, exclude=facts.exclude)
|
|
if best is None or best.day != facts.day:
|
|
return None
|
|
return best
|