<commit_before>local A, L = unpack(select(2, ...))
local M = A:NewModule("dpsRole", "AceEvent-3.0", "AceTimer-3.0")
A.dpsRole = M
M.private = {
  needToInspect = {},
  sessionCache = {melee={}, ranged={}},
  dbCleanedUp = false,
}
local R = M.private

local CLASS_DPS_ROLES = {
  ROGUE       = "melee",
  HUNTER      = "ranged", -- remove for Legion
  MONK        = "melee",
  PALADIN     = "melee",
  PRIEST      = "ranged",
  WARRIOR     = "melee",
  DEATHKNIGHT = "melee",
  DEMONHUNTER = "melee",
  MAGE        = "ranged",
  WARLOCK     = "ranged",
}
local SPECID_ROLES = {
  [102] = "ranged",  -- Balance Druid
  [103] = "melee",   -- Feral Druid
  [253] = "ranged",  -- Beast Mastery Hunter
  [254] = "ranged",  -- Marksmanship Hunter
  [255] = "ranged",  -- Survival Hunter - change to MELEE for Legion
  [262] = "ranged",  -- Elemental Shaman
  [263] = "melee",   -- Enhancement Shaman
}
local DELAY_DB_CLEANUP = 20.0
local DB_CLEANUP_MAX_AGE_DAYS = 21

local format, pairs, select, tconcat, time, tostring = format, pairs, select, table.concat, time, tostring
local GetInspectSpecialization, GetPlayerInfoByGUID, GetSpecialization, GetSpecializationInfo, InCombatLockdown, UnitExists, UnitIsUnit = GetInspectSpecialization, GetPlayerInfoByGUID, GetSpecialization, GetSpecializationInfo, InCombatLockdown, UnitExists, UnitIsUnit

local function cleanDbCache(role)
  local earliest = time() - (60*60*24*DB_CLEANUP_MAX_AGE_DAYS)
  local cache = A.db.faction.dpsRoleCache[role]
  local total, removed = 0, 0
  for fullName, when in pairs(cache) do
    total = total + 1
    if when < earliest then
      cache[fullName] = nil
      removed = removed + 1
    end
  end
  if A.debug >= 1 then A.console:Debugf(M, "cleanDbCache removed %d/%d %s older than %d days", removed, total, role, DB_CLEANUP_MAX_AGE_DAYS) end
end

function M:OnEnable()
  M:RegisterEvent("INSPECT_READY")
  M:RegisterMessage("FIXGROUPS_RAID_LEFT")
  -- TODO: handle events for when the player or another raid member changes spec?
  if not A.db.faction.dpsRoleCache then
    A.db.faction.dpsRoleCache = {melee={}, ranged={}}
  end
  if not R.dbCleanedUp and not InCombatLockdown() then
    R.dbCleanedUp = M:ScheduleTimer(function ()
      R.dbCleanedUp = true
      if InCombatLockdown() or A.sorter:IsProcessing() then
        -- Don't worry about trying to reschedule the timer. DB cleanup is
        -- very low priority. Another session will get it done eventually.
        return
      end
      cleanDbCache("melee")
      cleanDbCache("ranged")
    end, DELAY_DB_CLEANUP)
  end
end

function M:INSPECT_READY(event, guid)
  -- Look up name, fullName, and specId.
  local name, realm = select(6, GetPlayerInfoByGUID(guid))
  if name and realm then
    name = name.."-"..realm
  end

  -- Ignore any garbage responses from the server.
  local specId = GetInspectSpecialization(name)
  if not specId or not name then
    return
  end

  local fullName = R.needToInspect[name]
  if not fullName then
    -- We didn't request this inspect, but let's see if we can make use of it.
    if not SPECID_ROLES[specId] then
      return
    end
    fullName = A.util:UnitNameWithRealm(name)
    if not fullName then
      return
    end
    if A.debug >= 2 then A.console:Debugf(M, "unsolicited inspect ready for %s", name) end
  end

  -- Remove from needToInspect and add to sessionCache.<commit_msg>missed a reference to renamed method<commit_after>local A, L = unpack(select(2, ...))
local M = A:NewModule("dpsRole", "AceEvent-3.0", "AceTimer-3.0")
A.dpsRole = M
M.private = {
  needToInspect = {},
  sessionCache = {melee={}, ranged={}},
  dbCleanedUp = false,
}
local R = M.private

local CLASS_DPS_ROLES = {
  ROGUE       = "melee",
  HUNTER      = "ranged", -- remove for Legion
  MONK        = "melee",
  PALADIN     = "melee",
  PRIEST      = "ranged",
  WARRIOR     = "melee",
  DEATHKNIGHT = "melee",
  DEMONHUNTER = "melee",
  MAGE        = "ranged",
  WARLOCK     = "ranged",
}
local SPECID_ROLES = {
  [102] = "ranged",  -- Balance Druid
  [103] = "melee",   -- Feral Druid
  [253] = "ranged",  -- Beast Mastery Hunter
  [254] = "ranged",  -- Marksmanship Hunter
  [255] = "ranged",  -- Survival Hunter - change to MELEE for Legion
  [262] = "ranged",  -- Elemental Shaman
  [263] = "melee",   -- Enhancement Shaman
}
local DELAY_DB_CLEANUP = 20.0
local DB_CLEANUP_MAX_AGE_DAYS = 21

local format, pairs, select, tconcat, time, tostring = format, pairs, select, table.concat, time, tostring
local GetInspectSpecialization, GetPlayerInfoByGUID, GetSpecialization, GetSpecializationInfo, InCombatLockdown, UnitExists, UnitIsUnit = GetInspectSpecialization, GetPlayerInfoByGUID, GetSpecialization, GetSpecializationInfo, InCombatLockdown, UnitExists, UnitIsUnit

local function cleanDbCache(role)
  local earliest = time() - (60*60*24*DB_CLEANUP_MAX_AGE_DAYS)
  local cache = A.db.faction.dpsRoleCache[role]
  local total, removed = 0, 0
  for fullName, when in pairs(cache) do
    total = total + 1
    if when < earliest then
      cache[fullName] = nil
      removed = removed + 1
    end
  end
  if A.debug >= 1 then A.console:Debugf(M, "cleanDbCache removed %d/%d %s older than %d days", removed, total, role, DB_CLEANUP_MAX_AGE_DAYS) end
end

function M:OnEnable()
  M:RegisterEvent("INSPECT_READY")
  M:RegisterMessage("FIXGROUPS_RAID_LEFT")
  -- TODO: handle events for when the player or another raid member changes spec?
  if not A.db.faction.dpsRoleCache then
    A.db.faction.dpsRoleCache = {melee={}, ranged={}}
  end
  if not R.dbCleanedUp and not InCombatLockdown() then
    R.dbCleanedUp = M:ScheduleTimer(function ()
      R.dbCleanedUp = true
      if InCombatLockdown() or A.sorter:IsProcessing() then
        -- Don't worry about trying to reschedule the timer. DB cleanup is
        -- very low priority. Another session will get it done eventually.
        return
      end
      cleanDbCache("melee")
      cleanDbCache("ranged")
    end, DELAY_DB_CLEANUP)
  end
end

function M:INSPECT_READY(event, guid)
  -- Look up name, fullName, and specId.
  local name, realm = select(6, GetPlayerInfoByGUID(guid))
  if name and realm then
    name = name.."-"..realm
  end

  -- Ignore any garbage responses from the server.
  local specId = GetInspectSpecialization(name)
  if not specId or not name then
    return
  end

  local fullName = R.needToInspect[name]
  if not fullName then
    -- We didn't request this inspect, but let's see if we can make use of it.
    if not SPECID_ROLES[specId] then
      return
    end
    fullName = A.util:NameAndRealm(name)
    if not fullName then
      return
    end
    if A.debug >= 2 then A.console:Debugf(M, "unsolicited inspect ready for %s", name) end
  end

  -- Remove from needToInspect and add to sessionCache.
{'fraction_non_alphanumeric': 0.1085477669328601, 'fraction_numerical': 0.013013901212658977, 'mean_word_length': 3.346401028277635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6844686', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2328, 'n_words': 678}