-
Notifications
You must be signed in to change notification settings - Fork 3
/
rms.go
43 lines (37 loc) · 1.15 KB
/
rms.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
package math
import (
"math"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
)
func RegisterRMS(b reg.ProcessorRegistry) {
b.RegisterBatchStep("rms",
func(_ map[string]interface{}) (bitflow.BatchProcessingStep, error) {
return new(BatchRms), nil
},
"Compute the Root Mean Square value for every metric in a data batch. Output a single sample with all values.")
}
type BatchRms struct {
}
func (r *BatchRms) ProcessBatch(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
if len(samples) == 0 {
return header, samples, nil
}
res := make([]bitflow.Value, len(header.Fields))
num := float64(len(samples))
for i := range header.Fields {
rms := float64(0)
for _, sample := range samples {
val := float64(sample.Values[i])
rms += val * val / num
}
rms = math.Sqrt(rms)
res[i] = bitflow.Value(rms)
}
outSample := samples[0].Clone() // Use the first sample as the reference for metadata (timestamp and tags)
outSample.Values = res
return header, []*bitflow.Sample{outSample}, nil
}
func (r *BatchRms) String() string {
return "Root Mean Square"
}