xref: /openbmc/telemetry/src/report.hpp (revision 32305f14)
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 "state.hpp"
11 #include "types/error_message.hpp"
12 #include "types/readings.hpp"
13 #include "types/report_action.hpp"
14 #include "types/report_types.hpp"
15 #include "types/report_updates.hpp"
16 #include "types/reporting_type.hpp"
17 #include "utils/circular_vector.hpp"
18 #include "utils/dbus_path_utils.hpp"
19 #include "utils/ensure.hpp"
20 #include "utils/messanger.hpp"
21 
22 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/steady_timer.hpp>
24 #include <sdbusplus/asio/object_server.hpp>
25 
26 #include <chrono>
27 #include <memory>
28 #include <unordered_set>
29 
30 class Report : public interfaces::Report, public interfaces::MetricListener
31 {
32     class OnChangeContext
33     {
34       public:
35         OnChangeContext(Report& report) : report(report)
36         {}
37 
38         ~OnChangeContext()
39         {
40             if (updated)
41             {
42                 report.updateReadings();
43             }
44         }
45 
46         void metricUpdated()
47         {
48             updated = true;
49         }
50 
51       private:
52         Report& report;
53         bool updated = false;
54     };
55 
56   public:
57     Report(boost::asio::io_context& ioc,
58            const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
59            const std::string& reportId, const std::string& reportName,
60            const ReportingType reportingType,
61            std::vector<ReportAction> reportActions, const Milliseconds period,
62            const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
63            interfaces::ReportManager& reportManager,
64            interfaces::JsonStorage& reportStorage,
65            std::vector<std::shared_ptr<interfaces::Metric>> metrics,
66            const interfaces::ReportFactory& reportFactory, const bool enabled,
67            std::unique_ptr<interfaces::Clock> clock, Readings);
68     ~Report();
69 
70     Report(const Report&) = delete;
71     Report(Report&&) = delete;
72     Report& operator=(const Report&) = delete;
73     Report& operator=(Report&&) = delete;
74 
75     std::string getId() const override
76     {
77         return id;
78     }
79 
80     std::string getPath() const override
81     {
82         return path.str;
83     }
84 
85     void metricUpdated() override;
86 
87     void activate();
88     void deactivate();
89 
90   private:
91     std::unique_ptr<sdbusplus::asio::dbus_interface>
92         makeReportInterface(const interfaces::ReportFactory& reportFactory);
93     static void timerProcForPeriodicReport(boost::system::error_code,
94                                            Report& self);
95     static void timerProcForOnChangeReport(boost::system::error_code,
96                                            Report& self);
97     void scheduleTimerForPeriodicReport(Milliseconds interval);
98     void scheduleTimerForOnChangeReport();
99     std::optional<uint64_t>
100         deduceAppendLimit(const uint64_t appendLimitIn) const;
101     uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
102                               const ReportingType reportingTypeIn) const;
103     void setReadingBuffer(const ReportUpdates newReportUpdates);
104     void setReportUpdates(const ReportUpdates newReportUpdates);
105     static uint64_t getSensorCount(
106         const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
107     interfaces::JsonStorage::FilePath reportFileName() const;
108     std::unordered_set<std::string>
109         collectTriggerIds(boost::asio::io_context& ioc) const;
110     bool storeConfiguration() const;
111     bool shouldStoreMetricValues() const;
112     void updateReadings();
113     void scheduleTimer();
114     std::vector<ErrorMessage> verify() const;
115 
116     std::string id;
117     const sdbusplus::message::object_path path;
118     std::string name;
119     ReportingType reportingType;
120     Milliseconds interval;
121     std::unordered_set<ReportAction> reportActions;
122     ReadingParametersPastVersion readingParametersPastVersion;
123     ReadingParameters readingParameters;
124     bool persistency = false;
125     uint64_t sensorCount;
126     std::optional<uint64_t> appendLimit;
127     ReportUpdates reportUpdates;
128     Readings readings = {};
129     CircularVector<ReadingData> readingsBuffer;
130     std::shared_ptr<sdbusplus::asio::object_server> objServer;
131     std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
132     std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
133     std::vector<std::shared_ptr<interfaces::Metric>> metrics;
134     boost::asio::steady_timer timer;
135     std::unordered_set<std::string> triggerIds;
136 
137     interfaces::JsonStorage& reportStorage;
138     std::unique_ptr<interfaces::Clock> clock;
139     utils::Messanger messanger;
140     std::optional<OnChangeContext> onChangeContext;
141     utils::Ensure<std::function<void()>> unregisterFromMetrics;
142     State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{
143         *this};
144     std::vector<ErrorMessage> errorMessages;
145 
146   public:
147     static constexpr const char* reportIfaceName =
148         "xyz.openbmc_project.Telemetry.Report";
149     static constexpr const char* deleteIfaceName =
150         "xyz.openbmc_project.Object.Delete";
151     static constexpr size_t reportVersion = 6;
152 };
153