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:
+20
-8
@@ -2,30 +2,31 @@ import asyncio
|
||||
import logging
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Path, Query
|
||||
|
||||
from app.api_description import build_description
|
||||
from app.config import load_settings
|
||||
from app.conversion import CZK, BaseCurrencyNotAvailable, rebase_rates
|
||||
from app.models import RateSheetResponse
|
||||
from app.refresh import refresh_forever
|
||||
from app.sources import SOURCES
|
||||
from app.status import RefreshHistory, ServiceStatus, build_status, is_stale
|
||||
from app.storage import read_sheet
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||||
|
||||
# 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
|
||||
|
||||
settings = load_settings()
|
||||
refresh_history: RefreshHistory = {}
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||
# Runs in the background, so the health check answers while the first refresh is in progress.
|
||||
task = asyncio.create_task(refresh_forever(settings.data_dir, settings.refresh_interval_minutes))
|
||||
task = asyncio.create_task(
|
||||
refresh_forever(settings.data_dir, settings.refresh_interval_minutes, refresh_history)
|
||||
)
|
||||
yield
|
||||
task.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
@@ -35,11 +36,13 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||
app = FastAPI(
|
||||
title=settings.app_name,
|
||||
version=settings.app_version,
|
||||
description=build_description(),
|
||||
root_path=settings.root_path,
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
|
||||
# Stays 200 when a bank is down: the application itself works and a restart would not help.
|
||||
@app.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
@@ -55,6 +58,16 @@ def version() -> dict[str, str]:
|
||||
}
|
||||
|
||||
|
||||
@app.get("/status", description="Výpadky stahování: stav posledního stažení u každé banky.")
|
||||
def status() -> ServiceStatus:
|
||||
return build_status(
|
||||
refresh_history,
|
||||
settings.data_dir,
|
||||
datetime.now(UTC),
|
||||
settings.refresh_interval_minutes,
|
||||
)
|
||||
|
||||
|
||||
# Must stay the last route, otherwise it would capture the static paths above.
|
||||
@app.get(
|
||||
"/{source_id}",
|
||||
@@ -87,10 +100,9 @@ def get_rates(
|
||||
detail=f"Source {source_id} does not publish base currency {base}",
|
||||
) from error
|
||||
|
||||
max_age = timedelta(minutes=settings.refresh_interval_minutes * STALE_AFTER_REFRESH_INTERVALS)
|
||||
return RateSheetResponse(
|
||||
**sheet.model_dump(exclude={"rates"}),
|
||||
rates=rates,
|
||||
base=base,
|
||||
stale=datetime.now(UTC) - sheet.fetched_at > max_age,
|
||||
stale=is_stale(sheet.fetched_at, datetime.now(UTC), settings.refresh_interval_minutes),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user