Skip to content

Commit

Permalink
fix live no request (#81)
Browse files Browse the repository at this point in the history
* in some case allow sending 0 value processed request
  • Loading branch information
sabban authored Oct 22, 2024
1 parent 87a5edf commit 2be0fb3
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions lib/plugins/crowdsec/metrics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,17 @@ end

-- Export the store data to JSON
function metrics:toJson(window)
local data_exists = false
local metrics_array = {}
local metrics_data = cjson.decode(self.cache:get("metrics_data"))
local keys = self:get_all_keys()
for _,key in pairs(keys) do
local metrics_data = self.cache:get("metrics_data")
local keys = metrics:get_all_keys()
for _,key in ipairs(keys) do
local cache_key = "metrics_" .. key
local value = self.cache:get(cache_key)
ngx.log(ngx.INFO, "cache_key: " .. cache_key .. " value: " .. tostring(self.cache:get(cache_key)))--debug
if value ~= nil then
local final_key, label = get_information_on_key(key)
ngx.log(ngx.INFO, "final_key: " .. final_key)
ngx.log(ngx.INFO, "value: " .. value)
if label ~= nil then
ngx.log(ngx.INFO, "label: " .. label)
end
Expand Down Expand Up @@ -235,40 +236,41 @@ function metrics:toJson(window)
})

end
if final_key ~= "active_decisions" then

if final_key ~= "active_decisions" and final_key ~= "processed" then
local success, err = self.cache:delete(cache_key)
if success then
ngx.log(ngx.INFO, "Cache key '", cache_key, "' deleted successfully")
else
ngx.log(ngx.INFO, "Failed to delete cache key '", cache_key, "': ", err)
end
else
if final_key == "processed" then
self.cache:set(cache_key, 0)
end
end
data_exists = true
end
end

--setmetatable(metrics_data, cjson.array_mt)
local remediation_components = {}
local remediation_component = metrics_data
if data_exists then
remediation_component["feature_flags"] = setmetatable({}, cjson.array_mt)
remediation_component["metrics"]= {
{
items = metrics_array,
meta = {
utc_now_timestamp = ngx.time(),
window_size_seconds = window
}
}
}
-- for k, v in pairs(metrics_data) do
-- remediation_components[k] = v
-- end
table.insert(remediation_components, remediation_component)
else
remediation_components = cjson.null
end
return cjson.encode({log_processors = cjson.null, remediation_components=remediation_components})
--

local remediation_components = {}
local remediation_component = cjson.decode(metrics_data)
remediation_component["feature_flags"] = setmetatable({}, cjson.array_mt)
remediation_component["metrics"]= {
{
items = metrics_array,
meta = {
utc_now_timestamp = ngx.time(),
window_size_seconds = window
}
}
}
table.insert(remediation_components, remediation_component)
return cjson.encode({log_processors = cjson.null, remediation_components = remediation_components})
end

function metrics:sendMetrics(link, headers, ssl, window)
Expand Down

0 comments on commit 2be0fb3

Please sign in to comment.