<commit_before>/* eslint-disable max-lines */

var async = require('async');
var neighbors = require('./lib/neighbors');
var maxDistance = require('./lib/maxDistance');
var DensityList = require('./lib/DensityList');

var BOTTOM_LAYER = 15;
var NEIGHBORHOOD = 7;  // k

exports.recomputeNeighborsAvgDist = function (db, callback) {
  // Recompute average distance value for each location.


  var coll = db.get('locations');

  neighbors.updateEachAvgDist(coll, NEIGHBORHOOD, function (err) {
    if (err) {
      return callback(err);
    }  // else

    return callback(null);
  });
};

exports.computeNeighborsAvgDistForPoint = function (db, geom, callback) {
  // Compute neighborsAvgDist for given GeoJSON point.
  //
  // Parameters:
  //   db
  //   geom
  //     GeoJSON Point
  //   callback
  //     function (err, avgDist)

  var coll = db.get('locations');

  // Dummy location
  var loc = {
    geom: geom,
  };

  neighbors.computeAvgDist(coll, loc, NEIGHBORHOOD, function (err, newLoc) {
    if (err) {
      return callback(err);
    }

    return callback(null, newLoc.neighborsAvgDist);
  });
};

exports.findLayerForPoint = function (db, geom, callback) {
  // Find highest layer for a point.
  //
  // Parameters:
  //   db
  //     Monk DB instance
  //   geom
  //     GeoJSON Point
  //   callback
  //     function (err, layer)
  //       Parameters:
  //         err
  //           null if no error
  //         layer
  //           integer
  //

  neighbors.findNearestOne(db.get('locations'), geom, function (err, result) {
    if (err) {
      return callback(err);
    }

    var layer, r;
    var nearDist = result.dist;

    // Begin from the highest layer. For each layer, compute the required
    // cluster radius. Stop when the radius is decreased below the distance
    // to the nearest.
    for (layer = 1; layer < BOTTOM_LAYER; layer += 1) {
      r = maxDistance.getFromZoomLevel(layer);
      if (r < nearDist) {
        return callback(null, layer);
      }
    }

    return callback(null, BOTTOM_LAYER);
  });
};

exports.getBottomLayerNumber = function () {
  return BOTTOM_LAYER;
};

exports.findWithin = function (options) {
  // Used by the locations handler.
  // Most calls to this function are coming from real users.
  //
  // Options:
  //   db
  //     monk db instance
  //   center
  //     [lng lat]
  //   radius
  //     integer, meters
  //   query
  //     a limiting query
  //   callback
  //     function (err, locations)

  var db = options.db;
  var center = options.center;
  var radius = options.radius;
  var query = options.query;
  var callback = options.callback;

  var coll = db.get('locations');

  coll.aggregate([
    {
      // See docs:
      // https://docs.mongodb.com/v3.2/reference/operator/aggregation/geoNear/
      $geoNear: {
        near: {
          type: 'Point',
          coordinates: center,
        },
        // If near is specified in GeoJSON, maxDistance takes meters.
        // If near is specified in legacy coordinates, it takes radians.
        maxDistance: radius,
        limit: 1000,
        query: query,
        distanceField: 'dist',
        spherical: true,
      },
    },
    // Return only what is needed for displaying markers and infowindows.
    {
      $project: {
        name: true,
        geom: true,
        tags: true,
      },
    },
  ]).then(function (results) {
    return callback(null, results);
  }).catch(callback);
};

exports.findDensest = function (coll, callback) {
  // Find all locations, order by density, densest first.
  //
  // Parameters
  //   coll
  //     locations collection
  //   callback
  //     function (err, locations)
<commit_msg>server: include layer to marker data<commit_after>/* eslint-disable max-lines */

var async = require('async');
var neighbors = require('./lib/neighbors');
var maxDistance = require('./lib/maxDistance');
var DensityList = require('./lib/DensityList');

var BOTTOM_LAYER = 15;
var NEIGHBORHOOD = 7;  // k

exports.recomputeNeighborsAvgDist = function (db, callback) {
  // Recompute average distance value for each location.


  var coll = db.get('locations');

  neighbors.updateEachAvgDist(coll, NEIGHBORHOOD, function (err) {
    if (err) {
      return callback(err);
    }  // else

    return callback(null);
  });
};

exports.computeNeighborsAvgDistForPoint = function (db, geom, callback) {
  // Compute neighborsAvgDist for given GeoJSON point.
  //
  // Parameters:
  //   db
  //   geom
  //     GeoJSON Point
  //   callback
  //     function (err, avgDist)

  var coll = db.get('locations');

  // Dummy location
  var loc = {
    geom: geom,
  };

  neighbors.computeAvgDist(coll, loc, NEIGHBORHOOD, function (err, newLoc) {
    if (err) {
      return callback(err);
    }

    return callback(null, newLoc.neighborsAvgDist);
  });
};

exports.findLayerForPoint = function (db, geom, callback) {
  // Find highest layer for a point.
  //
  // Parameters:
  //   db
  //     Monk DB instance
  //   geom
  //     GeoJSON Point
  //   callback
  //     function (err, layer)
  //       Parameters:
  //         err
  //           null if no error
  //         layer
  //           integer
  //

  neighbors.findNearestOne(db.get('locations'), geom, function (err, result) {
    if (err) {
      return callback(err);
    }

    var layer, r;
    var nearDist = result.dist;

    // Begin from the highest layer. For each layer, compute the required
    // cluster radius. Stop when the radius is decreased below the distance
    // to the nearest.
    for (layer = 1; layer < BOTTOM_LAYER; layer += 1) {
      r = maxDistance.getFromZoomLevel(layer);
      if (r < nearDist) {
        return callback(null, layer);
      }
    }

    return callback(null, BOTTOM_LAYER);
  });
};

exports.getBottomLayerNumber = function () {
  return BOTTOM_LAYER;
};

exports.findWithin = function (options) {
  // Used by the locations handler.
  // Most calls to this function are coming from real users.
  //
  // Options:
  //   db
  //     monk db instance
  //   center
  //     [lng lat]
  //   radius
  //     integer, meters
  //   query
  //     a limiting query
  //   callback
  //     function (err, locations)

  var db = options.db;
  var center = options.center;
  var radius = options.radius;
  var query = options.query;
  var callback = options.callback;

  var coll = db.get('locations');

  coll.aggregate([
    {
      // See docs:
      // https://docs.mongodb.com/v3.2/reference/operator/aggregation/geoNear/
      $geoNear: {
        near: {
          type: 'Point',
          coordinates: center,
        },
        // If near is specified in GeoJSON, maxDistance takes meters.
        // If near is specified in legacy coordinates, it takes radians.
        maxDistance: radius,
        limit: 1000,
        query: query,
        distanceField: 'dist',
        spherical: true,
      },
    },
    // Return only what is needed for displaying markers and infowindows.
    {
      $project: {
        name: true,
        geom: true,
        tags: true,
        layer: true,
      },
    },
  ]).then(function (results) {
    return callback(null, results);
  }).catch(callback);
};

exports.findDensest = function (coll, callback) {
  // Find all locations, order by density, densest first.
  //
  // Parameters
  //   coll
  //     locations collection
  //   callback
  //     function (err, locations)

{'fraction_non_alphanumeric': 0.12452779276848354, 'fraction_numerical': 0.0029681597409606042, 'mean_word_length': 2.1278481012658226, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652548', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2288, 'n_words': 645}