Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 1 | #include "report_manager.hpp" |
| 2 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 3 | #include "report.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 4 | #include "types/report_types.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 5 | #include "utils/conversion.hpp" |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 6 | #include "utils/dbus_path_utils.hpp" |
| 7 | #include "utils/make_id_name.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 8 | #include "utils/transform.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 9 | |
| 10 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 11 | #include <sdbusplus/exception.hpp> |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 12 | #include <sdbusplus/unpack_properties.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 13 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 14 | #include <optional> |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 15 | #include <stdexcept> |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 16 | #include <system_error> |
| 17 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 18 | ReadingParameters |
| 19 | convertToReadingParameters(ReadingParametersPastVersion params) |
| 20 | { |
| 21 | return utils::transform(params, [](const auto& param) { |
| 22 | using namespace std::chrono_literals; |
| 23 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 24 | const auto& [sensorPath, operationType, id, metadata] = param; |
| 25 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 26 | return ReadingParameters::value_type( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 27 | std::vector< |
| 28 | std::tuple<sdbusplus::message::object_path, std::string>>{ |
| 29 | {sensorPath, metadata}}, |
| 30 | operationType, id, utils::enumToString(CollectionTimeScope::point), |
| 31 | 0u); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 32 | }); |
| 33 | } |
| 34 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 35 | ReportManager::ReportManager( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 36 | std::unique_ptr<interfaces::ReportFactory> reportFactoryIn, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 37 | std::unique_ptr<interfaces::JsonStorage> reportStorageIn, |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 38 | const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) : |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 39 | reportFactory(std::move(reportFactoryIn)), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 40 | reportStorage(std::move(reportStorageIn)), objServer(objServerIn) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 41 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 42 | reports.reserve(maxReports); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 43 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 44 | loadFromPersistent(); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 45 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 46 | reportManagerIface = objServer->add_unique_interface( |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 47 | reportManagerPath, reportManagerIfaceName, |
| 48 | [this](auto& dbusIface) { |
| 49 | dbusIface.register_property_r("MaxReports", size_t{}, |
| 50 | sdbusplus::vtable::property_::const_, |
| 51 | [](const auto&) { return maxReports; }); |
| 52 | dbusIface.register_property_r( |
| 53 | "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_, |
| 54 | [](const auto&) -> uint64_t { return minInterval.count(); }); |
| 55 | dbusIface.register_property_r( |
| 56 | "SupportedOperationTypes", std::vector<std::string>{}, |
| 57 | sdbusplus::vtable::property_::const_, |
| 58 | [](const auto&) -> std::vector<std::string> { |
| 59 | return utils::transform<std::vector>( |
| 60 | utils::convDataOperationType, |
| 61 | [](const auto& item) { return std::string(item.first); }); |
| 62 | }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 63 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 64 | dbusIface.register_method( |
| 65 | "AddReport", |
| 66 | [this](boost::asio::yield_context& yield, |
| 67 | const std::string& reportId, |
| 68 | const std::string& reportingType, |
| 69 | const bool emitsReadingsUpdate, |
| 70 | const bool logToMetricReportsCollection, |
| 71 | const uint64_t interval, |
| 72 | ReadingParametersPastVersion metricParams) { |
| 73 | constexpr auto enabledDefault = true; |
| 74 | constexpr ReportUpdates reportUpdatesDefault = |
| 75 | ReportUpdates::overwrite; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 76 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 77 | std::vector<ReportAction> reportActions; |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 78 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 79 | if (emitsReadingsUpdate) |
| 80 | { |
| 81 | reportActions.emplace_back(ReportAction::emitsReadingsUpdate); |
| 82 | } |
| 83 | if (logToMetricReportsCollection) |
| 84 | { |
| 85 | reportActions.emplace_back( |
| 86 | ReportAction::logToMetricReportsCollection); |
| 87 | } |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 88 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 89 | return addReport( |
| 90 | yield, reportId, reportId, |
| 91 | utils::toReportingType(reportingType), reportActions, |
| 92 | Milliseconds(interval), maxAppendLimit, |
| 93 | reportUpdatesDefault, |
| 94 | convertToReadingParameters(std::move(metricParams)), |
| 95 | enabledDefault) |
| 96 | .getPath(); |
| 97 | }); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 98 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 99 | dbusIface.register_method( |
| 100 | "AddReportFutureVersion", |
| 101 | [this](boost::asio::yield_context& yield, |
| 102 | const std::vector< |
| 103 | std::pair<std::string, AddReportFutureVersionVariant>>& |
| 104 | properties) { |
| 105 | std::optional<std::string> reportId; |
| 106 | std::optional<std::string> reportName; |
| 107 | std::optional<std::string> reportingType; |
| 108 | std::optional<std::vector<std::string>> reportActions; |
| 109 | std::optional<uint64_t> interval; |
| 110 | std::optional<uint64_t> appendLimit; |
| 111 | std::optional<std::string> reportUpdates; |
| 112 | std::optional<ReadingParameters> metricParams; |
| 113 | std::optional<ReadingParameters> readingParameters; |
| 114 | std::optional<bool> enabled; |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 115 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 116 | try |
| 117 | { |
| 118 | sdbusplus::unpackProperties( |
| 119 | properties, "Id", reportId, "Name", reportName, |
| 120 | "ReportingType", reportingType, "ReportActions", |
| 121 | reportActions, "Interval", interval, "AppendLimit", |
| 122 | appendLimit, "ReportUpdates", reportUpdates, "MetricParams", |
| 123 | metricParams, "Enabled", enabled, "ReadingParameters", |
| 124 | readingParameters); |
| 125 | } |
| 126 | catch (const sdbusplus::exception::UnpackPropertyError& e) |
| 127 | { |
| 128 | throw errors::InvalidArgument(e.propertyName); |
| 129 | } |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 130 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 131 | if (readingParameters == std::nullopt) |
| 132 | { |
| 133 | readingParameters = metricParams; |
| 134 | } |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 135 | |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 136 | return addReport( |
| 137 | yield, reportId.value_or(""), reportName.value_or(""), |
| 138 | utils::toReportingType(reportingType.value_or( |
| 139 | utils::enumToString(ReportingType::onRequest))), |
| 140 | utils::transform( |
| 141 | reportActions.value_or(std::vector<std::string>{}), |
| 142 | [](const auto& reportAction) { |
| 143 | return utils::toReportAction(reportAction); |
| 144 | }), |
| 145 | Milliseconds(interval.value_or(0)), |
| 146 | appendLimit.value_or(maxAppendLimit), |
| 147 | utils::toReportUpdates(reportUpdates.value_or( |
| 148 | utils::enumToString(ReportUpdates::overwrite))), |
| 149 | readingParameters.value_or(ReadingParameters{}), |
| 150 | enabled.value_or(true)) |
| 151 | .getPath(); |
| 152 | }); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 153 | }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 156 | void ReportManager::removeReport(const interfaces::Report* report) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 157 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 158 | reports.erase( |
| 159 | std::remove_if(reports.begin(), reports.end(), |
| 160 | [report](const auto& x) { return report == x.get(); }), |
| 161 | reports.end()); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 162 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 163 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 164 | void ReportManager::verifyMetricParameters( |
| 165 | const std::vector<LabeledMetricParameters>& readingParams) |
| 166 | { |
| 167 | namespace ts = utils::tstring; |
| 168 | |
| 169 | for (auto readingParam : readingParams) |
| 170 | { |
| 171 | if (readingParam.at_label<ts::Id>().length() > |
| 172 | utils::constants::maxIdNameLength) |
| 173 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 174 | throw errors::InvalidArgument("ReadingParameters.Id", "Too long."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 179 | void ReportManager::verifyAddReport( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 180 | const std::string& reportId, const std::string& reportName, |
| 181 | const ReportingType reportingType, Milliseconds interval, |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 182 | const ReportUpdates reportUpdates, const uint64_t appendLimit, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 183 | const std::vector<LabeledMetricParameters>& readingParams) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 184 | { |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 185 | namespace ts = utils::tstring; |
| 186 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 187 | if (reports.size() >= maxReports) |
| 188 | { |
| 189 | throw sdbusplus::exception::SdBusError( |
| 190 | static_cast<int>(std::errc::too_many_files_open), |
| 191 | "Reached maximal report count"); |
| 192 | } |
| 193 | |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 194 | if (appendLimit > maxAppendLimit && |
| 195 | appendLimit != std::numeric_limits<uint64_t>::max()) |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 196 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 197 | throw errors::InvalidArgument("AppendLimit", "Out of range."); |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 200 | if ((reportingType == ReportingType::periodic && interval < minInterval) || |
| 201 | (reportingType != ReportingType::periodic && |
| 202 | interval != Milliseconds{0})) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 203 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 204 | throw errors::InvalidArgument("Interval"); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 205 | } |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 206 | |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 207 | size_t metricCount = 0; |
| 208 | for (auto metricParam : readingParams) |
| 209 | { |
| 210 | auto metricParamsVec = |
| 211 | metricParam.at_label<utils::tstring::SensorPath>(); |
| 212 | metricCount += metricParamsVec.size(); |
| 213 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 214 | |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 215 | if (readingParams.size() > maxNumberMetrics || |
| 216 | metricCount > maxNumberMetrics) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 217 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 218 | throw errors::InvalidArgument("MetricParams", "Too many."); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 219 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 220 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 221 | verifyMetricParameters(readingParams); |
| 222 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 223 | for (const LabeledMetricParameters& item : readingParams) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 224 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 225 | utils::toOperationType( |
| 226 | utils::toUnderlying(item.at_label<ts::OperationType>())); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 227 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 228 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 229 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 230 | interfaces::Report& ReportManager::addReport( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 231 | boost::asio::yield_context& yield, const std::string& reportId, |
| 232 | const std::string& reportName, const ReportingType reportingType, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 233 | const std::vector<ReportAction>& reportActions, Milliseconds interval, |
| 234 | const uint64_t appendLimit, const ReportUpdates reportUpdates, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 235 | ReadingParameters metricParams, const bool enabled) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 236 | { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 237 | auto labeledMetricParams = reportFactory->convertMetricParams(yield, |
| 238 | metricParams); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 239 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 240 | return addReport(reportId, reportName, reportingType, reportActions, |
| 241 | interval, appendLimit, reportUpdates, |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 242 | std::move(labeledMetricParams), enabled, Readings{}); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | interfaces::Report& ReportManager::addReport( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 246 | const std::string& reportId, const std::string& reportName, |
| 247 | const ReportingType reportingType, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 248 | const std::vector<ReportAction>& reportActions, Milliseconds interval, |
| 249 | const uint64_t appendLimit, const ReportUpdates reportUpdates, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 250 | std::vector<LabeledMetricParameters> labeledMetricParams, |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 251 | const bool enabled, Readings readings) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 252 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 253 | const auto existingReportIds = utils::transform( |
| 254 | reports, [](const auto& report) { return report->getId(); }); |
| 255 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 256 | auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault, |
| 257 | existingReportIds); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 258 | |
| 259 | verifyAddReport(id, name, reportingType, interval, reportUpdates, |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame] | 260 | appendLimit, labeledMetricParams); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 261 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 262 | reports.emplace_back( |
| 263 | reportFactory->make(id, name, reportingType, reportActions, interval, |
| 264 | appendLimit, reportUpdates, *this, *reportStorage, |
| 265 | labeledMetricParams, enabled, std::move(readings))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 266 | return *reports.back(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void ReportManager::loadFromPersistent() |
| 270 | { |
| 271 | std::vector<interfaces::JsonStorage::FilePath> paths = |
| 272 | reportStorage->list(); |
| 273 | |
| 274 | for (const auto& path : paths) |
| 275 | { |
| 276 | std::optional<nlohmann::json> data = reportStorage->load(path); |
| 277 | try |
| 278 | { |
| 279 | size_t version = data->at("Version").get<size_t>(); |
| 280 | if (version != Report::reportVersion) |
| 281 | { |
| 282 | throw std::logic_error("Invalid version"); |
| 283 | } |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 284 | bool enabled = data->at("Enabled").get<bool>(); |
| 285 | std::string& id = data->at("Id").get_ref<std::string&>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 286 | std::string& name = data->at("Name").get_ref<std::string&>(); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 287 | |
| 288 | uint32_t reportingType = data->at("ReportingType").get<uint32_t>(); |
| 289 | std::vector<ReportAction> reportActions = utils::transform( |
| 290 | data->at("ReportActions").get<std::vector<uint32_t>>(), |
| 291 | [](const auto reportAction) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame^] | 292 | return utils::toReportAction(reportAction); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 293 | }); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 294 | uint64_t interval = data->at("Interval").get<uint64_t>(); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 295 | uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>(); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 296 | uint32_t reportUpdates = data->at("ReportUpdates").get<uint32_t>(); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 297 | auto readingParameters = |
| 298 | data->at("ReadingParameters") |
| 299 | .get<std::vector<LabeledMetricParameters>>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 300 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 301 | Readings readings = {}; |
| 302 | |
| 303 | if (auto it = data->find("MetricValues"); it != data->end()) |
| 304 | { |
| 305 | const auto labeledReadings = it->get<LabeledReadings>(); |
| 306 | readings = utils::toReadings(labeledReadings); |
| 307 | } |
| 308 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 309 | addReport(id, name, utils::toReportingType(reportingType), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 310 | reportActions, Milliseconds(interval), appendLimit, |
| 311 | utils::toReportUpdates(reportUpdates), |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 312 | std::move(readingParameters), enabled, |
| 313 | std::move(readings)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 314 | } |
| 315 | catch (const std::exception& e) |
| 316 | { |
| 317 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 318 | "Failed to load report from storage", |
| 319 | phosphor::logging::entry( |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 320 | "FILENAME=%s", |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 321 | static_cast<std::filesystem::path>(path).c_str()), |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 322 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 323 | reportStorage->remove(path); |
| 324 | } |
| 325 | } |
| 326 | } |