-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetricparser.go
144 lines (107 loc) · 3.41 KB
/
metricparser.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
package main
import (
"math"
log "github.com/sirupsen/logrus"
"github.com/jovandeginste/medisana-bs/plugins"
"github.com/jovandeginste/medisana-bs/structs"
)
var allPersons = make([]*structs.PersonMetrics, 8)
func metricParserLogger() log.FieldLogger {
return log.WithField("component", "metricparser")
}
// StartMetricParser will initialize the Persons from csv and parse incoming metrics
func StartMetricParser() {
for name, c := range config.People {
p := &structs.PersonMetrics{
Person: c.ID,
Name: name,
BodyMetrics: make(map[int]structs.BodyMetric),
}
p.ImportBodyMetrics(structs.ImportCsv(c.ID))
metricParserLogger().Debugf("Imported person %d (%s) with %d metrics", c.ID, p.Name, len(p.BodyMetrics))
plugins.InitializeData(p)
allPersons[c.ID] = p
}
go parseMetrics()
}
func parseMetrics() {
for {
partialMetric := <-metricChan
updatePerson(partialMetric.Person)
updateBody(partialMetric.Body)
updateWeight(partialMetric.Weight)
}
}
func getPersonMetrics(personID int) *structs.PersonMetrics {
return allPersons[personID]
}
func updatePerson(update structs.Person) {
if !update.Valid {
return
}
metricParserLogger().Infof("Received person metrics: %+v", update)
person := getPersonMetrics(update.Person)
person.Gender = update.Gender
person.Age = update.Age
person.Size = update.Size
person.Activity = update.Activity
printPerson(person)
}
func updateBody(update structs.Body) {
if !update.Valid {
return
}
metricParserLogger().Infof("Received body metrics: %+v", update)
person := getPersonMetrics(update.Person)
person.Updated = true
if _, ok := person.BodyMetrics[update.Timestamp]; !ok {
metricParserLogger().Infof("No body metric - creating")
person.BodyMetrics[update.Timestamp] = structs.BodyMetric{}
}
bodyMetric := person.BodyMetrics[update.Timestamp]
bodyMetric.Timestamp = update.Timestamp
bodyMetric.Kcal = update.Kcal
bodyMetric.Fat = update.Fat
bodyMetric.Tbw = update.Tbw
bodyMetric.Muscle = update.Muscle
bodyMetric.Bone = update.Bone
bodyMetric.TimestampString = update.ToRFC3339()
bodyMetric.Name = person.Name
person.BodyMetrics[update.Timestamp] = bodyMetric
printPerson(person)
}
func updateWeight(update structs.Weight) {
if !update.Valid {
return
}
metricParserLogger().Infof("Received weight metrics: %+v", update)
person := getPersonMetrics(update.Person)
person.Updated = true
if _, ok := person.BodyMetrics[update.Timestamp]; !ok {
metricParserLogger().Infof("No body metric - creating")
person.BodyMetrics[update.Timestamp] = structs.BodyMetric{}
}
bodyMetric := person.BodyMetrics[update.Timestamp]
bodyMetric.Weight = update.Weight
bodyMetric.Timestamp = update.Timestamp
bodyMetric.TimestampString = update.ToRFC3339()
bodyMetric.Name = person.Name
if bodyMetric.Weight > 0 && person.Size > 0 {
bodyMetric.Bmi = bodyMetric.Weight / float32(math.Pow(float64(person.Size)/100, 2))
}
person.BodyMetrics[update.Timestamp] = bodyMetric
printPerson(person)
}
func printPerson(person *structs.PersonMetrics) {
metricParserLogger().Infof("Person %d (%s) now has %d metrics.", person.Person, person.Name, len(person.BodyMetrics))
}
func debounce() {
for _, person := range allPersons {
if person == nil || !person.Updated {
continue
}
metricParserLogger().Infof("Person %d (%s) was updated -- calling all plugins.", person.Person, person.Name)
plugins.ParseData(person)
person.Updated = false
}
}