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 ReportingType reportingType, 21 const std::vector<ReportAction>& reportActions, Milliseconds period, 22 uint64_t appendLimit, const ReportUpdates reportUpdates, 23 interfaces::ReportManager& reportManager, 24 interfaces::JsonStorage& reportStorage, 25 std::vector<LabeledMetricParameters> labeledMetricParams, 26 bool enabled) const 27 { 28 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 29 labeledMetricParams, 30 [this](const LabeledMetricParameters& param) 31 -> std::shared_ptr<interfaces::Metric> { 32 namespace ts = utils::tstring; 33 34 return std::make_shared<Metric>( 35 getSensors(param.at_label<ts::SensorPath>()), 36 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 37 param.at_label<ts::MetricMetadata>(), 38 param.at_label<ts::CollectionTimeScope>(), 39 param.at_label<ts::CollectionDuration>(), 40 std::make_unique<Clock>()); 41 }); 42 43 return std::make_unique<Report>(bus->get_io_context(), objServer, name, 44 reportingType, reportActions, period, 45 appendLimit, reportUpdates, reportManager, 46 reportStorage, std::move(metrics), enabled); 47 } 48 49 Sensors ReportFactory::getSensors( 50 const std::vector<LabeledSensorParameters>& sensorPaths) const 51 { 52 using namespace utils::tstring; 53 54 return utils::transform(sensorPaths, 55 [this](const LabeledSensorParameters& sensorPath) 56 -> std::shared_ptr<interfaces::Sensor> { 57 return sensorCache.makeSensor<Sensor>( 58 sensorPath.at_label<Service>(), 59 sensorPath.at_label<Path>(), 60 bus->get_io_context(), bus); 61 }); 62 } 63 64 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 65 boost::asio::yield_context& yield, 66 const ReadingParameters& metricParams) const 67 { 68 auto tree = utils::getSubTreeSensors(yield, bus); 69 70 return utils::transform(metricParams, [&tree](const auto& item) { 71 const auto& [sensorPaths, operationType, id, metadata, 72 collectionTimeScope, collectionDuration] = item; 73 74 std::vector<LabeledSensorParameters> sensorParameters; 75 76 for (const auto& sensorPath : sensorPaths) 77 { 78 auto it = std::find_if( 79 tree.begin(), tree.end(), 80 [&sensorPath](const auto& v) { return v.first == sensorPath; }); 81 82 if (it != tree.end() && it->second.size() == 1) 83 { 84 const auto& [service, ifaces] = it->second.front(); 85 sensorParameters.emplace_back(service, sensorPath); 86 } 87 } 88 89 if (sensorParameters.size() != sensorPaths.size()) 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 return LabeledMetricParameters( 97 std::move(sensorParameters), utils::toOperationType(operationType), 98 id, metadata, utils::toCollectionTimeScope(collectionTimeScope), 99 CollectionDuration(Milliseconds(collectionDuration))); 100 }); 101 } 102