Display delays

This commit is contained in:
Emmy D'Anello 2024-02-04 23:58:27 +01:00
parent f2b6557cb7
commit 9ed97df4b5
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 65 additions and 9 deletions

View File

@ -79,7 +79,7 @@ function TrainsTableBody({stop, date, time, tableType}) {
const nullRef = useRef(null) const nullRef = useRef(null)
let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink" nodeRef={nullRef}> let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink" nodeRef={nullRef}>
<TrainRow train={train} tableType={tableType} /> <TrainRow train={train} tableType={tableType} date={date} time={time} />
</CSSTransition>) </CSSTransition>)
return <> return <>
@ -91,7 +91,7 @@ function TrainsTableBody({stop, date, time, tableType}) {
</> </>
} }
function TrainRow({train, tableType}) { function TrainRow({train, tableType, date, time}) {
const tripQuery = useQuery({ const tripQuery = useQuery({
queryKey: ['trip', train.trip], queryKey: ['trip', train.trip],
queryFn: () => fetch(`/api/gtfs/trip/${train.trip}/`) queryFn: () => fetch(`/api/gtfs/trip/${train.trip}/`)
@ -138,6 +138,19 @@ function TrainRow({train, tableType}) {
stopsFilter = (stop_time) => stop_time.stop_sequence < train.stop_sequence && stop_time.pickup_type === 0 stopsFilter = (stop_time) => stop_time.stop_sequence < train.stop_sequence && stop_time.pickup_type === 0
let stopsNames = stops.filter(stopsFilter).map(stopTime => stopTime?.stop.name ?? "").join(", ") ?? "" let stopsNames = stops.filter(stopsFilter).map(stopTime => stopTime?.stop.name ?? "").join(", ") ?? ""
const realtimeQuery = useQuery({
queryKey: ['realtime', train.id, date, time],
queryFn: () => fetch(`/api/gtfs-rt/stop_time_update/${train.id}/`)
.then(response => response.json()),
enabled: !!train.id,
})
const realtimeData = realtimeQuery.data ?? {}
if (!realtimeQuery.isError)
console.log(realtimeData)
const delay = tableType === "departures" ? realtimeData.departure_delay : realtimeData.arrival_delay
const prettyDelay = delay ? getPrettyDelay(delay) : ""
const visibleDelay = delay ? `${prettyDelay}` : ""
return <> return <>
<StyledTableRow tabletype={tableType}> <StyledTableRow tabletype={tableType}>
<TableCell> <TableCell>
@ -165,9 +178,16 @@ function TrainRow({train, tableType}) {
</Box> </Box>
</TableCell> </TableCell>
<TableCell> <TableCell>
<Box display="flex" alignItems="center" justifyContent="center" fontWeight="bold" color="#FFED02" fontSize={24}> <Box display="flex" alignItems="center" justifyContent="center">
<Box>
<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" : ""}>
{visibleDelay}
</Box>
</Box>
</Box>
</TableCell> </TableCell>
<TableCell> <TableCell>
<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>
@ -227,4 +247,12 @@ function getDisplayTime(train, tableType) {
return day_split[day_split.length - 1].substring(0, 5) return day_split[day_split.length - 1].substring(0, 5)
} }
function getPrettyDelay(delay) {
let delay_split = delay.split(':')
let hours = parseInt(delay_split[0])
let minutes = parseInt(delay_split[1])
let full_minutes = hours * 60 + minutes
return full_minutes ? `+${full_minutes} min` : "À l'heure"
}
export default TrainsTable; export default TrainsTable;

View File

@ -1,7 +1,7 @@
from rest_framework import serializers from rest_framework import serializers
from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \
Transfer, FeedInfo Transfer, FeedInfo, TripUpdate, StopTimeUpdate
class AgencySerializer(serializers.ModelSerializer): class AgencySerializer(serializers.ModelSerializer):
@ -61,3 +61,15 @@ class FeedInfoSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = FeedInfo model = FeedInfo
fields = '__all__' fields = '__all__'
class TripUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = TripUpdate
fields = '__all__'
class StopTimeUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = StopTimeUpdate
fields = '__all__'

View File

@ -10,10 +10,9 @@ from rest_framework.filters import OrderingFilter, SearchFilter
from sncf.api.serializers import AgencySerializer, StopSerializer, RouteSerializer, TripSerializer, \ from sncf.api.serializers import AgencySerializer, StopSerializer, RouteSerializer, TripSerializer, \
StopTimeSerializer, CalendarSerializer, CalendarDateSerializer, TransferSerializer, \ StopTimeSerializer, CalendarSerializer, CalendarDateSerializer, TransferSerializer, \
FeedInfoSerializer FeedInfoSerializer, TripUpdateSerializer, StopTimeUpdateSerializer
from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \
Transfer, FeedInfo Transfer, FeedInfo, TripUpdate, StopTimeUpdate
CACHE_CONTROL = cache_control(max_age=7200) CACHE_CONTROL = cache_control(max_age=7200)
LAST_MODIFIED = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version)) LAST_MODIFIED = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version))
@ -102,6 +101,20 @@ class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__' filterset_fields = '__all__'
class TripUpdateViewSet(viewsets.ReadOnlyModelViewSet):
queryset = TripUpdate.objects.all()
serializer_class = TripUpdateSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = '__all__'
class StopTimeUpdateViewSet(viewsets.ReadOnlyModelViewSet):
queryset = StopTimeUpdate.objects.all()
serializer_class = StopTimeUpdateSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = '__all__'
class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet): class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet):
queryset = StopTime.objects.none() queryset = StopTime.objects.none()
serializer_class = StopTimeSerializer serializer_class = StopTimeSerializer

View File

@ -19,7 +19,8 @@ from django.urls import path, include
from rest_framework import routers from rest_framework import routers
from sncf.api.views import AgencyViewSet, StopViewSet, RouteViewSet, TripViewSet, StopTimeViewSet, \ from sncf.api.views import AgencyViewSet, StopViewSet, RouteViewSet, TripViewSet, StopTimeViewSet, \
CalendarViewSet, CalendarDateViewSet, TransferViewSet, FeedInfoViewSet, NextDeparturesViewSet, NextArrivalsViewSet CalendarViewSet, CalendarDateViewSet, TransferViewSet, FeedInfoViewSet, NextDeparturesViewSet, NextArrivalsViewSet, \
TripUpdateViewSet, StopTimeUpdateViewSet
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register("gtfs/agency", AgencyViewSet) router.register("gtfs/agency", AgencyViewSet)
@ -31,6 +32,8 @@ router.register("gtfs/calendar", CalendarViewSet)
router.register("gtfs/calendar_date", CalendarDateViewSet) router.register("gtfs/calendar_date", CalendarDateViewSet)
router.register("gtfs/transfer", TransferViewSet) router.register("gtfs/transfer", TransferViewSet)
router.register("gtfs/feed_info", FeedInfoViewSet) router.register("gtfs/feed_info", FeedInfoViewSet)
router.register("gtfs-rt/trip_update", TripUpdateViewSet)
router.register("gtfs-rt/stop_time_update", StopTimeUpdateViewSet)
router.register("station/next_departures", NextDeparturesViewSet, basename="next_departures") router.register("station/next_departures", NextDeparturesViewSet, basename="next_departures")
router.register("station/next_arrivals", NextArrivalsViewSet, basename="next_arrivals") router.register("station/next_arrivals", NextArrivalsViewSet, basename="next_arrivals")