Fix trip stats new-country counting

This commit is contained in:
Edward Betts 2026-01-15 22:43:22 +00:00
parent 91a74fd887
commit 8a0df48a73
5 changed files with 61 additions and 18 deletions

View file

@ -36,6 +36,17 @@ def conferences(trip: Trip, yearly_stats: Mapping[int, StrDict]) -> None:
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)
first_visit_year: dict[str, int] = {}
for trip in trips:
year = trip.start.year
for country in trip.countries:
if country.alpha_2 == "GB":
continue
alpha_2 = country.alpha_2
if alpha_2 not in first_visit_year or year < first_visit_year[alpha_2]:
first_visit_year[alpha_2] = year
for trip in trips:
year = trip.start.year
dist = trip.total_distance()
@ -61,6 +72,9 @@ def calculate_yearly_stats(trips: list[Trip]) -> dict[int, StrDict]:
continue
yearly_stats[year].setdefault("countries", set())
yearly_stats[year]["countries"].add(country)
if first_visit_year.get(country.alpha_2) == year:
yearly_stats[year].setdefault("new_countries", set())
yearly_stats[year]["new_countries"].add(country)
travel_legs(trip, yearly_stats[year])

View file

@ -221,22 +221,18 @@ class Trip:
@property
def countries_str(self) -> str:
"""List of countries visited on this trip."""
return typing.cast(
str,
format_list_with_ampersand([f"{c.name} {c.flag}" for c in self.countries]),
return format_list_with_ampersand(
[f"{c.name} {c.flag}" for c in self.countries]
)
@property
def locations_str(self) -> str:
"""List of countries visited on this trip."""
return typing.cast(
str,
format_list_with_ampersand(
[
f"{location} ({c.name})" + (f" {c.flag}" if self.show_flags else "")
for location, c in self.locations()
]
),
return format_list_with_ampersand(
[
f"{location} ({c.name})" + (f" {c.flag}" if self.show_flags else "")
for location, c in self.locations()
]
)
@property