George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 1 | #include "calculate.hpp" |
| 2 | |
Jayanth Othayoth | bfb1721 | 2024-10-15 10:52:15 -0500 | [diff] [blame] | 3 | #include <algorithm> |
George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 4 | #include <limits> |
George Liu | 4e6081b | 2024-09-19 10:09:04 +0800 | [diff] [blame] | 5 | #include <numeric> |
George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 6 | |
| 7 | namespace phosphor::virtual_sensor |
| 8 | { |
| 9 | |
| 10 | double calculateModifiedMedianValue(std::vector<double>& values) |
| 11 | { |
| 12 | size_t size = values.size(); |
| 13 | std::sort(values.begin(), values.end()); |
| 14 | switch (size) |
| 15 | { |
| 16 | case 2: |
| 17 | /* Choose biggest value */ |
| 18 | return values.at(1); |
| 19 | case 0: |
| 20 | return std::numeric_limits<double>::quiet_NaN(); |
| 21 | default: |
| 22 | /* Choose median value */ |
| 23 | if (size % 2 == 0) |
| 24 | { |
| 25 | // Average of the two middle values |
| 26 | return (values.at(size / 2) + values.at(size / 2 - 1)) / 2; |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | return values.at((size - 1) / 2); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | double calculateMaximumValue(std::vector<double>& values) |
| 36 | { |
| 37 | auto maxIt = std::max_element(values.begin(), values.end()); |
| 38 | if (maxIt == values.end()) |
| 39 | { |
| 40 | return std::numeric_limits<double>::quiet_NaN(); |
| 41 | } |
| 42 | return *maxIt; |
| 43 | } |
| 44 | |
George Liu | c1f822c | 2024-08-28 17:26:03 +0800 | [diff] [blame] | 45 | double calculateMinimumValue(std::vector<double>& values) |
| 46 | { |
| 47 | auto maxIt = std::min_element(values.begin(), values.end()); |
| 48 | if (maxIt == values.end()) |
| 49 | { |
| 50 | return std::numeric_limits<double>::quiet_NaN(); |
| 51 | } |
| 52 | return *maxIt; |
| 53 | } |
| 54 | |
George Liu | 4e6081b | 2024-09-19 10:09:04 +0800 | [diff] [blame] | 55 | double calculateSumValue(std::vector<double>& values) |
| 56 | { |
| 57 | if (values.empty()) |
| 58 | { |
| 59 | return std::numeric_limits<double>::quiet_NaN(); |
| 60 | } |
| 61 | return std::accumulate(values.begin(), values.end(), 0.0); |
| 62 | } |
| 63 | |
George Liu | 60fab69 | 2024-09-19 10:33:15 +0800 | [diff] [blame] | 64 | double calculateAverageValue(std::vector<double>& values) |
| 65 | { |
| 66 | if (values.empty()) |
| 67 | { |
| 68 | return std::numeric_limits<double>::quiet_NaN(); |
| 69 | } |
| 70 | return std::accumulate(values.begin(), values.end(), 0.0) / values.size(); |
| 71 | } |
| 72 | |
George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 73 | std::map<Interface, CalculationFunc> calculationIfaces{ |
George Liu | 60fab69 | 2024-09-19 10:33:15 +0800 | [diff] [blame] | 74 | {"xyz.openbmc_project.Configuration.Average", calculateAverageValue}, |
George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 75 | {"xyz.openbmc_project.Configuration.Maximum", calculateMaximumValue}, |
George Liu | c1f822c | 2024-08-28 17:26:03 +0800 | [diff] [blame] | 76 | {"xyz.openbmc_project.Configuration.Minimum", calculateMinimumValue}, |
George Liu | 4e6081b | 2024-09-19 10:09:04 +0800 | [diff] [blame] | 77 | {"xyz.openbmc_project.Configuration.Sum", calculateSumValue}, |
George Liu | 7f41a0d | 2024-08-28 16:49:06 +0800 | [diff] [blame] | 78 | {"xyz.openbmc_project.Configuration.ModifiedMedian", |
| 79 | calculateModifiedMedianValue}}; |
| 80 | |
| 81 | } // namespace phosphor::virtual_sensor |