1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
--
-- (C) 2020-22 - ntop.org
--
local widgets_utils = {}
require ("lua_utils")
local json = require("dkjson")
local rest_utils = require "rest_utils"
local datasources_utils = require("datasources_utils")
-------------------------------------------------------------------------------
-- Answer to a widget request
-- @param widget Is a widget defined above
-- @param params Is a table which contains overriding params.
-- Example: {ifid, key, metric, begin_time, end_time, schema }
-------------------------------------------------------------------------------
function widgets_utils.generate_response(widget, params)
local ds = datasources_utils.get(widget.ds_hash)
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/datasources/?.lua;" .. package.path
-- Remove trailer .lua from the origin
local origin = ds.origin:gsub("%.lua", "")
-- io.write("Executing "..origin..".lua\n")
--tprint(widget)
-- Call the origin to return
local response = require(origin)
if((response == nil) or (response == true)) then
response = "{}"
else
response = response:getData(widget.type)
end
return json.encode({
widgetName = widget.name,
widgetType = widget.type,
dsRetention = ds.data_retention * 1000, -- msec
success = true,
data = response
})
end
-- @brief Generate a rest response for the widget, by requesting data from multiple datasources and filtering it
function widgets_utils.rest_response()
if not _POST or table.len(_POST) == 0 then
rest_utils.answer(rest_utils.consts.err.invalid_args)
return
end
-- Missing transformation
if not _POST["transformation"] then
rest_utils.answer(rest_utils.consts.err.widgets_missing_transformation)
return
end
-- Check for datasources
if not _POST["datasources"] or table.len(_POST["datasources"]) == 0 then
rest_utils.answer(rest_utils.consts.err.widgets_missing_datasources)
return
end
local datasources_data = {}
for _, datasource in pairs(_POST["datasources"]) do
-- Check if the datasource is valid and existing
if not datasource.ds_type then
rest_utils.answer(rest_utils.consts.err.widgets_missing_datasource_type)
return
end
-- Fetch the datasource class using the key
local datasource_type = datasources_utils.get_source_type_by_key(datasource.ds_type)
local datasource_instance = datasource_type.new()
-- Parse params into the instance
if not datasource_instance:read_params(datasource.params) then
rest_utils.answer(rest_utils.consts.err.widgets_missing_datasource_params)
return
end
-- Fetch according to datasource parameters received via REST
datasource_instance:fetch()
-- Transform the data according to the requested transformation
-- and set the transformed data as result
local transformed_data = datasource_instance:transform(_POST["transformation"])
datasources_data[#datasources_data + 1] = {
datasource = datasource,
metadata = datasource_instance:get_metadata(),
data = transformed_data
}
end
rest_utils.answer(rest_utils.consts.success.ok, {
datasources = datasources_data,
axes = {}
})
end
return widgets_utils
|