Skip to content

Commit

Permalink
Merge pull request #784 from go-graphite/njpm/fix-above-functions
Browse files Browse the repository at this point in the history
`currentAbove` and similar to do '>' and not '>='
  • Loading branch information
Civil authored Jul 16, 2023
2 parents c4a66e0 + f82b674 commit 7fc1aec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
17 changes: 3 additions & 14 deletions expr/functions/below/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (f *below) Do(ctx context.Context, e parser.Expr, from, until int64, values
}

isAbove := strings.HasSuffix(e.Target(), "Above")
isInclusive := true
var compute func([]float64) float64
switch {
case strings.HasPrefix(e.Target(), "average"):
Expand All @@ -55,28 +54,18 @@ func (f *below) Do(ctx context.Context, e parser.Expr, from, until int64, values
compute = consolidations.CurrentValue
case strings.HasPrefix(e.Target(), "maximum"):
compute = consolidations.MaxValue
isInclusive = false
case strings.HasPrefix(e.Target(), "minimum"):
compute = consolidations.MinValue
isInclusive = false
}
results := make([]*types.MetricData, 0, len(args))
for _, a := range args {
value := compute(a.Values)
if isAbove {
if isInclusive {
if value >= n {
results = append(results, a)
}
} else {
if value > n {
results = append(results, a)
}
}
} else {
if value <= n {
if value > n {
results = append(results, a)
}
} else if value <= n {
results = append(results, a)
}
}

Expand Down
6 changes: 3 additions & 3 deletions expr/functions/below/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestBelow(t *testing.T) {
types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 7}, 1, now32),
},
},
[]*types.MetricData{types.MakeMetricData("metricB",
Expand All @@ -55,13 +56,12 @@ func TestBelow(t *testing.T) {
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {
types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32), // avg=5.5
types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32), // avg=5
},
},
[]*types.MetricData{
types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
},
},
{
Expand Down

0 comments on commit 7fc1aec

Please sign in to comment.