<commit_before>local ipairs = ipairs
local format = string.format
local insert = table.insert
local concat = table.concat
local sort = table.sort
local unpack = table.unpack
local ngx_re = ngx.re
local table = table

local _M = {}

local function creds_part_in_key(creds)
  if creds.app_id and creds.app_key then
    return format("app_id:%s,app_key:%s", creds.app_id, creds.app_key)
  elseif creds.user_key then
    return format("user_key:%s", creds.user_key)
  elseif creds.access_token then
    return format("access_token:%s", creds.access_token)
  end
end

local function metrics_part_in_key(usage)
  local usages = table.new(#usage.metrics, 0)

  local deltas = usage.deltas

  -- Need to sort the metrics. Otherwise, same metrics but in different order,
  -- would end up in a different key.
  local metrics = { unpack(usage.metrics) } -- Does not modify the original.
  sort(metrics)

  for _, metric in ipairs(metrics) do
    insert(usages, format("%s=%s", metric, deltas[metric]))
  end

  return format("metrics:%s", concat(usages, ';'))
end

local regexes_report_key = {
  "service_id:(?<service_id>[\\w-]+),user_key:(?<user_key>[\\w-]+),metric:(?<metric>[\\w-]+)",
  "service_id:(?<service_id>[\\w-]+),access_token:(?<access_token>[\\w-]+),metric:(?<metric>[\\w-]+)",
  "service_id:(?<service_id>[\\w-]+),app_id:(?<app_id>[\\w-]+),app_key:(?<app_key>[\\w-]+),metric:(?<metric>[\\w-]+)"
}
<commit_msg>policy/3scale_batcher/keys_helper: require table.new<commit_after>local ipairs = ipairs
local format = string.format
local insert = table.insert
local concat = table.concat
local sort = table.sort
local unpack = table.unpack
local ngx_re = ngx.re
local new_tab = require('resty.core.base').new_tab

local _M = {}

local function creds_part_in_key(creds)
  if creds.app_id and creds.app_key then
    return format("app_id:%s,app_key:%s", creds.app_id, creds.app_key)
  elseif creds.user_key then
    return format("user_key:%s", creds.user_key)
  elseif creds.access_token then
    return format("access_token:%s", creds.access_token)
  end
end

local function metrics_part_in_key(usage)
  local usages = new_tab(#usage.metrics, 0)

  local deltas = usage.deltas

  -- Need to sort the metrics. Otherwise, same metrics but in different order,
  -- would end up in a different key.
  local metrics = { unpack(usage.metrics) } -- Does not modify the original.
  sort(metrics)

  for _, metric in ipairs(metrics) do
    insert(usages, format("%s=%s", metric, deltas[metric]))
  end

  return format("metrics:%s", concat(usages, ';'))
end

local regexes_report_key = {
  "service_id:(?<service_id>[\\w-]+),user_key:(?<user_key>[\\w-]+),metric:(?<metric>[\\w-]+)",
  "service_id:(?<service_id>[\\w-]+),access_token:(?<access_token>[\\w-]+),metric:(?<metric>[\\w-]+)",
  "service_id:(?<service_id>[\\w-]+),app_id:(?<app_id>[\\w-]+),app_key:(?<app_key>[\\w-]+),metric:(?<metric>[\\w-]+)"
}

{'fraction_non_alphanumeric': 0.1781767955801105, 'fraction_numerical': 0.0010359116022099447, 'mean_word_length': 4.817269076305221, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '3224331', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1062, 'n_words': 220}