| 182 | |
| 183 | |
| 184 | class FileRouteStatsStorage(RouteStatsStorage): |
| 185 | def __init__(self, path: Path | None = None) -> None: |
| 186 | self._path = path or (_DATA_DIR / "stats.json") |
| 187 | |
| 188 | def load(self) -> list[dict[str, Any]]: |
| 189 | try: |
| 190 | if self._path.exists(): |
| 191 | data = json.loads(self._path.read_text()) |
| 192 | if isinstance(data, list): |
| 193 | return data |
| 194 | except Exception: |
| 195 | pass |
| 196 | return [] |
| 197 | |
| 198 | def save(self, records: list[dict[str, Any]]) -> None: |
| 199 | try: |
| 200 | self._path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) |
| 201 | self._path.write_text(json.dumps(records, default=str)) |
| 202 | self._path.chmod(0o600) |
| 203 | except Exception as exc: |
| 204 | import sys |
| 205 | print(f"[UncommonRoute] Failed to save stats: {exc}", file=sys.stderr) |
| 206 | |
| 207 | |
| 208 | class InMemoryRouteStatsStorage(RouteStatsStorage): |