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