-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(combinator): displayExpression with prefix notation #21
Open
floork
wants to merge
6
commits into
metricq:master
Choose a base branch
from
floork:displayExpression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48fc443
feat(combinator): displayExpression with prefix notation
floork 50e1130
add(combinator): displayExpression aggregation handling
floork a47b2e9
change(combinator): display as inline
floork 438f391
add(tests): test cases for displayExpression
floork d904680
fix(test_display_expression): clearer test cases
floork 5fab0d1
fix(combinator): catch and log Display Expression errors
floork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
#include <fmt/format.h> | ||
|
||
#include <numeric> | ||
#include <stdexcept> | ||
#include <unordered_set> | ||
|
||
using Log = metricq::logger::nitro::Log; | ||
|
||
|
@@ -49,6 +51,112 @@ Combinator::~Combinator() | |
{ | ||
} | ||
|
||
std::string handleBasicExpression(const nlohmann::json& expression) | ||
{ | ||
if (expression.is_number()) | ||
{ | ||
auto value = expression.get<double>(); | ||
return (value == static_cast<int>(value)) ? std::to_string(static_cast<int>(value)) : | ||
std::to_string(value); | ||
} | ||
|
||
if (expression.is_string()) | ||
{ | ||
return expression.get<std::string>(); | ||
} | ||
|
||
throw std::runtime_error("Expression is not a basic type (number or string)!"); | ||
} | ||
|
||
std::string handleOperatorExpression(const std::string& operation, const std::string& leftStr, | ||
const std::string& rightStr) | ||
{ | ||
if (operation.size() > 1) | ||
{ | ||
throw std::logic_error("Invalid operator length!"); | ||
} | ||
|
||
switch (operation[0]) | ||
{ | ||
case '+': | ||
case '-': | ||
case '*': | ||
case '/': | ||
return "(" + leftStr + " " + operation + " " + rightStr + ")"; | ||
default: | ||
throw std::runtime_error("Invalid operator: " + operation); | ||
} | ||
} | ||
|
||
std::string handleCombinationExpression(const std::string& operation, | ||
const std::vector<std::string>& inputs) | ||
{ | ||
static const std::unordered_set<std::string> validAggregates = { "sum", "min", "max" }; | ||
|
||
if (validAggregates.find(operation) == validAggregates.end()) | ||
{ | ||
throw std::runtime_error("Invalid aggregate operation: " + operation); | ||
} | ||
|
||
if (inputs.empty()) | ||
{ | ||
throw std::logic_error("Aggregate operation missing inputs!"); | ||
} | ||
|
||
auto input = std::accumulate(std::next(inputs.begin()), inputs.end(), inputs[0], | ||
[](std::string a, const std::string& b) { return a + ", " + b; }); | ||
|
||
return operation + "[" + input + "]"; | ||
} | ||
|
||
std::string Combinator::displayExpression(const nlohmann::json& expression) | ||
{ | ||
if (expression.is_number() || expression.is_string()) | ||
{ | ||
return handleBasicExpression(expression); | ||
} | ||
|
||
if (!expression.is_object() || !expression.contains("operation")) | ||
{ | ||
throw std::runtime_error("Unknown expression format!"); | ||
} | ||
|
||
std::string operation = expression.value("operation", ""); | ||
|
||
if (operation == "throttle") | ||
{ | ||
if (!expression.contains("input")) | ||
{ | ||
throw std::logic_error("Throttle does not contain a input"); | ||
} | ||
return handleBasicExpression(expression["input"]); | ||
} | ||
|
||
if (expression.contains("left") && expression.contains("right")) | ||
{ | ||
std::string leftStr = displayExpression(expression["left"]); | ||
std::string rightStr = displayExpression(expression["right"]); | ||
return handleOperatorExpression(operation, leftStr, rightStr); | ||
} | ||
|
||
if (expression.contains("inputs")) | ||
{ | ||
if (!expression["inputs"].is_array()) | ||
{ | ||
throw std::logic_error("Inputs must be an array!"); | ||
} | ||
|
||
std::vector<std::string> inputStrings; | ||
for (const auto& input : expression["inputs"]) | ||
{ | ||
inputStrings.push_back(displayExpression(input)); | ||
} | ||
return handleCombinationExpression(operation, inputStrings); | ||
} | ||
|
||
throw std::runtime_error("Unsupported operation type: " + operation); | ||
} | ||
|
||
void Combinator::on_transformer_config(const metricq::json& config) | ||
{ | ||
input_metrics.clear(); | ||
|
@@ -93,6 +201,15 @@ void Combinator::on_transformer_config(const metricq::json& config) | |
// Register the combined metric as a new source metric | ||
auto& metric = (*this)[combined_name]; | ||
|
||
try | ||
{ | ||
metric.metadata["displayExpression"] = displayExpression(combined_expression); | ||
} | ||
catch (std::runtime_error&) | ||
{ | ||
Log::error("Failed to create the Display Expression"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add context to the error message.
|
||
} | ||
|
||
if (combined_config.count("chunk_size")) | ||
{ | ||
auto chunk_size = combined_config["chunk_size"].get<int>(); | ||
|
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
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
add_executable(metricq-combinator.test_single_derivation test_single_derivation.cpp) | ||
add_test(metricq-combinator.test_single_derivation metricq-combinator.test_single_derivation) | ||
macro(add_test_case test_name) | ||
add_executable(${test_name} ${test_name}.cpp) | ||
add_test(${test_name} ${test_name}) | ||
target_link_libraries(${test_name} PRIVATE metricq-combinator-lib) | ||
endmacro() | ||
|
||
target_link_libraries( | ||
metricq-combinator.test_single_derivation | ||
PRIVATE | ||
metricq-combinator-lib | ||
) | ||
add_test_case(test_single_derivation) | ||
add_test_case(test_display_expression) |
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,71 @@ | ||
#include "../src/combinator.hpp" | ||
#include <iostream> | ||
#include <metricq/json.hpp> | ||
|
||
struct TestDisplayExpression | ||
{ | ||
nlohmann::json input; | ||
std::string expected; | ||
}; | ||
|
||
int main() | ||
{ | ||
try | ||
{ | ||
std::vector<TestDisplayExpression> testCases; | ||
|
||
testCases.emplace_back(TestDisplayExpression{ | ||
{ { "expression", | ||
{ { "operation", "*" }, | ||
{ "left", 5 }, | ||
{ "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } }, | ||
"(5 * (45 - 3))" }); | ||
|
||
testCases.emplace_back(TestDisplayExpression{ | ||
{ { "expression", | ||
{ { "operation", "*" }, | ||
{ "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } }, | ||
{ "right", | ||
{ { "operation", "-" }, { "left", 10 }, { "right", "dummy.source" } } } } } }, | ||
"((1 + 2) * (10 - dummy.source))" }); | ||
|
||
testCases.emplace_back(TestDisplayExpression{ | ||
{ { "expression", | ||
{ { "operation", "-" }, | ||
{ "left", | ||
{ { "operation", "+" }, | ||
{ "left", 15.3 }, | ||
{ "right", { { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } }, | ||
{ "right", | ||
{ { "operation", "throttle" }, | ||
{ "cooldown_period", "42" }, | ||
{ "input", 8 } } } } } }, | ||
"((15.300000 + min[42, 24, 8, 12]) - 8)" }); | ||
|
||
// Test the cases | ||
for (const auto& testCase : testCases) | ||
{ | ||
|
||
auto expression = testCase.input["expression"]; | ||
std::string result = Combinator::displayExpression(expression); | ||
|
||
// Compare with expected result | ||
if (result != testCase.expected) // comparing with the expected output | ||
{ | ||
std::cerr << "Test case " << testCase.input << " failed:\n"; | ||
std::cerr << "Expected: " << testCase.expected << "\n"; | ||
std::cerr << "Got: " << result << "\n"; | ||
return 1; | ||
} | ||
} | ||
|
||
std::cout << "All test cases passed successfully!" << std::endl; | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.