@@ -59,11 +59,15 @@ function estimateTravelTime(distanceKm, startTime, city) {
5959 return minutes > 5 ? minutes : 5 ;
6060}
6161
62+ // This file now exclusively serves as an Express route handler for local development or a traditional server.
6263export const predictRoute = ( req , res ) => {
64+ const requestId = `req-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 7 ) } ` ;
65+ console . log ( `[predict.js] [${ requestId } ] Received prediction request.` ) ;
6366 try {
6467 const { from, to, startTime, city } = req . body || { } ;
6568
6669 if ( ! from || ! to || ! startTime || ! city ) {
70+ console . warn ( `[predict.js] [${ requestId } ] Bad Request: Missing required fields. Body:` , req . body ) ;
6771 return res . status ( 400 ) . json ( { error : 'Missing required fields: from, to, startTime, city' } ) ;
6872 }
6973
@@ -73,6 +77,7 @@ export const predictRoute = (req, res) => {
7377 from . lat < - 90 || from . lat > 90 || to . lat < - 90 || to . lat > 90 ||
7478 from . lon < - 180 || from . lon > 180 || to . lon < - 180 || to . lon > 180
7579 ) {
80+ console . warn ( `[predict.js] [${ requestId } ] Bad Request: Invalid coordinates. From:` , from , "To:" , to ) ;
7681 return res . status ( 400 ) . json ( { error : 'Invalid coordinates' } ) ;
7782 }
7883
@@ -81,9 +86,11 @@ export const predictRoute = (req, res) => {
8186 const cacheKey = createCacheKey ( from , to , startTime , cityKey ) ;
8287 const cached = predictionCache . get ( cacheKey ) ;
8388 if ( cached !== undefined ) {
89+ console . log ( `[predict.js] [${ requestId } ] Cache HIT for key: ${ cacheKey } ` ) ;
8490 return res . json ( { ...cached , cached : true } ) ;
8591 }
8692
93+ console . log ( `[predict.js] [${ requestId } ] Cache MISS for key: ${ cacheKey } . Calculating new prediction.` ) ;
8794 const distanceKm = calculateDistance ( from . lat , from . lon , to . lat , to . lon ) ;
8895 const minutes = estimateTravelTime ( distanceKm , startTime , cityKey ) ;
8996
@@ -98,9 +105,11 @@ export const predictRoute = (req, res) => {
98105 } ;
99106
100107 predictionCache . set ( cacheKey , prediction ) ;
108+ console . log ( `[predict.js] [${ requestId } ] Prediction successful. Result:` , prediction ) ;
101109 return res . json ( { ...prediction , cached : false } ) ;
102110 } catch ( error ) {
103- console . error ( 'Prediction error:' , error ) ;
111+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
112+ console . error ( `[predict.js] [${ requestId } ] Prediction error: ${ errorMessage } ` , { error } ) ;
104113 return res . status ( 500 ) . json ( {
105114 error : 'Internal server error' ,
106115 message : error ?. message ?? String ( error ) ,
0 commit comments