Skip to content

Commit

Permalink
Updated GTFS-Realtime alerts fields
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Nov 21, 2024
1 parent 4b6ebe9 commit ce24879
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Updated
- Updated GTFS-Realtime alerts fields
- Dependency Updates

## [4.15.10] - 2024-11-18

### Updated
Expand Down
51 changes: 30 additions & 21 deletions src/lib/import-gtfs-realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getNestedProperty(obj: any, defaultValue: any, path?: string) {
}
}

if (obj.__isLong__) return convertLongTimeToDate(obj);
if (obj?.__isLong__) return convertLongTimeToDate(obj);

return obj;
}
Expand Down Expand Up @@ -177,27 +177,36 @@ async function processRealtimeAlerts(
.join(', ')}) VALUES (${fieldValues.join(', ')})`,
).run();
} catch (error: any) {
task.logWarning('Import error: ' + error.message);
task.logWarning(`Import error: ${error.message}`);
}

const alertTargetArray = [];
for (const informedEntity of entity.alert.informedEntity) {
informedEntity.parent = entity;
const subValues = models.serviceAlertTargets.schema.map((column) =>
prepareRealtimeFieldValue(informedEntity, column, task),
if (
!entity.alert.informedEntity ||
entity.alert.informedEntity.length === 0
) {
task.logWarning(
`Import error: No informed entities found for alert id=${entity.id}`,
);
alertTargetArray.push(`(${subValues.join(', ')})`);
totalLineCount++;
}
} else {
const alertTargetArray = [];
for (const informedEntity of entity.alert.informedEntity) {
informedEntity.parent = entity;
const subValues = models.serviceAlertTargets.schema.map((column) =>
prepareRealtimeFieldValue(informedEntity, column, task),
);
alertTargetArray.push(`(${subValues.join(', ')})`);
totalLineCount++;
}

try {
db.prepare(
`REPLACE INTO ${models.serviceAlertTargets.filenameBase} (${models.serviceAlertTargets.schema
.map((column) => column.name)
.join(', ')}) VALUES ${alertTargetArray.join(', ')}`,
).run();
} catch (error: any) {
task.logWarning('Import error: ' + error.message);
try {
db.prepare(
`REPLACE INTO ${models.serviceAlertTargets.filenameBase} (${models.serviceAlertTargets.schema
.map((column) => column.name)
.join(', ')}) VALUES ${alertTargetArray.join(', ')}`,
).run();
} catch (error: any) {
task.logWarning(`Import error: ${error.message}`);
}
}

task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
Expand Down Expand Up @@ -226,7 +235,7 @@ async function processRealtimeTripUpdates(
.join(', ')}) VALUES (${fieldValues.join(', ')})`,
).run();
} catch (error: any) {
task.logWarning('Import error: ' + error.message);
task.logWarning(`Import error: ${error.message}`);
}

const stopTimeUpdateArray = [];
Expand All @@ -246,7 +255,7 @@ async function processRealtimeTripUpdates(
.join(', ')}) VALUES ${stopTimeUpdateArray.join(', ')}`,
).run();
} catch (error: any) {
task.logWarning('Import error: ' + error.message);
task.logWarning(`Import error: ${error.message}`);
}

task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
Expand Down Expand Up @@ -275,7 +284,7 @@ async function processRealtimeVehiclePositions(
.join(', ')}) VALUES (${fieldValues.join(', ')})`,
).run();
} catch (error: any) {
task.logWarning('Import error: ' + error.message);
task.logWarning(`Import error: ${error.message}`);
}

task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
Expand Down
35 changes: 29 additions & 6 deletions src/models/gtfs-realtime/service-alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ export const serviceAlerts = {
},
{
name: 'cause',
type: 'integer',
required: true,
min: 0,
type: 'text',
source: 'alert.cause',
default: 0,
},
{
name: 'effect',
type: 'text',
source: 'alert.effect',
},
{
name: 'url',
type: 'text',
source: 'alert.url',
default: '',
},
{
name: 'start_time',
Expand All @@ -33,19 +41,34 @@ export const serviceAlerts = {
default: '',
},
{
name: 'headline',
name: 'headerText',
type: 'text',
required: true,
source: 'alert.headerText.translation[0].text',
default: '',
},
{
name: 'description',
name: 'descriptionText',
type: 'text',
required: true,
source: 'alert.descriptionText.translation[0].text',
default: '',
},
{
name: 'ttsHeaderText',
type: 'text',
source: 'alert.ttsHeaderText.translation[0].text',
},
{
name: 'ttsDescriptionText',
type: 'text',
source: 'alert.ttsDescriptionText.translation[0].text',
},
{
name: 'severityLevel',
type: 'text',
source: 'alert.severityLevel',
},
{
name: 'created_timestamp',
type: 'integer',
Expand Down

0 comments on commit ce24879

Please sign in to comment.