diff --git a/AGENTS.md b/AGENTS.md index 87c46c9..dba80ce 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -33,3 +33,7 @@ This is a personal agenda web application built with Flask that tracks various e ## Notes - Trip stats new-country badges come from `agenda.stats.calculate_yearly_stats` via `year_stats.new_countries` (first-visit year, excluding `PREVIOUSLY_VISITED`). +- Trip stats are calculated in `agenda/stats.py`: + - `travel_legs()` extracts airlines, airports, and stations from individual trip travel legs + - `calculate_yearly_stats()` aggregates stats per year including flight/train counts, airlines, airports, stations + - `calculate_overall_stats()` aggregates yearly stats into overall totals for the summary section diff --git a/agenda/stats.py b/agenda/stats.py index f9002a1..2e4dc67 100644 --- a/agenda/stats.py +++ b/agenda/stats.py @@ -61,6 +61,29 @@ def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None: yearly_stats[c["start"].year]["conferences"] += 1 +def calculate_overall_stats(yearly_stats: dict[int, StrDict]) -> StrDict: + """Aggregate yearly stats into overall stats for airlines, airports, stations.""" + overall: StrDict = { + "airlines": Counter(), + "airports": Counter(), + "stations": Counter(), + "flight_count": 0, + "train_count": 0, + } + + for year_stats in yearly_stats.values(): + if "airlines" in year_stats: + overall["airlines"] += year_stats["airlines"] + if "airports" in year_stats: + overall["airports"] += year_stats["airports"] + if "stations" in year_stats: + overall["stations"] += year_stats["stations"] + overall["flight_count"] += year_stats.get("flight_count", 0) + overall["train_count"] += year_stats.get("train_count", 0) + + return overall + + def calculate_yearly_stats( trips: list[Trip], previously_visited: set[str] | None = None ) -> dict[int, StrDict]: diff --git a/templates/trip/stats.html b/templates/trip/stats.html index e7eb51e..f912177 100644 --- a/templates/trip/stats.html +++ b/templates/trip/stats.html @@ -20,6 +20,36 @@ {% endfor %} +