Skip to content

Commit

Permalink
add barrier ast
Browse files Browse the repository at this point in the history
  • Loading branch information
sputnik13 committed Nov 28, 2017
1 parent 8866b7b commit c0f7d08
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pipeline/tick/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (a *AST) Create(n pipeline.Node, parents []ast.Node) (ast.Node, error) {
return NewJoin(parents).Build(node)
case *pipeline.AlertNode:
return NewAlert(parents).Build(node)
case *pipeline.BarrierNode:
return NewBarrierNode(parents).Build(node)
case *pipeline.CombineNode:
return NewCombine(parents).Build(node)
case *pipeline.DefaultNode:
Expand Down
28 changes: 28 additions & 0 deletions pipeline/tick/barrier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tick

import (
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
)

// BarrierNode converts the window pipeline node into the TICKScript AST
type BarrierNode struct {
Function
}

// NewBarrierNode creates a Barrier function builder
func NewBarrierNode(parents []ast.Node) *BarrierNode {
return &BarrierNode{
Function{
Parents: parents,
},
}
}

// Build creates a window ast.Node
func (n *BarrierNode) Build(b *pipeline.BarrierNode) (ast.Node, error) {
n.Pipe("barrier").
Dot("idle", b.Idle).
Dot("period", b.Period)
return n.prev, n.err
}
61 changes: 61 additions & 0 deletions pipeline/tick/barrier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tick_test

import (
"testing"
"time"

"github.com/influxdata/kapacitor/pipeline"
)

func TestBarrierNode(t *testing.T) {
type args struct {
idle time.Duration
period time.Duration
}
tests := []struct {
name string
args args
want string
}{
{
name: "barrier with idle",
args: args{
idle: time.Second,
},
want: `stream
|from()
|barrier()
.idle(1s)
`,
},
{
name: "barrier with period",
args: args{
period: time.Second,
},
want: `stream
|from()
|barrier()
.period(1s)
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stream := &pipeline.StreamNode{}
pipe := pipeline.CreatePipelineSources(stream)

b := stream.From().Barrier()
b.Idle = tt.args.idle
b.Period = tt.args.period

got, err := PipelineTick(pipe)
if err != nil {
t.Fatalf("Unexpected error building pipeline %v", err)
}
if got != tt.want {
t.Errorf("%q. TestBarrier() =\n%v\n want\n%v\n", tt.name, got, tt.want)
}
})
}
}

0 comments on commit c0f7d08

Please sign in to comment.