<commit_before>const ErrorHTTP = require('mapbox-error').ErrorHTTP;
const db = require('../database/index');
const Quadkey = db.Quadkey;
const validator = require('validator');
const Sequelize = require('sequelize');
const validateBody = require('../lib/helper/validateBody');

module.exports = {
  getQuadkey,
  postQuadkey
};

function isValidQuadkey(quadkey) {
  return /^[0-3]+$/.test(quadkey);
}

/**
 * Gets priority value for a quadkey+project. If quadkey does not exist, will return a priority of -1
 * 
 * @name get-quadkey-priority
 * @param {Object} params - Request URL parameters
 * @param {string} params.quadkey - Quadkey to request priority for
 * @param {Object} [query] - Request URL query parameters
 * @param {string} [query.set_id] - Quadkey Set ID to get priority values for
 * @example
 * curl https://host/v1/quadkeys/11002211
 * { "priority": 0.777 }
 */
function getQuadkey(req, res, next) {
  const quadkey = req.params.quadkey;
  if (!isValidQuadkey(quadkey)) {
    return next(new ErrorHTTP('Please supply a valid quadkey identifier', 400));
  }
  const setId = req.query.set_id || null;
  Quadkey.findOne({
    where: {
      quadkey: quadkey,
      set_id: setId
    }
  })
    .then(quadkey => {
      res.json({ priority: quadkey.priority });
    })
    .catch(() => {
      res.json({ priority: -1 });
    });
}

/**
 * Returns a list of quadkeys with priority and item count data.
 * The use-case is, for eg:
 *   - Give me data for all quadkeys at z8 within this z4 tile, with counts
 *      for items that have a status=open 
 *
 * @name get-quadkeys
 * @param {Object} query - query parameters
 * @param {string} query.project_id - the project_id this is scoped to (should this be a URL param instead?)
 * @param {string} query.within - Quadkey to search within
 * @param {integer} query.zoom_level - The zoom level you want results in (can be max 4 greater than zoom level of `within` quadkey param)
 * @param {string} query.item_status - item status to filter by for item counts
 * @param {Array<string>} query.item_tags - item tags to filter by for item counts
 * @example curl https://host/v1/quadkeys?within=0011&zoom_level=7&item_status=open
 *  [
 *    {
 *      quadkey: '0011000',
 *      item_count: 243,
 *      priority: 0.004
 *    },
 *    {
 *      quadkey: '00111001',
 *      item_count: 12,
 *      priority: 0.002
 *    },<commit_msg>tweak documentation, change priority to max_priority, add filter for lock<commit_after>const ErrorHTTP = require('mapbox-error').ErrorHTTP;
const db = require('../database/index');
const Quadkey = db.Quadkey;
const validator = require('validator');
const Sequelize = require('sequelize');
const validateBody = require('../lib/helper/validateBody');

module.exports = {
  getQuadkey,
  postQuadkey
};

function isValidQuadkey(quadkey) {
  return /^[0-3]+$/.test(quadkey);
}

/**
 * Gets priority value for a quadkey+project. If quadkey does not exist, will return a priority of -1
 * 
 * @name get-quadkey-priority
 * @param {Object} params - Request URL parameters
 * @param {string} params.quadkey - Quadkey to request priority for
 * @param {Object} [query] - Request URL query parameters
 * @param {string} [query.set_id] - Quadkey Set ID to get priority values for
 * @example
 * curl https://host/v1/quadkeys/11002211
 * { "priority": 0.777 }
 */
function getQuadkey(req, res, next) {
  const quadkey = req.params.quadkey;
  if (!isValidQuadkey(quadkey)) {
    return next(new ErrorHTTP('Please supply a valid quadkey identifier', 400));
  }
  const setId = req.query.set_id || null;
  Quadkey.findOne({
    where: {
      quadkey: quadkey,
      set_id: setId
    }
  })
    .then(quadkey => {
      res.json({ priority: quadkey.priority });
    })
    .catch(() => {
      res.json({ priority: -1 });
    });
}

/**
 * Returns a list of quadkeys with priority and item count data.
 * The use-case is, for eg:
 *   - Give me data for all quadkeys at z8 within this z4 tile, with counts
 *      for items that have a status=open 
 *
 * @name get-quadkeys
 * @param {Object} query - query parameters
 * @param {string} query.project_id - the project_id this is scoped to (should this be a URL param instead?)
 * @param {string} query.within - Quadkey to search within
 * @param {integer} query.zoom_level - The zoom level you want results in (can be max 4 greater than zoom level of `within` quadkey param)
 * @param {string} [query.item_status] - item status to filter by for item counts
 * @param {string} [query.item_tags] - item tags (comma separated) to filter by for item counts
 * @param {('locked'|'unlocked')} [query.item_lock] - The item's lock status, must be 'locked' or 'unlocked'
 * @returns {Array<Object>} array of quadkey objects with the following keys:
 *   - `quadkey`: quadkey value at zoom_level requested
 *   - `item_count`: number of items within quadkey (after applying filters)
 *   - `max_priority`: max priority of `constants.DEFAULT_ZOOM` tile within aggregation
 *
 * @example curl https://host/v1/quadkeys?within=0011&zoom_level=7&item_status=open
 *  [
 *    {
 *      quadkey: '0011000',
 *      item_count: 243,
 *      max_priority: 0.004
 *    },
 *    {
 *      quadkey: '00111001',
 *      item_count: 12,
 *      max_priority: 0.002
 *    },
{'fraction_non_alphanumeric': 0.12793783169067474, 'fraction_numerical': 0.021607278241091737, 'mean_word_length': 3.4233025984911984, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '3901818', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1654, 'n_words': 540}