xref: /openbmc/telemetry/src/report.hpp (revision c1dbac16fe3c705b773155f607a4541cf2ad79fa)
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 
37         OnChangeContext(const OnChangeContext&) = delete;
38         OnChangeContext& operator=(const OnChangeContext&) = delete;
39         OnChangeContext(OnChangeContext&&) = delete;
40         OnChangeContext& operator=(OnChangeContext&&) = delete;
~OnChangeContext()41         ~OnChangeContext()
42         {
43             if (updated)
44             {
45                 report.updateReadings();
46             }
47         }
48 
metricUpdated()49         void metricUpdated()
50         {
51             updated = true;
52         }
53 
54       private:
55         Report& report;
56         bool updated = false;
57     };
58 
59   public:
60     Report(boost::asio::io_context& ioc,
61            const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
62            const std::string& reportId, const std::string& reportName,
63            const ReportingType reportingType,
64            std::vector<ReportAction> reportActions, const Milliseconds period,
65            const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
66            interfaces::ReportManager& reportManager,
67            interfaces::JsonStorage& reportStorage,
68            std::vector<std::shared_ptr<interfaces::Metric>> metrics,
69            const interfaces::ReportFactory& reportFactory, const bool enabled,
70            std::unique_ptr<interfaces::Clock> clock, Readings);
71     ~Report();
72 
73     Report(const Report&) = delete;
74     Report(Report&&) = delete;
75     Report& operator=(const Report&) = delete;
76     Report& operator=(Report&&) = delete;
77 
getId() const78     std::string getId() const override
79     {
80         return id;
81     }
82 
getPath() const83     std::string getPath() const override
84     {
85         return path.str;
86     }
87 
88     void metricUpdated() override;
89 
90     void activate();
91     void deactivate();
92 
93   private:
94     std::unique_ptr<sdbusplus::asio::dbus_interface> makeReportInterface(
95         const interfaces::ReportFactory& reportFactory);
96     static void timerProcForPeriodicReport(boost::system::error_code,
97                                            Report& self);
98     static void timerProcForOnChangeReport(boost::system::error_code,
99                                            Report& self);
100     void scheduleTimerForPeriodicReport(Milliseconds interval);
101     void scheduleTimerForOnChangeReport();
102     uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
103                               const ReportingType reportingTypeIn) const;
104     void setReadingBuffer(const ReportUpdates newReportUpdates);
105     void setReportUpdates(const ReportUpdates newReportUpdates);
106     static uint64_t getMetricCount(
107         const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
108     interfaces::JsonStorage::FilePath reportFileName() const;
109     std::unordered_set<std::string> collectTriggerIds(
110         boost::asio::io_context& ioc) const;
111     bool storeConfiguration() const;
112     bool shouldStoreMetricValues() const;
113     void updateReadings();
114     void scheduleTimer();
115     static std::vector<ErrorMessage> verify(ReportingType, Milliseconds);
116 
117     std::string id;
118     const sdbusplus::message::object_path path;
119     std::string name;
120     ReportingType reportingType;
121     Milliseconds interval;
122     std::unordered_set<ReportAction> reportActions;
123     ReadingParameters readingParameters;
124     bool persistency = false;
125     uint64_t metricCount;
126     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 
145   public:
146     static constexpr const char* reportIfaceName =
147         "xyz.openbmc_project.Telemetry.Report";
148     static constexpr const char* deleteIfaceName =
149         "xyz.openbmc_project.Object.Delete";
150     static constexpr size_t reportVersion = 7;
151 };
152