Monitoramento e Estatísticas

📈 Sistema de Estatísticas Built-in

-- Obter estatísticas completas dos webhooks
local stats = LockSystem.Webhook.stats()

-- Estrutura retornada:
{
    sent = 245,                    -- Webhooks enviados com sucesso
    failed = 12,                   -- Webhooks que falharam
    retries = 38,                  -- Tentativas de retry realizadas
    rate_limited = 5,              -- Requests bloqueados por rate limit
    queue_size = 3,                -- Webhooks na fila aguardando envio
    active_rate_limits = 2,        -- Domínios com rate limit ativo
    success_rate = 95.35           -- Taxa de sucesso em %
}

🖥️ Comando Admin para Monitoramento

-- Comando administrativo para visualizar estatísticas
addCommandHandler("webhooks", function(player)
    if not hasObjectPermissionTo(player, "general.administrator") then
        outputChatBox("Acesso negado!", player, 255, 0, 0)
        return
    end
    
    local stats = LockSystem.Webhook.stats()
    
    outputChatBox("=== ESTATÍSTICAS DOS WEBHOOKS ===", player, 255, 215, 0)
    outputChatBox("📤 Enviados: " .. stats.sent, player)
    outputChatBox("❌ Falharam: " .. stats.failed, player)
    outputChatBox("🔄 Retries: " .. stats.retries, player)
    outputChatBox("⏸️ Rate Limited: " .. stats.rate_limited, player)
    outputChatBox("📋 Na Fila: " .. stats.queue_size, player)
    outputChatBox("🎯 Taxa Sucesso: " .. stats.success_rate .. "%", player)
    outputChatBox("🚫 Rate Limits Ativos: " .. stats.active_rate_limits, player)
end)

-- Comando para limpar fila de webhooks
addCommandHandler("clearwebhooks", function(player)
    if not hasObjectPermissionTo(player, "general.administrator") then
        return
    end
    
    local cleared = LockSystem.Webhook.clear()
    outputChatBox("🧹 Removidos " .. cleared .. " webhooks da fila", player, 255, 255, 0)
end)

🔔 Sistema de Alertas de Performance

-- Monitoramento automático da performance dos webhooks
setTimer(function()
    local stats = LockSystem.Webhook.stats()
    
    -- Alerta se taxa de sucesso estiver baixa
    if stats.success_rate < 80 and stats.sent + stats.failed > 10 then
        LockSystem.Webhook.sendLog("warn", "Taxa de sucesso de webhooks baixa", {
            success_rate = stats.success_rate,
            failed_count = stats.failed,
            total_sent = stats.sent
        })
    end
    
    -- Alerta se fila estiver muito cheia
    if stats.queue_size > 50 then
        LockSystem.Webhook.sendLog("warn", "Fila de webhooks muito cheia", {
            queue_size = stats.queue_size,
            rate_limits = stats.active_rate_limits
        })
    end
    
    -- Alerta se muitos rate limits ativos
    if stats.active_rate_limits > 5 then
        LockSystem.Webhook.sendLog("error", "Muitos rate limits ativos", {
            active_limits = stats.active_rate_limits,
            rate_limited_count = stats.rate_limited
        })
    end
    
    -- Log de sucesso se tudo estiver funcionando bem
    if stats.success_rate > 95 and stats.queue_size < 10 then
        LockSystem.Webhook.sendLog("success", "Webhooks operando perfeitamente", {
            success_rate = stats.success_rate,
            queue_size = stats.queue_size
        })
    end
    
end, 1800000, 0)  -- A cada 30 minutos

🧹 Sistema de Limpeza e Manutenção

-- Limpeza automática de rate limits antigos
setTimer(function()
    local stats = LockSystem.Webhook.stats()
    
    -- Log da situação atual
    if stats.queue_size > 0 or stats.active_rate_limits > 0 then
        outputDebugString("[WEBHOOK-MAINTENANCE] Fila: " .. stats.queue_size .. 
                         ", Rate Limits: " .. stats.active_rate_limits, 3)
    end
    
    -- Alertar sobre problemas persistentes
    if stats.queue_size > 20 then
        outputDebugString("[WEBHOOK-WARNING] Fila de webhooks muito cheia: " .. 
                         stats.queue_size .. " itens", 2)
    end
    
    if stats.failed > 100 then
        outputDebugString("[WEBHOOK-ERROR] Muitas falhas de webhook: " .. 
                         stats.failed .. " falhas", 1)
    end
    
end, 600000, 0)  -- A cada 10 minutos

-- Reset estatísticas de tempos em tempos para não acumular
setTimer(function()
    local stats = LockSystem.Webhook.stats()
    
    -- Se estatísticas ficaram muito grandes, fazer reset parcial
    if stats.sent > 10000 or stats.failed > 1000 then
        outputDebugString("[WEBHOOK-RESET] Fazendo reset das estatísticas acumuladas", 3)
        -- Note: O reset seria interno, aqui é só exemplo de log
    end
    
end, 86400000, 0)  -- A cada 24 horas

Atualizado