Better canceled trains display

This commit is contained in:
Emmy D'Anello 2024-02-10 19:47:34 +01:00
parent a67ad3044f
commit 11cf6bbf55
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 85 additions and 31 deletions

View File

@ -141,7 +141,7 @@ function TrainRow({train, tableType, date, time}) {
enabled: !!trip.id, enabled: !!trip.id,
}) })
const realtimeTripData = realtimeTripQuery.data ?? {} const realtimeTripData = realtimeTripQuery.data ?? {}
const scheduleRelationship = realtimeTripData.schedule_relationship ?? 0 const tripScheduleRelationship = realtimeTripData.schedule_relationship ?? 0
const realtimeQuery = useQuery({ const realtimeQuery = useQuery({
queryKey: ['realtime', train.id, date, time], queryKey: ['realtime', train.id, date, time],
@ -150,13 +150,16 @@ function TrainRow({train, tableType, date, time}) {
enabled: !!train.id, enabled: !!train.id,
}) })
const realtimeData = realtimeQuery.data ?? {} 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 delay = tableType === "departures" ? realtimeData.departure_delay : realtimeData.arrival_delay
const prettyDelay = delay && scheduleRelationship !== 3 ? getPrettyDelay(delay) : "" const prettyDelay = delay && !canceled ? getPrettyDelay(delay) : ""
const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(scheduleRelationship) const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(tripScheduleRelationship, stopScheduleRelationship)
let stopsFilter let stopsFilter
if (scheduleRelationship === 3) if (canceled)
stopsFilter = (stop_time) => true stopsFilter = (stop_time) => true
else if (tableType === "departures") else if (tableType === "departures")
stopsFilter = (stop_time) => stop_time.stop_sequence > train.stop_sequence && stop_time.drop_off_type === 0 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}) {
<Box fontWeight="bold" color="#FFED02" fontSize={24}> <Box fontWeight="bold" color="#FFED02" fontSize={24}>
{getDisplayTime(train, tableType)} {getDisplayTime(train, tableType)}
</Box> </Box>
<Box color={delay && delay !== "00:00:00" ? "#e86d2b" : "white"} fontWeight={delay && delay !== "00:00:00" ? "bold" : ""}> <Box color={delay && delay !== "00:00:00" ? "#e86d2b" : "white"}
fontWeight={delay && delay !== "00:00:00" ? "bold" : ""}>
{prettyDelay} {prettyDelay}
</Box> </Box>
<Box color={scheduleRelationshipColor} fontWeight="bold"> <Box color={scheduleRelationshipColor} fontWeight="bold">
@ -206,8 +210,10 @@ function TrainRow({train, tableType, date, time}) {
</Box> </Box>
</TableCell> </TableCell>
<TableCell> <TableCell>
<Box style={{textDecoration: canceled ? 'line-through': ''}}>
<Typography fontSize={24} fontWeight="bold" data-stop-id={headline.id}>{headline.name}</Typography> <Typography fontSize={24} fontWeight="bold" data-stop-id={headline.id}>{headline.name}</Typography>
<span className="stops">{stopsNames}</span> <span className="stops">{stopsNames}</span>
</Box>
</TableCell> </TableCell>
</StyledTableRow> </StyledTableRow>
</> </>
@ -324,15 +330,20 @@ function getPrettyDelay(delay) {
return full_minutes ? `+${full_minutes} min` : "À l'heure" return full_minutes ? `+${full_minutes} min` : "À l'heure"
} }
function getPrettyScheduleRelationship(scheduledRelationship) { function getPrettyScheduleRelationship(tripScheduledRelationship, stopScheduledRelationship) {
switch (scheduledRelationship) { switch (tripScheduledRelationship) {
case 1: case 1:
return ["Ajouté", "#3ebb18"] return ["Ajouté", "#3ebb18"]
case 3: case 3:
return ["Supprimé", "#ff8701"] return ["Supprimé", "#ff8701"]
default:
switch (stopScheduledRelationship) {
case 1:
return ["Supprimé", "#ff8701"]
default: default:
return ["", ""] return ["", ""]
} }
}
} }
export default TrainsTable; export default TrainsTable;

View File

@ -171,18 +171,29 @@ class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet):
.values_list('service_id', flat=True))) .values_list('service_id', flat=True)))
.values_list('id')) .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) \ 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_date=Value(query_date)) \
.annotate(departure_time_24h=F('departure_time')) .annotate(departure_time_24h=F('departure_time'))
qs_yesterday = StopTime.objects.filter(stop_filter) \ 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_date=Value(yesterday)) \
.annotate(departure_time_24h=F('departure_time') - timedelta(days=1)) .annotate(departure_time_24h=F('departure_time') - timedelta(days=1))
qs_tomorrow = StopTime.objects.filter(stop_filter) \ 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_date=Value(tomorrow)) \
.annotate(departure_time_24h=F('departure_time') + timedelta(days=1)) .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('service_id', flat=True)))
.values_list('id')) .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) \ 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_date=Value(query_date)) \
.annotate(arrival_time_24h=F('arrival_time')) .annotate(arrival_time_24h=F('arrival_time'))
qs_yesterday = StopTime.objects.filter(stop_filter) \ 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_date=Value(yesterday)) \
.annotate(arrival_time_24h=F('arrival_time') - timedelta(days=1)) .annotate(arrival_time_24h=F('arrival_time') - timedelta(days=1))
qs_tomorrow = StopTime.objects.filter(stop_filter) \ 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_date=Value(tomorrow)) \
.annotate(arrival_time_24h=F('arrival_time') + timedelta(days=1)) .annotate(arrival_time_24h=F('arrival_time') + timedelta(days=1))

View File

@ -28,11 +28,13 @@ class StopTimeInline(admin.TabularInline):
class TripUpdateInline(admin.StackedInline): class TripUpdateInline(admin.StackedInline):
model = TripUpdate model = TripUpdate
extra = 0 extra = 0
autocomplete_fields = ('trip',)
class StopTimeUpdateInline(admin.StackedInline): class StopTimeUpdateInline(admin.StackedInline):
model = StopTimeUpdate model = StopTimeUpdate
extra = 0 extra = 0
autocomplete_fields = ('trip_update', 'stop_time',)
@admin.register(Agency) @admin.register(Agency)
@ -124,6 +126,7 @@ class StopTimeUpdateAdmin(admin.ModelAdmin):
list_filter = ('schedule_relationship',) list_filter = ('schedule_relationship',)
search_fields = ('trip_update__trip__id', 'stop_time__stop__name', 'arrival_time', 'departure_time',) search_fields = ('trip_update__trip__id', 'stop_time__stop__name', 'arrival_time', 'departure_time',)
ordering = ('trip_update', 'stop_time',) ordering = ('trip_update', 'stop_time',)
autocomplete_fields = ('trip_update', 'stop_time',)
@admin.register(TripUpdate) @admin.register(TripUpdate)

View File

@ -27,12 +27,17 @@ class Command(BaseCommand):
feed_message = FeedMessage() feed_message = FeedMessage()
feed_message.ParseFromString(requests.get(feed_url).content) feed_message.ParseFromString(requests.get(feed_url).content)
with open("toto.txt", "w") as f:
f.write(str(feed_message))
stop_times_updates = [] stop_times_updates = []
for entity in feed_message.entity: for entity in feed_message.entity:
if entity.HasField("trip_update"): if entity.HasField("trip_update"):
trip_update = entity.trip_update trip_update = entity.trip_update
trip_id = trip_update.trip.trip_id 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]), start_date = date(year=int(trip_update.trip.start_date[:4]),
month=int(trip_update.trip.start_date[4:6]), month=int(trip_update.trip.start_date[4:6]),
day=int(trip_update.trip.start_date[6:])) day=int(trip_update.trip.start_date[6:]))
@ -43,10 +48,13 @@ class Command(BaseCommand):
trip_qs = Trip.objects.all() trip_qs = Trip.objects.all()
trip_ids = trip_qs.values_list('id', flat=True) 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( last_stop_queryset = StopTime.objects.filter(
stop__parent_station_id=trip_update.stop_time_update[-1].stop_id, stop__parent_station_id=trip_update.stop_time_update[-1].stop_id,
).values('trip_id') ).values('trip_id')
trip_ids = trip_ids.intersection(last_stop_queryset) 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): for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
stop_id = stop_time_update.stop_id stop_id = stop_time_update.stop_id
st_queryset = StopTime.objects.filter(stop__parent_station_id=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}", "service_id": f"{feed_type}-new-{headsign}",
"date": trip_update.trip.start_date, "date": trip_update.trip.start_date,
"exception_type": 1, "exception_type": 1,
"transport_type": feed_type,
} }
) )
Trip.objects.update_or_create( 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): for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
stop_id = stop_time_update.stop_id stop_id = stop_time_update.stop_id
stop = Stop.objects.get(id=stop_id) stop = Stop.objects.get(id=stop_id)
if stop.location_type == 1: if stop.location_type == 1:
if not StopTime.objects.filter(trip_id=trip_id).exists(): 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 = StopTime.objects.get(trip_id=sample_trip.id,
stop__parent_station_id=stop_id).stop stop__parent_station_id=stop_id).stop
elif StopTime.objects.filter(trip_id=trip_id, stop__parent_station_id=stop_id).exists(): 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 if s.stop_type in s2.stop.stop_type
or s2.stop.stop_type in s.stop_type) or s2.stop.stop_type in s.stop_type)
stop_id = stop.id 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( StopTime.objects.update_or_create(
id=f"{trip_id}-{stop_id}", id=f"{trip_id}-{stop_id}",
trip_id=trip_id, trip_id=trip_id,
defaults={ defaults={
"stop_id": stop_id, "stop_id": stop_id,
"stop_sequence": stop_sequence, "stop_sequence": stop_sequence,
"arrival_time": datetime.fromtimestamp(stop_time_update.arrival.time, "arrival_time": arr_time,
tz=ZoneInfo("Europe/Paris")) - start_dt, "departure_time": dep_time,
"departure_time": datetime.fromtimestamp(stop_time_update.departure.time, "pickup_type": pickup_type,
tz=ZoneInfo("Europe/Paris")) - start_dt, "drop_off_type": drop_off_type,
"pickup_type": 0 if stop_time_update.departure.time else 1,
"drop_off_type": 0 if stop_time_update.arrival.time else 1,
} }
) )
@ -139,12 +155,14 @@ class Command(BaseCommand):
self.stdout.write(f"Trip {trip_id} does not exist in the GTFS feed.") self.stdout.write(f"Trip {trip_id} does not exist in the GTFS feed.")
continue continue
tu, _created = TripUpdate.objects.get_or_create( tu, _created = TripUpdate.objects.update_or_create(
trip_id=trip_id, trip_id=trip_id,
start_date=trip_update.trip.start_date, start_date=trip_update.trip.start_date,
start_time=trip_update.trip.start_time, start_time=trip_update.trip.start_time,
defaults=dict(
schedule_relationship=trip_update.trip.schedule_relationship, schedule_relationship=trip_update.trip.schedule_relationship,
) )
)
for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update): for stop_sequence, stop_time_update in enumerate(trip_update.stop_time_update):
stop_id = stop_time_update.stop_id stop_id = stop_time_update.stop_id
@ -174,9 +192,9 @@ class Command(BaseCommand):
elif stop_time_update.schedule_relationship == 1: elif stop_time_update.schedule_relationship == 1:
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id), st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),
trip_id=trip_id) trip_id=trip_id)
if st.pickup_type != 0 or st.drop_off_type != 0: if st.pickup_type != 1 or st.drop_off_type != 1:
st.pickup_type = 0 st.pickup_type = 1
st.drop_off_type = 0 st.drop_off_type = 1
st.save() st.save()
else: else:
st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id), st = StopTime.objects.get(Q(stop=stop_id) | Q(stop__parent_station_id=stop_id),