diff --git a/smartscore/scripts/backtrack/run_daily_picks.py b/smartscore/scripts/backtrack/run_daily_picks.py index 453975a..6d5c864 100644 --- a/smartscore/scripts/backtrack/run_daily_picks.py +++ b/smartscore/scripts/backtrack/run_daily_picks.py @@ -1254,6 +1254,35 @@ def write_historic_picks_if_missing(picks, on_date, env): return columns = _historic_pick_columns(env) + + # Keep the table to the most recent DAYS_TO_KEEP_HISTORIC_DATA days (mirrors + # the site's own publish pruning) so it stays a ~week visual instead of + # growing forever. Prune before inserting today's rows. + DAYS_TO_KEEP_HISTORIC_DATA = 8 + try: + all_rows = SUPABASE_ADMIN_CLIENT.table(table).select("date").execute().data or [] + distinct_dates = sorted({r.get("date") for r in all_rows if r.get("date")}) + if len(distinct_dates) >= DAYS_TO_KEEP_HISTORIC_DATA: + to_remove = distinct_dates[: len(distinct_dates) - (DAYS_TO_KEEP_HISTORIC_DATA - 1)] + for d in to_remove: + SUPABASE_ADMIN_CLIENT.table(table).delete().eq("date", d).execute() + except Exception as exc: # noqa: BLE001 + print(f"WARNING: Historic-Picks pruning failed: {exc}", flush=True) + + # `id` is the table's (non-null, no-default) primary key. The site's own + # publish renumbers every row sequentially on each write, but this ML pass + # INSERTs brand-new rows, so a fixed 1..N would collide with the rows + # already stored from earlier dates. Derive the next free id from the + # current max instead (and fall back to 1..N only on an empty/odd table). + next_id = 1 + if "id" in columns: + try: + existing_ids = SUPABASE_ADMIN_CLIENT.table(table).select("id").execute().data or [] + parsed = [int(r.get("id")) for r in existing_ids if r.get("id") is not None] + if parsed: + next_id = max(parsed) + 1 + except (TypeError, ValueError): + next_id = 1 records = [] for i, (_, row) in enumerate(picks.iterrows()): record = {"date": on_date} @@ -1262,7 +1291,7 @@ def write_historic_picks_if_missing(picks, on_date, env): pid = pid.item() record["player_id"] = int(pid) if pid is not None and not (isinstance(pid, float) and np.isnan(pid)) else None if "id" in columns: - record["id"] = i + 1 + record["id"] = next_id + i if "Scored" in columns: record["Scored"] = None for col in columns: @@ -1270,6 +1299,7 @@ def write_historic_picks_if_missing(picks, on_date, env): continue if col == "stat": value = stat_value(row.get("ensemble", np.nan)) + record[col] = value elif col in getattr(row, "index", []): value = row.get(col) if isinstance(value, (np.generic,)):