forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request influxdata#1844 from influxdata/aa_643_changedetect
Node to detect changes between consecutive values in a series field
- Loading branch information
Showing
13 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package kapacitor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/influxdata/kapacitor/edge" | ||
"github.com/influxdata/kapacitor/keyvalue" | ||
"github.com/influxdata/kapacitor/models" | ||
"github.com/influxdata/kapacitor/pipeline" | ||
) | ||
|
||
type ChangeDetectNode struct { | ||
node | ||
d *pipeline.ChangeDetectNode | ||
} | ||
|
||
// Create a new changeDetect node. | ||
func newChangeDetectNode(et *ExecutingTask, n *pipeline.ChangeDetectNode, d NodeDiagnostic) (*ChangeDetectNode, error) { | ||
dn := &ChangeDetectNode{ | ||
node: node{Node: n, et: et, diag: d}, | ||
d: n, | ||
} | ||
// Create stateful expressions | ||
dn.node.runF = dn.runChangeDetect | ||
return dn, nil | ||
} | ||
|
||
func (n *ChangeDetectNode) runChangeDetect([]byte) error { | ||
consumer := edge.NewGroupedConsumer( | ||
n.ins[0], | ||
n, | ||
) | ||
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar()) | ||
return consumer.Consume() | ||
} | ||
|
||
func (n *ChangeDetectNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) { | ||
return edge.NewReceiverFromForwardReceiverWithStats( | ||
n.outs, | ||
edge.NewTimedForwardReceiver(n.timer, n.newGroup()), | ||
), nil | ||
} | ||
|
||
func (n *ChangeDetectNode) newGroup() *changeDetectGroup { | ||
return &changeDetectGroup{ | ||
n: n, | ||
} | ||
} | ||
|
||
type changeDetectGroup struct { | ||
n *ChangeDetectNode | ||
previous edge.FieldsTagsTimeGetter | ||
} | ||
|
||
func (g *changeDetectGroup) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) { | ||
if s := begin.SizeHint(); s > 0 { | ||
begin = begin.ShallowCopy() | ||
begin.SetSizeHint(0) | ||
} | ||
g.previous = nil | ||
return begin, nil | ||
} | ||
|
||
func (g *changeDetectGroup) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) { | ||
emit := g.doChangeDetect(bp) | ||
if emit { | ||
return bp, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (g *changeDetectGroup) EndBatch(end edge.EndBatchMessage) (edge.Message, error) { | ||
return end, nil | ||
} | ||
|
||
func (g *changeDetectGroup) Point(p edge.PointMessage) (edge.Message, error) { | ||
emit := g.doChangeDetect(p) | ||
if emit { | ||
return p, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
// doChangeDetect computes the changeDetect with respect to g.previous and p. | ||
// The resulting changeDetect value will be set on n. | ||
func (g *changeDetectGroup) doChangeDetect(p edge.FieldsTagsTimeGetter) bool { | ||
var prevFields, currFields models.Fields | ||
if g.previous != nil { | ||
prevFields = g.previous.Fields() | ||
} | ||
currFields = p.Fields() | ||
emit := g.n.changeDetect(prevFields, currFields) | ||
|
||
if !emit { | ||
return false | ||
} | ||
g.previous = p | ||
return true | ||
} | ||
|
||
func (g *changeDetectGroup) Barrier(b edge.BarrierMessage) (edge.Message, error) { | ||
return b, nil | ||
} | ||
func (g *changeDetectGroup) DeleteGroup(d edge.DeleteGroupMessage) (edge.Message, error) { | ||
return d, nil | ||
} | ||
|
||
// changeDetect calculates the changeDetect between prev and cur. | ||
// Return is the resulting changeDetect, whether the current point should be | ||
// stored as previous, and whether the point result should be emitted. | ||
func (n *ChangeDetectNode) changeDetect(prev, curr models.Fields) bool { | ||
|
||
value, ok := curr[n.d.Field] | ||
if !ok { | ||
n.diag.Error("Invalid field in change detect", | ||
fmt.Errorf("expected field %s not found", n.d.Field), | ||
keyvalue.KV("field", n.d.Field)) | ||
return false | ||
} | ||
if prev[n.d.Field] == value { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"packets","points":[{"fields":{"value":"bad"},"time":"2015-10-18T00:00:00Z"},{"fields":{"value":"good"},"time":"2015-10-18T00:00:02Z"},{"fields":{"value":"good"},"time":"2015-10-18T00:00:04Z"},{"fields":{"value2":"good"},"time":"2015-10-18T00:00:05Z"},{"fields":{"value":"bad"},"time":"2015-10-18T00:00:06Z"},{"fields":{"value":"good"},"time":"2015-10-18T00:00:08Z"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
dbname | ||
rpname | ||
packets value="bad" 0000000000 | ||
dbname | ||
rpname | ||
packets value="good" 0000000001 | ||
dbname | ||
rpname | ||
packets value="bad" 0000000002 | ||
dbname | ||
rpname | ||
packets value="bad" 0000000003 | ||
dbname | ||
rpname | ||
packets value="bad" 0000000004 | ||
dbname | ||
rpname | ||
packets value="good" 0000000005 | ||
dbname | ||
rpname | ||
packets value2="good" 0000000006 | ||
dbname | ||
rpname | ||
packets value="bad" 0000000007 | ||
dbname | ||
rpname | ||
packets value="good" 0000000008 | ||
dbname | ||
rpname | ||
packets value="good" 0000000009 | ||
dbname | ||
rpname | ||
packets value="good" 0000000010 | ||
dbname | ||
rpname | ||
packets value="bad" 0000000011 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package pipeline | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Compute the changeDetect of a stream or batch. | ||
// The changeDetect is computed on a single field, | ||
// discarding consecutive duplicate values, detecting | ||
// detects the points at which the series field | ||
// changes from one value to another. | ||
// | ||
// | ||
// Example: | ||
// stream | ||
// |from() | ||
// .measurement('packets') | ||
// |changeDetect('value') | ||
// ... | ||
// | ||
// with source data: | ||
// packets value="bad" 0000000000 | ||
// packets value="good" 0000000001 | ||
// packets value="bad" 0000000002 | ||
// packets value="bad" 0000000003 | ||
// packets value="bad" 0000000004 | ||
// packets value="good" 0000000005 | ||
// | ||
// Would have output: | ||
// packets value="bad" 0000000000 | ||
// packets value="good" 0000000001 | ||
// packets value="bad" 0000000002 | ||
// packets value="good" 0000000005 | ||
// | ||
// Where the data are unchanged, but only the points | ||
// where the value changes from the previous value are | ||
// emitted. | ||
|
||
type ChangeDetectNode struct { | ||
chainnode `json:"-"` | ||
|
||
// The field to use when calculating the changeDetect | ||
// tick:ignore | ||
Field string `json:"field"` | ||
} | ||
|
||
func newChangeDetectNode(wants EdgeType, field string) *ChangeDetectNode { | ||
return &ChangeDetectNode{ | ||
chainnode: newBasicChainNode("changeDetect", wants, wants), | ||
Field: field, | ||
} | ||
} | ||
|
||
// MarshalJSON converts ChangeDetectNode to JSON | ||
// tick:ignore | ||
func (n *ChangeDetectNode) MarshalJSON() ([]byte, error) { | ||
type Alias ChangeDetectNode | ||
var raw = &struct { | ||
TypeOf | ||
*Alias | ||
}{ | ||
TypeOf: TypeOf{ | ||
Type: "changeDetect", | ||
ID: n.ID(), | ||
}, | ||
Alias: (*Alias)(n), | ||
} | ||
return json.Marshal(raw) | ||
} | ||
|
||
// UnmarshalJSON converts JSON to an ChangeDetectNode | ||
// tick:ignore | ||
func (n *ChangeDetectNode) UnmarshalJSON(data []byte) error { | ||
type Alias ChangeDetectNode | ||
var raw = &struct { | ||
TypeOf | ||
*Alias | ||
}{ | ||
Alias: (*Alias)(n), | ||
} | ||
err := json.Unmarshal(data, raw) | ||
if err != nil { | ||
return err | ||
} | ||
if raw.Type != "changeDetect" { | ||
return fmt.Errorf("error unmarshaling node %d of type %s as ChangeDetectNode", raw.ID, raw.Type) | ||
} | ||
|
||
n.setID(raw.ID) | ||
return nil | ||
} |
Oops, something went wrong.