Skip to content
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

Implement si and binary system for legendValue #490

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/expr/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestEvalExpression(t *testing.T) {
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("metric1 (sum: 15.000000) (avg: 3.000000)",
[]*types.MetricData{types.MakeMetricData("metric1 (sum: 15.000000, avg: 3.000000)",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
},
{
Expand Down
31 changes: 26 additions & 5 deletions pkg/expr/functions/legendValue/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package legendValue
import (
"context"
"fmt"
"strings"

"github.com/bookingcom/carbonapi/pkg/expr/helper"
"github.com/bookingcom/carbonapi/pkg/expr/interfaces"
"github.com/bookingcom/carbonapi/pkg/expr/types"
"github.com/bookingcom/carbonapi/pkg/parser"
"github.com/dustin/go-humanize"
)

type legendValue struct {
Expand Down Expand Up @@ -35,28 +37,47 @@ func (f *legendValue) Do(ctx context.Context, e parser.Expr, from, until int32,
return nil, err
}

methods := make([]string, len(e.Args())-1)
var system string
var methods []string
for i := 1; i < len(e.Args()); i++ {
method, err := e.GetStringArg(i)
if err != nil {
return nil, err
}

methods[i-1] = method
if method == "si" || method == "binary" {
system = method
} else {
methods = append(methods, method)
}
}

var results []*types.MetricData

for _, a := range arg {
r := *a
var values []string
for _, method := range methods {
summary, _, err := helper.SummarizeValues(method, a.Values)
summaryVal, _, err := helper.SummarizeValues(method, a.Values)
if err != nil {
return []*types.MetricData{}, err
}
r.Name = fmt.Sprintf("%s (%s: %f)", r.Name, method, summary)

summary := ""
if system == "si" {
sv, sf := humanize.ComputeSI(summaryVal)
summary = fmt.Sprintf("%.1f %s", sv, sf)
} else if system == "binary" {
summary = humanize.IBytes(uint64(summaryVal))
} else if system == "" {
summary = fmt.Sprintf("%f", summaryVal)
} else {
return nil, fmt.Errorf("%s is not supported for system", system)
}
values = append(values, fmt.Sprintf("%s: %s", method, summary))
}

r := *a
r.Name = fmt.Sprintf("%s (%s)", r.Name, strings.Join(values, ", "))
results = append(results, &r)
}
return results, nil
Expand Down
Loading