12f9f9b87SWludzik, Jozef #include "report_factory.hpp" 22f9f9b87SWludzik, Jozef 36ccfcbf5SKrzysztof Grobelny #include "metric.hpp" 42f9f9b87SWludzik, Jozef #include "report.hpp" 5e2362796SWludzik, Jozef #include "sensor.hpp" 6e8fc5751SKrzysztof Grobelny #include "utils/conversion.hpp" 71477fe6aSWludzik, Jozef #include "utils/dbus_mapper.hpp" 8e2362796SWludzik, Jozef #include "utils/transform.hpp" 92f9f9b87SWludzik, Jozef 102f9f9b87SWludzik, Jozef ReportFactory::ReportFactory( 11e2362796SWludzik, Jozef std::shared_ptr<sdbusplus::asio::connection> bus, 121477fe6aSWludzik, Jozef const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 131477fe6aSWludzik, Jozef SensorCache& sensorCache) : 14e2362796SWludzik, Jozef bus(std::move(bus)), 151477fe6aSWludzik, Jozef objServer(objServer), sensorCache(sensorCache) 162f9f9b87SWludzik, Jozef {} 172f9f9b87SWludzik, Jozef 182f9f9b87SWludzik, Jozef std::unique_ptr<interfaces::Report> ReportFactory::make( 192f9f9b87SWludzik, Jozef const std::string& name, const std::string& reportingType, 202f9f9b87SWludzik, Jozef bool emitsReadingsSignal, bool logToMetricReportsCollection, 21*dcc4e193SKrzysztof Grobelny Milliseconds period, interfaces::ReportManager& reportManager, 22d2238194SKrzysztof Grobelny interfaces::JsonStorage& reportStorage, 23d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> labeledMetricParams) const 242f9f9b87SWludzik, Jozef { 25d2238194SKrzysztof Grobelny std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 26d2238194SKrzysztof Grobelny labeledMetricParams, 27d2238194SKrzysztof Grobelny [this](const LabeledMetricParameters& param) 28d2238194SKrzysztof Grobelny -> std::shared_ptr<interfaces::Metric> { 29*dcc4e193SKrzysztof Grobelny namespace ts = utils::tstring; 30*dcc4e193SKrzysztof Grobelny 31d2238194SKrzysztof Grobelny return std::make_shared<Metric>( 32*dcc4e193SKrzysztof Grobelny getSensors(param.at_label<ts::SensorPath>()), 33*dcc4e193SKrzysztof Grobelny param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 34*dcc4e193SKrzysztof Grobelny param.at_label<ts::MetricMetadata>(), 35*dcc4e193SKrzysztof Grobelny param.at_label<ts::CollectionTimeScope>(), 36*dcc4e193SKrzysztof Grobelny param.at_label<ts::CollectionDuration>()); 37d2238194SKrzysztof Grobelny }); 38c8e3a64aSKrzysztof Grobelny 392f9f9b87SWludzik, Jozef return std::make_unique<Report>( 40e2362796SWludzik, Jozef bus->get_io_context(), objServer, name, reportingType, 41*dcc4e193SKrzysztof Grobelny emitsReadingsSignal, logToMetricReportsCollection, period, 42e2362796SWludzik, Jozef reportManager, reportStorage, std::move(metrics)); 432f9f9b87SWludzik, Jozef } 446ccfcbf5SKrzysztof Grobelny 45*dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors( 46*dcc4e193SKrzysztof Grobelny const std::vector<LabeledSensorParameters>& sensorPaths) const 476ccfcbf5SKrzysztof Grobelny { 48d2238194SKrzysztof Grobelny using namespace utils::tstring; 49d2238194SKrzysztof Grobelny 50*dcc4e193SKrzysztof Grobelny return utils::transform(sensorPaths, 51*dcc4e193SKrzysztof Grobelny [this](const LabeledSensorParameters& sensorPath) 52*dcc4e193SKrzysztof Grobelny -> std::shared_ptr<interfaces::Sensor> { 53*dcc4e193SKrzysztof Grobelny return sensorCache.makeSensor<Sensor>( 54*dcc4e193SKrzysztof Grobelny sensorPath.at_label<Service>(), 55e8fc5751SKrzysztof Grobelny sensorPath.at_label<Path>(), 56d2238194SKrzysztof Grobelny bus->get_io_context(), bus); 57*dcc4e193SKrzysztof Grobelny }); 586ccfcbf5SKrzysztof Grobelny } 596ccfcbf5SKrzysztof Grobelny 60d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 61d2238194SKrzysztof Grobelny boost::asio::yield_context& yield, 62d2238194SKrzysztof Grobelny const ReadingParameters& metricParams) const 636ccfcbf5SKrzysztof Grobelny { 641477fe6aSWludzik, Jozef auto tree = utils::getSubTreeSensors(yield, bus); 65d2238194SKrzysztof Grobelny 66d2238194SKrzysztof Grobelny return utils::transform(metricParams, [&tree](const auto& item) { 67*dcc4e193SKrzysztof Grobelny const auto& [sensorPaths, operationType, id, metadata, 68*dcc4e193SKrzysztof Grobelny collectionTimeScope, collectionDuration] = item; 69d2238194SKrzysztof Grobelny 70*dcc4e193SKrzysztof Grobelny std::vector<LabeledSensorParameters> sensorParameters; 71*dcc4e193SKrzysztof Grobelny 72*dcc4e193SKrzysztof Grobelny for (const auto& sensorPath : sensorPaths) 73*dcc4e193SKrzysztof Grobelny { 74d2238194SKrzysztof Grobelny auto it = std::find_if( 75d2238194SKrzysztof Grobelny tree.begin(), tree.end(), 76d2238194SKrzysztof Grobelny [&sensorPath](const auto& v) { return v.first == sensorPath; }); 77d2238194SKrzysztof Grobelny 78e8fc5751SKrzysztof Grobelny if (it != tree.end() && it->second.size() == 1) 79d2238194SKrzysztof Grobelny { 80e8fc5751SKrzysztof Grobelny const auto& [service, ifaces] = it->second.front(); 81*dcc4e193SKrzysztof Grobelny sensorParameters.emplace_back(service, sensorPath); 82*dcc4e193SKrzysztof Grobelny } 83d2238194SKrzysztof Grobelny } 84d2238194SKrzysztof Grobelny 85*dcc4e193SKrzysztof Grobelny if (sensorParameters.size() != sensorPaths.size()) 86*dcc4e193SKrzysztof Grobelny { 87e8fc5751SKrzysztof Grobelny throw sdbusplus::exception::SdBusError( 88e8fc5751SKrzysztof Grobelny static_cast<int>(std::errc::invalid_argument), 89e8fc5751SKrzysztof Grobelny "Could not find service for provided sensors"); 90*dcc4e193SKrzysztof Grobelny } 91*dcc4e193SKrzysztof Grobelny 92*dcc4e193SKrzysztof Grobelny return LabeledMetricParameters( 93*dcc4e193SKrzysztof Grobelny std::move(sensorParameters), 94*dcc4e193SKrzysztof Grobelny utils::stringToOperationType(operationType), id, metadata, 95*dcc4e193SKrzysztof Grobelny utils::stringToCollectionTimeScope(collectionTimeScope), 96*dcc4e193SKrzysztof Grobelny CollectionDuration(Milliseconds(collectionDuration))); 97d2238194SKrzysztof Grobelny }); 986ccfcbf5SKrzysztof Grobelny } 99