diff --git a/agenda/stats.py b/agenda/stats.py index 930e948..62b67c2 100644 --- a/agenda/stats.py +++ b/agenda/stats.py @@ -1,10 +1,31 @@ """Trip statistic functions.""" from collections import defaultdict +from typing import Counter, Mapping from agenda.types import StrDict, Trip +def travel_legs(trip: Trip, stats: StrDict) -> None: + """Calcuate stats for travel legs.""" + for leg in trip.travel: + if leg["type"] == "flight": + stats.setdefault("flight_count", 0) + stats.setdefault("airlines", Counter()) + stats["flight_count"] += 1 + stats["airlines"][leg["airline_name"]] += 1 + if leg["type"] == "train": + stats.setdefault("train_count", 0) + stats["train_count"] += 1 + + +def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None: + """Calculate conference stats.""" + for c in trip.conferences: + yearly_stats[c["start"].year].setdefault("conferences", 0) + yearly_stats[c["start"].year]["conferences"] += 1 + + def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]: """Calculate total distance and distance by transport type grouped by year.""" yearly_stats: defaultdict[int, StrDict] = defaultdict(dict) @@ -14,9 +35,7 @@ def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]: yearly_stats[year].setdefault("count", 0) yearly_stats[year]["count"] += 1 - for c in trip.conferences: - yearly_stats[c["start"].year].setdefault("conferences", 0) - yearly_stats[c["start"].year]["conferences"] += 1 + conferences(trip, yearly_stats) if dist: yearly_stats[year]["total_distance"] = ( @@ -35,11 +54,7 @@ def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]: continue yearly_stats[year].setdefault("countries", set()) yearly_stats[year]["countries"].add(country) - for leg in trip.travel: - if leg["type"] == "flight": - yearly_stats[year].setdefault("flight_count", 0) - yearly_stats[year]["flight_count"] += 1 - if leg["type"] == "train": - yearly_stats[year].setdefault("train_count", 0) - yearly_stats[year]["train_count"] += 1 + + travel_legs(trip, yearly_stats[year]) + return dict(yearly_stats) diff --git a/templates/trip/stats.html b/templates/trip/stats.html index d6f379e..8a117ef 100644 --- a/templates/trip/stats.html +++ b/templates/trip/stats.html @@ -30,8 +30,14 @@ {{ c.flag }} {{ c.name }} {% endfor %} -
Flights in {{ year }}: {{ year_stats.flight_count }}
-
Trains in {{ year }}: {{ year_stats.train_count }}
+
+ Flight segments in {{ year }}: {{ year_stats.flight_count }} + [ by airline: + {% for airline, count in year_stats.airlines.most_common() %} + {{ airline }}: {{ count }}{% if not loop.last %},{% endif %} + {% endfor %} ] +
+
Trains segments in {{ year }}: {{ year_stats.train_count }}
Total distance in {{ year}}: {{ format_distance(year_stats.total_distance) }}
{% for transport_type, distance in year_stats.distances_by_transport_type.items() %}