2022-05-11 01:03:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-11 20:24:12 +00:00
|
|
|
"context"
|
2022-05-11 01:03:37 +00:00
|
|
|
"database/sql"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.arsenm.dev/infinitime"
|
2023-01-04 23:06:05 +00:00
|
|
|
"go.arsenm.dev/logger/log"
|
2022-05-11 01:03:37 +00:00
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
func initMetrics(ctx context.Context, dev *infinitime.Device) error {
|
2022-05-11 01:03:37 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// If heart rate metrics enabled in config
|
|
|
|
if k.Bool("metrics.heartRate.enabled") {
|
|
|
|
// Watch heart rate
|
2022-05-11 20:24:12 +00:00
|
|
|
heartRateCh, err := dev.WatchHeartRate(ctx)
|
2022-05-11 01:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// For every heart rate sample
|
|
|
|
for heartRate := range heartRateCh {
|
|
|
|
// Get current time
|
|
|
|
unixTime := time.Now().UnixNano()
|
|
|
|
// Insert sample and time into database
|
|
|
|
db.Exec("INSERT INTO heartRate VALUES (?, ?);", unixTime, heartRate)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If step count metrics enabled in config
|
|
|
|
if k.Bool("metrics.stepCount.enabled") {
|
|
|
|
// Watch step count
|
2022-05-11 20:24:12 +00:00
|
|
|
stepCountCh, err := dev.WatchStepCount(ctx)
|
2022-05-11 01:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// For every step count sample
|
|
|
|
for stepCount := range stepCountCh {
|
|
|
|
// Get current time
|
|
|
|
unixTime := time.Now().UnixNano()
|
|
|
|
// Insert sample and time into database
|
|
|
|
db.Exec("INSERT INTO stepCount VALUES (?, ?);", unixTime, stepCount)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If battery level metrics enabled in config
|
|
|
|
if k.Bool("metrics.battLevel.enabled") {
|
|
|
|
// Watch battery level
|
2022-05-11 20:24:12 +00:00
|
|
|
battLevelCh, err := dev.WatchBatteryLevel(ctx)
|
2022-05-11 01:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// For every battery level sample
|
|
|
|
for battLevel := range battLevelCh {
|
|
|
|
// Get current time
|
|
|
|
unixTime := time.Now().UnixNano()
|
|
|
|
// Insert sample and time into database
|
|
|
|
db.Exec("INSERT INTO battLevel VALUES (?, ?);", unixTime, battLevel)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If motion metrics enabled in config
|
|
|
|
if k.Bool("metrics.motion.enabled") {
|
|
|
|
// Watch motion values
|
2022-05-11 20:24:12 +00:00
|
|
|
motionCh, err := dev.WatchMotion(ctx)
|
2022-05-11 01:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// For every motion sample
|
|
|
|
for motionVals := range motionCh {
|
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:06:05 +00:00
|
|
|
log.Info("Initialized metrics collection").Send()
|
2022-05-11 01:03:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|