mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-06 03:45:26 +00:00
Implements unique keys and getters for datasource types
This commit is contained in:
parent
7ab4d846e8
commit
df7ac9429c
4 changed files with 69 additions and 4 deletions
19
scripts/lua/modules/datasources/datasource_keys.lua
Normal file
19
scripts/lua/modules/datasources/datasource_keys.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
--
|
||||
-- (C) 2021 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Every datasource must be keyed using one of the following keys
|
||||
-- NOTE: integer IDs must be unique
|
||||
|
||||
local datasource_keys = {
|
||||
-- Keys for interface datasources coded in datasources/interface, datasources/host, etc
|
||||
interface_packet_distro = 0,
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
return datasource_keys
|
||||
|
||||
-- ##############################################
|
||||
|
|
@ -12,6 +12,8 @@ package.path = dirs.installdir .. "/scripts/lua/modules/datasources/?.lua;" .. p
|
|||
local classes = require "classes"
|
||||
-- Import the base class
|
||||
local datasource = require "datasource"
|
||||
-- Import the datasource keys
|
||||
local datasource_keys = require "datasource_keys"
|
||||
-- This is the datamodel used to represent data associated with this datasource
|
||||
local datamodel = require "datamodel"
|
||||
-- Rest utilities
|
||||
|
|
@ -24,6 +26,7 @@ local packet_distro = classes.class(datasource)
|
|||
-- ##############################################
|
||||
|
||||
packet_distro.meta = {
|
||||
datasource_key = datasource_keys.interface_packet_distro, -- Uniquely identifies this datasource
|
||||
i18n_title = "Interface Packet Distribution",
|
||||
icon = "fas fa-exclamation",
|
||||
rest_endpoint = "/lua/rest/v1/get/datasource/interface/packet_distro.lua",
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ local dirs = ntop.getDirs()
|
|||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/datasources/?.lua;" .. package.path
|
||||
|
||||
local datasource_keys = require "datasource_keys"
|
||||
local os_utils = require "os_utils"
|
||||
require ("lua_utils")
|
||||
local json = require("dkjson")
|
||||
|
|
@ -219,10 +220,21 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Returns all the available datasource types, i.e., all possible subclasses of datasource.lua in modules/datasources
|
||||
function datasources_utils.get_all_source_types()
|
||||
-- A cache of all the available datasources, keyed by datasource_keys as found in datasource_keys.lua
|
||||
local source_key_source_type_cache
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local function cache_source_types(recache)
|
||||
if source_key_source_type_cache and not recache then
|
||||
-- Already cached
|
||||
return
|
||||
end
|
||||
|
||||
-- Cache available datasource types
|
||||
source_key_source_type_cache = {}
|
||||
|
||||
local datasources_dir = os_utils.fixPath(dirs.installdir .. "/scripts/lua/modules/datasources/")
|
||||
local res = {}
|
||||
|
||||
-- The base datasources directory
|
||||
for datasource_dir in pairs(ntop.readdir(datasources_dir)) do
|
||||
|
|
@ -237,16 +249,43 @@ function datasources_utils.get_all_source_types()
|
|||
local datasource = require(string.format("%s.%s", datasource_dir, datasource_file:gsub("%.lua$", "")))
|
||||
|
||||
if datasource then
|
||||
res[#res + 1] = datasource
|
||||
source_key_source_type_cache[datasource.meta.datasource_key] = datasource
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Returns all the available datasource types, i.e., all possible subclasses of datasource.lua in modules/datasources
|
||||
function datasources_utils.get_all_source_types()
|
||||
local res = {}
|
||||
|
||||
-- Build the cache, if not already built
|
||||
cache_source_types()
|
||||
|
||||
for datasource_key, datasource in pairs(source_key_source_type_cache) do
|
||||
res[#res + 1] = datasource
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Returns a datasource, given its `datasource_key` identifier
|
||||
-- @param datasource_key One of the keys defined in datasource_keys.lua
|
||||
-- @return The datasource that can be then instantiated with :new()
|
||||
function datasources_utils.get_source_type_by_key(datasource_key)
|
||||
-- Build the cache (if not already built)
|
||||
cache_source_types()
|
||||
|
||||
-- Return the cached datasource
|
||||
return source_key_source_type_cache[datasource_key]
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
return datasources_utils
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue