blob: 6d8c6b41781269a69126c5e256a1901807710323 [file] [log] [blame]
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02001#pragma once
2
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01003#include "interfaces/clock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "interfaces/json_storage.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02005#include "interfaces/metric.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +01006#include "interfaces/metric_listener.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "interfaces/report.hpp"
Szymon Dompkefdb06a12022-02-11 11:04:44 +01008#include "interfaces/report_factory.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "interfaces/report_manager.hpp"
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010010#include "types/readings.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010011#include "types/report_action.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000012#include "types/report_types.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010013#include "types/report_updates.hpp"
14#include "types/reporting_type.hpp"
Szymon Dompke3eb56862021-09-20 15:32:04 +020015#include "utils/circular_vector.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010016#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010017#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020018
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020019#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020020#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020021#include <sdbusplus/asio/object_server.hpp>
22
23#include <chrono>
24#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010025#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020026
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010027class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020028{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010029 class OnChangeContext
30 {
31 public:
32 OnChangeContext(Report& report) : report(report)
33 {}
34
35 ~OnChangeContext()
36 {
37 if (updated)
38 {
39 report.updateReadings();
40 }
41 }
42
43 void metricUpdated()
44 {
45 updated = true;
46 }
47
48 private:
49 Report& report;
50 bool updated = false;
51 };
52
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020053 public:
54 Report(boost::asio::io_context& ioc,
55 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010056 const std::string& reportId, const std::string& reportName,
57 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010058 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020059 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020060 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010061 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020062 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010063 const interfaces::ReportFactory& reportFactory, const bool enabled,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010064 std::unique_ptr<interfaces::Clock> clock, Readings);
65 ~Report();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020066
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020067 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020068 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020069 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020070 Report& operator=(Report&&) = delete;
71
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010072 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020073 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010074 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020075 }
76
77 std::string getPath() const override
78 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010079 return reportDir + id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020080 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020081
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010082 void metricUpdated() override;
83
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020084 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010085 std::unique_ptr<sdbusplus::asio::dbus_interface>
86 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010087 static void timerProcForPeriodicReport(boost::system::error_code,
88 Report& self);
89 static void timerProcForOnChangeReport(boost::system::error_code,
90 Report& self);
91 void scheduleTimerForPeriodicReport(Milliseconds interval);
92 void scheduleTimerForOnChangeReport();
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +010093 std::optional<uint64_t>
94 deduceAppendLimit(const uint64_t appendLimitIn) const;
Szymon Dompke3eb56862021-09-20 15:32:04 +020095 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
96 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +010097 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +020098 void setReportUpdates(const ReportUpdates newReportUpdates);
99 static uint64_t getSensorCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100100 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100101 interfaces::JsonStorage::FilePath reportFileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100102 std::unordered_set<std::string>
103 collectTriggerIds(boost::asio::io_context& ioc) const;
104 bool storeConfiguration() const;
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100105 bool shouldStoreMetricValues() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100106 void updateReadings();
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100107 void updateReportingType(ReportingType);
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200108
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100109 std::string id;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200110 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200111 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000112 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100113 std::unordered_set<ReportAction> reportActions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000114 ReadingParametersPastVersion readingParametersPastVersion;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100115 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000116 bool persistency = false;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200117 uint64_t sensorCount;
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100118 std::optional<uint64_t> appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200119 ReportUpdates reportUpdates;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200120 Readings readings = {};
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100121 CircularVector<ReadingData> readingsBuffer;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200122 std::shared_ptr<sdbusplus::asio::object_server> objServer;
123 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
124 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200125 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
126 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100127 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200128
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100129 interfaces::JsonStorage& reportStorage;
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200130 bool enabled;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100131 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100132 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100133 std::optional<OnChangeContext> onChangeContext;
134 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100135
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200136 public:
137 static constexpr const char* reportIfaceName =
138 "xyz.openbmc_project.Telemetry.Report";
139 static constexpr const char* reportDir =
140 "/xyz/openbmc_project/Telemetry/Reports/";
141 static constexpr const char* deleteIfaceName =
142 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100143 static constexpr size_t reportVersion = 6;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200144};