From bd7e0a802005463e237dba10567039428b0b1353 Mon Sep 17 00:00:00 2001 From: Mariano Ramon Date: Sun, 19 Apr 2020 08:15:14 -0300 Subject: [PATCH] fixed non existing key when fist date < start date --- dmapp/dmweb/dm.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dmapp/dmweb/dm.py b/dmapp/dmweb/dm.py index 762d5a8..c4c90c7 100644 --- a/dmapp/dmweb/dm.py +++ b/dmapp/dmweb/dm.py @@ -8,10 +8,8 @@ client = MongoClient() db = client.deskmeter switches = db.switch - dmbp = Blueprint("deskmeter", __name__, url_prefix="/", template_folder='templates') - @dmbp.route("/") def index(): """ @@ -50,6 +48,11 @@ def totals(): def get_period_totals(start, end): + """ + TODOs: refactor to have available the ws array and the active ws function in order to remove + the delta = 0 thing + refactor to pass in the timezone and make the variable names clearer + """ bsas = pytz.timezone("Etc/GMT+3") local_start = start.replace(tzinfo=bsas) @@ -63,7 +66,7 @@ def get_period_totals(start, end): length = switches.count_documents(queries["period"]) docs = switches.find(queries["period"]).sort([("date", 1)]) - previous_doc = switches.find_one(queries["previous_doc"]) + previous_doc = switches.find(queries["previous_doc"]).sort([("date", -1)]).limit(1)[0] #next_doc = switches.find_one(queries["next_doc"]) last_doc = docs[length-1] @@ -82,10 +85,12 @@ def get_period_totals(start, end): for total in switches.aggregate(pipeline=pipe): pre_rows[total["_id"]] = total["totals"] - if datetime.datetime.now().astimezone(bsas) < local_end: delta_to_end = 0 + if previous_doc["workspace"] not in pre_rows: + pre_rows[previous_doc["workspace"]] = 0 + pre_rows[previous_doc["workspace"]] += round(delta_to_start) pre_rows[last_doc["workspace"]] += round(delta_to_end)