From 5037871062c7df515a35bcf805eb4316599227f8 Mon Sep 17 00:00:00 2001 From: Evan Siroky Date: Mon, 15 Jan 2018 00:30:44 -0800 Subject: [PATCH] fix(interpolation): account for pre-interpolated stop_times --- lib/operations.js | 23 +++++++++++++++++++++++ tests/db.load.test.js | 11 +++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/operations.js b/lib/operations.js index 25c88f2..70c928d 100644 --- a/lib/operations.js +++ b/lib/operations.js @@ -62,6 +62,8 @@ function interpolateStopTimes (db, callback) { const querier = dbStreamer.getQuerier(streamerConfig) const maxUpdateConcurrency = db.trip.sequelize.getDialect() === 'sqlite' ? 1 : 100 const updateQueue = async.queue(updateInterpolatedTimes, maxUpdateConcurrency) + let isComplete = false + let numUpdates = 0 /** * Helper function to call upon completion of interpolation @@ -71,11 +73,30 @@ function interpolateStopTimes (db, callback) { console.log('interpolation encountered an error: ', err) return callback(err) } + // set is complete and create a queue drain function + // however, a feed may not have any interpolated times, so + // `isComplete` is set in case nothing is pushed to the queue + isComplete = true updateQueue.drain = () => { + console.log('interpolation completed successfully') callback(err) } } + let rowTimeout + + function onRowComplete () { + if (rowTimeout) { + clearTimeout(rowTimeout) + } + if (isComplete && numUpdates === 0) { + rowTimeout = setTimeout(() => { + console.log('interpolation completed successfully (no interpolations needed)') + callback() + }, 10000) + } + } + // TODO: fix this cause it doesn't work w/ sqlite with a schema for some reason const statement = `SELECT trip_id FROM ${streamerConfig.tableName}` querier.execute( @@ -111,6 +132,7 @@ function interpolateStopTimes (db, callback) { lastTimepoint: lastTimepoint, nextTimepoint: stopTime }) + numUpdates++ lookingForNextTimepoint = false } } else { @@ -122,6 +144,7 @@ function interpolateStopTimes (db, callback) { } lastStopTime = stopTime }) + onRowComplete() }) .catch(onComplete) }, diff --git a/tests/db.load.test.js b/tests/db.load.test.js index ba22fd2..2078183 100644 --- a/tests/db.load.test.js +++ b/tests/db.load.test.js @@ -131,6 +131,17 @@ describe(process.env.DIALECT, function () { gtfs.loadGtfs(done) }) + it('should load a gtfs and try to interpolate stop times that do not need interpolation', function (done) { + var config = util.getConfig() + this.timeout(config.maxLoadTimeout) + + config.gtfsFileOrFolder = 'only_calendar' + config.interpolateStopTimes = true + + var gtfs = GTFS(config) + gtfs.loadGtfs(done) + }) + describe('with schema', () => { afterEach(function (done) { // drop and create the database before each test