1 #include "report_factory.hpp" 2 3 #include "metric.hpp" 4 #include "report.hpp" 5 #include "sensor.hpp" 6 #include "utils/clock.hpp" 7 #include "utils/conversion.hpp" 8 #include "utils/dbus_mapper.hpp" 9 #include "utils/transform.hpp" 10 11 ReportFactory::ReportFactory( 12 std::shared_ptr<sdbusplus::asio::connection> bus, 13 const std::shared_ptr<sdbusplus::asio::object_server>& objServer, 14 SensorCache& sensorCache) : 15 bus(std::move(bus)), 16 objServer(objServer), sensorCache(sensorCache) 17 {} 18 19 std::unique_ptr<interfaces::Report> ReportFactory::make( 20 const std::string& name, const std::string& reportingType, 21 bool emitsReadingsSignal, bool logToMetricReportsCollection, 22 Milliseconds period, interfaces::ReportManager& reportManager, 23 interfaces::JsonStorage& reportStorage, 24 std::vector<LabeledMetricParameters> labeledMetricParams, 25 bool enabled) const 26 { 27 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 28 labeledMetricParams, 29 [this](const LabeledMetricParameters& param) 30 -> std::shared_ptr<interfaces::Metric> { 31 namespace ts = utils::tstring; 32 33 return std::make_shared<Metric>( 34 getSensors(param.at_label<ts::SensorPath>()), 35 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 36 param.at_label<ts::MetricMetadata>(), 37 param.at_label<ts::CollectionTimeScope>(), 38 param.at_label<ts::CollectionDuration>(), 39 std::make_unique<Clock>()); 40 }); 41 42 return std::make_unique<Report>( 43 bus->get_io_context(), objServer, name, reportingType, 44 emitsReadingsSignal, logToMetricReportsCollection, period, 45 reportManager, reportStorage, std::move(metrics), enabled); 46 } 47 48 Sensors ReportFactory::getSensors( 49 const std::vector<LabeledSensorParameters>& sensorPaths) const 50 { 51 using namespace utils::tstring; 52 53 return utils::transform(sensorPaths, 54 [this](const LabeledSensorParameters& sensorPath) 55 -> std::shared_ptr<interfaces::Sensor> { 56 return sensorCache.makeSensor<Sensor>( 57 sensorPath.at_label<Service>(), 58 sensorPath.at_label<Path>(), 59 bus->get_io_context(), bus); 60 }); 61 } 62 63 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 64 boost::asio::yield_context& yield, 65 const ReadingParameters& metricParams) const 66 { 67 auto tree = utils::getSubTreeSensors(yield, bus); 68 69 return utils::transform(metricParams, [&tree](const auto& item) { 70 const auto& [sensorPaths, operationType, id, metadata, 71 collectionTimeScope, collectionDuration] = item; 72 73 std::vector<LabeledSensorParameters> sensorParameters; 74 75 for (const auto& sensorPath : sensorPaths) 76 { 77 auto it = std::find_if( 78 tree.begin(), tree.end(), 79 [&sensorPath](const auto& v) { return v.first == sensorPath; }); 80 81 if (it != tree.end() && it->second.size() == 1) 82 { 83 const auto& [service, ifaces] = it->second.front(); 84 sensorParameters.emplace_back(service, sensorPath); 85 } 86 } 87 88 if (sensorParameters.size() != sensorPaths.size()) 89 { 90 throw sdbusplus::exception::SdBusError( 91 static_cast<int>(std::errc::invalid_argument), 92 "Could not find service for provided sensors"); 93 } 94 95 return LabeledMetricParameters( 96 std::move(sensorParameters), 97 utils::stringToOperationType(operationType), id, metadata, 98 utils::stringToCollectionTimeScope(collectionTimeScope), 99 CollectionDuration(Milliseconds(collectionDuration))); 100 }); 101 } 102