Skip to content

Commit

Permalink
change(combinator): display as inline
Browse files Browse the repository at this point in the history
  • Loading branch information
floork committed Jan 27, 2025
1 parent 7a814b2 commit 1b2f647
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/combinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ std::string handleBasicExpression(const nlohmann::json& expression)
std::string handleOperatorExpression(const std::string& operation, const std::string& leftStr,
const std::string& rightStr)
{
static const std::unordered_set<std::string> validOperators = { "+", "-", "*", "/" };
if (operation.size() > 1)
{
throw std::logic_error("Invalid operator length!");
}

if (validOperators.find(operation) == validOperators.end())
switch (operation[0])
{
case '+':
case '-':
case '*':
case '/':
return "(" + leftStr + " " + operation + " " + rightStr + ")";
default:
throw std::runtime_error("Invalid operator: " + operation);
}

return operation + (leftStr.empty() ? "" : " " + leftStr) +
(rightStr.empty() ? "" : " " + rightStr);
}

std::string handleAggregateExpression(const std::string& operation,
const std::vector<std::string>& inputs)
std::string handleCombinationExpression(const std::string& operation,
const std::vector<std::string>& inputs)
{
static const std::unordered_set<std::string> validAggregates = { "sum", "min", "max" };

Expand All @@ -100,10 +106,10 @@ std::string handleAggregateExpression(const std::string& operation,
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 + "]";
return operation + "[" + input + "]";
}

std::string displayExpression(const nlohmann::json& expression)
std::string Combinator::displayExpression(const nlohmann::json& expression)
{
if (expression.is_number() || expression.is_string())
{
Expand All @@ -119,15 +125,17 @@ std::string displayExpression(const nlohmann::json& expression)

if (operation == "throttle")
{
return operation;
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"))
if (expression.contains("left") && expression.contains("right"))
{
std::string leftStr =
expression.contains("left") ? displayExpression(expression["left"]) : "";
std::string rightStr =
expression.contains("right") ? displayExpression(expression["right"]) : "";
std::string leftStr = displayExpression(expression["left"]);
std::string rightStr = displayExpression(expression["right"]);
return handleOperatorExpression(operation, leftStr, rightStr);
}

Expand All @@ -143,7 +151,7 @@ std::string displayExpression(const nlohmann::json& expression)
{
inputStrings.push_back(displayExpression(input));
}
return handleAggregateExpression(operation, inputStrings);
return handleCombinationExpression(operation, inputStrings);
}

throw std::runtime_error("Unsupported operation type: " + operation);
Expand Down
1 change: 1 addition & 0 deletions src/combinator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Combinator : public metricq::Transformer
public:
Combinator(const std::string& manager_host, const std::string& token);
~Combinator();
static std::string displayExpression(const nlohmann::json& expression);

private:
void on_transformer_config(const metricq::json& config) override;
Expand Down

0 comments on commit 1b2f647

Please sign in to comment.