mirror of
https://github.com/gmod-integration/lua.git
synced 2025-03-15 22:07:34 +00:00
refactor: add flushLogs / remove request avg for log
This commit is contained in:
parent
9144bdf977
commit
b8d6c2e87d
|
@ -12,7 +12,6 @@ local function showableBody(endpoint)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local requestIndicator = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
||||||
function gmInte.http.requestAPI(params)
|
function gmInte.http.requestAPI(params)
|
||||||
local body = params.body && util.TableToJSON(params.body || {}) || ""
|
local body = params.body && util.TableToJSON(params.body || {}) || ""
|
||||||
local bodyLength = string.len(body)
|
local bodyLength = string.len(body)
|
||||||
|
@ -22,10 +21,7 @@ function gmInte.http.requestAPI(params)
|
||||||
local success = params.success || function() end
|
local success = params.success || function() end
|
||||||
local failed = params.failed || function() end
|
local failed = params.failed || function() end
|
||||||
local version = gmInte.version || "Unknown"
|
local version = gmInte.version || "Unknown"
|
||||||
local actualSec = math.Round(SysTime()) % 10
|
|
||||||
local localRequestID = util.CRC(tostring(SysTime()))
|
local localRequestID = util.CRC(tostring(SysTime()))
|
||||||
requestIndicator[actualSec] = requestIndicator[actualSec] && requestIndicator[actualSec] + 1 || 1
|
|
||||||
timer.Simple(10, function() requestIndicator[actualSec] = requestIndicator[actualSec] - 1 end)
|
|
||||||
if token == "" then
|
if token == "" then
|
||||||
return failed(401, {
|
return failed(401, {
|
||||||
["error"] = "No token provided"
|
["error"] = "No token provided"
|
||||||
|
@ -95,44 +91,28 @@ function gmInte.http.post(endpoint, data, onSuccess, onFailed)
|
||||||
end
|
end
|
||||||
|
|
||||||
local nextLogPacket = {}
|
local nextLogPacket = {}
|
||||||
local requestAverage = 0
|
local function flushLogs()
|
||||||
timer.Create("gmInte:http:requestAverage", 1, 0, function()
|
if #nextLogPacket > 0 then
|
||||||
requestAverage = 0
|
|
||||||
for k, v in ipairs(requestIndicator) do
|
|
||||||
requestAverage = requestAverage + v
|
|
||||||
end
|
|
||||||
|
|
||||||
requestAverage = requestAverage / 10
|
|
||||||
end)
|
|
||||||
|
|
||||||
function gmInte.http.postLog(endpoint, data)
|
|
||||||
if requestAverage >= 2 then
|
|
||||||
local logPacketIndex = #nextLogPacket + 1
|
|
||||||
table.insert(nextLogPacket, {
|
|
||||||
["endpoint"] = endpoint,
|
|
||||||
["data"] = data
|
|
||||||
})
|
|
||||||
|
|
||||||
timer.Simple(3, function()
|
|
||||||
if #nextLogPacket == logPacketIndex || #nextLogPacket >= 30 then
|
|
||||||
gmInte.http.requestAPI({
|
|
||||||
["endpoint"] = "/servers/:serverID/logs",
|
|
||||||
["method"] = "POST",
|
|
||||||
["body"] = nextLogPacket
|
|
||||||
})
|
|
||||||
|
|
||||||
nextLogPacket = {}
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
else
|
|
||||||
gmInte.http.requestAPI({
|
gmInte.http.requestAPI({
|
||||||
["endpoint"] = endpoint,
|
["endpoint"] = "/servers/:serverID/logs",
|
||||||
["method"] = "POST",
|
["method"] = "POST",
|
||||||
["body"] = data
|
["body"] = nextLogPacket
|
||||||
})
|
})
|
||||||
|
|
||||||
|
nextLogPacket = {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
timer.Create("gmInte:http:flushLogs", 3, 0, flushLogs)
|
||||||
|
function gmInte.http.postLog(endpoint, data)
|
||||||
|
table.insert(nextLogPacket, {
|
||||||
|
["endpoint"] = endpoint,
|
||||||
|
["data"] = data
|
||||||
|
})
|
||||||
|
|
||||||
|
if #nextLogPacket >= 30 then flushLogs() end
|
||||||
|
end
|
||||||
|
|
||||||
function gmInte.http.put(endpoint, data, onSuccess, onFailed)
|
function gmInte.http.put(endpoint, data, onSuccess, onFailed)
|
||||||
gmInte.http.requestAPI({
|
gmInte.http.requestAPI({
|
||||||
["endpoint"] = endpoint,
|
["endpoint"] = endpoint,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user