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 Readings readings) const 28 { 29 auto 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>( 44 bus->get_io_context(), objServer, id, name, reportingType, 45 reportActions, period, appendLimit, reportUpdates, reportManager, 46 reportStorage, std::move(metrics), *this, enabled, 47 std::make_unique<Clock>(), std::move(readings)); 48 } 49 50 void ReportFactory::updateMetrics( 51 std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled, 52 const std::vector<LabeledMetricParameters>& labeledMetricParams) const 53 { 54 std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics; 55 std::vector<std::shared_ptr<interfaces::Metric>> newMetrics; 56 57 for (const auto& labeledMetricParam : labeledMetricParams) 58 { 59 auto existing = std::find_if(oldMetrics.begin(), oldMetrics.end(), 60 [labeledMetricParam](auto metric) { 61 return labeledMetricParam == 62 metric->dumpConfiguration(); 63 }); 64 65 if (existing != oldMetrics.end()) 66 { 67 newMetrics.emplace_back(*existing); 68 oldMetrics.erase(existing); 69 continue; 70 } 71 72 namespace ts = utils::tstring; 73 newMetrics.emplace_back(std::make_shared<Metric>( 74 getSensors(labeledMetricParam.at_label<ts::SensorPath>()), 75 labeledMetricParam.at_label<ts::OperationType>(), 76 labeledMetricParam.at_label<ts::Id>(), 77 labeledMetricParam.at_label<ts::CollectionTimeScope>(), 78 labeledMetricParam.at_label<ts::CollectionDuration>(), 79 std::make_unique<Clock>())); 80 81 if (enabled) 82 { 83 newMetrics.back()->initialize(); 84 } 85 } 86 87 if (enabled) 88 { 89 for (auto& metric : oldMetrics) 90 { 91 metric->deinitialize(); 92 } 93 } 94 95 metrics = std::move(newMetrics); 96 } 97 98 Sensors ReportFactory::getSensors( 99 const std::vector<LabeledSensorInfo>& sensorPaths) const 100 { 101 using namespace utils::tstring; 102 103 return utils::transform( 104 sensorPaths, 105 [this](const LabeledSensorInfo& sensorPath) 106 -> std::shared_ptr<interfaces::Sensor> { 107 return sensorCache.makeSensor<Sensor>( 108 sensorPath.at_label<Service>(), sensorPath.at_label<Path>(), 109 sensorPath.at_label<Metadata>(), bus->get_io_context(), bus); 110 }); 111 } 112 113 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 114 boost::asio::yield_context& yield, 115 const ReadingParameters& metricParams) const 116 { 117 if (metricParams.empty()) 118 { 119 return {}; 120 } 121 auto tree = utils::getSubTreeSensors(yield, bus); 122 return getMetricParamsFromSensorTree(metricParams, tree); 123 } 124 125 std::vector<LabeledMetricParameters> ReportFactory::convertMetricParams( 126 const ReadingParameters& metricParams) const 127 { 128 if (metricParams.empty()) 129 { 130 return {}; 131 } 132 auto tree = utils::getSubTreeSensors(bus); 133 return getMetricParamsFromSensorTree(metricParams, tree); 134 } 135 136 std::vector<LabeledMetricParameters> 137 ReportFactory::getMetricParamsFromSensorTree( 138 const ReadingParameters& metricParams, 139 const std::vector<utils::SensorTree>& tree) const 140 { 141 return utils::transform(metricParams, [&tree](const auto& item) { 142 auto [sensorPaths, operationType, id, collectionTimeScope, 143 collectionDuration] = item; 144 145 std::vector<LabeledSensorInfo> sensorParameters; 146 147 for (const auto& [sensorPath, metadata] : sensorPaths) 148 { 149 auto it = std::find_if( 150 tree.begin(), tree.end(), 151 [path = sensorPath](const auto& v) { return v.first == path; }); 152 153 if (it != tree.end() && it->second.size() == 1) 154 { 155 const auto& [service, ifaces] = it->second.front(); 156 sensorParameters.emplace_back(service, sensorPath, metadata); 157 } 158 } 159 160 if (sensorParameters.size() != sensorPaths.size()) 161 { 162 throw sdbusplus::exception::SdBusError( 163 static_cast<int>(std::errc::invalid_argument), 164 "Could not find service for provided sensors"); 165 } 166 167 if (operationType.empty()) 168 { 169 operationType = utils::enumToString(OperationType::avg); 170 } 171 else if (operationType == "SINGLE") 172 { 173 operationType = utils::enumToString(OperationType::avg); 174 collectionTimeScope = 175 utils::enumToString(CollectionTimeScope::point); 176 } 177 178 if (collectionTimeScope.empty()) 179 { 180 collectionTimeScope = 181 utils::enumToString(CollectionTimeScope::point); 182 } 183 184 return LabeledMetricParameters( 185 std::move(sensorParameters), utils::toOperationType(operationType), 186 id, utils::toCollectionTimeScope(collectionTimeScope), 187 CollectionDuration(Milliseconds(collectionDuration))); 188 }); 189 } 190