|
|
|
@ -31,16 +31,20 @@ example response from interpolation web service:
|
|
|
|
|
// that async.map never returns an error.
|
|
|
|
|
function error_intercepting_service(service, req) { |
|
|
|
|
return (street_result, next) => { |
|
|
|
|
service(req, street_result, (err, interpolation_result) => { |
|
|
|
|
service(req, street_result, (err, interpolation_result, metadata) => { |
|
|
|
|
if (err) { |
|
|
|
|
logger.error(`[middleware:interpolation] ${_.defaultTo(err.message, err)}`); |
|
|
|
|
// now that the error has been caught and reported, act as if there was no error
|
|
|
|
|
return next(null, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// metadata can't be passed as 3rd parameter here, so include in result
|
|
|
|
|
if (interpolation_result) { |
|
|
|
|
interpolation_result.metadata = metadata; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// no error occurred, so pass along the result
|
|
|
|
|
return next(null, interpolation_result); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
@ -61,26 +65,42 @@ function setup(service, should_execute) {
|
|
|
|
|
const start = (new Date()).getTime(); |
|
|
|
|
const initialTime = debugLog.beginTimer(req); |
|
|
|
|
|
|
|
|
|
logger.info(`[interpolation] [street_results] count=${street_results.length}`); |
|
|
|
|
const startTime = Date.now(); |
|
|
|
|
const logInfo = { |
|
|
|
|
controller: 'interpolation', //technically middleware, but stay consistent with other log lines
|
|
|
|
|
street_count: street_results.length, |
|
|
|
|
params: req.clean, |
|
|
|
|
responses: [] |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// call the interpolation service asynchronously on every street result
|
|
|
|
|
async.map(street_results, error_intercepting_service(service, req), (err, interpolation_results) => { |
|
|
|
|
|
|
|
|
|
// iterate the interpolation results, mapping back into the source results
|
|
|
|
|
interpolation_results.forEach((interpolation_result, idx) => { |
|
|
|
|
const source_result = street_results[idx]; |
|
|
|
|
const debugLogInfo = { |
|
|
|
|
outcome: 'hit', //assume hit, update later if not
|
|
|
|
|
text: req.clean.parsed_text, |
|
|
|
|
result: interpolation_result |
|
|
|
|
}; |
|
|
|
|
const resultLogInfo = { |
|
|
|
|
outcome: 'hit', //assume hit, update later if not
|
|
|
|
|
response_time: _.get(interpolation_result, 'metadata.response_time') |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// invalid / not useful response, debug log for posterity
|
|
|
|
|
// note: leave this hit unmodified
|
|
|
|
|
if (!_.has(interpolation_result, 'properties')) { |
|
|
|
|
logger.debug(`[interpolation] [miss] ${req.clean.parsed_text}`); |
|
|
|
|
debugLogInfo.outcome = 'miss'; |
|
|
|
|
resultLogInfo.outcome = 'miss'; |
|
|
|
|
logger.debug('interpolation', debugLogInfo); |
|
|
|
|
debugLog.push(req, 'miss'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// the interpolation service returned a valid result, debug log for posterity
|
|
|
|
|
// note: we now merge those values with the existing 'street' record
|
|
|
|
|
logger.debug(`[interpolation] [hit] ${req.clean.parsed_text} ${JSON.stringify(interpolation_result)}`); |
|
|
|
|
logger.debug('interpolation', debugLogInfo); |
|
|
|
|
debugLog.push(req, interpolation_result); |
|
|
|
|
|
|
|
|
|
// -- metadata --
|
|
|
|
@ -129,15 +149,14 @@ function setup(service, should_execute) {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// log the execution time, continue
|
|
|
|
|
logger.info( `[interpolation] [took] ${(new Date()).getTime() - start} ms`); |
|
|
|
|
|
|
|
|
|
// log and continue
|
|
|
|
|
logInfo.total_response_time = Date.now() - startTime; |
|
|
|
|
logger.info('interpolation', logInfo); |
|
|
|
|
debugLog.stopTimer(req, initialTime); |
|
|
|
|
next(); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = setup; |
|
|
|
|