Browse Source

code cleanup, shorter function syntax

pull/912/head
Stephen Hess 8 years ago
parent
commit
568a258406
  1. 1
      controller/search.js
  2. 15
      controller/search_with_ids.js
  3. 12
      query/address_search_using_ids.js

1
controller/search.js

@ -25,6 +25,7 @@ function setup( apiConfig, esclient, query, should_execute ){
logger.info('[req]', 'endpoint=' + req.path, cleanOutput); logger.info('[req]', 'endpoint=' + req.path, cleanOutput);
const renderedQuery = query(req.clean); 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 there's no query to call ES with, skip the service
if (_.isUndefined(renderedQuery)) { if (_.isUndefined(renderedQuery)) {

15
controller/search_with_ids.js

@ -1,5 +1,3 @@
'use strict';
const _ = require('lodash'); const _ = require('lodash');
const searchService = require('../service/search'); const searchService = require('../service/search');
@ -17,12 +15,12 @@ function setup( apiConfig, esclient, query, should_execute ){
return next(); return next();
} }
let cleanOutput = _.cloneDeep(req.clean); const cleanOutput = _.cloneDeep(req.clean);
if (logging.isDNT(req)) { if (logging.isDNT(req)) {
cleanOutput = logging.removeFields(cleanOutput); logging.removeFields(cleanOutput);
} }
// log clean parameters for stats // 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); const renderedQuery = query(req.clean, res);
// console.log(JSON.stringify(renderedQuery.body, null, 2)); // console.log(JSON.stringify(renderedQuery.body, null, 2));
@ -72,11 +70,8 @@ function setup( apiConfig, esclient, query, should_execute ){
// error handler // error handler
if( err ){ if( err ){
if (_.isObject(err) && err.message) { // push err.message or err onto req.errors
req.errors.push( err.message ); req.errors.push( _.get(err, 'message', err));
} else {
req.errors.push( err );
}
} }
else { else {
// log that a retry was successful // log that a retry was successful

12
query/address_search_using_ids.js

@ -73,11 +73,11 @@ const granularity_bands = [
]; ];
function anyResultsAtGranularityBand(results, band) { 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) { 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 ); vs.var( 'input:street', clean.parsed_text.street );
const granularity_band = granularity_bands.find((band) => { const granularity_band = granularity_bands.find(band => anyResultsAtGranularityBand(results, band));
return anyResultsAtGranularityBand(results, band);
});
if (granularity_band) { if (granularity_band) {
const layers_to_ids = granularity_band.reduce((acc, layer) => { const layers_to_ids = granularity_band.reduce((acc, layer) => {
@ -117,6 +115,8 @@ function generateQuery( clean, res ){
return acc; 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)); 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 // 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 { return {
type: 'fallback_using_ids', type: 'fallback_using_ids',

Loading…
Cancel
Save