Skip to content

Commit

Permalink
fix(interpolation): account for pre-interpolated stop_times
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Jan 15, 2018
1 parent 442b9b2 commit 5037871
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -111,6 +132,7 @@ function interpolateStopTimes (db, callback) {
lastTimepoint: lastTimepoint,
nextTimepoint: stopTime
})
numUpdates++
lookingForNextTimepoint = false
}
} else {
Expand All @@ -122,6 +144,7 @@ function interpolateStopTimes (db, callback) {
}
lastStopTime = stopTime
})
onRowComplete()
})
.catch(onComplete)
},
Expand Down
11 changes: 11 additions & 0 deletions tests/db.load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5037871

Please sign in to comment.