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.
- Loading branch information
1 parent
da7c428
commit c087369
Showing
9 changed files
with
304 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,81 @@ | ||
package kapacitor | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/influxdata/kapacitor/models" | ||
"github.com/influxdata/kapacitor/pipeline" | ||
) | ||
|
||
type DefaultNode struct { | ||
node | ||
d *pipeline.DefaultNode | ||
} | ||
|
||
// Create a new DefaultNode which applies a transformation func to each point in a stream and returns a single point. | ||
func newDefaultNode(et *ExecutingTask, n *pipeline.DefaultNode, l *log.Logger) (*DefaultNode, error) { | ||
dn := &DefaultNode{ | ||
node: node{Node: n, et: et, logger: l}, | ||
d: n, | ||
} | ||
dn.node.runF = dn.runDefault | ||
return dn, nil | ||
} | ||
|
||
func (e *DefaultNode) runDefault(snapshot []byte) error { | ||
switch e.Provides() { | ||
case pipeline.StreamEdge: | ||
for p, ok := e.ins[0].NextPoint(); ok; p, ok = e.ins[0].NextPoint() { | ||
e.timer.Start() | ||
p.Fields, p.Tags = e.setDefaults(p.Fields, p.Tags) | ||
e.timer.Stop() | ||
for _, child := range e.outs { | ||
err := child.CollectPoint(p) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
case pipeline.BatchEdge: | ||
for b, ok := e.ins[0].NextBatch(); ok; b, ok = e.ins[0].NextBatch() { | ||
e.timer.Start() | ||
for i := range b.Points { | ||
b.Points[i].Fields, b.Points[i].Tags = e.setDefaults(b.Points[i].Fields, b.Points[i].Tags) | ||
} | ||
e.timer.Stop() | ||
for _, child := range e.outs { | ||
err := child.CollectBatch(b) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *DefaultNode) setDefaults(fields models.Fields, tags models.Tags) (models.Fields, models.Tags) { | ||
newFields := fields | ||
fieldsCopied := false | ||
for field, value := range d.d.Fields { | ||
if _, ok := fields[field]; !ok { | ||
if !fieldsCopied { | ||
newFields = newFields.Copy() | ||
fieldsCopied = true | ||
} | ||
newFields[field] = value | ||
} | ||
} | ||
newTags := tags | ||
tagsCopied := false | ||
for tag, value := range d.d.Tags { | ||
if _, ok := tags[tag]; !ok { | ||
if !tagsCopied { | ||
newTags = newTags.Copy() | ||
tagsCopied = true | ||
} | ||
newTags[tag] = value | ||
} | ||
} | ||
return newFields, newTags | ||
} |
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,2 @@ | ||
{"name":"cpu_usage_idle","tags":{"cpu":"cpu-total"},"points":[{"fields":{},"time":"2015-10-30T17:14:12Z"},{"fields":{"mean":86},"time":"2015-10-30T17:14:14Z"},{"fields":{"mean":91},"time":"2015-10-30T17:14:16Z"},{"fields":{"mean":87},"time":"2015-10-30T17:14:18Z"},{"fields":{},"time":"2015-10-30T17:14:20Z"}]} | ||
{"name":"cpu_usage_idle","tags":{"cpu":"cpu-total"},"points":[{"fields":{},"time":"2015-10-30T17:14:22Z"},{"fields":{"mean":86},"time":"2015-10-30T17:14:24Z"},{"fields":{"mean":91},"time":"2015-10-30T17:14:26Z"},{"fields":{"mean":85},"time":"2015-10-30T17:14:28Z"},{"fields":{"mean":89},"time":"2015-10-30T17:14:30Z"}]} |
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,81 @@ | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=9 0000000001 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=9 0000000001 | ||
dbname | ||
rpname | ||
disk,type=sda,host=serverB value=3 0000000001 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=4 0000000002 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=7 0000000002 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=2 0000000003 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=1 0000000003 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=8 0000000004 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=4 0000000004 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=6 0000000005 | ||
dbname | ||
rpname | ||
cpu,type=idle value=2 0000000005 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=8 0000000006 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=8 0000000006 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverC value=4 0000000006 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA anothervalue=9 0000000007 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=7 0000000007 | ||
dbname | ||
rpname | ||
cpu,type=idle anothervalue=3 0000000008 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=6 0000000008 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=9 0000000009 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=9 0000000009 | ||
dbname | ||
rpname | ||
disk,type=sda,host=serverB value=4 0000000009 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=7 0000000010 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=3 0000000010 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=2 0000000011 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=1 0000000011 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverA value=7 0000000012 | ||
dbname | ||
rpname | ||
cpu,type=idle,host=serverB value=9 0000000012 |
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,61 @@ | ||
package pipeline | ||
|
||
import "fmt" | ||
|
||
// Defaults fields and tags on data points. | ||
// | ||
// Example: | ||
// stream | ||
// |default() | ||
// .field('value', 0.0) | ||
// .tag('host', '') | ||
// | ||
// The above example will set the field `value` to float64(0) if it does not already exist | ||
// It will also set the tag `host` to string("") if it does not already exist. | ||
type DefaultNode struct { | ||
chainnode | ||
|
||
// Set of fields to default | ||
// tick:ignore | ||
Fields map[string]interface{} `tick:"Field"` | ||
|
||
// Set of tags to default | ||
Tags map[string]string `tick:"Tag"` | ||
} | ||
|
||
func newDefaultNode(e EdgeType) *DefaultNode { | ||
n := &DefaultNode{ | ||
chainnode: newBasicChainNode("default", e, e), | ||
Fields: make(map[string]interface{}), | ||
Tags: make(map[string]string), | ||
} | ||
return n | ||
} | ||
|
||
// Define a field default. | ||
// tick:property | ||
func (n *DefaultNode) Field(name string, value interface{}) *DefaultNode { | ||
n.Fields[name] = value | ||
return n | ||
} | ||
|
||
// Define a tag default. | ||
// tick:property | ||
func (n *DefaultNode) Tag(name string, value string) *DefaultNode { | ||
n.Tags[name] = value | ||
return n | ||
} | ||
|
||
func (n *DefaultNode) validate() error { | ||
for field, value := range n.Fields { | ||
switch value.(type) { | ||
case float64: | ||
case int64: | ||
case bool: | ||
case string: | ||
default: | ||
return fmt.Errorf("unsupported type %T for field %q, field default values must be float,int,string or bool", value, field) | ||
} | ||
} | ||
return nil | ||
} |
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