@@ -99,14 +99,16 @@ def get_context_data(self, **kwargs):
99
99
100
100
total_span += span
101
101
102
+ # errors here if there are 0 or 1 usages, obviously
102
103
average_span = round_timedelta_to_15min_floor (total_span / (usage_count - 1 ))
104
+ scale_factor = get_graph_normalization_divisor (longest_span .total_seconds (), 600 )
103
105
104
106
return add_header_info ({'usages' : usages , 'usage_count' : usage_count , 'usage_average' : usage_average ,
105
107
'usage_high' : highest_administered , 'usage_low' : lowest_administered ,
106
108
'usage_total' : total_administered ,
107
109
'sub_name' : Substance .objects .filter (pk = self .kwargs ['pk' ])[0 ].common_name ,
108
110
'sub_id' : self .kwargs ['pk' ], 'longest_span' : longest_span ,
109
- 'shortest_span' : shortest_span , 'timespans' : timespans ,
111
+ 'shortest_span' : shortest_span , 'timespans' : timespans , 'scale_factor' : scale_factor ,
110
112
'average_span' : average_span })
111
113
112
114
@@ -126,22 +128,49 @@ def dump_dose_graph_data(request, sub_id):
126
128
:return:
127
129
"""
128
130
129
- usage_graph_data = {}
130
- # usage_sub_id = request.POST['sub']
131
- usages = Usage .objects .filter (sub = sub_id )[:20 ] # pk or usage_sub_id?
131
+ dosage_graph_data = []
132
+ # dosage_sub_id = request.POST['sub']
133
+ usages = Usage .objects .filter (sub = sub_id )[:20 ]
132
134
133
- cntr = 0
135
+ # cntr = 0
134
136
for use in usages :
135
- # usage_graph_data [str(use.timestamp)] = float(use.dosage)
137
+ # dosage_graph_data [str(use.timestamp)] = float(use.dosage)
136
138
# NOTE: switching to a standard array for now for simplicity in the
137
139
# template's javascript; we can add more gravy later
138
140
139
141
# later on we can look at using use.notes as hover-over text for each
140
142
# graph bar, or something of the like
141
- usage_graph_data [ cntr ] = float (use .dosage )
142
- cntr += 1
143
+ dosage_graph_data . append ( float (use .dosage ) )
144
+ # cntr += 1
143
145
144
- return HttpResponse (json .dumps (usage_graph_data ), content_type = 'application/json' )
146
+ return HttpResponse (json .dumps (dosage_graph_data ), content_type = 'application/json' )
147
+
148
+
149
+ def dump_interval_graph_data (request , sub_id ):
150
+ # interval_graph_data = {}
151
+ usages = Usage .objects .filter (sub = sub_id )[:20 ]
152
+
153
+ timespans = []
154
+ prev_time = None # would we (perhaps optionally) want timezone.now()?
155
+ max_span = datetime .timedelta (0 )
156
+ for use in usages :
157
+ if prev_time is not None :
158
+ current_delta = datetime .timedelta
159
+ current_delta = use .timestamp - prev_time
160
+ current_delta = round_timedelta_to_15min_floor (current_delta )
161
+ timespans .append (current_delta .total_seconds ())
162
+
163
+ if current_delta > max_span :
164
+ max_span = current_delta
165
+
166
+ prev_time = use .timestamp
167
+
168
+ scale_factor = get_graph_normalization_divisor (max_span .total_seconds (), 300 )
169
+ for cntr in range (0 , len (timespans )):
170
+ timespans [cntr ] = timespans [cntr ] * scale_factor
171
+
172
+ return HttpResponse (json .dumps ({ 'scale_factor' : scale_factor , 'timespans' : timespans }),
173
+ content_type = 'application/json' )
145
174
146
175
147
176
def add_header_info (page_data ):
@@ -173,3 +202,21 @@ def round_timedelta_to_15min_floor(span):
173
202
dingleberry = span .total_seconds () % fifteen_min .seconds
174
203
175
204
return span - datetime .timedelta (seconds = dingleberry )
205
+
206
+
207
+ def get_graph_normalization_divisor (max_qty , graph_max_boundary ):
208
+ """
209
+ Method takes the maximum quantity (dimensionless), along with the maximum
210
+ dimension on the quantity axis, and returns the scale to divide by in
211
+ order to make things fit properly in the graph.
212
+
213
+ :param max_qty:
214
+ :param graph_max_boundary:
215
+ :return:
216
+ """
217
+
218
+ scale_factor = 1
219
+ if max_qty <= (graph_max_boundary / 2 ) or max_qty > graph_max_boundary :
220
+ scale_factor = graph_max_boundary / max_qty
221
+
222
+ return scale_factor
0 commit comments