Skip to content

Commit

Permalink
refactor(gtfs+): simplify lookupMissingEntities function
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Feb 19, 2019
1 parent 7d150d8 commit da0e230
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
50 changes: 24 additions & 26 deletions lib/gtfsplus/actions/gtfsplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function loadGtfsEntities (
})
: rows
const {namespace} = feedVersion
const {routesToLoad, stopsToLoad} = lookupMissingEntities(tableId, filteredRows, getState().gtfsplus.gtfsEntityLookup)
const {routesToLoad, stopsToLoad} = lookupMissingEntities(filteredRows, getState().gtfsplus.gtfsEntityLookup)
if (routesToLoad.length === 0 && stopsToLoad.length === 0) {
return
}
Expand All @@ -225,40 +225,38 @@ export function loadGtfsEntities (
}
}

function lookupMissingEntities (tableId: string, rows: Array<GTFSPlusEntity>, currentLookup: Object) {
// lookup table for mapping tableId:fieldName keys to inputType values
const typeLookup = {}
const getDataType = function (tableId, fieldName) {
const lookupKey = tableId + ':' + fieldName
if (lookupKey in typeLookup) return typeLookup[lookupKey]
const specTable = getGtfsPlusSpec().find(t => t.id === tableId)
if (!specTable) {
console.warn(`unable to find spec table for tableId: ${tableId}`)
return null
}
const fieldInfo = specTable.fields.find(f => f.name === fieldName)
if (!fieldInfo) return null
typeLookup[lookupKey] = fieldInfo.inputType
return fieldInfo.inputType
}

// Determine which routes, stops, etc. aren't currently in the
/**
* Given a set of GTFS+ rows, determine (from the current state's lookup) if the
* rows contain references to GTFS routes or stops that have not yet been fetched
* from the GraphQL GTFS API and return the list of unfetched/missing stops and
* routes. NOTE: only a single feed version is considered at once, so there is
* no need to scope the route/stop IDs by feed.
*/
function lookupMissingEntities (
gtfsPlusRows: Array<GTFSPlusEntity>,
currentLookup: {[string]: GtfsRoute | GtfsStop}
) {
// Determine which routes and stops aren't currently in the
// gtfsEntityLookup table and need to be loaded from the API.
const routesToLoad: string[] = []
const stopsToLoad: string[] = []
for (const rowData of rows) {
for (const rowData of gtfsPlusRows) {
for (const fieldName in rowData) {
const dataType = getDataType(tableId, fieldName)
switch (dataType) {
case 'GTFS_ROUTE':
const routeId = rowData.route_id
// Currently, the only references to stops/routes in GTFS+ are fields named
// route_id or stop_id. If this changes, this logic should revert to
// commit 7d150d85e203b336147ba106eef7c3be90b39fb7 which contained a way
// to lookup based on inputType in gtfsplus.yml.
switch (fieldName) {
case 'route_id':
const {route_id: routeId} = rowData
if (routeId && !(`route_${routeId}` in currentLookup)) routesToLoad.push(routeId)
break
case 'GTFS_STOP':
const stopId = rowData.stop_id
case 'stop_id':
const {stop_id: stopId} = rowData
if (stopId && !(`stop_${stopId}` in currentLookup)) stopsToLoad.push(stopId)
break
default:
// No entities to search for.
break
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/types/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
Alert,
ControlPoint,
FetchStatus,
GtfsRoute,
GtfsStop,
Organization,
Project,
Expand Down Expand Up @@ -443,7 +444,9 @@ export type GtfsPlusReducerState = {
activeTableId: 'realtime_routes',
currentPage: number,
feedVersionId: null | string,
gtfsEntityLookup: Object, // TODO specify more exact type
gtfsEntityLookup: {
[string]: GtfsStop | GtfsRoute
},
pageCount: number,
recordsPerPage: number,
tableData: null | any, // TODO specify more exact type
Expand Down

0 comments on commit da0e230

Please sign in to comment.