#include "systeminfo_task_runner.hpp"
#include "system_info.hpp"
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
namespace doWhile::doct
{
TaskResult SystemInfoTaskRunner::runTask(std::shared_ptr<data::SysInfoTask> task, ext_tools::SystemInfo* systemInfo)
{
TaskResult taskResult{task};
taskResult.setSuccess(true);
std::vector<std::string> tableRows;
tableRows.push_back("----------------------------------------");
tableRows.push_back("| Hostname: " + getHosname(systemInfo));
tableRows.push_back("| Cpus: " + std::to_string(systemInfo->getCpus().size()));
for(int i = 0; i < systemInfo->getCpus().size(); i++)
{
auto c = systemInfo->getCpus()[i];
tableRows.push_back("| - Cpu #" + std::to_string(i) + " core: " + std::to_string(c->getCore()));
tableRows.push_back("| - Cpu #" + std::to_string(i) + " utilisation: " + std::to_string(c->getCpuUtilization()));
}
auto cpus = systemInfo->getCpus();
std::vector<double> cpuUtilizations;
std::transform(cpus.begin(), cpus.end(), std::back_inserter(cpuUtilizations), [](std::shared_ptr<ext_tools::CpuCoreInfo> cpu) { return cpu->getCpuUtilization(); });
auto average = std::to_string(std::accumulate(cpuUtilizations.begin(), cpuUtilizations.end(), 0.0) / cpuUtilizations.size());
tableRows.push_back("| Avarege utilisation: " + average.substr(0,4));
auto memTotal = humanise(systemInfo->getTotalMemory() * 1000 * 1000 /* To bytes for humanisation */);
auto memUsedl = humanise(systemInfo->getMemoryUsage() * 1000 * 1000 /* To bytes for humanisation */);
tableRows.push_back("| Memory: " + memUsedl + " of " + memTotal);
tableRows.push_back("-------------------------------------");
for (const auto row : tableRows)
taskResult.addMessage(row);
return taskResult;
}
std::string SystemInfoTaskRunner::humanise(long input)
{
std::vector<std::string> _orders { "B", "kB", "MB", "GB", "PB", "TB" };
int divisions = 0;
while (input > 1024)
{
input = input / 1024;
divisions = divisions + 1;
}
std::ostringstream stream;
stream << input << " " << _orders[divisions];
return stream.str();
}
std::string SystemInfoTaskRunner::getHosname(ext_tools::SystemInfo* systemInfo)
{
return systemInfo->hostname;
}
}