@@ -219,6 +219,14 @@ def processing_request(request, instrument, run_id, destination):
219
219
return redirect (reverse ("report:detail" , args = [instrument , run_id ]))
220
220
221
221
222
+ def __generate_empty_rates (n_hours ):
223
+ runs = []
224
+ num_runs = 0 # there are no runs
225
+ for i in range (n_hours ):
226
+ runs .append ([- i , num_runs ])
227
+ return runs
228
+
229
+
222
230
def retrieve_rates (instrument_id , last_run_id ):
223
231
"""
224
232
Retrieve the run rate and error rate for an instrument.
@@ -227,6 +235,8 @@ def retrieve_rates(instrument_id, last_run_id):
227
235
:param instrument_id: Instrument object
228
236
:param last_run_id: DataRun object
229
237
"""
238
+ n_hours = 24
239
+
230
240
last_run = None
231
241
if last_run_id is not None :
232
242
last_run = last_run_id .run_number
@@ -249,14 +259,37 @@ def _get_rate(id_name):
249
259
250
260
# If we didn't find good rates in the cache, recalculate them
251
261
if runs is None or errors is None :
252
- runs = run_rate (instrument_id )
253
- errors = error_rate (instrument_id )
262
+ # check for any run in the last n-hours
263
+ time_oldest = timezone .now () - datetime .timedelta (hours = n_hours + 1 ) # is +1 needed?
264
+
265
+ # only query further if there are runs for the instrument
266
+ have_runs = bool (DataRun .objects .filter (instrument_id = instrument_id , created_on__gte = time_oldest ).count () > 0 )
267
+ if have_runs :
268
+ runs = run_rate (instrument_id , n_hours = n_hours )
269
+ else :
270
+ runs = __generate_empty_rates (n_hours )
271
+
272
+ # same for errors
273
+ have_errors = bool (
274
+ Error .objects .filter (
275
+ run_status_id__run_id__instrument_id = instrument_id , run_status_id__created_on__gte = time_oldest
276
+ ).count ()
277
+ > 0
278
+ )
279
+ if have_errors :
280
+ errors = error_rate (instrument_id , n_hours = n_hours )
281
+ else :
282
+ errors = __generate_empty_rates (n_hours )
283
+
284
+ # cache the run rate
254
285
cache .set ("%s_run_rate" % instrument_id .name , runs , settings .RUN_RATE_CACHE_TIMEOUT )
255
286
cache .set (
256
287
"%s_error_rate" % instrument_id .name ,
257
288
errors ,
258
289
settings .RUN_RATE_CACHE_TIMEOUT ,
259
290
)
291
+
292
+ # add the last run view for this instrument to the cache
260
293
cache .set ("%s_rate_last_run" % instrument_id .name , last_run )
261
294
262
295
return runs , errors
0 commit comments