Dalsi banky, prehled ve Swaggeru, /status a prazdne promenne
- zdroje rb, unicredit a air - nahore ve Swaggeru prehled nabizenych a nenabizenych bank s duvody - GET /status s prehledem vypadku stahovani u jednotlivych bank - prazdna promenna prostredi znamena vychozi hodnotu - popis stahovani v documentation/stahovani.md
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Tells whether the sources are being refreshed successfully."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.sources import SOURCES
|
||||
from app.storage import read_sheet
|
||||
|
||||
# A single missed refresh is normal, for example a short outage on the bank side.
|
||||
# Three missed refreshes in a row mean the consumer should know about it.
|
||||
STALE_AFTER_REFRESH_INTERVALS = 3
|
||||
|
||||
SourceState = Literal["ok", "failing", "pending"]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RefreshAttempt:
|
||||
attempted_at: datetime
|
||||
error: str | None
|
||||
|
||||
|
||||
# Kept in memory on purpose: every start refreshes all sources right away, so the
|
||||
# history is complete again within seconds and there is nothing worth persisting.
|
||||
RefreshHistory = dict[str, RefreshAttempt]
|
||||
|
||||
|
||||
class SourceStatus(BaseModel):
|
||||
source: str
|
||||
source_name: str
|
||||
state: SourceState = Field(
|
||||
description="ok: last download succeeded, failing: last download failed, "
|
||||
"pending: no download attempted since the start"
|
||||
)
|
||||
stale: bool = Field(description="True when the stored data may be outdated")
|
||||
last_success_at: datetime | None = Field(description="Time of the stored data, null if none")
|
||||
last_attempt_at: datetime | None
|
||||
last_error: str | None = Field(description="Error of the last attempt, null if it succeeded")
|
||||
|
||||
|
||||
class ServiceStatus(BaseModel):
|
||||
status: Literal["ok", "degraded"] = Field(description="ok only when all sources are ok")
|
||||
problems: list[str] = Field(description="Identifiers of the sources that are not ok")
|
||||
refresh_interval_minutes: int
|
||||
sources: list[SourceStatus]
|
||||
|
||||
|
||||
def is_stale(fetched_at: datetime, now: datetime, interval_minutes: int) -> bool:
|
||||
return now - fetched_at > timedelta(minutes=interval_minutes * STALE_AFTER_REFRESH_INTERVALS)
|
||||
|
||||
|
||||
def _source_status(
|
||||
source_id: str,
|
||||
attempt: RefreshAttempt | None,
|
||||
data_dir: Path,
|
||||
now: datetime,
|
||||
interval_minutes: int,
|
||||
) -> SourceStatus:
|
||||
sheet = read_sheet(data_dir, source_id)
|
||||
state: SourceState = "pending"
|
||||
if attempt is not None:
|
||||
state = "ok" if attempt.error is None else "failing"
|
||||
return SourceStatus(
|
||||
source=source_id,
|
||||
source_name=SOURCES[source_id].name,
|
||||
state=state,
|
||||
stale=sheet is None or is_stale(sheet.fetched_at, now, interval_minutes),
|
||||
last_success_at=sheet.fetched_at if sheet else None,
|
||||
last_attempt_at=attempt.attempted_at if attempt else None,
|
||||
last_error=attempt.error if attempt else None,
|
||||
)
|
||||
|
||||
|
||||
def build_status(
|
||||
history: RefreshHistory,
|
||||
data_dir: Path,
|
||||
now: datetime,
|
||||
interval_minutes: int,
|
||||
) -> ServiceStatus:
|
||||
sources = [
|
||||
_source_status(source_id, history.get(source_id), data_dir, now, interval_minutes)
|
||||
for source_id in SOURCES
|
||||
]
|
||||
problems = [item.source for item in sources if item.state != "ok"]
|
||||
return ServiceStatus(
|
||||
status="degraded" if problems else "ok",
|
||||
problems=problems,
|
||||
refresh_interval_minutes=interval_minutes,
|
||||
sources=sources,
|
||||
)
|
||||
Reference in New Issue
Block a user