From fb7a8102996863a1f4204f09f763c6126920b193 Mon Sep 17 00:00:00 2001 From: cborla Date: Thu, 16 Jan 2025 11:44:26 -0300 Subject: [PATCH] feat: split NotifyChange method in 4 functions --- src/modules/inventory/include/inventory.hpp | 4 + src/modules/inventory/src/inventoryImp.cpp | 135 +++++++++----------- 2 files changed, 61 insertions(+), 78 deletions(-) diff --git a/src/modules/inventory/include/inventory.hpp b/src/modules/inventory/include/inventory.hpp index 2bd29de75a..485e097ca9 100644 --- a/src/modules/inventory/include/inventory.hpp +++ b/src/modules/inventory/include/inventory.hpp @@ -67,6 +67,10 @@ class Inventory { void UpdateChanges(const std::string& table, const nlohmann::json& values, const bool isFirstScan); void NotifyChange(ReturnTypeCallback result, const nlohmann::json& data, const std::string& table); + void ProcessEvent(ReturnTypeCallback result, const nlohmann::json& item, const std::string& table); + nlohmann::json GenerateMessage(ReturnTypeCallback result, const nlohmann::json& item, const std::string& table); + void NotifyEvent(ReturnTypeCallback result, nlohmann::json& msg, const nlohmann::json& item, const std::string& table); + void TryCatchTask(const std::function& task) const; void ScanHardware(); void ScanSystem(); diff --git a/src/modules/inventory/src/inventoryImp.cpp b/src/modules/inventory/src/inventoryImp.cpp index ebd89fe195..7d8b7b4f71 100644 --- a/src/modules/inventory/src/inventoryImp.cpp +++ b/src/modules/inventory/src/inventoryImp.cpp @@ -324,100 +324,79 @@ void Inventory::NotifyChange(ReturnTypeCallback result, const nlohmann::json& da if (DB_ERROR == result) { LogErrorInventory(data.dump()); + return; } - else if (m_notify && !m_stopping) + + if (!m_notify || m_stopping) { + return; + } - if (data.is_array()) + if (data.is_array()) + { + for (const auto& item : data) { - for (const auto& item : data) - { - // LCOV_EXCL_START - nlohmann::json msg{ - {"metadata", { - {"type", table}, - {"operation", OPERATION_MAP.at(result)}, - {"module", Name()} - }} - }; - - msg["data"] = EcsData(result == MODIFIED ? item["new"] : item, table); - nlohmann::json oldData = (result == MODIFIED) ? EcsData(item["old"], table, false) : nlohmann::json{}; - - msg["metadata"]["id"] = CalculateHashId(msg["data"], table); - - if (msg["metadata"]["id"].is_string() && msg["metadata"]["id"].get().size() <= MAX_ID_SIZE) { - - nlohmann::json stateless; - stateless = GenerateStatelessEvent(OPERATION_MAP.at(result), table, msg["data"]); + ProcessEvent(result, item, table); + } + } + else + { + ProcessEvent(result, data, table); + } +} - nlohmann::json eventWithChanges; - eventWithChanges = msg["data"]; - if (!oldData.empty()) - { - stateless["event"]["changed_fields"] = AddPreviousFields(eventWithChanges, oldData); - } +void Inventory::ProcessEvent(ReturnTypeCallback result, const nlohmann::json& item, const std::string& table) +{ + nlohmann::json msg = GenerateMessage(result, item, table); - stateless.update(eventWithChanges); - msg["stateless"] = stateless; - msg["data"]["@timestamp"] = m_scanTime; + if (msg["metadata"]["id"].is_string() && msg["metadata"]["id"].get().size() <= MAX_ID_SIZE) + { + NotifyEvent(result, msg, item, table); + } + else + { + LogWarn("Event discarded for exceeding maximum size allowed in id field."); + LogTrace("Event discarded: {}", msg.dump()); + } +} - const auto msgToSend{msg.dump()}; - m_reportDiffFunction(msgToSend); - } - else - { - LogWarn("Event discarded for exceeding maximum size allowed in id field."); - LogTrace("Event discarded: {}", msg.dump()); - } - // LCOV_EXCL_STOP - } - } - else - { - // LCOV_EXCL_START - nlohmann::json msg{ - {"metadata", { - {"type", table}, - {"operation", OPERATION_MAP.at(result)}, - {"module", Name()} - }} - }; +nlohmann::json Inventory::GenerateMessage(ReturnTypeCallback result, const nlohmann::json& item, const std::string& table) +{ + nlohmann::json msg{ + {"metadata", { + {"type", table}, + {"operation", OPERATION_MAP.at(result)}, + {"module", Name()} + }} + }; - msg["data"] = EcsData(result == MODIFIED ? data["new"] : data, table); - nlohmann::json oldData = (result == MODIFIED) ? EcsData(data["old"], table, false) : nlohmann::json{}; + msg["data"] = EcsData(result == MODIFIED ? item["new"] : item, table); + msg["metadata"]["id"] = CalculateHashId(msg["data"], table); - msg["metadata"]["id"] = CalculateHashId(msg["data"], table); + return msg; +} - if (msg["metadata"]["id"].is_string() && msg["metadata"]["id"].get().size() <= MAX_ID_SIZE) { +void Inventory::NotifyEvent(ReturnTypeCallback result, nlohmann::json& msg, const nlohmann::json& item, const std::string& table) +{ + nlohmann::json oldData = (result == MODIFIED) ? EcsData(item["old"], table, false) : nlohmann::json{}; - nlohmann::json stateless; - stateless = GenerateStatelessEvent(OPERATION_MAP.at(result), table, msg["data"]); + nlohmann::json stateless = GenerateStatelessEvent(OPERATION_MAP.at(result), table, msg["data"]); + nlohmann::json eventWithChanges = msg["data"]; - nlohmann::json eventWithChanges; - eventWithChanges = msg["data"]; - if (!oldData.empty()) - { - stateless["event"]["changed_fields"] = AddPreviousFields(eventWithChanges, oldData); - } + if (!oldData.empty()) + { + stateless["event"]["changed_fields"] = AddPreviousFields(eventWithChanges, oldData); + } - stateless.update(eventWithChanges); - msg["stateless"] = stateless; - msg["data"]["@timestamp"] = m_scanTime; + stateless.update(eventWithChanges); + msg["stateless"] = stateless; + msg["data"]["@timestamp"] = m_scanTime; - const auto msgToSend{msg.dump()}; - m_reportDiffFunction(msgToSend); - } - else - { - LogWarn("Event discarded for exceeding maximum size allowed in id field."); - LogTrace("Event discarded: {}", msg.dump()); - } - // LCOV_EXCL_STOP - } - } + const auto msgToSend = msg.dump(); + m_reportDiffFunction(msgToSend); } + void Inventory::UpdateChanges(const std::string& table, const nlohmann::json& values, const bool isFirstScan)