mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-01 00:19:33 +00:00
119 lines
3.4 KiB
Lua
119 lines
3.4 KiB
Lua
--
|
|
-- (C) 2013-21 - ntop.org
|
|
--
|
|
|
|
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 json = require "dkjson"
|
|
local rest_utils = require "rest_utils"
|
|
-- Import the classes library.
|
|
local classes = require "classes"
|
|
|
|
-- ##############################################
|
|
|
|
local datasource = classes.class()
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Base class constructor
|
|
function datasource:init()
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Parses params
|
|
-- @param params_table A table with submitted params
|
|
-- @return True if parameters parsing is successful, false otherwise
|
|
function datasource:read_params(params_table)
|
|
if not params_table then
|
|
self.parsed_params = nil
|
|
return false
|
|
end
|
|
|
|
self.parsed_params = {}
|
|
for _, param in pairs(self.meta.params or {}) do
|
|
local parsed_param = params_table[param]
|
|
|
|
-- Assumes all params mandatory and not empty
|
|
-- May override this behavior in subclasses
|
|
if isEmptyString(parsed_param) then
|
|
-- Reset any possibly set param
|
|
self.parsed_params = nil
|
|
|
|
return false
|
|
end
|
|
|
|
self.parsed_params[param] = parsed_param
|
|
end
|
|
|
|
-- Ok, parsin has been successful
|
|
return true
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Parses params submitted along with the REST endpoint request. If parsing fails, a REST error is sent.
|
|
-- @param params_table A table with submitted params, either _POST or _GET
|
|
-- @return True if parameters parsing is successful, false otherwise
|
|
function datasource:_rest_read_params(params_table)
|
|
if not self:read_params(params_table) then
|
|
rest_utils.answer(rest_utils.consts.err.widgets_missing_datasource_params)
|
|
return false
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Send datasource data via REST
|
|
function datasource:rest_send_response()
|
|
if not self:_rest_read_params(_POST) then
|
|
-- Params parsing has failed, error response already sent by the caller
|
|
return
|
|
end
|
|
|
|
self:fetch()
|
|
|
|
rest_utils.answer(
|
|
rest_utils.consts.success.ok,
|
|
self.datamodel_instance:get_data()
|
|
)
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Deserializes REST endpoint response into an internal datamodel
|
|
-- @param rest_response_data Response data as obtained from the REST call
|
|
function datasource:deserialize(rest_response_data)
|
|
if rest_response_data and rest_response_data.RESPONSE_CODE == 200 then
|
|
local data = json.decode(rest_response_data.CONTENT)
|
|
local when = os.time()
|
|
|
|
if data and data.rc == rest_utils.consts.success.ok.rc then
|
|
self.datamodel_instance = self.meta.datamodel:create(data.rsp.header)
|
|
|
|
for _, row in ipairs(data.rsp.rows) do
|
|
self.datamodel_instance:appendRow(when, data.rsp.header, row)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
-- @brief Transform data according to the specified transformation
|
|
-- @param data The data to be transformed
|
|
-- @param transformation The transformation to be applied
|
|
-- @return transformed data
|
|
function datasource:transform(transformation)
|
|
return self.datamodel_instance:getData(transformation)
|
|
end
|
|
|
|
-- ##############################################
|
|
|
|
return datasource
|
|
|
|
-- ##############################################
|