-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.go
149 lines (142 loc) · 3.85 KB
/
summary.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
package export
import (
"github.com/opendroid/hk/logger"
"go.uber.org/zap"
"sort"
)
// RecordsSummary returns a summary of HealthData in a sorted slice.
func (h *HealthData) RecordsSummary() NameTypeKeyCounts {
if h == nil {
return nil
}
// d holds map of summary data
d := make(map[string]map[string]map[string]int)
// Get unique [SourceNames][Type][MetadataEntryKey] count
entries := 0
for _, v := range h.Records {
if _, ok := d[v.SourceName]; !ok {
d[v.SourceName] = make(map[string]map[string]int)
}
if _, ok := d[v.SourceName][v.Type]; !ok {
d[v.SourceName][v.Type] = make(map[string]int)
}
for _, md := range v.MetadataEntries {
mdKey := shortenHKKey(md.Key)
d[v.SourceName][v.Type][mdKey]++
entries++
}
hrvCount := 0
if len(v.HRV) > 0 {
for _, h := range v.HRV {
d[v.SourceName][v.Type]["HRV"] += len(h.BPM)
entries++
hrvCount += len(h.BPM)
}
}
if len(v.MetadataEntries) == 0 && hrvCount == 0 {
d[v.SourceName][v.Type]["-"]++
entries++
}
}
// Convert map to an array NameTypeKeyCounts{{Name, Type, Key, Count},{}}
rs := make(NameTypeKeyCounts, 0, entries)
total := 0
for ks, vs := range d { // SourceNames
for kt, vt := range vs { // Types
if kc, err := maps2SortedSlice(vt); err != nil {
logger.Error("RecordsSummary maps2SortedSlice error",
zap.String("info", err.Error()),
zap.Int("entries", entries))
continue
} else {
for _, v := range kc {
rs = append(rs, NameTypeKeyCount{ks, shortenHKKey(kt), v})
total += v.Count
}
}
}
}
sort.Sort(rs) // Sort each type by decreasing order
// Add total as last element
rs = append(rs, NameTypeKeyCount{"Total", "-", KeyCount{"-", total}})
return rs
}
// RecordsSources returns summary of all Record by SourceName
func (h *HealthData) RecordsSources() KeyCounts {
if h == nil {
return nil
}
// d holds map of summary data
d := make(map[string]int)
entries := 0
for _, v := range h.Records {
d[v.SourceName] += len(v.MetadataEntries) // Add all metadata entries
entries += len(v.MetadataEntries)
hrvCount := 0
for _, hrv := range v.HRV {
d[v.SourceName] += len(hrv.BPM)
entries += len(hrv.BPM)
hrvCount += len(hrv.BPM)
}
if len(v.MetadataEntries) == 0 && hrvCount == 0 {
entries++
d[v.SourceName]++ // Yet another records
}
}
// Convert map to an array NameTypeKeyCounts{{Name, Type, Key, Count},{}}
rs, err := maps2SortedSlice(d)
if err != nil {
logger.Error("RecordsSources maps2SortedSlice error",
zap.String("info", err.Error()),
zap.Int("entries", entries))
return nil
}
sort.Sort(rs)
rs = append(rs, KeyCount{"Total", entries})
return rs
}
// RecordsTypes returns summary of all Record by SourceName
func (h *HealthData) RecordsTypes() KeyCounts {
if h == nil {
return nil
}
// d holds map of summary data
d := make(map[string]int)
entries := 0
for _, v := range h.Records {
t := shortenHKKey(v.Type)
d[t] += len(v.MetadataEntries) // Add all metadata entries
entries += len(v.MetadataEntries)
hrvCount := 0
for _, hrv := range v.HRV {
d[t] += len(hrv.BPM)
entries += len(hrv.BPM)
hrvCount += len(hrv.BPM)
}
if len(v.MetadataEntries) == 0 && hrvCount == 0 {
entries++
d[t]++ // Yet another records
}
}
// Convert map to an array NameTypeKeyCounts{{Name, Type, Key, Count},{}}
rs, err := maps2SortedSlice(d)
if err != nil {
logger.Error("RecordsTypes maps2SortedSlice error",
zap.String("info", err.Error()),
zap.Int("entries", entries))
return nil
}
sort.Sort(rs)
rs = append(rs, KeyCount{"Total", entries})
return rs
}
// Summary returns a sorted []ActivitySummary data
func (h *HealthData) Summary() []ActivitySummary {
if h == nil {
return nil
}
d := make([]ActivitySummary, len(h.ActivitiesSummary))
copy(d, h.ActivitiesSummary) // Copy slice
sort.Slice(d, func(i, j int) bool { return d[i].YYYYMMDD < d[j].YYYYMMDD })
return d
}