forked from cloudflare/go-stream
/
operator.go
44 lines (36 loc) · 1.24 KB
/
operator.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
package cube
import (
"github.com/cloudflare/go-stream/stream"
// "reflect"
"time"
)
type TimePartitionedCubeContainer struct {
cube *TimePartitionedCube
parse func(stream.Object) (Dimensions, Aggregates)
batchGranularity time.Duration
outputGranularity time.Duration
}
func (cont *TimePartitionedCubeContainer) Flush(outch chan<- stream.Object) bool {
out := NewTimeRepartitionedCube(cont.batchGranularity, cont.outputGranularity)
out.Add(cont.cube)
outch <- out
cont.cube = NewTimePartitionedCube(cont.batchGranularity)
return true
}
func (cont *TimePartitionedCubeContainer) Add(obj stream.Object) {
d, a := cont.parse(obj)
cont.cube.Insert(d, a)
}
func (cont *TimePartitionedCubeContainer) FlushAll(outch chan<- stream.Object) bool {
return cont.Flush(outch)
}
func (cont *TimePartitionedCubeContainer) HasItems() bool {
return cont.cube.HasItems()
}
func NewPgBatchOperator(parse func(stream.Object) (Dimensions, Aggregates),
downstreamProcessed stream.ProcessedNotifier) stream.Operator {
batchGran := time.Second
outGran := time.Hour
cont := &TimePartitionedCubeContainer{NewTimePartitionedCube(batchGran), parse, batchGran, outGran}
return stream.NewBatchOperator("PgBatchOp", cont, downstreamProcessed)
}