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