-
Notifications
You must be signed in to change notification settings - Fork 453
/
percentiles.go
158 lines (137 loc) · 4.86 KB
/
percentiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"fmt"
"math"
"sort"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
)
const (
// FloatingPointFormat is the floating point format for naming
FloatingPointFormat = "%.3f"
)
// ErrInvalidPercentile is used when the percentile specified is incorrect
func ErrInvalidPercentile(percentile float64) error {
return errors.NewInvalidParamsError(fmt.Errorf("invalid percentile, percentile="+FloatingPointFormat, percentile))
}
// PercentileNamer formats a string with a percentile
type PercentileNamer func(name string, percentile float64) string
// ThresholdComparator compares two floats for other comparison
// functions such as Percentile checks.
type ThresholdComparator func(v, threshold float64) bool
// GreaterThan is a ThresholdComparator function for when
// a value is greater than a threshold
func GreaterThan(v, threshold float64) bool {
return v > threshold
}
// LessThan is a ThresholdComparator function for when
// a value is less than a threshold
func LessThan(v, threshold float64) bool {
return v < threshold
}
// SafeSort sorts the input slice and returns the number of NaNs in the input.
func SafeSort(input []float64) int {
nans := 0
for i := 0; i < len(input); i++ {
if math.IsNaN(input[i]) {
nans++
}
}
sort.Float64s(input)
return nans
}
// GetPercentile computes the percentile cut off for an array of floats
func GetPercentile(input []float64, percentile float64, interpolate bool) float64 {
nans := SafeSort(input)
series := input[nans:]
if len(series) == 0 {
return math.NaN()
}
fractionalRank := (percentile / 100.0) * (float64(len(series)))
rank := math.Ceil(fractionalRank)
rankAsInt := int(rank)
if rankAsInt <= 1 {
return series[0]
}
percentileResult := series[rankAsInt-1]
if interpolate {
prevValue := series[rankAsInt-2]
fraction := fractionalRank - (rank - 1)
percentileResult = prevValue + (fraction * (percentileResult - prevValue))
}
return percentileResult
}
// NPercentile returns percentile-percent of each series in the seriesList.
func NPercentile(ctx *Context, in ts.SeriesList, percentile float64, pn PercentileNamer) (ts.SeriesList, error) {
if percentile < 0.0 || percentile > 100.0 {
return ts.SeriesList{}, ErrInvalidPercentile(percentile)
}
results := make([]*ts.Series, 0, in.Len())
for _, s := range in.Values {
safeValues := s.SafeValues()
if len(safeValues) == 0 {
continue
}
percentileVal := GetPercentile(safeValues, percentile, false)
if !math.IsNaN(percentileVal) {
vals := ts.NewConstantValues(ctx, percentileVal, s.Len(), s.MillisPerStep())
percentileSeries := ts.NewSeries(ctx, pn(s.Name(), percentile), s.StartTime(), vals)
results = append(results, percentileSeries)
}
}
in.Values = results
return in, nil
}
// RemoveByPercentile removes all series above or below the given percentile, as
// determined by the PercentileComparator
func RemoveByPercentile(
ctx *Context,
in ts.SeriesList,
percentile float64,
pn PercentileNamer,
tc ThresholdComparator,
) (ts.SeriesList, error) {
results := make([]*ts.Series, 0, in.Len())
for _, series := range in.Values {
single := ts.SeriesList{Values: []*ts.Series{series}}
percentileSeries, err := NPercentile(ctx, single, percentile, pn)
if err != nil {
return ts.SeriesList{}, err
}
numSteps := series.Len()
vals := ts.NewValues(ctx, series.MillisPerStep(), numSteps)
if percentileSeries.Len() == 1 {
percentile := percentileSeries.Values[0].ValueAt(0)
for i := 0; i < numSteps; i++ {
v := series.ValueAt(i)
if !tc(v, percentile) {
vals.SetValueAt(i, v)
}
}
}
name := pn(series.Name(), percentile)
newSeries := ts.NewSeries(ctx, name, series.StartTime(), vals)
results = append(results, newSeries)
}
in.Values = results
return in, nil
}