-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I needed this for an ingress/egress chart and was surprised we didn't have that yet. This was easy enough to implement, so I just went ahead and did it.
- Loading branch information
1 parent
a34ec47
commit c5996ca
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// _ _____ __________ | ||
// | | / / _ | / __/_ __/ Visibility | ||
// | |/ / __ |_\ \ / / Across | ||
// |___/_/ |_/___/ /_/ Space and Time | ||
// | ||
// SPDX-FileCopyrightText: (c) 2024 The Tenzir Contributors | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <tenzir/aggregation_function.hpp> | ||
#include <tenzir/plugin.hpp> | ||
|
||
namespace tenzir::plugins::mean { | ||
|
||
namespace { | ||
|
||
template <basic_type Type> | ||
class mean_function final : public aggregation_function { | ||
public: | ||
explicit mean_function(type input_type) noexcept | ||
: aggregation_function(std::move(input_type)) { | ||
// nop | ||
} | ||
|
||
private: | ||
auto output_type() const -> type override { | ||
return type{double_type{}}; | ||
} | ||
|
||
auto add(const data_view& view) -> void override { | ||
using view_type = tenzir::view<type_to_data_t<Type>>; | ||
if (caf::holds_alternative<caf::none_t>(view)) { | ||
return; | ||
} | ||
count_ += 1; | ||
mean_ += (static_cast<double>(caf::get<view_type>(view)) - mean_) / count_; | ||
} | ||
|
||
void add(const arrow::Array& array) override { | ||
const auto& typed_array = caf::get<type_to_arrow_array_t<Type>>(array); | ||
for (auto&& value : values(Type{}, typed_array)) { | ||
if (not value) { | ||
continue; | ||
} | ||
count_ += 1; | ||
mean_ += (static_cast<double>(*value) - mean_) / count_; | ||
} | ||
} | ||
|
||
auto finish() && -> caf::expected<data> override { | ||
if (count_ == 0) { | ||
return data{}; | ||
} | ||
return data{mean_}; | ||
} | ||
|
||
double mean_ = {}; | ||
size_t count_ = {}; | ||
}; | ||
|
||
class plugin : public virtual aggregation_function_plugin { | ||
caf::error initialize([[maybe_unused]] const record& plugin_config, | ||
[[maybe_unused]] const record& global_config) override { | ||
return {}; | ||
} | ||
|
||
[[nodiscard]] std::string name() const override { | ||
return "mean"; | ||
}; | ||
|
||
[[nodiscard]] caf::expected<std::unique_ptr<aggregation_function>> | ||
make_aggregation_function(const type& input_type) const override { | ||
auto f = detail::overload{ | ||
[&](const uint64_type&) | ||
-> caf::expected<std::unique_ptr<aggregation_function>> { | ||
return std::make_unique<mean_function<uint64_type>>(input_type); | ||
}, | ||
[&](const int64_type&) | ||
-> caf::expected<std::unique_ptr<aggregation_function>> { | ||
return std::make_unique<mean_function<int64_type>>(input_type); | ||
}, | ||
[&](const double_type&) | ||
-> caf::expected<std::unique_ptr<aggregation_function>> { | ||
return std::make_unique<mean_function<double_type>>(input_type); | ||
}, | ||
[](const concrete_type auto& type) | ||
-> caf::expected<std::unique_ptr<aggregation_function>> { | ||
return caf::make_error(ec::invalid_configuration, | ||
fmt::format("mean aggregation function does not " | ||
"support type {}", | ||
type)); | ||
}, | ||
}; | ||
return caf::visit(f, input_type); | ||
} | ||
|
||
auto aggregation_default() const -> data override { | ||
return caf::none; | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
} // namespace tenzir::plugins::mean | ||
|
||
TENZIR_REGISTER_PLUGIN(tenzir::plugins::mean::plugin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters