-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathtransform.go
312 lines (262 loc) · 8.88 KB
/
transform.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
)
// TransformFunc is used by Transform to apply a function
// to all values in a series.
type TransformFunc func(float64) float64
// TransformFuncFactory creates transformation functions
type TransformFuncFactory func() TransformFunc
// Transformer transforms a value
type Transformer interface {
// Apply applies the transformation
Apply(value float64) float64
// Reset resets the state
Reset()
}
type statelessTransformer struct {
fn TransformFunc
}
// NewStatelessTransformer creates a new stateless transformer
func NewStatelessTransformer(fn TransformFunc) Transformer {
return statelessTransformer{fn: fn}
}
func (t statelessTransformer) Apply(value float64) float64 {
return t.fn(value)
}
func (t statelessTransformer) Reset() {}
// MaintainNaNTransformer only applies a given ValueTransformer to
// non-NaN values.
func MaintainNaNTransformer(f TransformFunc) TransformFunc {
return func(v float64) float64 {
if math.IsNaN(v) {
return v
}
return f(v)
}
}
// Scale multiplies each element of a series list by a given value.
func Scale(scale float64) TransformFunc {
return MaintainNaNTransformer(func(v float64) float64 {
return v * scale
})
}
// Offset adds a value to each element of a series list.
func Offset(factor float64) TransformFunc {
return MaintainNaNTransformer(func(v float64) float64 {
return v + factor
})
}
// TransformNull transforms all nulls in a series to a value.
func TransformNull(value float64) TransformFunc {
return func(v float64) float64 {
if math.IsNaN(v) {
return value
}
return v
}
}
// IsNonNull takes a series or series list and counts up how many non-null values are specified.
// This is useful for understanding which series have data at a given point in time (i.e. to count
// which servers are alive).
func IsNonNull() TransformFunc {
return func(v float64) float64 {
if math.IsNaN(v) {
return 0
}
return 1
}
}
// PredicateFn is a predicate function.
type PredicateFn func(v float64) bool
// Filter removes data that does not satisfy a given predicate.
func Filter(fn PredicateFn) TransformFunc {
return MaintainNaNTransformer(func(v float64) float64 {
if !fn(v) {
return math.NaN()
}
return v
})
}
// Logarithm takes one series or a series list, and draws the y-axis in logarithmic format. Only support
// base 10 logarithms.
func Logarithm() TransformFunc {
return func(v float64) float64 {
if !math.IsNaN(v) && v > 0 {
return math.Log10(v)
}
return math.NaN()
}
}
// Integral returns a function that accumulates values it has seen
func Integral() TransformFunc {
currentSum := 0.0
return func(v float64) float64 {
if !math.IsNaN(v) {
currentSum += v
} else {
return v
}
return currentSum
}
}
// Derivative returns a function that computes the derivative among the values
// it has seen
func Derivative() TransformFunc {
previousValue := math.NaN()
return func(v float64) float64 {
var r float64
if math.IsNaN(v) || math.IsNaN(previousValue) {
previousValue, r = v, math.NaN()
} else {
previousValue, r = v, v-previousValue
}
return r
}
}
// NonNegativeDerivative returns a function that computes the derivative among the
// values it has seen but ignores datapoints that trend down
func NonNegativeDerivative(maxValue float64) TransformFunc {
previousValue := math.NaN()
return func(v float64) float64 {
var r float64
if math.IsNaN(v) || math.IsNaN(previousValue) {
previousValue, r = v, math.NaN()
} else if difference := v - previousValue; difference >= 0 {
previousValue, r = v, difference
} else if !math.IsNaN(maxValue) && maxValue >= v {
previousValue, r = v, (maxValue-previousValue)+v+1.0
} else {
previousValue, r = v, math.NaN()
}
return r
}
}
// Transform applies a specified ValueTransform to all values in each series, renaming
// each series with the given SeriesRenamer.
func Transform(ctx *Context, in ts.SeriesList, t Transformer, renamer SeriesRenamer) (ts.SeriesList, error) {
results := make([]*ts.Series, in.Len())
for i, series := range in.Values {
t.Reset()
values := ts.NewValues(ctx, series.MillisPerStep(), series.Len())
for step := 0; step < series.Len(); step++ {
value := series.ValueAt(step)
values.SetValueAt(step, t.Apply(value))
}
results[i] = ts.NewSeries(ctx, renamer(series), series.StartTime(), values)
}
in.Values = results
return in, nil
}
// Stdev takes one metric or a wildcard seriesList followed by an integer N. Draw the standard deviation
// of all metrics passed for the past N datapoints. If the ratio of null points in the window is greater than
// windowTolerance, skip the calculation.
func Stdev(ctx *Context, in ts.SeriesList, points int, windowTolerance float64, renamer RenamerWithNumPoints) (ts.SeriesList, error) {
if points <= 0 {
return ts.SeriesList{}, errors.NewInvalidParamsError(fmt.Errorf("invalid window size, points=%d", points))
}
results := make([]*ts.Series, 0, in.Len())
for _, series := range in.Values {
stdevName := renamer(series, points)
stdevVals := ts.NewValues(ctx, series.MillisPerStep(), series.Len())
validPoints := 0
currentSum := 0.0
currentSumOfSquares := 0.0
for index := 0; index < series.Len(); index++ {
newValue := series.ValueAt(index)
var bootstrapping bool
var droppedValue float64
// Mark whether we've reached our window size, don't drop points out otherwise
if index < points {
bootstrapping = true
droppedValue = math.NaN()
} else {
bootstrapping = false
droppedValue = series.ValueAt(index - points)
}
// Remove the value that just dropped out of the window
if !bootstrapping && !math.IsNaN(droppedValue) {
validPoints--
currentSum -= droppedValue
currentSumOfSquares -= droppedValue * droppedValue
}
// Add in the value that just popped in the window
if !math.IsNaN(newValue) {
validPoints++
currentSum += newValue
currentSumOfSquares += newValue * newValue
}
if validPoints > 0 && float64(validPoints)/float64(points) >= windowTolerance {
deviation := math.Sqrt(float64(validPoints)*currentSumOfSquares-currentSum*currentSum) / float64(validPoints)
stdevVals.SetValueAt(index, deviation)
}
}
stdevSeries := ts.NewSeries(ctx, stdevName, series.StartTime(), stdevVals)
results = append(results, stdevSeries)
}
in.Values = results
return in, nil
}
// RenamerWithNumPoints is a signature for renaming a single series that is passed to Stdev
type RenamerWithNumPoints func(series *ts.Series, points int) string
// PerSecond computes the derivative between consecutive values in the a time series, taking into
// account the time interval between the values. It skips missing values, and calculates the
// derivative between consecutive non-missing values.
func PerSecond(ctx *Context, in ts.SeriesList, renamer SeriesRenamer) (ts.SeriesList, error) {
results := make([]*ts.Series, 0, in.Len())
for _, series := range in.Values {
var (
vals = ts.NewValues(ctx, series.MillisPerStep(), series.Len())
prev = math.NaN()
secsPerStep = float64(series.MillisPerStep()) / 1000
secsSinceLastVal = secsPerStep
)
for step := 0; step < series.Len(); step++ {
cur := series.ValueAt(step)
if math.IsNaN(prev) {
vals.SetValueAt(step, math.NaN())
prev = cur
continue
}
if math.IsNaN(cur) {
vals.SetValueAt(step, math.NaN())
secsSinceLastVal += secsPerStep
continue
}
diff := cur - prev
if diff >= 0 {
vals.SetValueAt(step, diff/secsSinceLastVal)
} else {
vals.SetValueAt(step, math.NaN())
}
prev = cur
secsSinceLastVal = secsPerStep
}
s := ts.NewSeries(ctx, renamer(series), series.StartTime(), vals)
results = append(results, s)
}
in.Values = results
return in, nil
}