Fix same-day flight location tracking
When multiple flights arrived on the same date, location tracking only compared dates and missed later flights. Now compares both date and time to handle same-day connecting flights correctly. Example: Nov 21, 2023 - 06:15: Arrive Madrid (MVD → MAD) - 09:15: Arrive London (MAD → LHR) ← Now properly detected as most recent This ensures users show as "home" when they've returned to UK on the same day as an international connection. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
af492750cb
commit
a92debea5b
|
|
@ -82,10 +82,10 @@ def get_busy_events(
|
||||||
|
|
||||||
|
|
||||||
def get_location_for_date(
|
def get_location_for_date(
|
||||||
target_date: date,
|
target_date: date,
|
||||||
trips: list[Trip],
|
trips: list[Trip],
|
||||||
bookings: list[dict],
|
bookings: list[dict],
|
||||||
accommodations: list[dict],
|
accommodations: list[dict],
|
||||||
airports: dict
|
airports: dict
|
||||||
) -> tuple[str, pycountry.db.Country | None]:
|
) -> tuple[str, pycountry.db.Country | None]:
|
||||||
"""Get location (city, country) for a specific date using travel history."""
|
"""Get location (city, country) for a specific date using travel history."""
|
||||||
|
|
@ -101,24 +101,30 @@ def get_location_for_date(
|
||||||
trip_most_recent_location = None
|
trip_most_recent_location = None
|
||||||
|
|
||||||
# Check flights within trip period
|
# Check flights within trip period
|
||||||
|
trip_most_recent_datetime = None
|
||||||
for booking in bookings:
|
for booking in bookings:
|
||||||
for flight in booking.get("flights", []):
|
for flight in booking.get("flights", []):
|
||||||
if "arrive" in flight:
|
if "arrive" in flight:
|
||||||
arrive_date = flight["arrive"]
|
arrive_datetime_obj = flight["arrive"]
|
||||||
if hasattr(arrive_date, "date"):
|
if hasattr(arrive_datetime_obj, "date"):
|
||||||
arrive_date = arrive_date.date()
|
arrive_datetime = arrive_datetime_obj
|
||||||
elif isinstance(arrive_date, str):
|
arrive_date = arrive_datetime_obj.date()
|
||||||
arrive_date = datetime.fromisoformat(
|
elif isinstance(arrive_datetime_obj, str):
|
||||||
arrive_date.replace("Z", "+00:00")
|
arrive_datetime = datetime.fromisoformat(
|
||||||
).date()
|
arrive_datetime_obj.replace("Z", "+00:00")
|
||||||
|
)
|
||||||
|
arrive_date = arrive_datetime.date()
|
||||||
|
|
||||||
# Only consider flights within this trip and before target date
|
# Only consider flights within this trip and before target date
|
||||||
if trip.start <= arrive_date <= target_date:
|
if trip.start <= arrive_date <= target_date:
|
||||||
if (
|
# Compare both date and time to handle same-day flights correctly
|
||||||
trip_most_recent_date is None
|
if (trip_most_recent_date is None or
|
||||||
or arrive_date > trip_most_recent_date
|
arrive_date > trip_most_recent_date or
|
||||||
):
|
(arrive_date == trip_most_recent_date and
|
||||||
|
(trip_most_recent_datetime is None or arrive_datetime > trip_most_recent_datetime))):
|
||||||
|
|
||||||
trip_most_recent_date = arrive_date
|
trip_most_recent_date = arrive_date
|
||||||
|
trip_most_recent_datetime = arrive_datetime
|
||||||
destination_airport = flight["to"]
|
destination_airport = flight["to"]
|
||||||
|
|
||||||
if destination_airport in uk_airports:
|
if destination_airport in uk_airports:
|
||||||
|
|
@ -189,20 +195,29 @@ def get_location_for_date(
|
||||||
most_recent_date = None
|
most_recent_date = None
|
||||||
|
|
||||||
# Check flights
|
# Check flights
|
||||||
|
most_recent_datetime = None
|
||||||
for booking in bookings:
|
for booking in bookings:
|
||||||
for flight in booking.get("flights", []):
|
for flight in booking.get("flights", []):
|
||||||
if "arrive" in flight:
|
if "arrive" in flight:
|
||||||
arrive_date = flight["arrive"]
|
arrive_datetime_obj = flight["arrive"]
|
||||||
if hasattr(arrive_date, "date"):
|
if hasattr(arrive_datetime_obj, "date"):
|
||||||
arrive_date = arrive_date.date()
|
arrive_datetime = arrive_datetime_obj
|
||||||
elif isinstance(arrive_date, str):
|
arrive_date = arrive_datetime_obj.date()
|
||||||
arrive_date = datetime.fromisoformat(
|
elif isinstance(arrive_datetime_obj, str):
|
||||||
arrive_date.replace("Z", "+00:00")
|
arrive_datetime = datetime.fromisoformat(
|
||||||
).date()
|
arrive_datetime_obj.replace("Z", "+00:00")
|
||||||
|
)
|
||||||
|
arrive_date = arrive_datetime.date()
|
||||||
|
|
||||||
if arrive_date <= target_date:
|
if arrive_date <= target_date:
|
||||||
if most_recent_date is None or arrive_date > most_recent_date:
|
# Compare both date and time to handle same-day flights correctly
|
||||||
|
if (most_recent_date is None or
|
||||||
|
arrive_date > most_recent_date or
|
||||||
|
(arrive_date == most_recent_date and
|
||||||
|
(most_recent_datetime is None or arrive_datetime > most_recent_datetime))):
|
||||||
|
|
||||||
most_recent_date = arrive_date
|
most_recent_date = arrive_date
|
||||||
|
most_recent_datetime = arrive_datetime
|
||||||
destination_airport = flight["to"]
|
destination_airport = flight["to"]
|
||||||
|
|
||||||
# If arriving at UK airport, assume back home in Bristol
|
# If arriving at UK airport, assume back home in Bristol
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
|
|
||||||
|
import agenda.travel as travel
|
||||||
import agenda.trip
|
import agenda.trip
|
||||||
from web_view import app
|
from web_view import app
|
||||||
|
|
||||||
|
|
@ -10,20 +11,30 @@ def test_get_location_for_date() -> None:
|
||||||
today = datetime.now().date()
|
today = datetime.now().date()
|
||||||
start = date(today.year, 1, 1)
|
start = date(today.year, 1, 1)
|
||||||
trips = [
|
trips = [
|
||||||
t for t in agenda.trip.build_trip_list() if t.start == date(2025, 2, 9)
|
t
|
||||||
|
for t in agenda.trip.build_trip_list()
|
||||||
|
if t.start in [date(2023, 11, 14), date(2025, 2, 9)]
|
||||||
]
|
]
|
||||||
assert len(trips) == 1
|
assert len(trips) == 2
|
||||||
|
|
||||||
data_dir = app.config["PERSONAL_DATA"]
|
data_dir = app.config["PERSONAL_DATA"]
|
||||||
|
|
||||||
# Parse YAML files once for the test
|
# Parse YAML files once for the test
|
||||||
import agenda.travel as travel
|
|
||||||
bookings = travel.parse_yaml("flights", data_dir)
|
bookings = travel.parse_yaml("flights", data_dir)
|
||||||
accommodations = travel.parse_yaml("accommodation", data_dir)
|
accommodations = travel.parse_yaml("accommodation", data_dir)
|
||||||
airports = travel.parse_yaml("airports", data_dir)
|
airports = travel.parse_yaml("airports", data_dir)
|
||||||
|
|
||||||
l1 = agenda.busy.get_location_for_date(date(2025, 2, 15), trips, bookings, accommodations, airports)
|
l1 = agenda.busy.get_location_for_date(
|
||||||
|
date(2025, 2, 15), trips, bookings, accommodations, airports
|
||||||
|
)
|
||||||
assert l1[0] == "Hackettstown"
|
assert l1[0] == "Hackettstown"
|
||||||
|
|
||||||
l2 = agenda.busy.get_location_for_date(date(2025, 7, 1), trips, bookings, accommodations, airports)
|
l2 = agenda.busy.get_location_for_date(
|
||||||
|
date(2025, 7, 1), trips, bookings, accommodations, airports
|
||||||
|
)
|
||||||
|
assert l2[0] == "Bristol"
|
||||||
|
|
||||||
|
l2 = agenda.busy.get_location_for_date(
|
||||||
|
date(2023, 12, 2), trips, bookings, accommodations, airports
|
||||||
|
)
|
||||||
assert l2[0] == "Bristol"
|
assert l2[0] == "Bristol"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue