forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
101 lines (92 loc) · 1.99 KB
/
sample.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
package pipeline
import (
"encoding/json"
"fmt"
"time"
"github.com/influxdata/influxdb/influxql"
)
// Sample points or batches.
// One point will be emitted every count or duration specified.
//
// Example:
// stream
// |sample(3)
//
// Keep every third data point or batch.
//
// Example:
// stream
// |sample(10s)
//
// Keep only samples that land on the 10s boundary.
// See FromNode.Truncate, QueryNode.GroupBy time or WindowNode.Align
// for ensuring data is aligned with a boundary.
type SampleNode struct {
chainnode `json:"-"`
// Keep every N point or batch
// tick:ignore
N int64 `json:"n"`
// Keep one point or batch every Duration
// tick:ignore
Duration time.Duration `json:"duration"`
}
func newSampleNode(wants EdgeType, rate interface{}) *SampleNode {
var n int64
var d time.Duration
switch r := rate.(type) {
case int64:
n = r
case time.Duration:
d = r
default:
panic("must pass int64 or duration to new sample node")
}
return &SampleNode{
chainnode: newBasicChainNode("sample", wants, wants),
N: n,
Duration: d,
}
}
// MarshalJSON converts SampleNode to JSON
// tick:ignore
func (n *SampleNode) MarshalJSON() ([]byte, error) {
type Alias SampleNode
var raw = &struct {
TypeOf
*Alias
Duration string `json:"duration"`
}{
TypeOf: TypeOf{
Type: "sample",
ID: n.ID(),
},
Alias: (*Alias)(n),
Duration: influxql.FormatDuration(n.Duration),
}
return json.Marshal(raw)
}
// UnmarshalJSON converts JSON to an SampleNode
// tick:ignore
func (n *SampleNode) UnmarshalJSON(data []byte) error {
type Alias SampleNode
var raw = &struct {
TypeOf
*Alias
Duration string `json:"duration"`
}{
Alias: (*Alias)(n),
}
err := json.Unmarshal(data, raw)
if err != nil {
return err
}
if raw.Type != "sample" {
return fmt.Errorf("error unmarshaling node %d of type %s as SampleNode", raw.ID, raw.Type)
}
n.Duration, err = influxql.ParseDuration(raw.Duration)
if err != nil {
return err
}
n.setID(raw.ID)
return nil
}