-
Notifications
You must be signed in to change notification settings - Fork 40
/
database.go
84 lines (67 loc) · 1.67 KB
/
database.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
package graphite
import (
"log"
"sync"
"github.com/FreifunkBremen/yanic/database"
"github.com/fgrosse/graphigo"
)
const (
MeasurementNode = "node" // Measurement for per-node statistics
MeasurementGlobal = "global" // Measurement for summarized global statistics
CounterMeasurementFirmware = "firmware" // Measurement for firmware statistics
CounterMeasurementModel = "model" // Measurement for model statistics
CounterMeasurementAutoupdater = "autoupdater" // Measurement for autoupdater
)
type Connection struct {
database.Connection
client graphigo.Client
points chan []graphigo.Metric
wg sync.WaitGroup
}
type Config map[string]interface{}
func (c Config) Address() string {
return c["address"].(string)
}
func (c Config) Prefix() string {
return c["prefix"].(string)
}
func Connect(configuration map[string]interface{}) (database.Connection, error) {
var config Config
config = configuration
con := &Connection{
client: graphigo.Client{
Address: config.Address(),
Prefix: config.Prefix(),
},
points: make(chan []graphigo.Metric, 1000),
}
if err := con.client.Connect(); err != nil {
return nil, err
}
con.wg.Add(1)
go con.addWorker()
return con, nil
}
func (c *Connection) Close() {
close(c.points)
if c.client.Connection != nil {
c.client.Close()
}
}
func (c *Connection) addWorker() {
defer c.wg.Done()
defer c.Close()
for point := range c.points {
err := c.client.SendAll(point)
if err != nil {
log.Fatal(err)
return
}
}
}
func (c *Connection) addPoint(point []graphigo.Metric) {
c.points <- point
}
func init() {
database.RegisterAdapter("graphite", Connect)
}