@@ -402,8 +402,9 @@ var station = {
402402 if ( parseInt ( req . params . direction ) > 1 || parseInt ( req . params . direction ) < 0 ) {
403403 return res . status ( 400 ) . send ( { error : 'Direction is not valid.' } )
404404 }
405- new Promise ( function ( resolve , reject ) {
406- const currentVersion = cache . currentVersion ( )
405+ const currentVersion = cache . currentVersion ( )
406+ let promises = [ ]
407+ promises . push ( new Promise ( function ( resolve , reject ) {
407408 const azCurrentVersion = currentVersion . split ( '_' ) . join ( '-' ) . split ( '.' ) . join ( '-' )
408409 const parser = csvparse ( { delimiter : ',' } )
409410
@@ -452,62 +453,53 @@ var station = {
452453 }
453454 blobSvc . createReadStream ( azCurrentVersion , req . params . station + '.txt' ) . pipe ( parser )
454455 } )
455- } ) . then ( function ( data ) {
456- const currentVersion = cache . currentVersion ( )
456+ } ) )
457+ promises . push ( new Promise ( function ( resolve , reject ) {
457458 const azCurrentVersion = currentVersion . split ( '_' ) [ 1 ]
458- let finalTripsArray = [ ]
459- const prefixBlacklist = [ ]
460- const getTrip = function ( queue , index , callback ) {
461- // finished
462- if ( index === queue . length ) {
463- console . log ( index , queue . length )
464- return callback ( )
465- }
466- // skips over things that are blacklisted to be wrong
467- const prefix = queue [ index ] [ 1 ] . substring ( 0 , 5 ) + queue [ index ] [ 4 ]
468- if ( prefixBlacklist . indexOf ( prefix ) !== - 1 ) {
469- return getTrip ( queue , index + 1 , callback )
470- }
471- // console.log(queue[index][0], currentVersion)
472- tableSvc . retrieveEntity ( 'trips' , azCurrentVersion , queue [ index ] [ 1 ] + '-' + currentVersion , function ( error , trip , response ) {
473- if ( error ) {
474- // ignore not found trips
475- if ( error . statusCode != 404 ) {
476- // fail if needed, but still resolve
477- console . warn ( error )
478- }
479- return getTrip ( queue , index + 1 , callback )
480- }
481459
482- if ( trip . direction_id . _ !== req . params . direction || trip . route_short_name . _ !== req . params . route ) {
483- prefixBlacklist . push ( queue [ index ] [ 1 ] . substring ( 0 , 5 ) + queue [ index ] [ 4 ] )
484- return getTrip ( queue , index + 1 , callback )
460+ const query = new azure . TableQuery ( )
461+ . select ( [ 'RowKey' , 'route_id' , 'shape_id' , 'trip_headsign' , 'route_long_name' , 'frequency' , 'start_date' , 'end_date' , 'agency_id' ] )
462+ . where ( 'PartitionKey eq ?' , azCurrentVersion )
463+ . and ( 'route_short_name eq ?' , req . params . route )
464+ . and ( 'direction_id eq ?' , req . params . direction )
465+
466+ tableSvc . queryEntities ( 'trips' , query , null , function ( err , result , response ) {
467+ const trips = { }
468+ result . entries . forEach ( function ( entry ) {
469+ trips [ entry . RowKey . _ ] = {
470+ route_id : entry . route_id . _ ,
471+ shape_id : entry . shape_id . _ ,
472+ trip_headsign : entry . trip_headsign . _ ,
473+ route_long_name : entry . route_long_name . _ ,
474+ frequency : entry . frequency . _ ,
475+ start_date : entry . start_date . _ ,
476+ end_date : entry . end_date . _ ,
477+ agency_id : entry . agency_id . _ ,
485478 }
486-
487- finalTripsArray . push ( {
488- arrival_time_seconds : queue [ index ] [ 0 ] ,
489- stop_sequence : parseInt ( queue [ index ] [ 4 ] ) ,
490- trip_id : trip . RowKey . _ ,
491- route_long_name : trip . route_long_name . _ ,
492- agency_id : trip . agency_id . _ ,
493- direction_id : trip . direction_id . _ ,
494- end_date : trip . end_date . _ ,
495- frequency : trip . frequency . _ ,
496- shape_id : trip . shape_id . _ ,
497- route_short_name : trip . route_short_name . _ ,
498- route_type : trip . route_type . _ ,
499- start_date : trip . start_date . _ ,
500- trip_headsign : trip . trip_headsign . _
501- } )
502-
503- // should have all gone well :)
504- return getTrip ( queue , index + 1 , callback )
505479 } )
506- }
507-
508- getTrip ( data , 0 , function ( ) {
509- res . send ( finalTripsArray )
480+ resolve ( trips )
481+ } )
482+ } ) )
483+ Promise . all ( promises ) . then ( function ( data ) {
484+ const result = [ ]
485+ data [ 0 ] . forEach ( function ( record ) {
486+ // console.log(record)
487+ const trip_id = record [ 1 ] + '-' + currentVersion
488+ const obj = {
489+ arrival_time_seconds : parseInt ( record [ 0 ] ) ,
490+ trip_id : trip_id ,
491+ service_id : record [ 2 ] + '-' + currentVersion ,
492+ frequency : record [ 3 ] ,
493+ stop_sequence : parseInt ( record [ 4 ] )
494+ }
495+ if ( trip_id in data [ 1 ] ) {
496+ Object . assign ( obj , data [ 1 ] [ trip_id ] )
497+ result . push ( obj )
498+ }
510499 } )
500+ res . send ( result )
501+ } ) . catch ( function ( err ) {
502+ res . send ( err )
511503 } )
512504 }
513505}
0 commit comments