From 672ca4b849df37a98217e9232432d9e5c12f5b0f Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 23 Jul 2025 22:52:22 +0100 Subject: [PATCH] Generalize and enhance yearly statistics The accommodation page has been updated to provide a more comprehensive and dynamic view of travel statistics. Previously, the page only displayed hard-coded statistics for total nights away and abroad for the year 2024. This required manual updates each year and didn't provide historical context. This commit introduces the following changes: * **Dynamically Calculate Yearly Stats:** The `accommodation_list` view in `web_view.py` now calculates statistics for every year found in the accommodation data. It correctly handles stays that span across multiple years. * **Display All Years:** The `accommodation.html` template now iterates through a list of all calculated yearly stats, displaying a summary for each year automatically. * **Add Percentage of Year:** The template also calculates and displays what percentage of the year the "total nights" and "nights abroad" represent. This includes logic to correctly account for leap years (366 days) for an accurate calculation. These changes make the statistics more informative and ensure the page remains relevant over time without needing further manual code adjustments. --- templates/accommodation.html | 13 +++++++++++-- web_view.py | 28 +++++++++++++++++----------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/templates/accommodation.html b/templates/accommodation.html index 4a5b7de..dda241d 100644 --- a/templates/accommodation.html +++ b/templates/accommodation.html @@ -37,9 +37,18 @@

Accommodation

+

Statistics

diff --git a/web_view.py b/web_view.py index 91a0e94..6e7da53 100755 --- a/web_view.py +++ b/web_view.py @@ -374,22 +374,29 @@ def past_conference_list() -> str: fx_rate=agenda.fx.get_rates(app.config), ) - @app.route("/accommodation") def accommodation_list() -> str: """Page showing a list of past, present and future accommodation.""" data_dir = app.config["PERSONAL_DATA"] items = travel.parse_yaml("accommodation", data_dir) - stays_in_2024 = [item for item in items if item["from"].year == 2024] - total_nights_2024 = sum( - (stay["to"].date() - stay["from"].date()).days for stay in stays_in_2024 - ) + # Create a dictionary to hold stats for each year + year_stats = defaultdict(lambda: {"total_nights": 0, "nights_abroad": 0}) - nights_abroad_2024 = sum( - (stay["to"].date() - stay["from"].date()).days - for stay in stays_in_2024 - if stay["country"] != "gb" + # Calculate stats for each year + for stay in items: + current_date = stay["from"].date() + end_date = stay["to"].date() + while current_date < end_date: + year = current_date.year + year_stats[year]["total_nights"] += 1 + if stay.get("country") != "gb": + year_stats[year]["nights_abroad"] += 1 + current_date += timedelta(days=1) + + # Sort the stats by year in descending order + sorted_year_stats = sorted( + year_stats.items(), key=lambda item: item[0], reverse=True ) trip_lookup = {} @@ -415,8 +422,7 @@ def accommodation_list() -> str: past=past, current=current, future=future, - total_nights_2024=total_nights_2024, - nights_abroad_2024=nights_abroad_2024, + year_stats=sorted_year_stats, get_country=agenda.get_country, fx_rate=agenda.fx.get_rates(app.config), )