12f9f9b87SWludzik, Jozef #include "report_factory.hpp" 22f9f9b87SWludzik, Jozef 36ccfcbf5SKrzysztof Grobelny #include "metric.hpp" 42f9f9b87SWludzik, Jozef #include "report.hpp" 5e2362796SWludzik, Jozef #include "sensor.hpp" 68069771cSKrzysztof Grobelny #include "utils/clock.hpp" 7e8fc5751SKrzysztof Grobelny #include "utils/conversion.hpp" 81477fe6aSWludzik, Jozef #include "utils/dbus_mapper.hpp" 9e2362796SWludzik, Jozef #include "utils/transform.hpp" 102f9f9b87SWludzik, Jozef 112f9f9b87SWludzik, Jozef ReportFactory::ReportFactory( 12e2362796SWludzik, Jozef std::shared_ptr<sdbusplus::asio::connection> bus, 131477fe6aSWludzik, Jozef const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 141477fe6aSWludzik, Jozef SensorCache& sensorCache) : 15e2362796SWludzik, Jozef bus(std::move(bus)), 161477fe6aSWludzik, Jozef objServer(objServer), sensorCache(sensorCache) 172f9f9b87SWludzik, Jozef {} 182f9f9b87SWludzik, Jozef 192f9f9b87SWludzik, Jozef std::unique_ptr<interfaces::Report> ReportFactory::make( 20*51497a0cSKrzysztof Grobelny const std::string& name, const ReportingType reportingType, 21*51497a0cSKrzysztof Grobelny const std::vector<ReportAction>& reportActions, Milliseconds period, 22*51497a0cSKrzysztof Grobelny uint64_t appendLimit, const ReportUpdates reportUpdates, 233eb56865SSzymon Dompke interfaces::ReportManager& reportManager, 24d2238194SKrzysztof Grobelny interfaces::JsonStorage& reportStorage, 257e098e93SLukasz Kazmierczak std::vector<LabeledMetricParameters> labeledMetricParams, 267e098e93SLukasz Kazmierczak bool enabled) const 272f9f9b87SWludzik, Jozef { 28d2238194SKrzysztof Grobelny std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 29d2238194SKrzysztof Grobelny labeledMetricParams, 30d2238194SKrzysztof Grobelny [this](const LabeledMetricParameters& param) 31d2238194SKrzysztof Grobelny -> std::shared_ptr<interfaces::Metric> { 32dcc4e193SKrzysztof Grobelny namespace ts = utils::tstring; 33dcc4e193SKrzysztof Grobelny 34d2238194SKrzysztof Grobelny return std::make_shared<Metric>( 35dcc4e193SKrzysztof Grobelny getSensors(param.at_label<ts::SensorPath>()), 36dcc4e193SKrzysztof Grobelny param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 37dcc4e193SKrzysztof Grobelny param.at_label<ts::MetricMetadata>(), 38dcc4e193SKrzysztof Grobelny param.at_label<ts::CollectionTimeScope>(), 398069771cSKrzysztof Grobelny param.at_label<ts::CollectionDuration>(), 408069771cSKrzysztof Grobelny std::make_unique<Clock>()); 41d2238194SKrzysztof Grobelny }); 42c8e3a64aSKrzysztof Grobelny 433eb56865SSzymon Dompke return std::make_unique<Report>(bus->get_io_context(), objServer, name, 44*51497a0cSKrzysztof Grobelny reportingType, reportActions, period, 453eb56865SSzymon Dompke appendLimit, reportUpdates, reportManager, 463eb56865SSzymon Dompke reportStorage, std::move(metrics), enabled); 472f9f9b87SWludzik, Jozef } 486ccfcbf5SKrzysztof Grobelny 49dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors( 50dcc4e193SKrzysztof Grobelny const std::vector<LabeledSensorParameters>& sensorPaths) const 516ccfcbf5SKrzysztof Grobelny { 52d2238194SKrzysztof Grobelny using namespace utils::tstring; 53d2238194SKrzysztof Grobelny 54dcc4e193SKrzysztof Grobelny return utils::transform(sensorPaths, 55dcc4e193SKrzysztof Grobelny [this](const LabeledSensorParameters& sensorPath) 56dcc4e193SKrzysztof Grobelny -> std::shared_ptr<interfaces::Sensor> { 57dcc4e193SKrzysztof Grobelny return sensorCache.makeSensor<Sensor>( 58dcc4e193SKrzysztof Grobelny sensorPath.at_label<Service>(), 59e8fc5751SKrzysztof Grobelny sensorPath.at_label<Path>(), 60d2238194SKrzysztof Grobelny bus->get_io_context(), bus); 61dcc4e193SKrzysztof Grobelny }); 626ccfcbf5SKrzysztof Grobelny } 636ccfcbf5SKrzysztof Grobelny 64d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 65d2238194SKrzysztof Grobelny boost::asio::yield_context& yield, 66d2238194SKrzysztof Grobelny const ReadingParameters& metricParams) const 676ccfcbf5SKrzysztof Grobelny { 681477fe6aSWludzik, Jozef auto tree = utils::getSubTreeSensors(yield, bus); 69d2238194SKrzysztof Grobelny 70d2238194SKrzysztof Grobelny return utils::transform(metricParams, [&tree](const auto& item) { 71dcc4e193SKrzysztof Grobelny const auto& [sensorPaths, operationType, id, metadata, 72dcc4e193SKrzysztof Grobelny collectionTimeScope, collectionDuration] = item; 73d2238194SKrzysztof Grobelny 74dcc4e193SKrzysztof Grobelny std::vector<LabeledSensorParameters> sensorParameters; 75dcc4e193SKrzysztof Grobelny 76dcc4e193SKrzysztof Grobelny for (const auto& sensorPath : sensorPaths) 77dcc4e193SKrzysztof Grobelny { 78d2238194SKrzysztof Grobelny auto it = std::find_if( 79d2238194SKrzysztof Grobelny tree.begin(), tree.end(), 80d2238194SKrzysztof Grobelny [&sensorPath](const auto& v) { return v.first == sensorPath; }); 81d2238194SKrzysztof Grobelny 82e8fc5751SKrzysztof Grobelny if (it != tree.end() && it->second.size() == 1) 83d2238194SKrzysztof Grobelny { 84e8fc5751SKrzysztof Grobelny const auto& [service, ifaces] = it->second.front(); 85dcc4e193SKrzysztof Grobelny sensorParameters.emplace_back(service, sensorPath); 86dcc4e193SKrzysztof Grobelny } 87d2238194SKrzysztof Grobelny } 88d2238194SKrzysztof Grobelny 89dcc4e193SKrzysztof Grobelny if (sensorParameters.size() != sensorPaths.size()) 90dcc4e193SKrzysztof Grobelny { 91e8fc5751SKrzysztof Grobelny throw sdbusplus::exception::SdBusError( 92e8fc5751SKrzysztof Grobelny static_cast<int>(std::errc::invalid_argument), 93e8fc5751SKrzysztof Grobelny "Could not find service for provided sensors"); 94dcc4e193SKrzysztof Grobelny } 95dcc4e193SKrzysztof Grobelny 96dcc4e193SKrzysztof Grobelny return LabeledMetricParameters( 97*51497a0cSKrzysztof Grobelny std::move(sensorParameters), utils::toOperationType(operationType), 98*51497a0cSKrzysztof Grobelny id, metadata, utils::toCollectionTimeScope(collectionTimeScope), 99dcc4e193SKrzysztof Grobelny CollectionDuration(Milliseconds(collectionDuration))); 100d2238194SKrzysztof Grobelny }); 1016ccfcbf5SKrzysztof Grobelny } 102