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