Skip to content

Commit

Permalink
feat: Added query to retrieve IN_PROGRESS commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sdvendramini committed Dec 2, 2024
1 parent 3f07d46 commit e298e50
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/agent/command_store/include/command_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace command_store
/// @return An optional containing the command if found, or nullopt if not
std::optional<module_command::CommandEntry> 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<std::vector<module_command::CommandEntry>>
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
Expand Down
65 changes: 65 additions & 0 deletions src/agent/command_store/src/command_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,71 @@ namespace command_store
}
}

std::optional<std::vector<module_command::CommandEntry>>
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<int>(status)))});

if (cmdData.empty())
{
return std::nullopt;
}

std::vector<module_command::CommandEntry> 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<sqlite_manager::Column> fields;
Expand Down

0 comments on commit e298e50

Please sign in to comment.