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