From c9c985c1efee8602ae4d794eb06f73f738e9a1cd Mon Sep 17 00:00:00 2001 From: bkfox Date: Mon, 9 Jan 2017 16:10:52 +0100 Subject: [PATCH] fix on_air algorithm pbm --- aircox/models.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/aircox/models.py b/aircox/models.py index 71200ae..4b85fa7 100755 --- a/aircox/models.py +++ b/aircox/models.py @@ -275,29 +275,35 @@ class Station(Nameable): the resulting list """ # we fill a list with diff and retrieve logs that happened between - # each to put them too there - items = [] + # each to put them too there. + # we do the algorithm in the reverse way in order to be able to limit + # process calculations using count if needed. diff_ = None now = tz.now() + items = [] + logs = logs.order_by('-date') for diff in diffs.order_by('-start'): - logs_ = \ - logs.filter(date__gt = diff.end, date__lt = diff_.start) \ - if diff_ else \ - logs.filter(date__gt = diff.end) + if diff_: + logs_ = logs.filter(date__gt = diff.end, date__lt = diff_.start) + else: + logs_ = logs.filter(date__gt = diff.end) - # a log can be started before the end of the diffusion and - # still is running => need to add it to the list and change - # the start date if diff.end < now: - partial_log = logs.filter( + # a log can be started before the end of the diffusion and still + # is running. We can't say if it has been properly finished + # before the end of the diffusion, but we assume that in most + # cases this is true. + # We just check if there is some other log after this partial + # one. + partial = logs.filter( date__gt = diff.start, date__lt = diff.end - ).last() - if partial_log: + ) + if partial: next_log = logs.filter(pk__gt = partial_log.pk).first() - if not next_log or next_log.date > diff.date: - partial_log.date = diff.end - logs_ = [partial_log] + list(logs_[:count]) + if not next_log or next_log.date > diff.end: + partial.date = diff.end + logs_ = list(logs_[:count]) + [partial] # append to list diff_ = diff