xref: /openbmc/phosphor-hwmon/average.hpp (revision 02e598ab)
1 #pragma once
2 
3 #include "sensorset.hpp"
4 
5 #include <optional>
6 #include <string>
7 #include <vector>
8 
9 /** @class AverageHandling
10  *  @brief Handle avergae value when AVERAGE_* is set in env
11  */
12 class Average
13 {
14   public:
15     /** @brief The key type of average_set */
16     using averageKey = SensorSet::key_type;
17 
18     /** @brief <average, average_interval>
19      *  average is the value of power*_average.
20      *  average_interval is the value of power*_average_interval.
21      */
22     using averageValue = std::pair<int64_t, int64_t>;
23 
24     /** @brief Store sensors' <averageKey, averageValue> map */
25     using averageMap = std::map<averageKey, averageValue>;
26 
27     /** @brief Get averageValue in averageMap based on averageKey.
28      *  This function will be called only when the env AVERAGE_xxx is set to
29      *  true.
30      *
31      *  @param[in] sensorKey - Sensor details
32      *
33      *  @return - Optional
34      *      return {}, if sensorKey can not be found in averageMap
35      *      return averageValue, if sensorKey can be found in averageMap
36      */
37     std::optional<averageValue>
38         getAverageValue(const averageKey& sensorKey) const;
39 
40     /** @brief Set average value in averageMap based on sensor key.
41      *  This function will be called only when the env AVERAGE_xxx is set to
42      *  true.
43      *
44      *  @param[in] sensorKey - Sensor details
45      *  @param[in] sensorValue - The related average values of this sensor
46      */
47     void setAverageValue(const averageKey& sensorKey,
48                          const averageValue& sensorValue);
49 
50     /** @brief Calculate the average value.
51      *
52      *  @param[in] preAverage - The previous average value from *_average file
53      *  @param[in] preInterval - The previous interval value from
54      *                           *_average_interval file
55      *  @param[in] curAverage - The current average value from *_average file
56      *  @param[in] curInterval - The current interval value from
57      *                           *_average_interval file
58      *
59      *  @return value - Optional
60      *      return {}, if curInterval-preInterval=0
61      *      return new calculated average value, if curInterval-preInterval>0
62      */
63     static std::optional<int64_t>
64         calcAverage(int64_t preAverage, int64_t preInterval, int64_t curAverage,
65                     int64_t curInterval);
66 
67   private:
68     /** @brief Store the previous average sensor map */
69     averageMap _previousAverageMap;
70 };
71