Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions backend/app/country_population.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
from functools import cache
from pathlib import Path

from pydantic import BaseModel


class CountryPopulation(BaseModel):
year: int
revision: str
populations: dict[str, int]


@cache
def get_country_population(year: int) -> CountryPopulation | None:
path = Path(__file__).resolve().parents[1] / "data" / "country_population.json"
with path.open(encoding="utf-8") as source:
data = json.load(source)
populations = data["populations"].get(str(year))
if populations is None:
return None
return CountryPopulation(
year=year, revision=data["revision"], populations=populations
)
6 changes: 5 additions & 1 deletion backend/app/routes/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import BaseModel

from .. import cache, database, models, stats, utils
from ..country_population import CountryPopulation, get_country_population
from ..types import JSONValue, PermissionCount

router = APIRouter(
Expand All @@ -30,6 +31,7 @@ class StatsResultCategoryTotals(BaseModel):
class StatsResult(BaseModel):
totals: dict[str, int]
countries: dict[str, int]
country_population: CountryPopulation | None = None
downloads_per_day: dict[str, int]
updates_per_day: dict[str, int]
delta_downloads_per_day: dict[str, int]
Expand Down Expand Up @@ -65,7 +67,9 @@ def _normalize_stats_result(value: dict[str, JSONValue]) -> StatsResult:
if "os_flatpak_versions" not in value or value["os_flatpak_versions"] is None:
value["os_flatpak_versions"] = {}

return StatsResult.model_validate(value)
return StatsResult.model_validate(
{**value, "country_population": get_country_population(utils.utcnow().year)}
)


class StatsResultApp(BaseModel):
Expand Down
Loading
Loading