From 0dcdd97a515d6345b27a73b0a3d03f6d5fe46bfa Mon Sep 17 00:00:00 2001 From: Simone Mainardi Date: Fri, 25 Jun 2021 16:45:53 +0200 Subject: [PATCH] Implements CSV download for system alerts --- .../modules/alert_store/system_alert_store.lua | 18 +++++++++++++++--- scripts/lua/rest/v1/get/system/alert/list.lua | 15 ++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/lua/modules/alert_store/system_alert_store.lua b/scripts/lua/modules/alert_store/system_alert_store.lua index 98f996f804..92cc3a3d00 100644 --- a/scripts/lua/modules/alert_store/system_alert_store.lua +++ b/scripts/lua/modules/alert_store/system_alert_store.lua @@ -59,6 +59,18 @@ end -- ############################################## +local RNAME = { + ALERT_NAME = { name = "alert_name", export = true}, + DESCRIPTION = { name = "description", export = true}, + MSG = { name = "msg", export = true, elements = {"name", "value", "description"}} +} + +function system_alert_store:get_rnames() + return RNAME +end + +-- ############################################## + --@brief Convert an alert coming from the DB (value) to a record returned by the REST API function system_alert_store:format_record(value, no_html) local record = self:format_json_record_common(value, no_html) @@ -67,15 +79,15 @@ function system_alert_store:format_record(value, no_html) local alert_name = alert_consts.alertTypeLabel(tonumber(value["alert_id"]), no_html, alert_entities.system.entity_id) local msg = alert_utils.formatAlertMessage(ifid, value, alert_info) - record["alert_name"] = alert_name + record[RNAME.ALERT_NAME.name] = alert_name if string.lower(noHtml(msg)) == string.lower(noHtml(alert_name)) then msg = "" end - record["description"] = msg + record[RNAME.DESCRIPTION.name] = msg - record["msg"] = { + record[RNAME.MSG.name] = { name = noHtml(alert_name), value = tonumber(value["alert_id"]), description = msg, diff --git a/scripts/lua/rest/v1/get/system/alert/list.lua b/scripts/lua/rest/v1/get/system/alert/list.lua index 1dffe9acf5..8bbc71d724 100644 --- a/scripts/lua/rest/v1/get/system/alert/list.lua +++ b/scripts/lua/rest/v1/get/system/alert/list.lua @@ -42,8 +42,13 @@ for _key,_value in ipairs(alerts or {}) do res[#res + 1] = record end -- for -rest_utils.extended_answer(rc, {records = res}, { - ["draw"] = tonumber(_GET["draw"]), - ["recordsFiltered"] = recordsFiltered, - ["recordsTotal"] = #res -}, format) +if no_html then + res = system_alert_store:to_csv(res) + rest_utils.vanilla_payload_response(rc, res, "text/csv") +else + rest_utils.extended_answer(rc, {records = res}, { + ["draw"] = tonumber(_GET["draw"]), + ["recordsFiltered"] = recordsFiltered, + ["recordsTotal"] = #res + }, format) +end