34 lines
950 B
Python
34 lines
950 B
Python
"""Helpers for retention policies based on active counting dates."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sqlite3
|
|
|
|
BATCH_ID_RE = re.compile(r"^(\d{4}-\d{2}-\d{2})-\d+[a-zA-Z]*$")
|
|
|
|
|
|
def active_counting_dates(conn: sqlite3.Connection, limit: int = 3, table: str = "batches") -> list[str]:
|
|
"""Return latest distinct counting dates that actually have rows."""
|
|
if table not in ("batches", "daily_summaries"):
|
|
raise ValueError("invalid table")
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
f"""
|
|
SELECT DISTINCT counting_date
|
|
FROM {table}
|
|
ORDER BY counting_date DESC
|
|
LIMIT ?
|
|
""",
|
|
(int(limit),),
|
|
)
|
|
return [row[0] for row in cur.fetchall()]
|
|
|
|
|
|
def batch_id_to_counting_date(batch_id: str) -> str | None:
|
|
"""Extract YYYY-MM-DD from a canonical batch id."""
|
|
m = BATCH_ID_RE.match((batch_id or "").strip())
|
|
if not m:
|
|
return None
|
|
return m.group(1)
|