-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
103 lines (86 loc) · 1.82 KB
/
graph.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
102
103
package rtgraph
import (
"fmt"
"github.com/minor-industries/rtgraph/broker"
"github.com/minor-industries/rtgraph/computed_series"
"github.com/minor-industries/rtgraph/messages"
"github.com/minor-industries/rtgraph/schema"
"github.com/minor-industries/rtgraph/storage"
"github.com/minor-industries/rtgraph/subscription"
"github.com/pkg/errors"
"time"
)
type Graph struct {
seriesNames []string
errCh chan error
broker *broker.Broker
db storage.StorageBackend
Parser *computed_series.Parser
}
type Opts struct {
ExternalMetrics func(broker *broker.Broker, errCh chan error)
}
func New(
backend storage.StorageBackend,
errCh chan error,
opts Opts,
) (*Graph, error) {
br := broker.NewBroker()
g := &Graph{
broker: br,
db: backend,
errCh: errCh,
Parser: computed_series.NewParser(),
}
if opts.ExternalMetrics != nil {
go opts.ExternalMetrics(g.broker, errCh)
}
go g.publishToDB()
go br.Start()
//go g.monitorDrops()
return g, nil
}
func (g *Graph) CreateValue(
seriesName string,
timestamp time.Time,
value float64,
) error {
// TODO: do we need to ensure the series exists?
g.broker.Publish(schema.Series{
SeriesName: seriesName,
Values: []schema.Value{{
Timestamp: timestamp,
Value: value,
}},
})
return nil
}
func (g *Graph) Subscribe(
req *subscription.Request,
now time.Time,
msgCh chan *messages.Data,
) {
msgCh <- &messages.Data{
Now: uint64(now.UnixMilli()),
}
start := req.Start(now)
sub, err := subscription.NewSubscription(g.Parser, req, start)
if err != nil {
msgCh <- &messages.Data{
Error: errors.Wrap(err, "new subscription").Error(),
}
return
}
sub.Run(
g.db,
g.broker,
msgCh,
start,
)
}
func (g *Graph) monitorDrops() {
ticker := time.NewTicker(time.Second)
for range ticker.C {
fmt.Println("drops", g.broker.DropCount())
}
}