From 568a258406dfbfe5fc584ee8615962180865383d Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 29 Jun 2017 11:17:14 -0400 Subject: [PATCH] code cleanup, shorter function syntax --- controller/search.js | 1 + controller/search_with_ids.js | 15 +++++---------- query/address_search_using_ids.js | 12 ++++++------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/controller/search.js b/controller/search.js index 989060a8..ad41b386 100644 --- a/controller/search.js +++ b/controller/search.js @@ -25,6 +25,7 @@ function setup( apiConfig, esclient, query, should_execute ){ logger.info('[req]', 'endpoint=' + req.path, cleanOutput); const renderedQuery = query(req.clean); + // console.log(`BODY: ` + JSON.stringify(renderedQuery.body, null, 2)); // if there's no query to call ES with, skip the service if (_.isUndefined(renderedQuery)) { diff --git a/controller/search_with_ids.js b/controller/search_with_ids.js index 9f6e2f6f..fb421d6f 100644 --- a/controller/search_with_ids.js +++ b/controller/search_with_ids.js @@ -1,5 +1,3 @@ -'use strict'; - const _ = require('lodash'); const searchService = require('../service/search'); @@ -17,12 +15,12 @@ function setup( apiConfig, esclient, query, should_execute ){ return next(); } - let cleanOutput = _.cloneDeep(req.clean); + const cleanOutput = _.cloneDeep(req.clean); if (logging.isDNT(req)) { - cleanOutput = logging.removeFields(cleanOutput); + logging.removeFields(cleanOutput); } // log clean parameters for stats - logger.info('[req]', 'endpoint=' + req.path, cleanOutput); + logger.info('[req]', `endpoint=${req.path}`, cleanOutput); const renderedQuery = query(req.clean, res); // console.log(JSON.stringify(renderedQuery.body, null, 2)); @@ -72,11 +70,8 @@ function setup( apiConfig, esclient, query, should_execute ){ // error handler if( err ){ - if (_.isObject(err) && err.message) { - req.errors.push( err.message ); - } else { - req.errors.push( err ); - } + // push err.message or err onto req.errors + req.errors.push( _.get(err, 'message', err)); } else { // log that a retry was successful diff --git a/query/address_search_using_ids.js b/query/address_search_using_ids.js index 432f38bc..2f14a043 100644 --- a/query/address_search_using_ids.js +++ b/query/address_search_using_ids.js @@ -73,11 +73,11 @@ const granularity_bands = [ ]; function anyResultsAtGranularityBand(results, band) { - return results.some((result) => { return _.includes(band, result.layer); }); + return results.some(result => _.includes(band, result.layer)); } function getIdsAtLayer(results, layer) { - return results.filter((result) => { return result.layer === layer; }).map(_.property('source_id')); + return results.filter(result => result.layer === layer).map(_.property('source_id')); } /** @@ -107,9 +107,7 @@ function generateQuery( clean, res ){ } vs.var( 'input:street', clean.parsed_text.street ); - const granularity_band = granularity_bands.find((band) => { - return anyResultsAtGranularityBand(results, band); - }); + const granularity_band = granularity_bands.find(band => anyResultsAtGranularityBand(results, band)); if (granularity_band) { const layers_to_ids = granularity_band.reduce((acc, layer) => { @@ -117,6 +115,8 @@ function generateQuery( clean, res ){ return acc; }, {}); + // use an object here instead of calling `set` since that flattens out an + // object into key/value pairs and makes identifying layers harder in query module vs.var('input:layers', JSON.stringify(layers_to_ids)); } @@ -167,7 +167,7 @@ function generateQuery( clean, res ){ } // format the log parts into a single coherent string - logger.info(logParts.map((part) => { return `[${part}]`;} ).join(' ') ); + logger.info(logParts.map(part => `[${part}]`).join(' ')); return { type: 'fallback_using_ids',