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& id, const std::string& name, 21 const ReportingType reportingType, 22 const std::vector<ReportAction>& reportActions, Milliseconds period, 23 uint64_t appendLimit, const ReportUpdates reportUpdates, 24 interfaces::ReportManager& reportManager, 25 interfaces::JsonStorage& reportStorage, 26 std::vector<LabeledMetricParameters> labeledMetricParams, bool enabled, 27 const std::vector<std::string>& triggerIds) const 28 { 29 std::vector<std::shared_ptr<interfaces::Metric>> metrics = utils::transform( 30 labeledMetricParams, 31 [this](const LabeledMetricParameters& param) 32 -> std::shared_ptr<interfaces::Metric> { 33 namespace ts = utils::tstring; 34 35 return std::make_shared<Metric>( 36 getSensors(param.at_label<ts::SensorPath>()), 37 param.at_label<ts::OperationType>(), param.at_label<ts::Id>(), 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, id, name, 44 reportingType, reportActions, period, 45 appendLimit, reportUpdates, reportManager, 46 reportStorage, std::move(metrics), enabled, 47 std::make_unique<Clock>(), triggerIds); 48 } 49 50 Sensors ReportFactory::getSensors( 51 const std::vector<LabeledSensorInfo>& sensorPaths) const 52 { 53 using namespace utils::tstring; 54 55 return utils::transform( 56 sensorPaths, 57 [this](const LabeledSensorInfo& sensorPath) 58 -> std::shared_ptr<interfaces::Sensor> { 59 return sensorCache.makeSensor<Sensor>( 60 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(), 61 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus); 62 }); 63 } 64 65 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 66 boost::asio::yield_context& yield, 67 const ReadingParameters& metricParams) const 68 { 69 auto tree = utils::getSubTreeSensors(yield, bus); 70 71 return utils::transform(metricParams, [&tree](const auto& item) { 72 auto [sensorPaths, operationType, id, collectionTimeScope, 73 collectionDuration] = item; 74 75 std::vector<LabeledSensorInfo> sensorParameters; 76 77 for (const auto& [sensorPath, metadata] : sensorPaths) 78 { 79 auto it = std::find_if( 80 tree.begin(), tree.end(), 81 [path = sensorPath](const auto& v) { return v.first == path; }); 82 83 if (it != tree.end() && it->second.size() == 1) 84 { 85 const auto& [service, ifaces] = it->second.front(); 86 sensorParameters.emplace_back(service, sensorPath, metadata); 87 } 88 } 89 90 if (sensorParameters.size() != sensorPaths.size()) 91 { 92 throw sdbusplus::exception::SdBusError( 93 static_cast<int>(std::errc::invalid_argument), 94 "Could not find service for provided sensors"); 95 } 96 97 if (operationType.empty()) 98 { 99 operationType = utils::enumToString(OperationType::avg); 100 } 101 102 if (collectionTimeScope.empty()) 103 { 104 collectionTimeScope = 105 utils::enumToString(CollectionTimeScope::point); 106 } 107 108 return LabeledMetricParameters( 109 std::move(sensorParameters), utils::toOperationType(operationType), 110 id, utils::toCollectionTimeScope(collectionTimeScope), 111 CollectionDuration(Milliseconds(collectionDuration))); 112 }); 113 } 114