Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 1 | #include "report_manager.hpp" |
| 2 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 3 | #include <sdbusplus/exception.hpp> |
| 4 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 5 | #include <system_error> |
| 6 | |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 7 | ReportManager::ReportManager( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 8 | std::unique_ptr<interfaces::ReportFactory> reportFactoryIn, |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 9 | const std::shared_ptr<sdbusplus::asio::object_server>& objServerIn) : |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | reportFactory(std::move(reportFactoryIn)), |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 11 | objServer(objServerIn) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 12 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 13 | reports.reserve(maxReports); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 14 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 15 | reportManagerIface = objServer->add_unique_interface( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 16 | reportManagerPath, reportManagerIfaceName, [this](auto& dbusIface) { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 17 | dbusIface.register_property_r( |
| 18 | "MaxReports", uint32_t{}, sdbusplus::vtable::property_::const_, |
| 19 | [](const auto&) { return maxReports; }); |
| 20 | dbusIface.register_property_r( |
| 21 | "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_, |
| 22 | [](const auto&) -> uint64_t { return minInterval.count(); }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 23 | |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 24 | dbusIface.register_method( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 25 | "AddReport", [this](const std::string& reportName, |
| 26 | const std::string& reportingType, |
| 27 | const bool emitsReadingsUpdate, |
| 28 | const bool logToMetricReportsCollection, |
| 29 | const uint64_t interval, |
| 30 | const ReadingParameters& metricParams) { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 31 | if (reports.size() >= maxReports) |
| 32 | { |
| 33 | throw sdbusplus::exception::SdBusError( |
| 34 | static_cast<int>(std::errc::too_many_files_open), |
| 35 | "Reached maximal report count"); |
| 36 | } |
| 37 | |
| 38 | for (const auto& report : reports) |
| 39 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 40 | if (report->getName() == reportName) |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 41 | { |
| 42 | throw sdbusplus::exception::SdBusError( |
| 43 | static_cast<int>(std::errc::file_exists), |
| 44 | "Duplicate report"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | std::chrono::milliseconds reportInterval{interval}; |
| 49 | if (reportInterval < minInterval) |
| 50 | { |
| 51 | throw sdbusplus::exception::SdBusError( |
| 52 | static_cast<int>(std::errc::invalid_argument), |
| 53 | "Invalid interval"); |
| 54 | } |
| 55 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 56 | reports.emplace_back(reportFactory->make( |
| 57 | reportName, reportingType, emitsReadingsUpdate, |
| 58 | logToMetricReportsCollection, std::move(reportInterval), |
| 59 | metricParams, *this)); |
| 60 | return reports.back()->getPath(); |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 61 | }); |
| 62 | }); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 65 | void ReportManager::removeReport(const interfaces::Report* report) |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 66 | { |
Wludzik, Jozef | cb88cfd | 2020-09-28 16:38:57 +0200 | [diff] [blame] | 67 | reports.erase( |
| 68 | std::remove_if(reports.begin(), reports.end(), |
| 69 | [report](const auto& x) { return report == x.get(); }), |
| 70 | reports.end()); |
Krzysztof Grobelny | 64b75a5 | 2020-09-18 10:17:16 +0200 | [diff] [blame] | 71 | } |