ntopng/scripts/lua/modules/alert_category_utils.lua
GabrieleDeri b21ea8a372
Some checks are pending
Build / build (push) Waiting to run
CIFuzz / Fuzzing (address) (push) Waiting to run
CIFuzz / Fuzzing (undefined) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
CodeQL / Analyze-1 (push) Waiting to run
Alert and flow query optimizations post SQL query (#11109)
2026-10-21 21:14:50 +02:00

52 lines
No EOL
1.4 KiB
Lua

--
-- (C) 2019-26 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
-- This is just a struct, try to keep this file without requires except for structs
local alert_categories = require "alert_categories"
local alert_category_utils = {}
-- ##############################################
-- @brief Given a category found in a user script, this method checks whether the category is valid
-- and, if not valid, it assigns to the script a default category
function alert_category_utils.checkCategory(category)
if not category or not category["id"] then
return alert_categories.other
end
for cat_k, cat_v in pairs(alert_categories) do
if category["id"] == cat_v["id"] then
return cat_v
end
end
return alert_categories.other
end
-- ##############################################
-- id -> category index, built on first use
local categories_by_id = nil
function alert_category_utils.getCategoryById(id)
if not categories_by_id then
categories_by_id = {}
for cat_k, cat_v in pairs(alert_categories) do
-- Keep the first match as the original linear scan did
if categories_by_id[cat_v["id"]] == nil then
categories_by_id[cat_v["id"]] = cat_v
end
end
end
return categories_by_id[id] or alert_categories.other
end
-- ##############################################
return alert_category_utils