Show number of flights and trains on travel stats page

Closes: #161
This commit is contained in:
Edward Betts 2024-06-17 16:28:14 +01:00
parent d5bf004912
commit 40578196bc
2 changed files with 9 additions and 0 deletions

View file

@ -28,6 +28,8 @@
{{ c.flag }} {{ c.name }} {{ c.flag }} {{ c.name }}
{% endfor %} {% endfor %}
</div> </div>
<div>Flights in {{ year }}: {{ year_stats.flight_count }}</div>
<div>Trains 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>

View file

@ -547,6 +547,13 @@ def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]:
for country in trip.countries: for country in trip.countries:
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":
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)