@@ -65,21 +65,6 @@ function cacheOperatorsAndShapes(prefix = 'nz-akl') {
6565 }
6666 getOperator ( index + 1 )
6767 } ) . catch ( err => console . warn ( err ) )
68-
69- // caches the shape
70- line . _getLine ( todo [ index ] , function ( err , data ) {
71- if ( err ) {
72- console . warn ( err )
73- }
74- shapeCount ++
75- if ( typeof ( data [ 0 ] ) !== 'undefined' ) {
76- shapesToCache . push ( { shape_id : data [ 0 ] . shape_id } )
77- }
78- if ( todo . length === shapeCount ) {
79- console . log ( 'nz-akl:' . green , 'Collected List of Shapes To Cache' )
80- line . cacheShapes ( shapesToCache )
81- }
82- } , 'nz-akl' )
8368 }
8469 getOperator ( 0 )
8570}
@@ -126,7 +111,7 @@ var line = {
126111 _getLine ( lineId , cb , prefix = 'nz-akl' ) {
127112 const sqlRequest = connection . get ( ) . request ( )
128113 sqlRequest . input ( 'prefix' , sql . VarChar ( 50 ) , prefix )
129- sqlRequest . input ( 'version' , sql . VarChar ( 50 ) , cache . currentVersion ( ) )
114+ sqlRequest . input ( 'version' , sql . VarChar ( 50 ) , cache . currentVersion ( prefix ) )
130115 sqlRequest . input ( 'route_short_name' , sql . VarChar ( 50 ) , lineId )
131116 sqlRequest . query ( `
132117 SELECT
@@ -210,61 +195,18 @@ var line = {
210195 } )
211196 } )
212197 } ,
213- getShapeFromAt ( arrayOfShapeId , cb ) {
214- if ( arrayOfShapeId . length === 0 ) {
215- return
216- }
217-
218- var newOpts = JSON . parse ( JSON . stringify ( shapeWKBOptions ) )
219- let shape_id = arrayOfShapeId [ 0 ]
220- newOpts . url += shape_id
221- request ( newOpts , function ( err , response , body ) {
222- if ( err ) {
223- console . warn ( err )
224- console . log ( `${ shape_id } : Failed to get Shape` )
225- return line . getShapeFromAt ( arrayOfShapeId . slice ( 1 ) , cb )
198+ getShapeJSON : function ( req , res ) {
199+ let prefix = req . params . prefix
200+ const containerName = encodeURIComponent ( ( prefix + '-' + cache . currentVersion ( prefix ) ) . replace ( '_' , '-' ) . replace ( '.' , '-' ) )
201+ const shape_id = req . params . shape_id
202+ const fileName = encodeURIComponent ( shape_id + '.json' )
203+ blobSvc . getBlobToStream ( containerName , fileName , res , function ( blobError ) {
204+ if ( blobError ) {
205+ res . status ( 404 )
226206 }
227-
228- // if AT doesn't send back a shape
229- let wkb = JSON . parse ( body ) . response
230- if ( wkb . length < 1 ) {
231- console . log ( `${ shape_id } : Shape not found!` )
232- return nextItem ( )
233- }
234-
235- let nextItem = function ( ) {
236- if ( arrayOfShapeId . length === 1 ) {
237- // only calls back the last shape, for immediate return
238- if ( cb ) {
239- cb ( wkb )
240- }
241- return
242- } else {
243- return line . getShapeFromAt ( arrayOfShapeId . slice ( 1 ) , cb )
244- }
245- }
246-
247- blobSvc . createBlockBlobFromText ( 'shapewkb' , shape_id , wkb [ 0 ] . the_geom , function ( blobErr , blobResult , blobResponse ) {
248- if ( blobErr ) {
249- console . warn ( blobErr )
250- return nextItem ( )
251- }
252- nextItem ( )
253-
254- // informs table storage that there is an item there
255- var task = {
256- PartitionKey : { '_' : 'shapewkb' } ,
257- RowKey : { '_' : shape_id } ,
258- date : { '_' : new Date ( ) , '$' :'Edm.DateTime' }
259- }
260- tableSvc . insertOrReplaceEntity ( 'meta' , task , function ( error ) {
261- if ( error ) {
262- return console . warn ( error )
263- }
264- console . log ( `${ shape_id } : Shape Saved from AT` )
265- } )
266- } )
267- } )
207+ res . end ( )
208+ return
209+ } )
268210 } ,
269211
270212 exceptionCheck : function ( route , bestMatchMode = false ) {
@@ -352,41 +294,6 @@ var line = {
352294 req . params . trip_id = trip_id
353295 line . getStopsFromTrip ( req , res )
354296 } )
355- } ,
356-
357- cacheShapes : function ( trips ) {
358- // makes a priority list
359- let allShapes = { }
360- trips . forEach ( function ( trip ) {
361- let shape = trip . shape_id
362- if ( shape in allShapes ) {
363- allShapes [ shape ] += 1
364- } else {
365- allShapes [ shape ] = 1
366- }
367- } )
368- // flattens to priority array
369- let sorted = Object . keys ( allShapes ) . sort ( function ( a , b ) {
370- return allShapes [ b ] - allShapes [ a ]
371- } ) . map ( function ( sortedKey ) {
372- return sortedKey
373- } )
374- let promises = [ ]
375- let requests = [ ]
376- sorted . forEach ( function ( shape ) {
377- promises . push ( new Promise ( function ( resolve , reject ) {
378- tableSvc . retrieveEntity ( 'meta' , 'shapewkb' , shape , function ( err , result , response ) {
379- if ( err ) {
380- requests . push ( shape )
381- }
382- resolve ( )
383- } )
384- } ) )
385- } )
386- // after it sees if they exist, fire the cache
387- Promise . all ( promises ) . then ( function ( ) {
388- line . getShapeFromAt ( requests )
389- } )
390297 }
391298}
392299
0 commit comments