Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

71 lines
2.4 KiB
Python

from datetime import UTC, datetime, timedelta
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from croniter import croniter # type: ignore[import-untyped,unused-ignore]
from dateutil.relativedelta import relativedelta
from products.reminders.backend.constants import MAX_FIRES_PER_DAY
UTC_ZONE = ZoneInfo("UTC")
def resolve_timezone(tz_name: str | None) -> ZoneInfo:
if not tz_name:
return UTC_ZONE
try:
return ZoneInfo(tz_name)
except ZoneInfoNotFoundError:
return UTC_ZONE
def _next_interval(current: datetime, interval: str) -> datetime:
if interval == "daily":
return current + relativedelta(days=1)
if interval == "weekly":
return current + relativedelta(weeks=1)
if interval == "monthly":
return current + relativedelta(months=1)
if interval == "yearly":
return current + relativedelta(years=1)
raise ValueError(f"Unknown recurrence interval: {interval}")
def _next_cron(cron_expression: str, current: datetime, tz: ZoneInfo) -> datetime:
reference = current.astimezone(tz)
return croniter(cron_expression, reference).get_next(datetime).astimezone(UTC)
def compute_next_fire_at(
current: datetime,
*,
interval: str | None,
cron_expression: str | None,
tz: ZoneInfo,
) -> datetime:
if cron_expression:
return _next_cron(cron_expression, current, tz)
if interval:
return _next_interval(current, interval)
raise ValueError("Recurring reminder requires interval or cron_expression")
def exceeds_daily_frequency_cap(cron_expression: str) -> bool:
# Count fires across a representative 24h window for every weekday, so a cron pinned
# to a single day (e.g. "* * * * 2") can't slip past the cap by avoiding the samples.
week_start = datetime(2026, 6, 15, 0, 0, tzinfo=UTC) # Monday
for offset in range(7):
start = week_start + timedelta(days=offset)
window_end = start + timedelta(days=1)
# Seed just before the window so a fire landing exactly on `start` is counted
# (croniter.get_next returns the first fire strictly after the seed).
itr = croniter(cron_expression, start - timedelta(microseconds=1))
fires = 0
while True:
nxt = itr.get_next(datetime)
if nxt >= window_end:
break
fires += 1
if fires > MAX_FIRES_PER_DAY:
return True
return False