From 11cf6bbf553cb096a3f534a1557fbd7c7cb683f7 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 10 Feb 2024 19:47:34 +0100 Subject: [PATCH] Better canceled trains display --- sncf-station/src/TrainsTable.js | 31 ++++++++---- sncf/api/views.py | 34 ++++++++++--- sncfgtfs/admin.py | 3 ++ .../commands/update_sncf_gtfs_rt.py | 48 +++++++++++++------ 4 files changed, 85 insertions(+), 31 deletions(-) diff --git a/sncf-station/src/TrainsTable.js b/sncf-station/src/TrainsTable.js index c96bd4b..ce5c6d0 100644 --- a/sncf-station/src/TrainsTable.js +++ b/sncf-station/src/TrainsTable.js @@ -141,7 +141,7 @@ function TrainRow({train, tableType, date, time}) { enabled: !!trip.id, }) const realtimeTripData = realtimeTripQuery.data ?? {} - const scheduleRelationship = realtimeTripData.schedule_relationship ?? 0 + const tripScheduleRelationship = realtimeTripData.schedule_relationship ?? 0 const realtimeQuery = useQuery({ queryKey: ['realtime', train.id, date, time], @@ -150,13 +150,16 @@ function TrainRow({train, tableType, date, time}) { enabled: !!train.id, }) const realtimeData = realtimeQuery.data ?? {} + const stopScheduleRelationship = realtimeData.schedule_relationship ?? 0 + + const canceled = tripScheduleRelationship === 3 || stopScheduleRelationship === 1 const delay = tableType === "departures" ? realtimeData.departure_delay : realtimeData.arrival_delay - const prettyDelay = delay && scheduleRelationship !== 3 ? getPrettyDelay(delay) : "" - const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(scheduleRelationship) + const prettyDelay = delay && !canceled ? getPrettyDelay(delay) : "" + const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(tripScheduleRelationship, stopScheduleRelationship) let stopsFilter - if (scheduleRelationship === 3) + if (canceled) stopsFilter = (stop_time) => true else if (tableType === "departures") stopsFilter = (stop_time) => stop_time.stop_sequence > train.stop_sequence && stop_time.drop_off_type === 0 @@ -196,7 +199,8 @@ function TrainRow({train, tableType, date, time}) { {getDisplayTime(train, tableType)} - + {prettyDelay} @@ -206,8 +210,10 @@ function TrainRow({train, tableType, date, time}) { - {headline.name} - {stopsNames} + + {headline.name} + {stopsNames} + @@ -324,14 +330,19 @@ function getPrettyDelay(delay) { return full_minutes ? `+${full_minutes} min` : "À l'heure" } -function getPrettyScheduleRelationship(scheduledRelationship) { - switch (scheduledRelationship) { +function getPrettyScheduleRelationship(tripScheduledRelationship, stopScheduledRelationship) { + switch (tripScheduledRelationship) { case 1: return ["Ajouté", "#3ebb18"] case 3: return ["Supprimé", "#ff8701"] default: - return ["", ""] + switch (stopScheduledRelationship) { + case 1: + return ["Supprimé", "#ff8701"] + default: + return ["", ""] + } } } diff --git a/sncf/api/views.py b/sncf/api/views.py index 8b92853..73b5306 100644 --- a/sncf/api/views.py +++ b/sncf/api/views.py @@ -171,18 +171,29 @@ class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet): .values_list('service_id', flat=True))) .values_list('id')) + def canceled_filter(d: date): + return Q(Q(update__schedule_relationship=1) | Q(update__trip_update__schedule_relationship=3), + Q(update__trip_update__start_date=d), + ~Q(update__departure_time=datetime.fromtimestamp(0))) + qs_today = StopTime.objects.filter(stop_filter) \ - .filter(Q(departure_time__gte=query_time, pickup_type=0), calendar_filter(query_date)) \ + .filter(Q(departure_time__gte=query_time)) \ + .filter(Q(pickup_type=0) | canceled_filter(query_date)) \ + .filter(calendar_filter(query_date)) \ .annotate(departure_date=Value(query_date)) \ .annotate(departure_time_24h=F('departure_time')) qs_yesterday = StopTime.objects.filter(stop_filter) \ - .filter(Q(departure_time__gte=time_yesterday, pickup_type=0), calendar_filter(yesterday)) \ + .filter(Q(departure_time__gte=time_yesterday)) \ + .filter(Q(pickup_type=0) | canceled_filter(yesterday)) \ + .filter(calendar_filter(yesterday)) \ .annotate(departure_date=Value(yesterday)) \ .annotate(departure_time_24h=F('departure_time') - timedelta(days=1)) qs_tomorrow = StopTime.objects.filter(stop_filter) \ - .filter(Q(departure_time__gte=timedelta(0), pickup_type=0), calendar_filter(tomorrow)) \ + .filter(Q(departure_time__gte=timedelta(0))) \ + .filter(Q(pickup_type=0) | canceled_filter(tomorrow)) \ + .filter(calendar_filter(tomorrow)) \ .annotate(departure_date=Value(tomorrow)) \ .annotate(departure_time_24h=F('departure_time') + timedelta(days=1)) @@ -232,18 +243,29 @@ class NextArrivalsViewSet(viewsets.ReadOnlyModelViewSet): .values_list('service_id', flat=True))) .values_list('id')) + def canceled_filter(d: date): + return Q(Q(update__schedule_relationship=1) | Q(update__trip_update__schedule_relationship=3), + Q(update__trip_update__start_date=d), + ~Q(update__arrival_time=datetime.fromtimestamp(0))) + qs_today = StopTime.objects.filter(stop_filter) \ - .filter(Q(arrival_time__gte=query_time, drop_off_type=0), calendar_filter(query_date)) \ + .filter(Q(departure_time__gte=query_time)) \ + .filter(Q(drop_off_type=0) | canceled_filter(query_date)) \ + .filter(calendar_filter(query_date)) \ .annotate(arrival_date=Value(query_date)) \ .annotate(arrival_time_24h=F('arrival_time')) qs_yesterday = StopTime.objects.filter(stop_filter) \ - .filter(Q(arrival_time__gte=time_yesterday, drop_off_type=0), calendar_filter(yesterday)) \ + .filter(Q(departure_time__gte=time_yesterday)) \ + .filter(Q(drop_off_type=0) | canceled_filter(yesterday)) \ + .filter(calendar_filter(yesterday)) \ .annotate(arrival_date=Value(yesterday)) \ .annotate(arrival_time_24h=F('arrival_time') - timedelta(days=1)) qs_tomorrow = StopTime.objects.filter(stop_filter) \ - .filter(Q(arrival_time__gte=timedelta(0), drop_off_type=0), calendar_filter(tomorrow)) \ + .filter(Q(departure_time__gte=timedelta(0))) \ + .filter(Q(drop_off_type=0) | canceled_filter(tomorrow)) \ + .filter(calendar_filter(tomorrow)) \ .annotate(arrival_date=Value(tomorrow)) \ .annotate(arrival_time_24h=F('arrival_time') + timedelta(days=1)) diff --git a/sncfgtfs/admin.py b/sncfgtfs/admin.py index fcc9600..a3e8f55 100644 --- a/sncfgtfs/admin.py +++ b/sncfgtfs/admin.py @@ -28,11 +28,13 @@ class StopTimeInline(admin.TabularInline): class TripUpdateInline(admin.StackedInline): model = TripUpdate extra = 0 + autocomplete_fields = ('trip',) class StopTimeUpdateInline(admin.StackedInline): model = StopTimeUpdate extra = 0 + autocomplete_fields = ('trip_update', 'stop_time',) @admin.register(Agency) @@ -124,6 +126,7 @@ class StopTimeUpdateAdmin(admin.ModelAdmin): list_filter = ('schedule_relationship',) search_fields = ('trip_update__trip__id', 'stop_time__stop__name', 'arrival_time', 'departure_time',) ordering = ('trip_update', 'stop_time',) + autocomplete_fields = ('trip_update', 'stop_time',) @admin.register(TripUpdate) diff --git a/sncfgtfs/management/commands/update_sncf_gtfs_rt.py b/sncfgtfs/management/commands/update_sncf_gtfs_rt.py index 0cd7d34..295d6a8 100644 --- a/sncfgtfs/management/commands/update_sncf_gtfs_rt.py +++ b/sncfgtfs/management/commands/update_sncf_gtfs_rt.py @@ -27,12 +27,17 @@ class Command(BaseCommand): feed_message = FeedMessage() feed_message.ParseFromString(requests.get(feed_url).content) + with open("toto.txt", "w") as f: + f.write(str(feed_message)) + stop_times_updates = [] for entity in feed_message.entity: if entity.HasField("trip_update"): trip_update = entity.trip_update trip_id = trip_update.trip.trip_id + if feed_type in ["TGV", "IC", "TER"]: + trip_id = trip_id.split(":", 1)[0] start_date = date(year=int(trip_update.trip.start_date[:4]), month=int(trip_update.trip.start_date[4:6]), day=int(trip_update.trip.start_date[6:])) @@ -43,10 +48,13 @@ class Command(BaseCommand): trip_qs = Trip.objects.all() trip_ids = trip_qs.values_list('id', flat=True) + first_stop_queryset = StopTime.objects.filter( + stop__parent_station_id=trip_update.stop_time_update[0].stop_id, + ).values('trip_id') last_stop_queryset = StopTime.objects.filter( stop__parent_station_id=trip_update.stop_time_update[-1].stop_id, - ).values('trip_id') - trip_ids = trip_ids.intersection(last_stop_queryset) + ).values('trip_id') + trip_ids = trip_ids.intersection(first_stop_queryset).intersection(last_stop_queryset) for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update): stop_id = stop_time_update.stop_id st_queryset = StopTime.objects.filter(stop__parent_station_id=stop_id) @@ -91,7 +99,6 @@ class Command(BaseCommand): "service_id": f"{feed_type}-new-{headsign}", "date": trip_update.trip.start_date, "exception_type": 1, - "transport_type": feed_type, } ) Trip.objects.update_or_create( @@ -104,12 +111,13 @@ class Command(BaseCommand): } ) - sample_trip = Trip.objects.filter(id__in=trip_ids).first() + sample_trip = Trip.objects.filter(id__in=trip_ids, route_id=route_id).first() for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update): stop_id = stop_time_update.stop_id stop = Stop.objects.get(id=stop_id) if stop.location_type == 1: if not StopTime.objects.filter(trip_id=trip_id).exists(): + print(trip_id, sample_trip.id) stop = StopTime.objects.get(trip_id=sample_trip.id, stop__parent_station_id=stop_id).stop elif StopTime.objects.filter(trip_id=trip_id, stop__parent_station_id=stop_id).exists(): @@ -120,18 +128,26 @@ class Command(BaseCommand): if s.stop_type in s2.stop.stop_type or s2.stop.stop_type in s.stop_type) stop_id = stop.id + + arr_time = datetime.fromtimestamp(stop_time_update.arrival.time, + tz=ZoneInfo("Europe/Paris")) - start_dt + dep_time = datetime.fromtimestamp(stop_time_update.departure.time, + tz=ZoneInfo("Europe/Paris")) - start_dt + + pickup_type = 0 if stop_time_update.departure.time and stop_sequence > 0 else 1 + drop_off_type = 0 if stop_time_update.arrival.time \ + and stop_sequence < len(trip_update.stop_time_update) - 1 else 1 + StopTime.objects.update_or_create( id=f"{trip_id}-{stop_id}", trip_id=trip_id, defaults={ "stop_id": stop_id, "stop_sequence": stop_sequence, - "arrival_time": datetime.fromtimestamp(stop_time_update.arrival.time, - tz=ZoneInfo("Europe/Paris")) - start_dt, - "departure_time": datetime.fromtimestamp(stop_time_update.departure.time, - tz=ZoneInfo("Europe/Paris")) - start_dt, - "pickup_type": 0 if stop_time_update.departure.time else 1, - "drop_off_type": 0 if stop_time_update.arrival.time else 1, + "arrival_time": arr_time, + "departure_time": dep_time, + "pickup_type": pickup_type, + "drop_off_type": drop_off_type, } ) @@ -139,11 +155,13 @@ class Command(BaseCommand): self.stdout.write(f"Trip {trip_id} does not exist in the GTFS feed.") continue - tu, _created = TripUpdate.objects.get_or_create( + tu, _created = TripUpdate.objects.update_or_create( trip_id=trip_id, start_date=trip_update.trip.start_date, start_time=trip_update.trip.start_time, - schedule_relationship=trip_update.trip.schedule_relationship, + defaults=dict( + schedule_relationship=trip_update.trip.schedule_relationship, + ) ) for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update): @@ -174,9 +192,9 @@ class Command(BaseCommand): elif stop_time_update.schedule_relationship == 1: st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id), trip_id=trip_id) - if st.pickup_type != 0 or st.drop_off_type != 0: - st.pickup_type = 0 - st.drop_off_type = 0 + if st.pickup_type != 1 or st.drop_off_type != 1: + st.pickup_type = 1 + st.drop_off_type = 1 st.save() else: st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),