Skip to content

Commit

Permalink
Merge pull request #59 from evansiroky/interpolate-timeout
Browse files Browse the repository at this point in the history
Account for pre-interpolated stop_times
  • Loading branch information
evansiroky authored Jan 15, 2018
2 parents 442b9b2 + 5037871 commit ccdefac
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 ccdefac

Please sign in to comment.