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