diff --git a/agenda/stats.py b/agenda/stats.py index c3c8e4f..f9002a1 100644 --- a/agenda/stats.py +++ b/agenda/stats.py @@ -3,7 +3,8 @@ from collections import defaultdict from typing import Counter, Mapping -from agenda.types import StrDict, Trip +import agenda +from agenda.types import StrDict, Trip, airport_label def travel_legs(trip: Trip, stats: StrDict) -> None: @@ -19,11 +20,38 @@ def travel_legs(trip: Trip, stats: StrDict) -> None: if leg["type"] == "flight": stats.setdefault("flight_count", 0) stats.setdefault("airlines", Counter()) + stats.setdefault("airports", Counter()) stats["flight_count"] += 1 stats["airlines"][leg["airline_detail"]["name"]] += 1 + for field in ("from_airport", "to_airport"): + airport = leg.get(field) + if airport: + country = agenda.get_country(airport.get("country")) + label = airport_label(airport) + display = f"{country.flag} {label}" if country else label + stats["airports"][display] += 1 if leg["type"] == "train": stats.setdefault("train_count", 0) stats["train_count"] += 1 + stats.setdefault("stations", Counter()) + train_legs = leg.get("legs", []) + if train_legs: + for train_leg in train_legs: + for field in ("from_station", "to_station"): + station = train_leg.get(field) + if station: + country = agenda.get_country(station.get("country")) + label = station["name"] + display = f"{country.flag} {label}" if country else label + stats["stations"][display] += 1 + else: + for field in ("from_station", "to_station"): + station = leg.get(field) + if station: + country = agenda.get_country(station.get("country")) + label = station["name"] + display = f"{country.flag} {label}" if country else label + stats["stations"][display] += 1 def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None: diff --git a/templates/trip/stats.html b/templates/trip/stats.html index e07766d..e7eb51e 100644 --- a/templates/trip/stats.html +++ b/templates/trip/stats.html @@ -42,12 +42,22 @@
Flight segments in {{ year }}: {{ year_stats.flight_count or 0 }} {% if year_stats.airlines %} + ({{ year_stats.airlines | count }} airlines) [ by airline: {% for airline, count in year_stats.airlines.most_common() %} {{ airline }}: {{ count }}{% if not loop.last %},{% endif %} {% endfor %} ] {% endif %}
+ {% if year_stats.airports %} +
+ Airports used in {{ year }}: {{ year_stats.airports | count }} + [ by airport: + {% for airport, count in year_stats.airports.most_common() %} + {{ airport }}: {{ count }}{% if not loop.last %},{% endif %} + {% endfor %} ] +
+ {% endif %} {% if year_stats.co2_kg %}
Total CO₂: {% if year_stats.co2_kg >= 1000 %} @@ -70,6 +80,15 @@ {% endif %} {% endif %}
Trains segments in {{ year }}: {{ year_stats.train_count or 0 }}
+ {% if year_stats.stations %} +
+ Stations used in {{ year }}: {{ year_stats.stations | count }} + [ by station: + {% for station, count in year_stats.stations.most_common() %} + {{ station }}: {{ count }}{% if not loop.last %},{% endif %} + {% endfor %} ] +
+ {% endif %}
Total distance in {{ year}}: {{ format_distance(year_stats.total_distance or 0) }}
{% if year_stats.distances_by_transport_type %} {% for transport_type, distance in year_stats.distances_by_transport_type.items() %}