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*3eb56865SSzymon Dompke const std::string& name, const std::string& reportingTypeStr, 212f9f9b87SWludzik, Jozef bool emitsReadingsSignal, bool logToMetricReportsCollection, 22*3eb56865SSzymon Dompke Milliseconds period, uint64_t appendLimit, 23*3eb56865SSzymon Dompke const std::string& reportUpdatesStr, 24*3eb56865SSzymon Dompke interfaces::ReportManager& reportManager, 25d2238194SKrzysztof Grobelny interfaces::JsonStorage& reportStorage, 267e098e93SLukasz Kazmierczak std::vector<LabeledMetricParameters> labeledMetricParams, 277e098e93SLukasz Kazmierczak bool enabled) const 282f9f9b87SWludzik, Jozef { 29d2238194SKrzysztof Grobelny std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 30d2238194SKrzysztof Grobelny labeledMetricParams, 31d2238194SKrzysztof Grobelny [this](const LabeledMetricParameters& param) 32d2238194SKrzysztof Grobelny -> std::shared_ptr<interfaces::Metric> { 33dcc4e193SKrzysztof Grobelny namespace ts = utils::tstring; 34dcc4e193SKrzysztof Grobelny 35d2238194SKrzysztof Grobelny return std::make_shared<Metric>( 36dcc4e193SKrzysztof Grobelny getSensors(param.at_label<ts::SensorPath>()), 37dcc4e193SKrzysztof Grobelny param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 38dcc4e193SKrzysztof Grobelny param.at_label<ts::MetricMetadata>(), 39dcc4e193SKrzysztof Grobelny param.at_label<ts::CollectionTimeScope>(), 408069771cSKrzysztof Grobelny param.at_label<ts::CollectionDuration>(), 418069771cSKrzysztof Grobelny std::make_unique<Clock>()); 42d2238194SKrzysztof Grobelny }); 43c8e3a64aSKrzysztof Grobelny 44*3eb56865SSzymon Dompke const ReportingType reportingType = stringToReportingType(reportingTypeStr); 45*3eb56865SSzymon Dompke const ReportUpdates reportUpdates = stringToReportUpdates(reportUpdatesStr); 46*3eb56865SSzymon Dompke 47*3eb56865SSzymon Dompke return std::make_unique<Report>(bus->get_io_context(), objServer, name, 48*3eb56865SSzymon Dompke reportingType, emitsReadingsSignal, 49*3eb56865SSzymon Dompke logToMetricReportsCollection, period, 50*3eb56865SSzymon Dompke appendLimit, reportUpdates, reportManager, 51*3eb56865SSzymon Dompke reportStorage, std::move(metrics), enabled); 522f9f9b87SWludzik, Jozef } 536ccfcbf5SKrzysztof Grobelny 54dcc4e193SKrzysztof Grobelny Sensors ReportFactory::getSensors( 55dcc4e193SKrzysztof Grobelny const std::vector<LabeledSensorParameters>& sensorPaths) const 566ccfcbf5SKrzysztof Grobelny { 57d2238194SKrzysztof Grobelny using namespace utils::tstring; 58d2238194SKrzysztof Grobelny 59dcc4e193SKrzysztof Grobelny return utils::transform(sensorPaths, 60dcc4e193SKrzysztof Grobelny [this](const LabeledSensorParameters& sensorPath) 61dcc4e193SKrzysztof Grobelny -> std::shared_ptr<interfaces::Sensor> { 62dcc4e193SKrzysztof Grobelny return sensorCache.makeSensor<Sensor>( 63dcc4e193SKrzysztof Grobelny sensorPath.at_label<Service>(), 64e8fc5751SKrzysztof Grobelny sensorPath.at_label<Path>(), 65d2238194SKrzysztof Grobelny bus->get_io_context(), bus); 66dcc4e193SKrzysztof Grobelny }); 676ccfcbf5SKrzysztof Grobelny } 686ccfcbf5SKrzysztof Grobelny 69d2238194SKrzysztof Grobelny std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 70d2238194SKrzysztof Grobelny boost::asio::yield_context& yield, 71d2238194SKrzysztof Grobelny const ReadingParameters& metricParams) const 726ccfcbf5SKrzysztof Grobelny { 731477fe6aSWludzik, Jozef auto tree = utils::getSubTreeSensors(yield, bus); 74d2238194SKrzysztof Grobelny 75d2238194SKrzysztof Grobelny return utils::transform(metricParams, [&tree](const auto& item) { 76dcc4e193SKrzysztof Grobelny const auto& [sensorPaths, operationType, id, metadata, 77dcc4e193SKrzysztof Grobelny collectionTimeScope, collectionDuration] = item; 78d2238194SKrzysztof Grobelny 79dcc4e193SKrzysztof Grobelny std::vector<LabeledSensorParameters> sensorParameters; 80dcc4e193SKrzysztof Grobelny 81dcc4e193SKrzysztof Grobelny for (const auto& sensorPath : sensorPaths) 82dcc4e193SKrzysztof Grobelny { 83d2238194SKrzysztof Grobelny auto it = std::find_if( 84d2238194SKrzysztof Grobelny tree.begin(), tree.end(), 85d2238194SKrzysztof Grobelny [&sensorPath](const auto& v) { return v.first == sensorPath; }); 86d2238194SKrzysztof Grobelny 87e8fc5751SKrzysztof Grobelny if (it != tree.end() && it->second.size() == 1) 88d2238194SKrzysztof Grobelny { 89e8fc5751SKrzysztof Grobelny const auto& [service, ifaces] = it->second.front(); 90dcc4e193SKrzysztof Grobelny sensorParameters.emplace_back(service, sensorPath); 91dcc4e193SKrzysztof Grobelny } 92d2238194SKrzysztof Grobelny } 93d2238194SKrzysztof Grobelny 94dcc4e193SKrzysztof Grobelny if (sensorParameters.size() != sensorPaths.size()) 95dcc4e193SKrzysztof Grobelny { 96e8fc5751SKrzysztof Grobelny throw sdbusplus::exception::SdBusError( 97e8fc5751SKrzysztof Grobelny static_cast<int>(std::errc::invalid_argument), 98e8fc5751SKrzysztof Grobelny "Could not find service for provided sensors"); 99dcc4e193SKrzysztof Grobelny } 100dcc4e193SKrzysztof Grobelny 101dcc4e193SKrzysztof Grobelny return LabeledMetricParameters( 102dcc4e193SKrzysztof Grobelny std::move(sensorParameters), 103dcc4e193SKrzysztof Grobelny utils::stringToOperationType(operationType), id, metadata, 104dcc4e193SKrzysztof Grobelny utils::stringToCollectionTimeScope(collectionTimeScope), 105dcc4e193SKrzysztof Grobelny CollectionDuration(Milliseconds(collectionDuration))); 106d2238194SKrzysztof Grobelny }); 1076ccfcbf5SKrzysztof Grobelny } 108