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