Show airline counts on trip stats page

This commit is contained in:
Edward Betts 2024-11-10 21:02:37 +00:00
parent 6d1b01485a
commit 1caf233075
2 changed files with 33 additions and 12 deletions

View file

@ -1,10 +1,31 @@
"""Trip statistic functions.""" """Trip statistic functions."""
from collections import defaultdict from collections import defaultdict
from typing import Counter, Mapping
from agenda.types import StrDict, Trip 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]: def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]:
"""Calculate total distance and distance by transport type grouped by year.""" """Calculate total distance and distance by transport type grouped by year."""
yearly_stats: defaultdict[int, StrDict] = defaultdict(dict) 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].setdefault("count", 0)
yearly_stats[year]["count"] += 1 yearly_stats[year]["count"] += 1
for c in trip.conferences: conferences(trip, yearly_stats)
yearly_stats[c["start"].year].setdefault("conferences", 0)
yearly_stats[c["start"].year]["conferences"] += 1
if dist: if dist:
yearly_stats[year]["total_distance"] = ( yearly_stats[year]["total_distance"] = (
@ -35,11 +54,7 @@ def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]:
continue continue
yearly_stats[year].setdefault("countries", set()) yearly_stats[year].setdefault("countries", set())
yearly_stats[year]["countries"].add(country) yearly_stats[year]["countries"].add(country)
for leg in trip.travel:
if leg["type"] == "flight": travel_legs(trip, yearly_stats[year])
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
return dict(yearly_stats) return dict(yearly_stats)

View file

@ -30,8 +30,14 @@
<span class="text-nowrap">{{ c.flag }} {{ c.name }}</span> <span class="text-nowrap">{{ c.flag }} {{ c.name }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div>Flights in {{ year }}: {{ year_stats.flight_count }}</div> <div>
<div>Trains in {{ year }}: {{ year_stats.train_count }}</div> 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 %} ]
</div>
<div>Trains segments in {{ year }}: {{ year_stats.train_count }}</div>
<div>Total distance in {{ year}}: {{ format_distance(year_stats.total_distance) }}</div> <div>Total distance in {{ year}}: {{ format_distance(year_stats.total_distance) }}</div>
{% for transport_type, distance in year_stats.distances_by_transport_type.items() %} {% for transport_type, distance in year_stats.distances_by_transport_type.items() %}
<div> <div>