Add airline, airport, and station stats

This commit is contained in:
Edward Betts 2026-01-15 23:05:39 +00:00
parent f22772d12d
commit bc5e595bb0
2 changed files with 48 additions and 1 deletions

View file

@ -3,7 +3,8 @@
from collections import defaultdict from collections import defaultdict
from typing import Counter, Mapping 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: def travel_legs(trip: Trip, stats: StrDict) -> None:
@ -19,11 +20,38 @@ def travel_legs(trip: Trip, stats: StrDict) -> None:
if leg["type"] == "flight": if leg["type"] == "flight":
stats.setdefault("flight_count", 0) stats.setdefault("flight_count", 0)
stats.setdefault("airlines", Counter()) stats.setdefault("airlines", Counter())
stats.setdefault("airports", Counter())
stats["flight_count"] += 1 stats["flight_count"] += 1
stats["airlines"][leg["airline_detail"]["name"]] += 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": if leg["type"] == "train":
stats.setdefault("train_count", 0) stats.setdefault("train_count", 0)
stats["train_count"] += 1 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: def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None:

View file

@ -42,12 +42,22 @@
<div> <div>
Flight segments in {{ year }}: {{ year_stats.flight_count or 0 }} Flight segments in {{ year }}: {{ year_stats.flight_count or 0 }}
{% if year_stats.airlines %} {% if year_stats.airlines %}
({{ year_stats.airlines | count }} airlines)
[ by airline: [ by airline:
{% for airline, count in year_stats.airlines.most_common() %} {% for airline, count in year_stats.airlines.most_common() %}
{{ airline }}: {{ count }}{% if not loop.last %},{% endif %} {{ airline }}: {{ count }}{% if not loop.last %},{% endif %}
{% endfor %} ] {% endfor %} ]
{% endif %} {% endif %}
</div> </div>
{% if year_stats.airports %}
<div>
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 %} ]
</div>
{% endif %}
{% if year_stats.co2_kg %} {% if year_stats.co2_kg %}
<div>Total CO₂: <div>Total CO₂:
{% if year_stats.co2_kg >= 1000 %} {% if year_stats.co2_kg >= 1000 %}
@ -70,6 +80,15 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
<div>Trains segments in {{ year }}: {{ year_stats.train_count or 0 }}</div> <div>Trains segments in {{ year }}: {{ year_stats.train_count or 0 }}</div>
{% if year_stats.stations %}
<div>
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 %} ]
</div>
{% endif %}
<div>Total distance in {{ year}}: {{ format_distance(year_stats.total_distance or 0) }}</div> <div>Total distance in {{ year}}: {{ format_distance(year_stats.total_distance or 0) }}</div>
{% if year_stats.distances_by_transport_type %} {% if year_stats.distances_by_transport_type %}
{% for transport_type, distance in year_stats.distances_by_transport_type.items() %} {% for transport_type, distance in year_stats.distances_by_transport_type.items() %}