forked from loudnate/openaps-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highchart.py
123 lines (100 loc) · 3.34 KB
/
highchart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# coding=utf-8
"""
Munges data into formats appropriate for Highcharts usage
http://api.highcharts.com/highcharts
"""
from datetime import datetime
from dateutil.parser import parse
from itertools import chain
def timestamp(a_datetime):
return int((a_datetime - datetime(1970,1,1)).total_seconds()) * 1000
def line_chart(entries, name=''):
rows = []
for entry in entries:
date = entry.get('dateString') or entry.get('display_time') or entry['date']
amount = entry.get('sgv', entry.get('amount', entry.get('glucose')))
rows.append({
'x': timestamp(parse(date)),
'y': amount,
'name': name
})
return rows
def glucose_target_range_chart(targets, *args):
rows = []
start_timestamp = None
end_timestamp = None
for entry in chain(*args):
date = entry['x']
if start_timestamp is None or date < start_timestamp:
start_timestamp = date
if end_timestamp is None or date > end_timestamp:
end_timestamp = date
if start_timestamp is not None and end_timestamp is not None:
start_time = datetime.fromtimestamp(start_timestamp / 1000).time()
end_time = datetime.fromtimestamp(end_timestamp / 1000).time()
start_target = targets.at(start_time)
# TODO: Parse multiple targets
end_target = targets.at(end_time)
rows.append({
'x': start_timestamp,
'low': end_target['low'],
'high': end_target['high']
})
rows.append({
'x': end_timestamp,
'low': end_target['low'],
'high': end_target['high']
})
return rows
def input_history_area_chart(normalized_history):
basal = []
bolus = []
square = []
carbs = []
for entry in normalized_history:
if entry['unit'] == 'U/hour':
values = [
{
'x': timestamp(parse(entry['start_at'])),
'y': entry['amount'],
'name': entry['description']
},
{
'x': timestamp(parse(entry['end_at'])),
'y': entry['amount'],
'name': entry['description']
},
{
'x': timestamp(parse(entry['end_at'])),
'y': None
}
]
if entry['type'] == 'TempBasal':
basal += values
else:
square += values
elif entry['unit'] == 'U':
bolus += [
{
'x': timestamp(parse(entry['start_at'])),
'y': entry['amount'],
'name': entry['description']
},
{
'x': timestamp(parse(entry['end_at'])),
'y': None
},
]
elif entry['unit'] == 'g':
carbs += [
{
'x': timestamp(parse(entry['start_at'])),
'y': entry['amount'],
'name': entry['description']
},
{
'x': timestamp(parse(entry['end_at'])),
'y': None
},
]
return basal, bolus, square, carbs