Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1 | #include "metrics/collection_function.hpp" |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 2 | |
| 3 | #include <cmath> |
| 4 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 5 | namespace metrics |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 6 | { |
| 7 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 8 | class FunctionMinimum : public CollectionFunction |
| 9 | { |
| 10 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 11 | double calculate(const std::vector<ReadingItem>& readings, |
| 12 | Milliseconds) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 13 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame^] | 14 | return std::min_element(readings.begin(), readings.end(), |
| 15 | [](const auto& left, const auto& right) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 16 | return std::make_tuple(!std::isfinite(left.second), left.second) < |
| 17 | std::make_tuple(!std::isfinite(right.second), right.second); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame^] | 18 | })->second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 21 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 22 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 23 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 24 | readings.assign( |
| 25 | {ReadingItem(timestamp, calculate(readings, timestamp))}); |
| 26 | return readings.back().second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 27 | } |
| 28 | }; |
| 29 | |
| 30 | class FunctionMaximum : public CollectionFunction |
| 31 | { |
| 32 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 33 | double calculate(const std::vector<ReadingItem>& readings, |
| 34 | Milliseconds) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 35 | { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame^] | 36 | return std::max_element(readings.begin(), readings.end(), |
| 37 | [](const auto& left, const auto& right) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 38 | return std::make_tuple(std::isfinite(left.second), left.second) < |
| 39 | std::make_tuple(std::isfinite(right.second), right.second); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame^] | 40 | })->second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 43 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 44 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 45 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 46 | readings.assign( |
| 47 | {ReadingItem(timestamp, calculate(readings, timestamp))}); |
| 48 | return readings.back().second; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
| 52 | class FunctionAverage : public CollectionFunction |
| 53 | { |
| 54 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 55 | double calculate(const std::vector<ReadingItem>& readings, |
| 56 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 57 | { |
| 58 | auto valueSum = 0.0; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 59 | auto timeSum = Milliseconds{0}; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 60 | for (auto it = readings.begin(); it != std::prev(readings.end()); ++it) |
| 61 | { |
| 62 | if (std::isfinite(it->second)) |
| 63 | { |
| 64 | const auto kt = std::next(it); |
| 65 | const auto duration = kt->first - it->first; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 66 | valueSum += it->second * duration.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 67 | timeSum += duration; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const auto duration = timestamp - readings.back().first; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 72 | valueSum += readings.back().second * duration.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 73 | timeSum += duration; |
| 74 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 75 | return valueSum / std::max(timeSum.count(), uint64_t{1u}); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 78 | double calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 79 | Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 80 | { |
| 81 | auto result = calculate(readings, timestamp); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 82 | if (std::isfinite(result)) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 83 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 84 | readings.assign({ReadingItem(readings.front().first, result), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 85 | ReadingItem(timestamp, readings.back().second)}); |
| 86 | } |
| 87 | return result; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | class FunctionSummation : public CollectionFunction |
| 92 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 93 | using Multiplier = std::chrono::duration<double>; |
| 94 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 95 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 96 | double calculate(const std::vector<ReadingItem>& readings, |
| 97 | const Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 98 | { |
| 99 | auto valueSum = 0.0; |
| 100 | for (auto it = readings.begin(); it != std::prev(readings.end()); ++it) |
| 101 | { |
| 102 | if (std::isfinite(it->second)) |
| 103 | { |
| 104 | const auto kt = std::next(it); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 105 | const auto multiplier = |
| 106 | calculateMultiplier(kt->first - it->first); |
| 107 | valueSum += it->second * multiplier.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 111 | const auto multiplier = |
| 112 | calculateMultiplier(timestamp - readings.back().first); |
| 113 | valueSum += readings.back().second * multiplier.count(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 114 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 115 | return valueSum; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 118 | double |
| 119 | calculateForStartupInterval(std::vector<ReadingItem>& readings, |
| 120 | const Milliseconds timestamp) const override |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 121 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 122 | const auto result = calculate(readings, timestamp); |
| 123 | if (readings.size() > 2 && std::isfinite(result)) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 124 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 125 | const auto multiplier = |
| 126 | calculateMultiplier(timestamp - readings.front().first).count(); |
| 127 | if (multiplier > 0.) |
| 128 | { |
| 129 | const auto prevValue = result / multiplier; |
| 130 | readings.assign( |
| 131 | {ReadingItem(readings.front().first, prevValue), |
| 132 | ReadingItem(timestamp, readings.back().second)}); |
| 133 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 134 | } |
| 135 | return result; |
| 136 | } |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | static constexpr Multiplier calculateMultiplier(Milliseconds duration) |
| 140 | { |
| 141 | constexpr auto m = Multiplier{Seconds{1}}; |
| 142 | return Multiplier{duration / m}; |
| 143 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | std::shared_ptr<CollectionFunction> |
| 147 | makeCollectionFunction(OperationType operationType) |
| 148 | { |
| 149 | using namespace std::string_literals; |
| 150 | |
| 151 | switch (operationType) |
| 152 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 153 | case OperationType::min: |
| 154 | return std::make_shared<FunctionMinimum>(); |
| 155 | case OperationType::max: |
| 156 | return std::make_shared<FunctionMaximum>(); |
| 157 | case OperationType::avg: |
| 158 | return std::make_shared<FunctionAverage>(); |
| 159 | case OperationType::sum: |
| 160 | return std::make_shared<FunctionSummation>(); |
| 161 | default: |
| 162 | throw std::runtime_error("op: "s + |
| 163 | utils::enumToString(operationType) + |
| 164 | " is not supported"s); |
| 165 | } |
| 166 | } |
| 167 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 168 | } // namespace metrics |