Skip to content

Commit

Permalink
feat: split NotifyChange method in 4 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cborla committed Jan 16, 2025
1 parent 736a8db commit fb7a810
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 78 deletions.
4 changes: 4 additions & 0 deletions src/modules/inventory/include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()>& task) const;
void ScanHardware();
void ScanSystem();
Expand Down
135 changes: 57 additions & 78 deletions src/modules/inventory/src/inventoryImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>().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<std::string>().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<std::string>().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)
Expand Down

0 comments on commit fb7a810

Please sign in to comment.