author | Mikael Berthe <mikael@lilotux.net> |
Sat, 10 Jul 2021 22:15:47 +0200 | |
changeset 30 | 0df0950f4c21 |
parent 28 | 3f39d3cd68ce |
permissions | -rw-r--r-- |
4 | 1 |
// Copyright (C) 2015-2017 Mikael Berthe <mikael@lilotux.net>. All rights reserved. |
2 |
// Use of this source code is governed by the MIT license, |
|
3 |
// which can be found in the LICENSE file. |
|
5 | 4 |
// |
5 |
// Thanks to atbrask's blog post <http://www.atbrask.dk/?p=98> for the |
|
6 |
// protocol details. |
|
4 | 7 |
|
8 |
// gobm65 is a Beurer BM65 Blood Pressure Monitor CLI reader. |
|
9 |
||
0 | 10 |
package main |
11 |
||
6
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
12 |
// Installation: |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
13 |
// |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
14 |
// % go get hg.lilotux.net/golang/mikael/gobm65 |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
15 |
// |
4 | 16 |
// Examples: |
17 |
// |
|
18 |
// Get help: |
|
19 |
// % gobm65 --help |
|
20 |
// |
|
21 |
// Get records and display the average: |
|
22 |
// % gobm65 --average |
|
10 | 23 |
// ... display more statistics: |
24 |
// % gobm65 --stats |
|
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
25 |
// ... also display World Health Organization classification: |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
26 |
// % gobm65 --stats --class |
4 | 27 |
// |
28 | 28 |
// Display all records but reduce the list so that close records are replaced |
29 |
// with their average: |
|
30 |
// % gobm65 --reduce |
|
4 | 31 |
// Display the latest 3 records with the average: |
32 |
// % gobm65 -l 3 --average |
|
33 |
// Display all records since a specific date: |
|
30 | 34 |
// % gobm65 --from-date "2016-06-01" |
4 | 35 |
// % gobm65 --since "2016-06-01" |
17 | 36 |
// Display all records before a specific date: |
18
f6646f63b11a
Change option names for consistency
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
37 |
// % gobm65 --to-date "2016-06-30" |
30 | 38 |
// % gobm65 --until "2016-06-30" |
4 | 39 |
// Display all records of the last 7 days: |
40 |
// % gobm65 --since "$(date "+%F" -d "7 days ago")" |
|
41 |
// |
|
10 | 42 |
// Display statistics for morning records: |
43 |
// % gobm65 --from-time 06:00 --to-time 12:00 --stats |
|
44 |
// One can invert times to get night data: |
|
45 |
// % gobm65 --from-time 21:00 --to-time 09:00 |
|
46 |
// |
|
4 | 47 |
// Display the last/first 10 records in JSON: |
48 |
// % gobm65 -l 10 --format json |
|
49 |
// |
|
50 |
// Save the records to a JSON file: |
|
51 |
// % gobm65 -o data_u2.json |
|
52 |
// |
|
53 |
// Read a JSON file and display average of the last 3 records: |
|
54 |
// % gobm65 -i data_u2.json -l 3 --average |
|
10 | 55 |
// % gobm65 -i data_u2.json -l 3 --stats |
4 | 56 |
// Read a JSON file, merge with device records, and save to another file: |
57 |
// % gobm65 -i data_u2.json --merge -o data_u2-new.json |
|
14
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
58 |
// |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
59 |
// Data from several JSON files can be merged, files are separated with a ';': |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
60 |
// % gobm65 -i "data_u0.json;data_u1.json;data_u2.json" |
4 | 61 |
|
0 | 62 |
import ( |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
63 |
"encoding/json" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
64 |
"fmt" |
0 | 65 |
"io" |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
66 |
"io/ioutil" |
0 | 67 |
"log" |
8 | 68 |
"math" |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
69 |
"os" |
8 | 70 |
"sort" |
14
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
71 |
"strings" |
3 | 72 |
"time" |
0 | 73 |
|
24 | 74 |
flag "github.com/spf13/pflag" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
75 |
"github.com/tarm/serial" |
0 | 76 |
) |
77 |
||
78 |
type measurement struct { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
79 |
Header int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
80 |
Systolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
81 |
Diastolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
82 |
Pulse int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
83 |
Month int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
84 |
Day int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
85 |
Hour int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
86 |
Minute int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
87 |
Year int |
0 | 88 |
} |
89 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
90 |
type simpleTime struct { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
91 |
hour, minute int |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
92 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
93 |
|
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
94 |
// World Heath Organization blood pressure classification |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
95 |
const ( |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
96 |
BPOptimal = iota // < 120,80: Optimal |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
97 |
BPNormal // < 130,85: Normal |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
98 |
BPHighNormal // < 140,90: High-Normal |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
99 |
BPMildHypertension // < 160,100: Grade 1 Mild Hypertension |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
100 |
BPModerateHypertension // < 180,110: Grade 2 Moderate Hypertension |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
101 |
BPSevereHypertension // >=180,110: Grade 3 Severe Hypertension |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
102 |
) |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
103 |
|
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
104 |
// Special cases that do not fit in the previous classification |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
105 |
const ( |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
106 |
// >=140, <90: Isolated Systolic Hypertension |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
107 |
IsolatedSystolicHypertension = 1 |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
108 |
) |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
109 |
|
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
110 |
// WHOPressureClassification contains the World Health Organization blood |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
111 |
// pressure categories |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
112 |
var WHOPressureClassification = []string{ |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
113 |
"Optimal", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
114 |
"Normal", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
115 |
"High-Normal", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
116 |
"Mild Hypertension", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
117 |
"Moderate Hypertension", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
118 |
"Severe Hypertension", |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
119 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
120 |
|
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
121 |
// WHOPressureFlag is an array of special cases |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
122 |
var WHOPressureFlag = []string{ |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
123 |
"", |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
124 |
"Isolated Systolic Hypertension", |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
125 |
} |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
126 |
|
0 | 127 |
func getData(s io.ReadWriteCloser, buf []byte, size int) (int, error) { |
128 |
t := 0 |
|
129 |
b := buf |
|
130 |
for t < size { |
|
131 |
n, err := s.Read(b[t:]) |
|
132 |
if err != nil { |
|
133 |
log.Fatal(err) // XXX |
|
134 |
return t, err |
|
135 |
} |
|
136 |
//log.Printf("(%d bytes) %q\n", n, b[t:t+1]) |
|
27
e882f78c302e
Add go modules; fix a linting warning
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
137 |
t += n |
0 | 138 |
} |
139 |
return t, nil |
|
140 |
} |
|
141 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
142 |
func fetchData(dev string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
143 |
c := &serial.Config{Name: dev, Baud: 4800} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
144 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
145 |
var s *serial.Port |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
146 |
s, err = serial.OpenPort(c) |
0 | 147 |
if err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
148 |
return items, err |
0 | 149 |
} |
150 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
151 |
// =================== Handshake ===================== |
0 | 152 |
q := []byte("\xaa") |
153 |
//log.Printf("Query: %q\n", q) |
|
154 |
log.Println("Starting handshake...") |
|
155 |
n, err := s.Write(q) |
|
156 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
157 |
return items, err |
0 | 158 |
} |
159 |
||
160 |
buf := make([]byte, 128) |
|
161 |
n, err = getData(s, buf, 1) |
|
162 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
163 |
return items, err |
0 | 164 |
} |
165 |
if n == 1 && buf[0] == '\x55' { |
|
166 |
log.Println("Handshake successful.") |
|
167 |
} else { |
|
168 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
169 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
170 |
return items, fmt.Errorf("handshake failed") |
0 | 171 |
} |
172 |
||
173 |
// =================== Desc ===================== |
|
174 |
q = []byte("\xa4") |
|
175 |
//log.Printf("Query: %q\n", q) |
|
176 |
log.Println("Requesting device description...") |
|
177 |
n, err = s.Write(q) |
|
178 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
179 |
return items, err |
0 | 180 |
} |
181 |
||
182 |
n, err = getData(s, buf, 32) |
|
183 |
log.Printf("DESC> %q\n", buf[:n]) |
|
184 |
||
185 |
// =================== Count ===================== |
|
186 |
q = []byte("\xa2") |
|
187 |
//log.Printf("Query: %q\n", q) |
|
188 |
log.Println("Requesting data counter...") |
|
189 |
n, err = s.Write(q) |
|
190 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
191 |
return items, err |
0 | 192 |
} |
193 |
||
194 |
n, err = getData(s, buf, 1) |
|
195 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
196 |
return items, err |
0 | 197 |
} |
198 |
var nRecords int |
|
199 |
if n == 1 { |
|
200 |
log.Printf("%d item(s) available.", buf[0]) |
|
201 |
nRecords = int(buf[0]) |
|
202 |
} else { |
|
203 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
204 |
return items, fmt.Errorf("no measurement found") |
0 | 205 |
} |
206 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
207 |
// =================== Records ===================== |
0 | 208 |
for i := 0; i < nRecords; i++ { |
209 |
q = []byte{'\xa3', uint8(i + 1)} |
|
210 |
//log.Printf("Query: %q\n", q) |
|
211 |
//log.Printf("Requesting measurement %d...", i+1) |
|
212 |
n, err = s.Write(q) |
|
213 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
214 |
return items, err |
0 | 215 |
} |
216 |
||
217 |
n, err = getData(s, buf, 9) |
|
218 |
//log.Printf("DESC> %q\n", buf[:n]) |
|
219 |
||
220 |
var data measurement |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
221 |
data.Header = int(buf[0]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
222 |
data.Systolic = int(buf[1]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
223 |
data.Diastolic = int(buf[2]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
224 |
data.Pulse = int(buf[3]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
225 |
data.Month = int(buf[4]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
226 |
data.Day = int(buf[5]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
227 |
data.Hour = int(buf[6]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
228 |
data.Minute = int(buf[7]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
229 |
data.Year = int(buf[8]) + 2000 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
230 |
items = append(items, data) |
0 | 231 |
} |
232 |
||
233 |
s.Close() |
|
15
009c05621fba
Ensure records read from the devices are correctly sorted
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
234 |
return mergeItems(items, []measurement{}), nil |
0 | 235 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
236 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
237 |
func loadFromJSONFile(filename string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
238 |
data, err := ioutil.ReadFile(filename) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
239 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
240 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
241 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
242 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
243 |
err = json.Unmarshal(data, &items) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
244 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
245 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
246 |
|
14
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
247 |
func loadFromJSONFiles(files string) (items []measurement, err error) { |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
248 |
filenames := strings.Split(files, ";") |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
249 |
|
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
250 |
for _, f := range filenames { |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
251 |
if f == "" { |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
252 |
continue |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
253 |
} |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
254 |
records, err := loadFromJSONFile(f) |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
255 |
if err != nil { |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
256 |
return items, err |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
257 |
} |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
258 |
items = mergeItems(records, items) |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
259 |
} |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
260 |
|
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
261 |
return |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
262 |
} |
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
263 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
264 |
func mergeItems(newItems, oldItems []measurement) []measurement { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
265 |
var result []measurement |
11 | 266 |
|
267 |
// Sort method: isLater returns true if mi's date is later or |
|
268 |
// equal to mj's date. |
|
269 |
isLater := func(mi, mj measurement) bool { |
|
270 |
switch { |
|
271 |
case mi.Year < mj.Year: |
|
272 |
return false |
|
273 |
case mi.Year > mj.Year: |
|
274 |
return true |
|
275 |
case mi.Month < mj.Month: |
|
276 |
return false |
|
277 |
case mi.Month > mj.Month: |
|
278 |
return true |
|
279 |
case mi.Day < mj.Day: |
|
280 |
return false |
|
281 |
case mi.Day > mj.Day: |
|
282 |
return true |
|
283 |
case mi.Hour < mj.Hour: |
|
284 |
return false |
|
285 |
case mi.Hour > mj.Hour: |
|
286 |
return true |
|
287 |
case mi.Minute < mj.Minute: |
|
288 |
return false |
|
289 |
default: |
|
290 |
return true |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
291 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
292 |
} |
11 | 293 |
|
12
fc4f5c69286b
Add note about sort.Slice()
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
294 |
// Note that sort.Slice was introduced in go 1.8 |
11 | 295 |
sort.Slice(oldItems, func(i, j int) bool { |
296 |
return isLater(oldItems[i], oldItems[j]) |
|
297 |
}) |
|
298 |
sort.Slice(newItems, func(i, j int) bool { |
|
299 |
return isLater(newItems[i], newItems[j]) |
|
300 |
}) |
|
301 |
||
13 | 302 |
// insertIfMissing inserts a measurement into a sorted slice |
303 |
insertIfMissing := func(l []measurement, m measurement) []measurement { |
|
304 |
var later bool |
|
11 | 305 |
var i int |
306 |
for i = range l { |
|
13 | 307 |
later = isLater(l[i], m) |
308 |
if !later { |
|
11 | 309 |
break |
310 |
} |
|
311 |
if l[i] == m { // Duplicate |
|
312 |
return l |
|
313 |
} |
|
314 |
} |
|
13 | 315 |
if later { |
316 |
return append(l, m) |
|
11 | 317 |
} |
13 | 318 |
|
319 |
return append(l[:i], append([]measurement{m}, l[i:]...)...) |
|
11 | 320 |
} |
321 |
||
322 |
for _, item := range newItems { |
|
13 | 323 |
result = insertIfMissing(result, item) |
11 | 324 |
} |
325 |
for _, item := range oldItems { |
|
13 | 326 |
result = insertIfMissing(result, item) |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
327 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
328 |
return result |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
329 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
330 |
|
3 | 331 |
func parseDate(dateStr string) (date time.Time, err error) { |
332 |
if dateStr == "" { |
|
333 |
return |
|
334 |
} |
|
335 |
||
336 |
var yy, mm, dd, h, m, s int |
|
337 |
n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
338 |
if e != nil && n < 3 { |
|
339 |
err = e |
|
340 |
return |
|
341 |
} |
|
342 |
if n < 6 { |
|
343 |
log.Printf("Date parsed with only %d fields\n", n) |
|
344 |
} |
|
345 |
date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
346 |
return |
|
347 |
} |
|
348 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
349 |
func parseTime(timeStr string) (t simpleTime, err error) { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
350 |
_, err = fmt.Sscanf(timeStr, "%d:%d", &t.hour, &t.minute) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
351 |
return |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
352 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
353 |
|
28 | 354 |
// diffTime returns the duration between two (decreasing) sorted measurements |
355 |
func diffTime(a, b measurement) time.Duration { |
|
356 |
dateA := time.Date(a.Year, time.Month(a.Month), a.Day, a.Hour, a.Minute, 0, 0, time.Local) |
|
357 |
dateB := time.Date(b.Year, time.Month(b.Month), b.Day, b.Hour, b.Minute, 0, 0, time.Local) |
|
358 |
return dateA.Sub(dateB) |
|
359 |
} |
|
360 |
||
8 | 361 |
func average(items []measurement) (measurement, error) { |
362 |
var avgMeasure measurement |
|
363 |
var avgCount int |
|
364 |
||
365 |
for _, data := range items { |
|
366 |
avgMeasure.Systolic += data.Systolic |
|
367 |
avgMeasure.Diastolic += data.Diastolic |
|
368 |
avgMeasure.Pulse += data.Pulse |
|
369 |
avgCount++ |
|
370 |
} |
|
371 |
||
372 |
roundDivision := func(a, b int) int { |
|
373 |
return int(0.5 + float64(a)/float64(b)) |
|
374 |
} |
|
375 |
||
376 |
if avgCount == 0 { |
|
377 |
return avgMeasure, fmt.Errorf("cannot compute average: empty set") |
|
378 |
} |
|
379 |
||
380 |
avgMeasure.Systolic = roundDivision(avgMeasure.Systolic, avgCount) |
|
381 |
avgMeasure.Diastolic = roundDivision(avgMeasure.Diastolic, avgCount) |
|
382 |
avgMeasure.Pulse = roundDivision(avgMeasure.Pulse, avgCount) |
|
383 |
||
384 |
return avgMeasure, nil |
|
385 |
} |
|
386 |
||
387 |
func intMedian(numbers []int) int { |
|
388 |
middle := len(numbers) / 2 |
|
389 |
med := numbers[middle] |
|
390 |
if len(numbers)%2 == 0 { |
|
391 |
med = (med + numbers[middle-1]) / 2 |
|
392 |
} |
|
393 |
return med |
|
394 |
} |
|
395 |
||
396 |
func median(items []measurement) (measurement, error) { |
|
397 |
var med measurement |
|
398 |
if len(items) == 0 { |
|
399 |
return med, fmt.Errorf("cannot compute average: empty set") |
|
400 |
} |
|
401 |
||
402 |
var sys, dia, pul []int |
|
403 |
for _, data := range items { |
|
404 |
sys = append(sys, data.Systolic) |
|
405 |
dia = append(dia, data.Diastolic) |
|
406 |
pul = append(pul, data.Pulse) |
|
407 |
} |
|
408 |
||
409 |
sort.Ints(sys) |
|
410 |
sort.Ints(dia) |
|
411 |
sort.Ints(pul) |
|
412 |
||
413 |
med.Systolic = intMedian(sys) |
|
414 |
med.Diastolic = intMedian(dia) |
|
415 |
med.Pulse = intMedian(pul) |
|
416 |
||
417 |
return med, nil |
|
418 |
} |
|
419 |
||
420 |
func stdDeviation(items []measurement) (measurement, error) { |
|
421 |
var sDev measurement |
|
422 |
||
423 |
if len(items) <= 1 { |
|
424 |
return sDev, fmt.Errorf("cannot compute deviation: set too small") |
|
425 |
} |
|
426 |
||
427 |
var sumSys, sumDia, sumPul float64 |
|
428 |
avg, err := average(items) |
|
429 |
if err != nil { |
|
430 |
return sDev, err |
|
431 |
} |
|
432 |
||
433 |
for _, data := range items { |
|
434 |
sumSys += math.Pow(float64(data.Systolic-avg.Systolic), 2) |
|
435 |
sumDia += math.Pow(float64(data.Diastolic-avg.Diastolic), 2) |
|
436 |
sumPul += math.Pow(float64(data.Pulse-avg.Pulse), 2) |
|
437 |
} |
|
438 |
||
20
fe86e794b63a
Fix standard deviation calculation
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
439 |
sDev.Systolic = int(math.Sqrt(sumSys / float64(len(items)))) |
fe86e794b63a
Fix standard deviation calculation
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
440 |
sDev.Diastolic = int(math.Sqrt(sumDia / float64(len(items)))) |
fe86e794b63a
Fix standard deviation calculation
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
441 |
sDev.Pulse = int(math.Sqrt(sumPul / float64(len(items)))) |
8 | 442 |
|
443 |
return sDev, nil |
|
444 |
} |
|
445 |
||
19
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
446 |
// avgAbsoluteDeviation computes the average absolute deviation (or Mean |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
447 |
// Absolute Deviation) of the measurement set |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
448 |
func avgAbsoluteDeviation(items []measurement) (measurement, error) { |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
449 |
var dev measurement |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
450 |
|
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
451 |
if len(items) <= 1 { |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
452 |
return dev, fmt.Errorf("cannot compute deviation: set too small") |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
453 |
} |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
454 |
|
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
455 |
var sumSys, sumDia, sumPul float64 |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
456 |
avg, err := average(items) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
457 |
if err != nil { |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
458 |
return dev, err |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
459 |
} |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
460 |
|
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
461 |
for _, data := range items { |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
462 |
sumSys += math.Abs(float64(data.Systolic - avg.Systolic)) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
463 |
sumDia += math.Abs(float64(data.Diastolic - avg.Diastolic)) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
464 |
sumPul += math.Abs(float64(data.Pulse - avg.Pulse)) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
465 |
} |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
466 |
|
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
467 |
dev.Systolic = int(sumSys / float64(len(items))) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
468 |
dev.Diastolic = int(sumDia / float64(len(items))) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
469 |
dev.Pulse = int(sumPul / float64(len(items))) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
470 |
|
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
471 |
return dev, nil |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
472 |
} |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
473 |
|
28 | 474 |
// reduceList reduces the measurement list by regrouping close measurements; |
475 |
// if the time difference between several measurements is less than the given |
|
476 |
// threshold, they are replaced with their average. |
|
477 |
func reduceList(list []measurement, threshold time.Duration) []measurement { |
|
478 |
var newList, acc []measurement |
|
479 |
||
480 |
// flushAcc flushes the accumulator and adds an average measurement to |
|
481 |
// the new list. |
|
482 |
flushAcc := func() { |
|
483 |
// setAvgTime computes the average date of the list and sets |
|
484 |
// the measurement date to this average. |
|
485 |
setAvgTime := func(m *measurement, l []measurement) { |
|
486 |
var timestamp int64 = 0 |
|
487 |
for _, i := range l { |
|
488 |
iDate := time.Date(i.Year, time.Month(i.Month), i.Day, |
|
489 |
i.Hour, i.Minute, 0, 0, time.Local) |
|
490 |
timestamp += iDate.Unix() |
|
491 |
} |
|
492 |
timestamp /= int64(len(l)) |
|
493 |
avgTime := time.Unix(timestamp, 0) |
|
494 |
m.Year = avgTime.Year() |
|
495 |
m.Month = int(avgTime.Month()) |
|
496 |
m.Day = avgTime.Day() |
|
497 |
m.Hour = avgTime.Hour() |
|
498 |
m.Minute = avgTime.Minute() |
|
499 |
} |
|
500 |
avg, _ := average(acc) |
|
501 |
avg.Header = acc[0].Header // Arbitrary… |
|
502 |
setAvgTime(&avg, acc) |
|
503 |
newList = append(newList, avg) |
|
504 |
acc = acc[:0] |
|
505 |
} |
|
506 |
||
507 |
for _, item := range list { |
|
508 |
if len(acc) > 0 && diffTime(acc[len(acc)-1], item) > threshold { |
|
509 |
flushAcc() |
|
510 |
} |
|
511 |
acc = append(acc, item) |
|
512 |
} |
|
513 |
flushAcc() |
|
514 |
return newList |
|
515 |
} |
|
516 |
||
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
517 |
func (m measurement) WHOClass() (int, int) { |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
518 |
flag := 0 |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
519 |
|
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
520 |
if m.Systolic >= 140 && m.Diastolic < 90 { |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
521 |
flag = IsolatedSystolicHypertension |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
522 |
} |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
523 |
|
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
524 |
switch { |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
525 |
case m.Systolic < 120 && m.Diastolic < 80: |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
526 |
return BPOptimal, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
527 |
case m.Systolic < 130 && m.Diastolic < 85: |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
528 |
return BPNormal, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
529 |
case m.Systolic < 140 && m.Diastolic < 90: |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
530 |
return BPHighNormal, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
531 |
case m.Systolic < 160 && m.Diastolic < 100: |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
532 |
return BPMildHypertension, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
533 |
case m.Systolic < 180 && m.Diastolic < 110: |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
534 |
return BPModerateHypertension, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
535 |
} |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
536 |
return BPSevereHypertension, flag |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
537 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
538 |
|
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
539 |
func (m measurement) WHOClassString() string { |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
540 |
flagStr := "" |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
541 |
class, flag := m.WHOClass() |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
542 |
if flag == IsolatedSystolicHypertension { |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
543 |
flagStr = " (" + WHOPressureFlag[flag] + ")" |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
544 |
} |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
545 |
return WHOPressureClassification[class] + flagStr |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
546 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
547 |
|
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
548 |
func displayWHOClassStats(items []measurement) { |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
549 |
sum := 0.0 |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
550 |
classes := make(map[int]int) |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
551 |
for _, m := range items { |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
552 |
s, flag := m.WHOClass() |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
553 |
classes[s]++ |
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
554 |
sum += float64(s) |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
555 |
if flag == IsolatedSystolicHypertension { |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
556 |
sum += 0.5 |
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
557 |
} |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
558 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
559 |
|
23
7be02d3facf4
Display Isolated Systolic Hypertension
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
560 |
avg := sum / float64(len(items)) |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
561 |
fmt.Fprintf(os.Stderr, "Average WHO classification: %s (%.2f)\n", |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
562 |
WHOPressureClassification[int(0.5+avg)], avg) |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
563 |
|
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
564 |
for c := range WHOPressureClassification { |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
565 |
fmt.Fprintf(os.Stderr, " . %21s: %3d (%d%%)\n", |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
566 |
WHOPressureClassification[c], classes[c], |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
567 |
classes[c]*100/len(items)) |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
568 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
569 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
570 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
571 |
func main() { |
24 | 572 |
inFile := flag.StringP("input-file", "i", "", "Input JSON file") |
573 |
outFile := flag.StringP("output-file", "o", "", "Output JSON file") |
|
574 |
limit := flag.UintP("limit", "l", 0, "Limit number of items to N first") |
|
575 |
toDate := flag.String("to-date", "", "Filter records before date (YYYY-mm-dd HH:MM:SS)") |
|
576 |
fromDate := flag.String("from-date", "", "Filter records from date (YYYY-mm-dd HH:MM:SS)") |
|
577 |
format := flag.StringP("format", "f", "", "Output format (csv, json)") |
|
578 |
avg := flag.BoolP("average", "a", false, "Compute average") |
|
579 |
stats := flag.Bool("stats", false, "Compute statistics") |
|
580 |
whoClass := flag.BoolP("class", "c", false, "Display WHO classification") |
|
581 |
merge := flag.BoolP("merge", "m", false, "Try to merge input JSON file with fetched data") |
|
582 |
device := flag.StringP("device", "d", "/dev/ttyUSB0", "Serial device") |
|
583 |
fromTime := flag.String("from-time", "", "Select records after time (HH:MM)") |
|
584 |
toTime := flag.String("to-time", "", "Select records bofore time (HH:MM)") |
|
28 | 585 |
reduce := flag.BoolP("reduce", "r", false, "Reduce number of measurements (regroup measurements)") |
24 | 586 |
|
587 |
flag.StringVar(fromDate, "since", "", "Same as --from-date") |
|
30 | 588 |
flag.StringVar(toDate, "until", "", "Same as --to-date") |
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
589 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
590 |
var startTime, endTime simpleTime |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
591 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
592 |
flag.Parse() |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
593 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
594 |
switch *format { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
595 |
case "": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
596 |
if *outFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
597 |
*format = "csv" |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
598 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
599 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
600 |
case "json", "csv": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
601 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
602 |
default: |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
603 |
log.Fatal("Unknown output format. Possible choices are csv, json.") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
604 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
605 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
606 |
if *fromTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
607 |
if t, err := parseTime(*fromTime); err != nil { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
608 |
log.Fatal("Cannot parse 'from' time: ", err) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
609 |
} else { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
610 |
startTime = t |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
611 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
612 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
613 |
if *toTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
614 |
if t, err := parseTime(*toTime); err != nil { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
615 |
log.Fatal("Cannot parse 'to' time: ", err) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
616 |
} else { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
617 |
endTime = t |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
618 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
619 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
620 |
|
18
f6646f63b11a
Change option names for consistency
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
621 |
startDate, err := parseDate(*fromDate) |
3 | 622 |
if err != nil { |
623 |
log.Fatal("Could not parse date: ", err) |
|
624 |
} |
|
625 |
||
18
f6646f63b11a
Change option names for consistency
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
626 |
endDate, err := parseDate(*toDate) |
16 | 627 |
if err != nil { |
628 |
log.Fatal("Could not parse date: ", err) |
|
629 |
} |
|
630 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
631 |
var items []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
632 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
633 |
// Read data |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
634 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
635 |
if *inFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
636 |
// Read from device |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
637 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
638 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
639 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
640 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
641 |
// Read from file |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
642 |
var fileItems []measurement |
14
be004d8634e1
Add support for several input JSON files
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
643 |
if fileItems, err = loadFromJSONFiles(*inFile); err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
644 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
645 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
646 |
if *merge { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
647 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
648 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
649 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
650 |
items = mergeItems(items, fileItems) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
651 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
652 |
items = fileItems |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
653 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
654 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
655 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
656 |
// Apply filters |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
657 |
|
3 | 658 |
if !startDate.IsZero() { |
659 |
log.Printf("Filtering out records before %v...\n", startDate) |
|
660 |
for i := range items { |
|
661 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
662 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
663 |
time.Local) |
|
664 |
if iDate.Sub(startDate) < 0 { |
|
665 |
items = items[0:i] |
|
666 |
break |
|
667 |
} |
|
668 |
} |
|
669 |
} |
|
670 |
||
16 | 671 |
if !endDate.IsZero() { |
672 |
log.Printf("Filtering out records after %v...\n", endDate) |
|
673 |
for i := range items { |
|
674 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
675 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
676 |
time.Local) |
|
677 |
if iDate.Sub(endDate) <= 0 { |
|
678 |
items = items[i:] |
|
679 |
break |
|
680 |
} |
|
681 |
} |
|
682 |
} |
|
683 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
684 |
if *fromTime != "" || *toTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
685 |
log.Println("Filtering hours...") |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
686 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
687 |
compare := func(m measurement, t simpleTime) int { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
688 |
if m.Hour*60+m.Minute < t.hour*60+t.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
689 |
return -1 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
690 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
691 |
if m.Hour*60+m.Minute > t.hour*60+t.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
692 |
return 1 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
693 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
694 |
return 0 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
695 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
696 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
697 |
inv := false |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
698 |
if *fromTime != "" && *toTime != "" && |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
699 |
startTime.hour*60+startTime.minute > endTime.hour*60+endTime.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
700 |
inv = true |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
701 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
702 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
703 |
var newItems []measurement |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
704 |
for _, data := range items { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
705 |
if inv { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
706 |
if compare(data, startTime) == -1 && compare(data, endTime) == 1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
707 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
708 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
709 |
newItems = append(newItems, data) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
710 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
711 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
712 |
if *fromTime != "" && compare(data, startTime) == -1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
713 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
714 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
715 |
if *toTime != "" && compare(data, endTime) == 1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
716 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
717 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
718 |
newItems = append(newItems, data) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
719 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
720 |
items = newItems |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
721 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
722 |
|
28 | 723 |
if *reduce { |
724 |
items = reduceList(items, time.Hour) |
|
725 |
} |
|
726 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
727 |
if *limit > 0 && len(items) > int(*limit) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
728 |
items = items[0:*limit] |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
729 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
730 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
731 |
// Done with filtering |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
732 |
|
8 | 733 |
if *format == "csv" { |
734 |
for i, data := range items { |
|
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
735 |
fmt.Printf("%d;%x;%d-%02d-%02d %02d:%02d;%d;%d;%d", |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
736 |
i+1, data.Header, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
737 |
data.Year, data.Month, data.Day, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
738 |
data.Hour, data.Minute, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
739 |
data.Systolic, data.Diastolic, data.Pulse) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
740 |
if *whoClass { |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
741 |
fmt.Printf(";%s", data.WHOClassString()) |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
742 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
743 |
fmt.Println() |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
744 |
} |
8 | 745 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
746 |
|
8 | 747 |
if *stats { |
748 |
*avg = true |
|
749 |
} |
|
750 |
||
751 |
if *avg && len(items) > 0 { |
|
752 |
avgMeasure, err := average(items) |
|
753 |
if err != nil { |
|
754 |
log.Println("Error:", err) |
|
755 |
} else { |
|
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
756 |
fmt.Fprintf(os.Stderr, "Average: %d;%d;%d", |
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
757 |
avgMeasure.Systolic, avgMeasure.Diastolic, |
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
758 |
avgMeasure.Pulse) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
759 |
if *whoClass { |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
760 |
fmt.Fprintf(os.Stderr, " [%s]", |
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
761 |
avgMeasure.WHOClassString()) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
762 |
} |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
763 |
fmt.Fprintln(os.Stderr) |
8 | 764 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
765 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
766 |
|
8 | 767 |
if *stats && len(items) > 1 { |
768 |
d, err := stdDeviation(items) |
|
769 |
if err != nil { |
|
770 |
log.Println("Error:", err) |
|
771 |
} else { |
|
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
772 |
fmt.Fprintf(os.Stderr, "Standard deviation: %d;%d;%d\n", |
8 | 773 |
d.Systolic, d.Diastolic, d.Pulse) |
7
17a1a3f4fb86
Round results for average calculation
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
774 |
} |
19
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
775 |
d, err = avgAbsoluteDeviation(items) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
776 |
if err != nil { |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
777 |
log.Println("Error:", err) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
778 |
} else { |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
779 |
fmt.Fprintf(os.Stderr, "Average absolute deviation: %d;%d;%d\n", |
19
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
780 |
d.Systolic, d.Diastolic, d.Pulse) |
afbb4d9ae536
Add Mean Absolute Deviation to the statistics
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
781 |
} |
8 | 782 |
} |
783 |
if *stats && len(items) > 0 { |
|
784 |
m, err := median(items) |
|
785 |
if err != nil { |
|
786 |
log.Println("Error:", err) |
|
787 |
} else { |
|
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
788 |
fmt.Fprintf(os.Stderr, "Median values: %d;%d;%d", |
8 | 789 |
m.Systolic, m.Diastolic, m.Pulse) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
790 |
if *whoClass { |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
791 |
fmt.Fprintf(os.Stderr, " [%s]", m.WHOClassString()) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
792 |
} |
22
853f58e76ba5
Output statistics to stderr, not stdout
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
793 |
fmt.Fprintln(os.Stderr) |
21
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
794 |
} |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
795 |
|
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
796 |
if *whoClass { |
c00a10738af3
Add option --class to display WHO blood pressure classification
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
797 |
displayWHOClassStats(items) |
8 | 798 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
799 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
800 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
801 |
if *format == "json" || *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
802 |
rawJSON, err := json.MarshalIndent(items, "", " ") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
803 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
804 |
log.Fatal("Error:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
805 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
806 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
807 |
if *format == "json" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
808 |
fmt.Println(string(rawJSON)) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
809 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
810 |
if *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
811 |
err = ioutil.WriteFile(*outFile, rawJSON, 0600) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
812 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
813 |
log.Println("Could not write output file:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
814 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
815 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
816 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
817 |
} |