{"size": 2383, "ext": "js", "max_stars_repo_path": "plugins/new_drivers_cohort/server/routes/new_drivers_cohort.js", "max_stars_repo_name": "ricepo/kibana", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "plugins/new_drivers_cohort/server/routes/new_drivers_cohort.js", "max_issues_repo_name": "ricepo/kibana", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "plugins/new_drivers_cohort/server/routes/new_drivers_cohort.js", "max_forks_repo_name": "ricepo/kibana", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.362745098, "max_line_length": 82, "alphanum_fraction": 0.4657994125}
import _ from 'lodash';

/**
 * routes
 * @param {*} server
 */
export default function(server) {

  /**
   * query orders
   */
  server.route({
    path:    '/api/new_drivers_cohort/query',
    method:  'POST',
    handler: async(req) => {

      const { aggs, query } = req.payload;
      const searchRequest = {
        index: 'orders',
        size:  0,
        body:  {
          _source: {
            include: []
          },
          query,
          aggs
        }
      };

      /* this is the way to get data from elasticsearch directly */
      const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
      const response = await callWithRequest(req, 'search', searchRequest);

      return response;
    }
  });

  /**
   * query drivers
   */
  server.route({
    path:    '/api/new_drivers_cohort/getDrivers',
    method:  'POST',
    handler: async(req) => {

      const { from, to, region, aggs } = req.payload;
      const searchRequest = {
        index: 'accounts',
        size:  0,
        body:  {
          _source: {
            include: []
          },
          query: {
            bool: {
              filter: [
                {
                  match_phrase: {
                    "roles.name.keyword": {
                      query: "region.driver"
                    }
                  }
                },
                { range: { "milestone.order1": { gte: from, lte: to } } }
              ],
              must_not: []
            }
          },
          aggs
        }
      };
      if (region) {
        if (!_.isArray(region.params)) {
          region.params = region.params.query.split();
        }
        const should = _.map(region.params,v => ({
          match_phrase: {
            "roles.description.keyword": v
          }
        }))
        const obj = {
          bool:{
            should,
            minimum_should_match: 1
          }
        }
        if (region.negate) {
          searchRequest.body.query.bool.must_not.push(obj);
        } else {
          searchRequest.body.query.bool.filter.push(obj);
        }
      }

      /* this is the way to get data from elasticsearch directly */
      const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
      const response = await callWithRequest(req, 'search', searchRequest);

      return response;
    }
  });
}
