gtfs 4.18.6 → 4.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -465,16 +465,14 @@ var serviceAlerts = {
465
465
  {
466
466
  name: "start_time",
467
467
  type: "text",
468
- required: true,
469
468
  source: "alert.activePeriod[0].start",
470
- default: ""
469
+ default: null
471
470
  },
472
471
  {
473
472
  name: "end_time",
474
473
  type: "text",
475
- required: true,
476
474
  source: "alert.activePeriod[0].end",
477
- default: ""
475
+ default: null
478
476
  },
479
477
  {
480
478
  name: "header_text",
@@ -534,7 +532,7 @@ var serviceAlertInformedEntities = {
534
532
  {
535
533
  name: "stop_id",
536
534
  type: "text",
537
- index: true,
535
+ primary: true,
538
536
  source: "stopId",
539
537
  default: null,
540
538
  prefix: true
@@ -542,7 +540,7 @@ var serviceAlertInformedEntities = {
542
540
  {
543
541
  name: "route_id",
544
542
  type: "text",
545
- index: true,
543
+ primary: true,
546
544
  source: "routeId",
547
545
  default: null,
548
546
  prefix: true
@@ -550,14 +548,14 @@ var serviceAlertInformedEntities = {
550
548
  {
551
549
  name: "route_type",
552
550
  type: "integer",
553
- index: true,
551
+ primary: true,
554
552
  source: "routeType",
555
553
  default: null
556
554
  },
557
555
  {
558
556
  name: "trip_id",
559
557
  type: "text",
560
- index: true,
558
+ primary: true,
561
559
  source: "trip.tripId",
562
560
  default: null,
563
561
  prefix: true
@@ -565,7 +563,7 @@ var serviceAlertInformedEntities = {
565
563
  {
566
564
  name: "direction_id",
567
565
  type: "integer",
568
- index: true,
566
+ primary: true,
569
567
  source: "directionId",
570
568
  default: null
571
569
  },
@@ -746,7 +744,7 @@ function convertLongTimeToDate(longDate) {
746
744
  ).toISOString();
747
745
  }
748
746
  function applyPrefixToValue(value, columnShouldBePrefixed, prefix) {
749
- if (!columnShouldBePrefixed || prefix === void 0 || value === null) {
747
+ if (!columnShouldBePrefixed || prefix === void 0 || value === null || value === void 0) {
750
748
  return value;
751
749
  }
752
750
  return `${prefix}${value}`;
@@ -809,13 +807,15 @@ async function fetchGtfsRealtimeData(type, task) {
809
807
  try {
810
808
  const response = await fetch(urlConfig.url, {
811
809
  method: "GET",
810
+ redirect: "follow",
812
811
  headers: {
812
+ "User-Agent": "node-gtfs",
813
813
  ...urlConfig.headers ?? {},
814
814
  "Accept-Encoding": "gzip"
815
815
  },
816
816
  signal: task.downloadTimeout ? AbortSignal.timeout(task.downloadTimeout) : void 0
817
817
  });
818
- if (response.status !== 200) {
818
+ if (!response.ok) {
819
819
  throw new GtfsError(`HTTP ${response.status}: ${response.statusText}`, {
820
820
  code: "GTFS_DOWNLOAD_HTTP" /* GTFS_DOWNLOAD_HTTP */,
821
821
  category: "download" /* DOWNLOAD */,
@@ -888,12 +888,17 @@ function createServiceAlertsProcessor(db, task) {
888
888
  db,
889
889
  serviceAlertInformedEntities
890
890
  );
891
+ const deleteInformedEntitiesStmt = db.prepare(
892
+ `DELETE FROM ${serviceAlertInformedEntities.filenameBase} WHERE alert_id = ?`
893
+ );
891
894
  return async (batch) => {
892
895
  let recordCount = 0;
893
896
  let errorCount = 0;
894
897
  db.transaction(() => {
895
898
  for (const entity of batch) {
896
899
  try {
900
+ const alertId = applyPrefixToValue(entity.id, true, task.prefix);
901
+ deleteInformedEntitiesStmt.run(alertId);
897
902
  const alertValues = serviceAlerts.schema.map((column) => prepareRealtimeFieldValue(entity, column, task));
898
903
  alertStmt.run(alertValues);
899
904
  recordCount++;
@@ -926,6 +931,9 @@ function createTripUpdatesProcessor(db, task) {
926
931
  db,
927
932
  stopTimeUpdates
928
933
  );
934
+ const deleteStopTimesByTripStmt = db.prepare(
935
+ `DELETE FROM ${stopTimeUpdates.filenameBase} WHERE trip_id = ? AND trip_start_time IS ?`
936
+ );
929
937
  return async (batch) => {
930
938
  let recordCount = 0;
931
939
  let errorCount = 0;
@@ -936,6 +944,15 @@ function createTripUpdatesProcessor(db, task) {
936
944
  tripUpdateStmt.run(tripUpdateValues);
937
945
  recordCount++;
938
946
  if (entity.tripUpdate?.stopTimeUpdate?.length) {
947
+ const tripId = applyPrefixToValue(
948
+ entity.tripUpdate?.trip?.tripId ?? null,
949
+ true,
950
+ task.prefix
951
+ );
952
+ const tripStartTime = entity.tripUpdate?.trip?.startTime ?? null;
953
+ if (tripId !== null) {
954
+ deleteStopTimesByTripStmt.run(tripId, tripStartTime);
955
+ }
939
956
  for (const stopTimeUpdate of entity.tripUpdate.stopTimeUpdate) {
940
957
  stopTimeUpdate.parent = entity;
941
958
  const stopTimeValues = stopTimeUpdates.schema.map(
@@ -1015,41 +1032,30 @@ async function updateGtfsRealtimeData(task) {
1015
1032
  tripupdates: 0,
1016
1033
  vehiclepositions: 0
1017
1034
  };
1018
- const processingPromises = [];
1019
1035
  if (alertsData?.entity?.length) {
1020
- processingPromises.push(
1021
- processBatch(
1022
- alertsData.entity,
1023
- BATCH_SIZE,
1024
- createServiceAlertsProcessor(db, task)
1025
- ).then((result) => {
1026
- recordCounts.alerts = result.recordCount;
1027
- })
1036
+ const result = await processBatch(
1037
+ alertsData.entity,
1038
+ BATCH_SIZE,
1039
+ createServiceAlertsProcessor(db, task)
1028
1040
  );
1041
+ recordCounts.alerts = result.recordCount;
1029
1042
  }
1030
1043
  if (tripUpdatesData?.entity?.length) {
1031
- processingPromises.push(
1032
- processBatch(
1033
- tripUpdatesData.entity,
1034
- BATCH_SIZE,
1035
- createTripUpdatesProcessor(db, task)
1036
- ).then((result) => {
1037
- recordCounts.tripupdates = result.recordCount;
1038
- })
1044
+ const result = await processBatch(
1045
+ tripUpdatesData.entity,
1046
+ BATCH_SIZE,
1047
+ createTripUpdatesProcessor(db, task)
1039
1048
  );
1049
+ recordCounts.tripupdates = result.recordCount;
1040
1050
  }
1041
1051
  if (vehiclePositionsData?.entity?.length) {
1042
- processingPromises.push(
1043
- processBatch(
1044
- vehiclePositionsData.entity,
1045
- BATCH_SIZE,
1046
- createVehiclePositionsProcessor(db, task)
1047
- ).then((result) => {
1048
- recordCounts.vehiclepositions = result.recordCount;
1049
- })
1052
+ const result = await processBatch(
1053
+ vehiclePositionsData.entity,
1054
+ BATCH_SIZE,
1055
+ createVehiclePositionsProcessor(db, task)
1050
1056
  );
1057
+ recordCounts.vehiclepositions = result.recordCount;
1051
1058
  }
1052
- await Promise.all(processingPromises);
1053
1059
  task.log(
1054
1060
  `GTFS-Realtime import complete: ${recordCounts.alerts} alerts, ${recordCounts.tripupdates} trip updates, ${recordCounts.vehiclepositions} vehicle positions`
1055
1061
  );