from typing import Dict, List, Optional from datetime import datetime, timedelta import json class DispatchStorage: @classmethod def path(cls, date: Optional[str] = None) -> str: if date is None: date = datetime.now().strftime("%Y-%m-%d") return f"out/{date}_dispatch.json" @classmethod def load(cls, date: Optional[str] = None) -> Dict[str, List[int]]: with open(cls.path(date), "r") as f: return json.load(f) @classmethod def save(cls, dispatch: Dict[str, List[int]], date: Optional[str] = None) -> None: with open(cls.path(date), "w") as f: json.dump(dispatch, f) @classmethod def load_week_to_date(cls) -> Dict[str, List[int]]: """ Loads and combines dispatch data from the start of the current week up to today. """ today = datetime.now() days_since_monday = today.weekday() current_date = today - timedelta(days=days_since_monday) combined_dispatch: Dict[str, List[int]] = {} while current_date.date() < today.date(): date_str = current_date.strftime("%Y-%m-%d") daily_dispatch = cls.load(date_str) for key, values in daily_dispatch.items(): if key in combined_dispatch: combined_dispatch[key].extend(values) else: combined_dispatch[key] = values.copy() current_date += timedelta(days=1) return combined_dispatch