-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.go
151 lines (135 loc) · 3.23 KB
/
metrics.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"database/sql"
"path/filepath"
"time"
"go.elara.ws/itd/infinitime"
"go.elara.ws/logger/log"
_ "modernc.org/sqlite"
)
func initMetrics(ctx context.Context, wg WaitGroup, dev *infinitime.Device) error {
// If metrics disabled, return nil
if !k.Bool("metrics.enabled") {
return nil
}
// Open metrics database
db, err := sql.Open("sqlite", filepath.Join(cfgDir, "metrics.db"))
if err != nil {
return err
}
// Create heartRate table
_, err = db.Exec("CREATE TABLE IF NOT EXISTS heartRate(time INT, bpm INT);")
if err != nil {
return err
}
// Create stepCount table
_, err = db.Exec("CREATE TABLE IF NOT EXISTS stepCount(time INT, steps INT);")
if err != nil {
return err
}
// Create battLevel table
_, err = db.Exec("CREATE TABLE IF NOT EXISTS battLevel(time INT, percent INT);")
if err != nil {
return err
}
// Create motion table
_, err = db.Exec("CREATE TABLE IF NOT EXISTS motion(time INT, X INT, Y INT, Z INT);")
if err != nil {
return err
}
// Watch heart rate
if k.Bool("metrics.heartRate.enabled") {
err := dev.WatchHeartRate(ctx, func(heartRate uint8, err error) {
if err != nil {
// Handle error
return
}
// Get current time
unixTime := time.Now().UnixNano()
// Insert sample and time into database
db.Exec("INSERT INTO heartRate VALUES (?, ?);", unixTime, heartRate)
})
if err != nil {
return err
}
}
// If step count metrics enabled in config
if k.Bool("metrics.stepCount.enabled") {
// Watch step count
err := dev.WatchStepCount(ctx, func(count uint32, err error) {
if err != nil {
return
}
// Get current time
unixTime := time.Now().UnixNano()
// Insert sample and time into database
db.Exec("INSERT INTO stepCount VALUES (?, ?);", unixTime, count)
})
if err != nil {
return err
}
}
// Watch step count
if k.Bool("metrics.stepCount.enabled") {
err := dev.WatchStepCount(ctx, func(count uint32, err error) {
if err != nil {
// Handle error
return
}
// Get current time
unixTime := time.Now().UnixNano()
// Insert sample and time into database
db.Exec("INSERT INTO stepCount VALUES (?, ?);", unixTime, count)
})
if err != nil {
return err
}
}
// Watch battery level
if k.Bool("metrics.battLevel.enabled") {
err := dev.WatchBatteryLevel(ctx, func(battLevel uint8, err error) {
if err != nil {
// Handle error
return
}
// Get current time
unixTime := time.Now().UnixNano()
// Insert sample and time into database
db.Exec("INSERT INTO battLevel VALUES (?, ?);", unixTime, battLevel)
})
if err != nil {
return err
}
}
// Watch motion values
if k.Bool("metrics.motion.enabled") {
err := dev.WatchMotion(ctx, func(motionVals infinitime.MotionValues, err error) {
if err != nil {
// Handle error
return
}
// Get current time
unixTime := time.Now().UnixNano()
// Insert sample values and time into database
db.Exec(
"INSERT INTO motion VALUES (?, ?, ?, ?);",
unixTime,
motionVals.X,
motionVals.Y,
motionVals.Z,
)
})
if err != nil {
return err
}
}
wg.Add(1)
go func() {
defer wg.Done("metrics")
<-ctx.Done()
db.Close()
}()
log.Info("Initialized metrics collection").Send()
return nil
}