diff --git a/expr/functions/removeEmptySeries/function.go b/expr/functions/removeEmptySeries/function.go index 1d6cc8338..1acfe5566 100644 --- a/expr/functions/removeEmptySeries/function.go +++ b/expr/functions/removeEmptySeries/function.go @@ -35,19 +35,30 @@ func (f *removeEmptySeries) Do(ctx context.Context, e parser.Expr, from, until i return nil, err } - // TODO: implement xFilesFactor + factor, err := e.GetFloatArgDefault(1, 0) + if err != nil { + return nil, err + } var results []*types.MetricData - for _, a := range args { - for _, v := range a.Values { + for _, arg := range args { + nonNull := 0.0 + for _, v := range arg.Values { if !math.IsNaN(v) { - if e.Target() == "removeEmptySeries" || (v != 0) { - results = append(results, a) - break + switch e.Target() { + case "removeEmptySeries": + nonNull++ + case "removeZeroSeries": + if v != 0 { + nonNull++ + } } } } + if nonNull/float64(len(arg.Values)) >= factor { + results = append(results, arg) + } } return results, nil } diff --git a/expr/functions/removeEmptySeries/function_test.go b/expr/functions/removeEmptySeries/function_test.go index 3be76f387..e9bdfb5b8 100644 --- a/expr/functions/removeEmptySeries/function_test.go +++ b/expr/functions/removeEmptySeries/function_test.go @@ -37,11 +37,12 @@ func TestFunction(t *testing.T) { }, []*types.MetricData{ types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32), + types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32), types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32), }, }, { - "removeZeroSeries(metric*)", + "removeEmptySeries(metric*,0.00001)", map[parser.MetricRequest][]*types.MetricData{ {"metric*", 0, 1}: { types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32), @@ -51,6 +52,49 @@ func TestFunction(t *testing.T) { }, []*types.MetricData{ types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32), + types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32), + }, + }, + { + "removeZeroSeries(metric*,0.000001)", + map[parser.MetricRequest][]*types.MetricData{ + {"metric*", 0, 1}: { + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32), + types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32), + types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32), + }, + }, + []*types.MetricData{ + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32), + }, + }, + { + "removeEmptySeries(metric*,0.8)", + map[parser.MetricRequest][]*types.MetricData{ + {"metric*", 0, 1}: { + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32), + types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32), + types.MakeMetricData("metric3", []float64{1, 2, -1, 7, 8, 20, 23, math.NaN(), math.NaN(), math.NaN()}, 1, now32), + types.MakeMetricData("metric4", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32), + types.MakeMetricData("metric5", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32), + }, + }, + []*types.MetricData{ + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32), + types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32), + types.MakeMetricData("metric5", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32), + }, + }, + { + "removeEmptySeries(metric*,1)", + map[parser.MetricRequest][]*types.MetricData{ + {"metric*", 0, 1}: { + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32), + types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32), + }, + }, + []*types.MetricData{ + types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32), }, }, }