Compare commits

..

No commits in common. "11cf6bbf553cb096a3f534a1557fbd7c7cb683f7" and "16520c36644d166e4d8028a43df1d8836fe77016" have entirely different histories.

5 changed files with 47 additions and 88 deletions

View File

@ -109,6 +109,7 @@ function TrainRow({train, tableType, date, time}) {
const route = routeQuery.data ?? {} const route = routeQuery.data ?? {}
const trainType = getTrainType(train, trip, route) const trainType = getTrainType(train, trip, route)
const backgroundColor = getBackgroundColor(train, trip, route) const backgroundColor = getBackgroundColor(train, trip, route)
console.log(backgroundColor)
const textColor = getTextColor(train, trip, route) const textColor = getTextColor(train, trip, route)
const trainTypeDisplay = getTrainTypeDisplay(trainType) const trainTypeDisplay = getTrainTypeDisplay(trainType)
@ -141,7 +142,7 @@ function TrainRow({train, tableType, date, time}) {
enabled: !!trip.id, enabled: !!trip.id,
}) })
const realtimeTripData = realtimeTripQuery.data ?? {} const realtimeTripData = realtimeTripQuery.data ?? {}
const tripScheduleRelationship = realtimeTripData.schedule_relationship ?? 0 const scheduleRelationship = realtimeTripData.schedule_relationship ?? 0
const realtimeQuery = useQuery({ const realtimeQuery = useQuery({
queryKey: ['realtime', train.id, date, time], queryKey: ['realtime', train.id, date, time],
@ -150,16 +151,13 @@ 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 && !canceled ? getPrettyDelay(delay) : "" const prettyDelay = delay && scheduleRelationship !== 3 ? getPrettyDelay(delay) : ""
const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(tripScheduleRelationship, stopScheduleRelationship) const [prettyScheduleRelationship, scheduleRelationshipColor] = getPrettyScheduleRelationship(scheduleRelationship)
let stopsFilter let stopsFilter
if (canceled) if (scheduleRelationship === 3)
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
@ -199,8 +197,7 @@ 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"} <Box color={delay && delay !== "00:00:00" ? "#e86d2b" : "white"} fontWeight={delay && delay !== "00:00:00" ? "bold" : ""}>
fontWeight={delay && delay !== "00:00:00" ? "bold" : ""}>
{prettyDelay} {prettyDelay}
</Box> </Box>
<Box color={scheduleRelationshipColor} fontWeight="bold"> <Box color={scheduleRelationshipColor} fontWeight="bold">
@ -210,10 +207,8 @@ 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>
</> </>
@ -330,19 +325,14 @@ function getPrettyDelay(delay) {
return full_minutes ? `+${full_minutes} min` : "À l'heure" return full_minutes ? `+${full_minutes} min` : "À l'heure"
} }
function getPrettyScheduleRelationship(tripScheduledRelationship, stopScheduledRelationship) { function getPrettyScheduleRelationship(scheduledRelationship) {
switch (tripScheduledRelationship) { switch (scheduledRelationship) {
case 1: case 1:
return ["Ajouté", "#3ebb18"] return ["Ajouté", "#3ebb18"]
case 3: case 3:
return ["Supprimé", "#ff8701"] return ["Supprimé", "#ff8701"]
default: default:
switch (stopScheduledRelationship) { return ["", ""]
case 1:
return ["Supprimé", "#ff8701"]
default:
return ["", ""]
}
} }
} }

View File

@ -171,29 +171,18 @@ 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)) \ .filter(Q(departure_time__gte=query_time, pickup_type=0), calendar_filter(query_date)) \
.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)) \ .filter(Q(departure_time__gte=time_yesterday, pickup_type=0), calendar_filter(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))) \ .filter(Q(departure_time__gte=timedelta(0), pickup_type=0), calendar_filter(tomorrow)) \
.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))
@ -243,29 +232,18 @@ 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(departure_time__gte=query_time)) \ .filter(Q(arrival_time__gte=query_time, drop_off_type=0), calendar_filter(query_date)) \
.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(departure_time__gte=time_yesterday)) \ .filter(Q(arrival_time__gte=time_yesterday, drop_off_type=0), calendar_filter(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(departure_time__gte=timedelta(0))) \ .filter(Q(arrival_time__gte=timedelta(0), drop_off_type=0), calendar_filter(tomorrow)) \
.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,13 +28,11 @@ 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)
@ -126,7 +124,6 @@ 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

@ -56,6 +56,8 @@ class Command(BaseCommand):
self.stdout.write("Updating database...") self.stdout.write("Updating database...")
all_trips = []
for transport_type, feed_url in self.GTFS_FEEDS.items(): for transport_type, feed_url in self.GTFS_FEEDS.items():
self.stdout.write(f"Downloading {transport_type} GTFS feed...") self.stdout.write(f"Downloading {transport_type} GTFS feed...")
with ZipFile(BytesIO(requests.get(feed_url).content)) as zipfile: with ZipFile(BytesIO(requests.get(feed_url).content)) as zipfile:
@ -102,7 +104,8 @@ class Command(BaseCommand):
zone_id=stop_dict.get('zone_id', ""), zone_id=stop_dict.get('zone_id', ""),
url=stop_dict.get('stop_url', ""), url=stop_dict.get('stop_url', ""),
location_type=stop_dict.get('location_type', 1) or 1, location_type=stop_dict.get('location_type', 1) or 1,
parent_station_id=stop_dict.get('parent_station', None) or None, parent_station_id=stop_dict.get('parent_station', None) or None
if last_update_date != "1970-01-01" or transport_type != "TN" else None,
timezone=stop_dict.get('stop_timezone', ""), timezone=stop_dict.get('stop_timezone', ""),
wheelchair_boarding=stop_dict.get('wheelchair_boarding', 0), wheelchair_boarding=stop_dict.get('wheelchair_boarding', 0),
level_id=stop_dict.get('level_id', ""), level_id=stop_dict.get('level_id', ""),
@ -111,9 +114,17 @@ class Command(BaseCommand):
) )
stops.append(stop) stops.append(stop)
if len(stops) >= bulk_size and not dry_run:
Stop.objects.bulk_create(stops,
update_conflicts=True,
update_fields=['name', 'desc', 'lat', 'lon', 'zone_id', 'url',
'location_type', 'parent_station_id', 'timezone',
'wheelchair_boarding', 'level_id', 'platform_code',
'transport_type'],
unique_fields=['id'])
stops.clear()
if stops and not dry_run: if stops and not dry_run:
Stop.objects.bulk_create(stops, Stop.objects.bulk_create(stops,
batch_size=bulk_size,
update_conflicts=True, update_conflicts=True,
update_fields=['name', 'desc', 'lat', 'lon', 'zone_id', 'url', update_fields=['name', 'desc', 'lat', 'lon', 'zone_id', 'url',
'location_type', 'parent_station_id', 'timezone', 'location_type', 'parent_station_id', 'timezone',
@ -159,7 +170,6 @@ class Command(BaseCommand):
unique_fields=['id']) unique_fields=['id'])
routes.clear() routes.clear()
Calendar.objects.filter(transport_type=transport_type).delete()
calendar_ids = [] calendar_ids = []
if "calendar.txt" in zipfile.namelist(): if "calendar.txt" in zipfile.namelist():
calendars = [] calendars = []
@ -298,6 +308,8 @@ class Command(BaseCommand):
unique_fields=['id']) unique_fields=['id'])
trips.clear() trips.clear()
all_trips.extend(trips)
stop_times = [] stop_times = []
for stop_time_dict in csv.DictReader(read_file("stop_times.txt")): for stop_time_dict in csv.DictReader(read_file("stop_times.txt")):
stop_time_dict: dict stop_time_dict: dict

View File

@ -27,17 +27,12 @@ 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:]))
@ -48,13 +43,10 @@ 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(first_stop_queryset).intersection(last_stop_queryset) trip_ids = trip_ids.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)
@ -99,6 +91,7 @@ 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(
@ -111,13 +104,12 @@ class Command(BaseCommand):
} }
) )
sample_trip = Trip.objects.filter(id__in=trip_ids, route_id=route_id).first() sample_trip = Trip.objects.filter(id__in=trip_ids).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():
@ -128,26 +120,18 @@ 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": arr_time, "arrival_time": datetime.fromtimestamp(stop_time_update.arrival.time,
"departure_time": dep_time, tz=ZoneInfo("Europe/Paris")) - start_dt,
"pickup_type": pickup_type, "departure_time": datetime.fromtimestamp(stop_time_update.departure.time,
"drop_off_type": drop_off_type, 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,
} }
) )
@ -155,13 +139,11 @@ 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.update_or_create( tu, _created = TripUpdate.objects.get_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):
@ -192,9 +174,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 != 1 or st.drop_off_type != 1: if st.pickup_type != 0 or st.drop_off_type != 0:
st.pickup_type = 1 st.pickup_type = 0
st.drop_off_type = 1 st.drop_off_type = 0
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),