Skip to content

Commit 06868a0

Browse files
Merge pull request #74 from linkedconnections/development
v2.1.4
2 parents 7a2a4a4 + ba85219 commit 06868a0

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed

lib/Gtfsrt2LC.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Gtfsrt2LC {
112112
// Figure service date and trip start time
113113
let serviceDay = null;
114114
let tripStartTime = null;
115-
115+
116116
if (tripUpdate.trip.startDate) {
117117
const rawStartDate = tripUpdate.trip.startDate;
118118
serviceDay = new Date(
@@ -401,21 +401,21 @@ class Gtfsrt2LC {
401401
);
402402
const tomorrowServiceDate = addDays(todayServiceDate, 1);
403403
const yesterdayServiceDate = addDays(todayServiceDate, -1);
404-
404+
405405
const todayDistance = service[today] === '1' ? Math.abs(now - todayServiceDate) : Number.POSITIVE_INFINITY;
406406
const tomorrowDistance = service[tomorrow] === '1' ? Math.abs(now - tomorrowServiceDate) : Number.POSITIVE_INFINITY;
407407
const yesterdayDistance = service[yesterday] === '1' ? Math.abs(now - yesterdayServiceDate) : Number.POSITIVE_INFINITY;
408408

409-
if(todayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
410-
return todayServiceDate.setUTCHours(0, 0, 0 ,0);
409+
if (todayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
410+
return todayServiceDate.setUTCHours(0, 0, 0, 0);
411411
}
412412

413-
if(tomorrowDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
414-
return tomorrowServiceDate.setUTCHours(0, 0, 0 ,0);
413+
if (tomorrowDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
414+
return tomorrowServiceDate.setUTCHours(0, 0, 0, 0);
415415
}
416416

417-
if(yesterdayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
418-
return yesterdayServiceDate.setUTCHours(0, 0, 0 ,0);
417+
if (yesterdayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
418+
return yesterdayServiceDate.setUTCHours(0, 0, 0, 0);
419419
}
420420
}
421421

@@ -557,6 +557,21 @@ class Gtfsrt2LC {
557557
update['departure']['time'] = (this.addDuration(serviceDay, this.parseGTFSDuration(staticData['departure_time'])).getTime() / 1000) + update['departure']['delay'];
558558
}
559559
}
560+
} else {
561+
// If this stop is not the last of the trip and the stop update is missing departure info
562+
// add it manually taking into account the arrival delay at this stop
563+
if (staticIndex < staticLength - 1 && update['arrival']) {
564+
update['departure'] = {
565+
'delay': update['arrival']['delay'] | 0,
566+
'time': (this.addDuration(serviceDay, this.parseGTFSDuration(staticData['departure_time'])).getTime() / 1000) + update['arrival']['delay']
567+
}
568+
} else {
569+
// Fallback to static data
570+
update['departure'] = {
571+
'delay': 0,
572+
'time': (this.addDuration(serviceDay, this.parseGTFSDuration(staticData['departure_time'])).getTime() / 1000)
573+
}
574+
}
560575
}
561576

562577
// Check if arrival time is explicitly defined. In some cases only the delay is given
@@ -570,23 +585,10 @@ class Gtfsrt2LC {
570585
update['arrival']['time'] = (this.addDuration(serviceDay, this.parseGTFSDuration(staticData['arrival_time'])).getTime() / 1000) + update['arrival']['delay'];
571586
}
572587
}
573-
}
574-
575-
// If this stop is not the last of the trip and the stop update is missing departure info
576-
// add it manually taking into account the arrival delay at this stop
577-
if (staticIndex != staticLength - 1) {
578-
if (!update['departure']) {
579-
update['departure'] = {
580-
'delay': update['arrival']['delay'],
581-
'time': (this.addDuration(serviceDay, this.parseGTFSDuration(staticData['departure_time'])).getTime() / 1000) + update['arrival']['delay']
582-
}
583-
}
584-
}
585-
586-
// If the stop update is missing arrival info and is not the first stop of the trip
587-
// add it manually taking into account the departure delay of the previous stop (if any)
588-
if (staticIndex != 0) {
589-
if (!update['arrival'] && prevUpdate) {
588+
} else {
589+
// If the stop update is missing arrival info and is not the first stop of the trip
590+
// add it manually taking into account the departure delay of the previous stop (if any)
591+
if (staticIndex > 0 && prevUpdate) {
590592
// We need to make sure that adding the departure delay of the previous stop
591593
// to the arrival time of this stop won't cause inconsistent times,
592594
// i.e. arrival > departure.
@@ -625,10 +627,10 @@ class Gtfsrt2LC {
625627

626628
}
627629
}
628-
}
630+
} else { /* This should never happen */ }
629631
}
630632

631-
// Check for consistencies between this update and the previous
633+
// Check for inconsistencies between this update and the previous
632634
if (prevUpdate && update['departure'] && prevUpdate['departure']['time'] > update['arrival']['time']) {
633635
// Enforce previous delay on this update to keep consistency
634636
let prevDepDelay = prevUpdate ? prevUpdate['departure']['delay'] : 0;
@@ -650,6 +652,8 @@ class Gtfsrt2LC {
650652
}
651653
} catch (err) {
652654
console.error(err);
655+
console.error('Issue encountered while processing this update: ', JSON.stringify(update, null, 3));
656+
console.error('From this trip: ', JSON.stringify(staticData, null, 3));
653657
}
654658

655659
return update;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gtfsrt2lc",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "Converts the GTFS-RT to Linked Connections",
55
"main": "./Gtfsrt2LC.js",
66
"bin": {

test/gtfsrt2lc.test.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const del = require('del');
33
const { Readable } = require('stream');
44
const uri_templates = require('uri-templates');
5+
const { Level } = require('level');
56
const GtfsIndex = require('../lib/GtfsIndex');
67
const Gtfsrt2lc = require('../lib/Gtfsrt2LC');
78
const Utils = require('../lib/Utils');
@@ -114,6 +115,50 @@ test('Extract all indexes when source is given as decompressed folder', async ()
114115
await del(['./test/data/decompressed'], { force: true });
115116
});
116117

118+
test('Historic records are used to prune unchanged connections', async () => {
119+
expect.assertions(4);
120+
const historyDB = new Level('./test/data/history.db', { valueEncoding: 'json' });
121+
const gti = new GtfsIndex({ path: static_path });
122+
const indexes = await gti.getIndexes({ store: 'MemStore' });
123+
124+
// First run
125+
const grt1 = new Gtfsrt2lc({
126+
path: rt_path,
127+
uris: mock_uris,
128+
});
129+
grt1.setIndexes({ ...indexes, historyDB });
130+
const connStream1 = await grt1.parse({ format: 'jsonld', objectMode: true });
131+
let count1 = 0;
132+
const endStream = new Promise(res => {
133+
connStream1.on('end', () => res(true))
134+
.on('error', () => res(false));
135+
});
136+
connStream1.on('data', conn => { count1++; });
137+
const success1 = await endStream;
138+
139+
// Second run
140+
const grt2 = new Gtfsrt2lc({
141+
path: rt_path,
142+
uris: mock_uris,
143+
});
144+
grt2.setIndexes({ ...indexes, historyDB });
145+
const connStream2 = await grt2.parse({ format: 'jsonld', objectMode: true });
146+
let count2 = 0;
147+
const endStream2 = new Promise(res => {
148+
connStream2.on('end', () => res(true))
149+
.on('error', () => res(false));
150+
});
151+
connStream2.on('data', conn => { count2++; });
152+
const success2 = await endStream2;
153+
154+
expect(success1).toBeTruthy();
155+
expect(count1).toBeGreaterThan(0);
156+
expect(success2).toBeTruthy();
157+
expect(count2).toBe(0);
158+
159+
await del(['./test/data/history.db'], { force: true });
160+
});
161+
117162
test('Check all parsed connections are consistent regarding departure and arrival times', async () => {
118163
grt.setIndexes(memIndexes);
119164
let connStream = await grt.parse({ format: 'jsonld' });
@@ -641,8 +686,8 @@ test('Cover Gtfsrt2LC functions', async () => {
641686
}
642687
expect(fail).toBeDefined();
643688

644-
const readStream = new Readable({ objectMode: true, read() {}});
645-
gtfsrt2lc.handleResponse({
689+
const readStream = new Readable({ objectMode: true, read() { } });
690+
gtfsrt2lc.handleResponse({
646691
statusCode: 200,
647692
headers: { 'content-encoding': 'fake-format' },
648693
body: Promise.resolve(readStream)

0 commit comments

Comments
 (0)