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" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 6 | #include "utils/transform.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 9 | #include <sdbusplus/exception.hpp> |
| 10 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | #include <stdexcept> |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 12 | #include <system_error> |
| 13 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 14 | ReadingParameters |
| 15 | convertToReadingParameters(ReadingParametersPastVersion params) |
| 16 | { |
| 17 | return utils::transform(params, [](const auto& param) { |
| 18 | using namespace std::chrono_literals; |
| 19 | |
| 20 | return ReadingParameters::value_type( |
| 21 | std::vector{{std::get<0>(param)}}, std::get<1>(param), |
| 22 | std::get<2>(param), std::get<3>(param), |
| 23 | utils::enumToString(CollectionTimeScope::point), 0u); |
| 24 | }); |
| 25 | } |
| 26 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 27 | ReportManager::ReportManager( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 28 | std::unique_ptr<interfaces::ReportFactory> reportFactoryIn, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 29 | std::unique_ptr<interfaces::JsonStorage> reportStorageIn, |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 30 | const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) : |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 31 | reportFactory(std::move(reportFactoryIn)), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 32 | reportStorage(std::move(reportStorageIn)), objServer(objServerIn) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 33 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 34 | reports.reserve(maxReports); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 35 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 36 | loadFromPersistent(); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 37 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 38 | reportManagerIface = objServer->add_unique_interface( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 39 | reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 40 | dbusIface.register_property_r( |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 41 | "MaxReports", size_t{}, sdbusplus::vtable::property_::const_, |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 42 | [](const auto&) { return maxReports; }); |
| 43 | dbusIface.register_property_r( |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 44 | "MaxReportNameLength", size_t{}, |
| 45 | sdbusplus::vtable::property_::const_, |
| 46 | [](const auto&) { return maxReportNameLength; }); |
| 47 | dbusIface.register_property_r( |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 48 | "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_, |
| 49 | [](const auto&) -> uint64_t { return minInterval.count(); }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 50 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 51 | dbusIface.register_method( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 52 | "AddReport", [this](boost::asio::yield_context& yield, |
| 53 | const std::string& reportName, |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 54 | const std::string& reportingType, |
| 55 | const bool emitsReadingsUpdate, |
| 56 | const bool logToMetricReportsCollection, |
| 57 | const uint64_t interval, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 58 | ReadingParametersPastVersion metricParams) { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 59 | constexpr auto enabledDefault = true; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 60 | return addReport(yield, reportName, reportingType, |
| 61 | emitsReadingsUpdate, |
| 62 | logToMetricReportsCollection, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 63 | Milliseconds(interval), |
| 64 | convertToReadingParameters( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 65 | std::move(metricParams)), |
| 66 | enabledDefault) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 67 | .getPath(); |
| 68 | }); |
| 69 | |
| 70 | dbusIface.register_method( |
| 71 | "AddReportFutureVersion", |
| 72 | [this](boost::asio::yield_context& yield, |
| 73 | const std::string& reportName, |
| 74 | const std::string& reportingType, |
| 75 | const bool emitsReadingsUpdate, |
| 76 | const bool logToMetricReportsCollection, |
| 77 | const uint64_t interval, |
| 78 | ReadingParameters metricParams) { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 79 | constexpr auto enabledDefault = true; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 80 | return addReport(yield, reportName, reportingType, |
| 81 | emitsReadingsUpdate, |
| 82 | logToMetricReportsCollection, |
| 83 | Milliseconds(interval), |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 84 | std::move(metricParams), enabledDefault) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 85 | .getPath(); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 86 | }); |
| 87 | }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 90 | void ReportManager::removeReport(const interfaces::Report* report) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 91 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 92 | reports.erase( |
| 93 | std::remove_if(reports.begin(), reports.end(), |
| 94 | [report](const auto& x) { return report == x.get(); }), |
| 95 | reports.end()); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 96 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 97 | |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 98 | void ReportManager::verifyReportNameLength(const std::string& reportName) |
| 99 | { |
| 100 | if (reportName.length() > maxReportNameLength) |
| 101 | { |
| 102 | throw sdbusplus::exception::SdBusError( |
| 103 | static_cast<int>(std::errc::invalid_argument), |
| 104 | "Report name exceed maximum length"); |
| 105 | } |
| 106 | } |
| 107 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 108 | void ReportManager::verifyAddReport( |
| 109 | const std::string& reportName, const std::string& reportingType, |
| 110 | Milliseconds interval, |
| 111 | const std::vector<LabeledMetricParameters>& readingParams) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 112 | { |
| 113 | if (reports.size() >= maxReports) |
| 114 | { |
| 115 | throw sdbusplus::exception::SdBusError( |
| 116 | static_cast<int>(std::errc::too_many_files_open), |
| 117 | "Reached maximal report count"); |
| 118 | } |
| 119 | |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 120 | verifyReportNameLength(reportName); |
| 121 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 122 | for (const auto& report : reports) |
| 123 | { |
| 124 | if (report->getName() == reportName) |
| 125 | { |
| 126 | throw sdbusplus::exception::SdBusError( |
| 127 | static_cast<int>(std::errc::file_exists), "Duplicate report"); |
| 128 | } |
| 129 | } |
| 130 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 131 | auto found = std::find(supportedReportingType.begin(), |
| 132 | supportedReportingType.end(), reportingType); |
| 133 | if (found == supportedReportingType.end()) |
| 134 | { |
| 135 | throw sdbusplus::exception::SdBusError( |
| 136 | static_cast<int>(std::errc::invalid_argument), |
| 137 | "Invalid reportingType"); |
| 138 | } |
| 139 | |
| 140 | if (reportingType == "Periodic" && interval < minInterval) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 141 | { |
| 142 | throw sdbusplus::exception::SdBusError( |
| 143 | static_cast<int>(std::errc::invalid_argument), "Invalid interval"); |
| 144 | } |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 145 | |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 146 | if (readingParams.size() > maxReadingParams) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 147 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 148 | { |
| 149 | throw sdbusplus::exception::SdBusError( |
| 150 | static_cast<int>(std::errc::argument_list_too_long), |
| 151 | "Too many reading parameters"); |
| 152 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 153 | |
| 154 | try |
| 155 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 156 | namespace ts = utils::tstring; |
| 157 | |
| 158 | for (const LabeledMetricParameters& item : readingParams) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 159 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 160 | utils::toOperationType( |
| 161 | utils::toUnderlying(item.at_label<ts::OperationType>())); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | catch (const std::exception& e) |
| 165 | { |
| 166 | throw sdbusplus::exception::SdBusError( |
| 167 | static_cast<int>(std::errc::invalid_argument), e.what()); |
| 168 | } |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 169 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 170 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 171 | interfaces::Report& ReportManager::addReport( |
| 172 | boost::asio::yield_context& yield, const std::string& reportName, |
| 173 | const std::string& reportingType, const bool emitsReadingsUpdate, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 174 | const bool logToMetricReportsCollection, Milliseconds interval, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 175 | ReadingParameters metricParams, const bool enabled) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 176 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 177 | auto labeledMetricParams = |
| 178 | reportFactory->convertMetricParams(yield, metricParams); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 179 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 180 | return addReport(reportName, reportingType, emitsReadingsUpdate, |
| 181 | logToMetricReportsCollection, interval, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 182 | std::move(labeledMetricParams), enabled); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | interfaces::Report& ReportManager::addReport( |
| 186 | const std::string& reportName, const std::string& reportingType, |
| 187 | const bool emitsReadingsUpdate, const bool logToMetricReportsCollection, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 188 | Milliseconds interval, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 189 | std::vector<LabeledMetricParameters> labeledMetricParams, |
| 190 | const bool enabled) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 191 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 192 | verifyAddReport(reportName, reportingType, interval, labeledMetricParams); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 193 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 194 | reports.emplace_back( |
| 195 | reportFactory->make(reportName, reportingType, emitsReadingsUpdate, |
| 196 | logToMetricReportsCollection, interval, *this, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 197 | *reportStorage, labeledMetricParams, enabled)); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 198 | return *reports.back(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void ReportManager::loadFromPersistent() |
| 202 | { |
| 203 | std::vector<interfaces::JsonStorage::FilePath> paths = |
| 204 | reportStorage->list(); |
| 205 | |
| 206 | for (const auto& path : paths) |
| 207 | { |
| 208 | std::optional<nlohmann::json> data = reportStorage->load(path); |
| 209 | try |
| 210 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 211 | bool enabled = data->at("Enabled").get<bool>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 212 | size_t version = data->at("Version").get<size_t>(); |
| 213 | if (version != Report::reportVersion) |
| 214 | { |
| 215 | throw std::logic_error("Invalid version"); |
| 216 | } |
| 217 | std::string& name = data->at("Name").get_ref<std::string&>(); |
| 218 | std::string& reportingType = |
| 219 | data->at("ReportingType").get_ref<std::string&>(); |
| 220 | bool emitsReadingsSignal = |
| 221 | data->at("EmitsReadingsUpdate").get<bool>(); |
| 222 | bool logToMetricReportsCollection = |
| 223 | data->at("LogToMetricReportsCollection").get<bool>(); |
| 224 | uint64_t interval = data->at("Interval").get<uint64_t>(); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 225 | auto readingParameters = |
| 226 | data->at("ReadingParameters") |
| 227 | .get<std::vector<LabeledMetricParameters>>(); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 228 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 229 | addReport(name, reportingType, emitsReadingsSignal, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 230 | logToMetricReportsCollection, Milliseconds(interval), |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame^] | 231 | std::move(readingParameters), enabled); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 232 | } |
| 233 | catch (const std::exception& e) |
| 234 | { |
| 235 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 236 | "Failed to load report from storage", |
| 237 | phosphor::logging::entry( |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 238 | "FILENAME=%s", |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 239 | static_cast<std::filesystem::path>(path).c_str()), |
Wludzik, Jozef | 982c5b5 | 2021-01-02 12:05:21 +0100 | [diff] [blame] | 240 | phosphor::logging::entry("EXCEPTION_MSG=%s", e.what())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 241 | reportStorage->remove(path); |
| 242 | } |
| 243 | } |
| 244 | } |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 245 | |
| 246 | void ReportManager::updateReport(const std::string& name) |
| 247 | { |
| 248 | for (auto& report : reports) |
| 249 | { |
| 250 | if (report->getName() == name) |
| 251 | { |
| 252 | report->updateReadings(); |
| 253 | return; |
| 254 | } |
| 255 | } |
| 256 | } |