Reworks widgets data structure and response

This commit is contained in:
Simone Mainardi 2021-01-20 16:01:57 +01:00
parent 397892d6fa
commit c4172a8e51
5 changed files with 228 additions and 24 deletions

View file

@ -25,6 +25,8 @@ datasource.BASE_REST_PREFIX = "/lua/rest/v1/get/datasource/"
-- @brief Base class constructor
function datasource:init()
self._dataset_params = {} -- Holds per-dataset params
self._datamodel_instance = nil -- Instance of the datamodel holding data for each dataset
end
-- ##############################################
@ -60,6 +62,51 @@ end
-- ##############################################
-- @brief Parses params
-- @param params_table A table with submitted params
-- @return An integer dataset id upon success, nil otherwise
function datasource:add_dataset(params_table)
if not params_table then
return nil
end
local cur_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
return nil
end
cur_params[param] = parsed_param
end
-- Ok, parsing has been successful
self._dataset_params[#self._dataset_params + 1] = cur_params
-- The id is basically the position in the array of dataset_params
return #self._dataset_params
end
-- ##############################################
-- @brief Fetches data of every added dataset
function datasource:fetch_data()
-- Instantiate the datamodel
self._datamodel_instance = self.meta.datamodel.new(
self.meta.i18n_title
)
-- Fetch datasets data
for dataset_id, dataset_params in ipairs(self._dataset_params) do
self:_fetch(dataset_id, dataset_params)
end
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
@ -148,6 +195,16 @@ 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_data(transformation)
return self._datamodel_instance:datasets_get_data(transformation)
end
-- ##############################################
return datasource
-- ##############################################

View file

@ -44,17 +44,17 @@ packet_distro.meta = {
-- ##############################################
-- Human-friendly labels for the distribution
packet_distro.labels = {
['upTo64'] = '<= 64',
['upTo128'] = '64 <= 128',
['upTo256'] = '128 <= 256',
['upTo512'] = '256 <= 512',
['upTo1024'] = '512 <= 1024',
['upTo1518'] = '1024 <= 1518',
['upTo2500'] = '1518 <= 2500',
['upTo6500'] = '2500 <= 6500',
['upTo9000'] = '6500 <= 9000',
['above9000'] = '> 9000'
packet_distro.slices = {
{ key = 'upTo64', label = '<= 64' },
{ key = 'upTo128', label = '64 <= 128' },
{ key = 'upTo256', label = '128 <= 256' },
{ key = 'upTo512', label = '256 <= 512' },
{ key = 'upTo1024', label = '512 <= 1024' },
{ key = 'upTo1518', label = '1024 <= 1518' },
{ key = 'upTo2500', label = '1518 <= 2500' },
{ key = 'upTo6500', label = '2500 <= 6500' },
{ key = 'upTo9000', label = '6500 <= 9000' },
{ key = 'above9000', label = '> 9000' },
}
-- ##############################################
@ -78,16 +78,35 @@ function packet_distro:fetch()
local size_bins = ifstats["pktSizeDistribution"]["size"]
self.datamodel_instance = self.meta.datamodel.new(
self.meta.i18n_title,
getHumanReadableInterfaceName(getInterfaceName(ifstats.id)),
10 --[[ Maximum number of slices ]],
3 --[[ Percentage under which the slice is ignored and added to other --]])
for bin, num_packets in pairsByKeys(size_bins) do
self.datamodel_instance:append(packet_distro.labels[bin], num_packets)
for _, slice in ipairs(packet_distro.slices) do
self.datamodel_instance:append(slice.label, size_bins[slice.key] or 0)
end
-- Consolidate `append`ed data
self.datamodel_instance:consolidate()
-- self.datamodel_instance:consolidate()
end
-- #######################################################
-- New version, throw fetch() when done
function packet_distro:_fetch(dataset_id, dataset_params)
-- Assumes all parameters listed in self.meta.params have been parsed successfully
-- and are available in dataset_params
interface.select(tostring(dataset_params.ifid))
local ifstats = interface.getStats()
local size_bins = ifstats["pktSizeDistribution"]["size"]
for bin, num_packets in pairsByKeys(size_bins) do
self._datamodel_instance:dataset_append(dataset_id, packet_distro.slices[bin], num_packets)
end
-- Add metadata to the dataset
self._datamodel_instance:dataset_metadata(dataset_id, getHumanReadableInterfaceName(getInterfaceName(ifstats.id)) --[[ This is the label --]])
end
-- #######################################################