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