From e298e504d7c33ed2e7f726fb42223b809040e461 Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Mon, 2 Dec 2024 09:35:46 +0100 Subject: [PATCH] feat: Added query to retrieve IN_PROGRESS commands --- .../command_store/include/command_store.hpp | 6 ++ src/agent/command_store/src/command_store.cpp | 65 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/agent/command_store/include/command_store.hpp b/src/agent/command_store/include/command_store.hpp index c715c64199..aacd769978 100644 --- a/src/agent/command_store/include/command_store.hpp +++ b/src/agent/command_store/include/command_store.hpp @@ -60,6 +60,12 @@ namespace command_store /// @return An optional containing the command if found, or nullopt if not std::optional GetCommand(const std::string& id); + /// @brief Retrieves a vector of commands from the database by status + /// @param status The status of the commands to retrieve + /// @return An optional containing the commands if found, or nullopt if not + std::optional> + GetCommandByStatus(const module_command::Status& status); + /// @brief Updates an existing command in the database /// @param cmd The updated command data /// @return True if successful, false otherwise diff --git a/src/agent/command_store/src/command_store.cpp b/src/agent/command_store/src/command_store.cpp index 3ca75250db..e90286f1f3 100644 --- a/src/agent/command_store/src/command_store.cpp +++ b/src/agent/command_store/src/command_store.cpp @@ -177,6 +177,71 @@ namespace command_store } } + std::optional> + CommandStore::GetCommandByStatus(const module_command::Status& status) + { + try + { + auto cmdData = m_dataBase->Select(COMMANDSTORE_TABLE_NAME, + {}, + {sqlite_manager::Column("status", + sqlite_manager::ColumnType::INTEGER, + std::to_string(static_cast(status)))}); + + if (cmdData.empty()) + { + return std::nullopt; + } + + std::vector commands; + + for (const auto& row : cmdData) + { + module_command::CommandEntry cmd; + + for (const sqlite_manager::Column& col : row) + { + if (col.Name == "id") + { + cmd.Id = col.Value; + } + else if (col.Name == "module") + { + cmd.Module = col.Value; + } + else if (col.Name == "command") + { + cmd.Command = col.Value; + } + else if (col.Name == "parameters") + { + cmd.Parameters = nlohmann::json::parse(col.Value); + } + else if (col.Name == "result") + { + cmd.ExecutionResult.Message = col.Value; + } + else if (col.Name == "status") + { + cmd.ExecutionResult.ErrorCode = StatusFromInt(std::stoi(col.Value)); + } + else if (col.Name == "time") + { + cmd.Time = std::stod(col.Value); + } + } + + commands.push_back(cmd); + } + return commands; + } + catch (const std::exception& e) + { + LogError("Select operation failed: {}.", e.what()); + return std::nullopt; + } + } + bool CommandStore::UpdateCommand(const module_command::CommandEntry& cmd) { std::vector fields;