xref: /openbmc/telemetry/src/report.hpp (revision 493e62eb2f2ff6cbf3282b9cb98ebe6e520b88fc)
1 #pragma once
2 
3 #include "interfaces/clock.hpp"
4 #include "interfaces/json_storage.hpp"
5 #include "interfaces/metric.hpp"
6 #include "interfaces/metric_listener.hpp"
7 #include "interfaces/report.hpp"
8 #include "interfaces/report_factory.hpp"
9 #include "interfaces/report_manager.hpp"
10 #include "types/readings.hpp"
11 #include "types/report_action.hpp"
12 #include "types/report_types.hpp"
13 #include "types/report_updates.hpp"
14 #include "types/reporting_type.hpp"
15 #include "utils/circular_vector.hpp"
16 #include "utils/ensure.hpp"
17 #include "utils/messanger.hpp"
18 
19 #include <boost/asio/io_context.hpp>
20 #include <boost/asio/steady_timer.hpp>
21 #include <sdbusplus/asio/object_server.hpp>
22 
23 #include <chrono>
24 #include <memory>
25 #include <unordered_set>
26 
27 class Report : public interfaces::Report, public interfaces::MetricListener
28 {
29     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 
53   public:
54     Report(boost::asio::io_context& ioc,
55            const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
56            const std::string& reportId, const std::string& reportName,
57            const ReportingType reportingType,
58            std::vector<ReportAction> reportActions, const Milliseconds period,
59            const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
60            interfaces::ReportManager& reportManager,
61            interfaces::JsonStorage& reportStorage,
62            std::vector<std::shared_ptr<interfaces::Metric>> metrics,
63            const interfaces::ReportFactory& reportFactory, const bool enabled,
64            std::unique_ptr<interfaces::Clock> clock, Readings);
65     ~Report();
66 
67     Report(const Report&) = delete;
68     Report(Report&&) = delete;
69     Report& operator=(const Report&) = delete;
70     Report& operator=(Report&&) = delete;
71 
72     std::string getId() const override
73     {
74         return id;
75     }
76 
77     std::string getPath() const override
78     {
79         return reportDir + id;
80     }
81 
82     void metricUpdated() override;
83 
84   private:
85     std::unique_ptr<sdbusplus::asio::dbus_interface>
86         makeReportInterface(const interfaces::ReportFactory& reportFactory);
87     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();
93     std::optional<uint64_t>
94         deduceAppendLimit(const uint64_t appendLimitIn) const;
95     uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
96                               const ReportingType reportingTypeIn) const;
97     void setReadingBuffer(const ReportUpdates newReportUpdates);
98     void setReportUpdates(const ReportUpdates newReportUpdates);
99     static uint64_t getSensorCount(
100         const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
101     interfaces::JsonStorage::FilePath reportFileName() const;
102     std::unordered_set<std::string>
103         collectTriggerIds(boost::asio::io_context& ioc) const;
104     bool storeConfiguration() const;
105     bool shouldStoreMetricValues() const;
106     void updateReadings();
107     void updateReportingType(ReportingType);
108 
109     std::string id;
110     std::string name;
111     ReportingType reportingType;
112     Milliseconds interval;
113     std::unordered_set<ReportAction> reportActions;
114     ReadingParametersPastVersion readingParametersPastVersion;
115     ReadingParameters readingParameters;
116     bool persistency = false;
117     uint64_t sensorCount;
118     std::optional<uint64_t> appendLimit;
119     ReportUpdates reportUpdates;
120     Readings readings = {};
121     CircularVector<ReadingData> readingsBuffer;
122     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;
125     std::vector<std::shared_ptr<interfaces::Metric>> metrics;
126     boost::asio::steady_timer timer;
127     std::unordered_set<std::string> triggerIds;
128 
129     interfaces::JsonStorage& reportStorage;
130     bool enabled;
131     std::unique_ptr<interfaces::Clock> clock;
132     utils::Messanger messanger;
133     std::optional<OnChangeContext> onChangeContext;
134     utils::Ensure<std::function<void()>> unregisterFromMetrics;
135 
136   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";
143     static constexpr size_t reportVersion = 6;
144 };
145