major redo of period totals function

This commit is contained in:
Mariano Ramon
2020-05-03 22:27:56 -03:00
parent c994745364
commit bc1a5370f2
3 changed files with 130 additions and 49 deletions

View File

@@ -1,16 +1,9 @@
import os
from flask import Flask
from dmweb import dm
def create_app(test_config=None):
def create_app():
app = Flask("deskmeter") #instance_relative_config=True
try:
os.makedirs(app.instance_path)
except OSError:
pass
app = Flask("deskmeter")
app.register_blueprint(dm.dmbp)

View File

@@ -1,7 +1,9 @@
from collections import Counter
import datetime
from pprint import pprint
from flask import Blueprint, render_template
from pymongo import MongoClient
from pprint import pprint
import datetime
import pytz
client = MongoClient()
@@ -24,11 +26,31 @@ def index():
minute=59,
second=59)
rows = get_period_totals(start, end)
rows = get_period_totals( local_date(start),
local_date(end))
return render_template("pages.html", rows=rows)
@dmbp.route("/day/<int:month>/<int:day>")
def oneday(month,day):
start = datetime.datetime(2020, month, day).replace(hour=0,
minute=0,
second=0)
end = datetime.datetime(2020, month, day).replace (hour=23,
minute=59,
second=59)
rows = get_period_totals( local_date(start),
local_date(end))
return render_template("pages.html", rows=rows)
@dmbp.route("/period/<start>/<end>")
def period(start,end):
@@ -40,7 +62,8 @@ def period(start,end):
minute=59,
second=59)
rows = get_period_totals(start, end)
rows = get_period_totals( local_date(start),
local_date(end))
return render_template("pages.html", rows=rows)
@@ -64,55 +87,82 @@ 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)
local_end = end.replace(tzinfo=bsas)
queries = {
"period" : {"date": {"$gt" : start, "$lt" : end }},
"previous_doc" : {"date" : {"$lt" : start }}
#"next_doc" : {"date":{"$lt":start}}
}
queries = {
"period" : {"date":{"$gt":local_start,"$lt":local_end}},
"previous_doc" : {"date":{"$lt":local_start}}
#"next_doc" : {"date":{"$lt":local_start}}
}
length = switches.count_documents(queries["period"])
docs = switches.find(queries["period"]).sort([("date", 1)])
#next_doc = switches.find_one(queries["next_doc"])
last_doc = docs[length-1]
length = switches.count_documents(queries["period"])
docs = switches.find(queries["period"]).sort([("date", 1)])
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]
first_date = local_date(docs[0]["date"], True)
last_date = local_date(last_doc["date"], True)
local_first_date = docs[0]["date"].replace(tzinfo=pytz.utc).astimezone(bsas)
local_last_date = last_doc["date"].replace(tzinfo=pytz.utc).astimezone(bsas)
pipe = [ {'$match': queries["period"] },
{'$group': { '_id':"$workspace",'totals': {'$sum': '$delta'}}},
{'$sort': { "_id": 1}}]
delta_to_start = (local_first_date - local_start).total_seconds()
delta_to_end = (local_end - local_last_date).total_seconds()
pre_rows = Counter({})
for total in switches.aggregate(pipeline=pipe):
pre_rows[total["_id"]] = total["totals"]
pipe = [ {'$match': queries["period"] },
{'$group': { '_id':"$workspace",'totals': {'$sum': '$delta'}}},
{'$sort': { "_id": 1}}]
pre_rows = {}
for total in switches.aggregate(pipeline=pipe):
pre_rows[total["_id"]] = total["totals"]
time_corrections = []
if first_date > start:
previous_doc = switches.find(queries["previous_doc"]).sort([("date", -1)]).limit(1)[0]
time_corrections.append(Counter({previous_doc["workspace"] : split_entry(previous_doc, start)}))
now = local_date(datetime.datetime.now())
if datetime.datetime.now().astimezone(bsas) < local_end:
delta_to_end = 0
if end > now:
time_corrections.append( Counter({ last_doc["workspace"] : split_entry(last_doc, now, after=False)}))
if previous_doc["workspace"] not in pre_rows:
pre_rows[previous_doc["workspace"]] = 0
if end > last_date and now > end:
time_corrections.append(Counter({ last_doc["workspace"] : split_entry(last_doc, end, after=False)}))
pre_rows[previous_doc["workspace"]] += round(delta_to_start)
pre_rows[last_doc["workspace"]] += round(delta_to_end)
rows = []
for ws, delta in pre_rows.items():
rows.append( {"ws": ws,
"total" : str(datetime.timedelta(seconds=delta))})
for correction in time_corrections:
pre_rows += correction
return rows
day = 0
rows = []
for ws, total in pre_rows.items():
day += total
rows.append( {"ws": ws,
"total" : datetime.timedelta(seconds=total)})
rows.append({"ws":"sum","total": day})
return rows
def split_entry(entry, split_time, after=True):
"""
entry must have a date an number
split time must be timezone aware
after, bolean to return the time after the split time
"""
first_half = round((split_time - local_date(entry["date"], True)).total_seconds())
last_half = entry["delta"] - first_half
if after:
return last_half
return last_half * -1
def local_date(date, convert=False):
bsas = pytz.timezone("Etc/GMT+3")
if convert:
return pytz.utc.localize(date, is_dst=None).astimezone(bsas)
return date.replace(tzinfo=bsas)

38
dmapp/tests/test_dmapp.py Normal file
View File

@@ -0,0 +1,38 @@
import os
import sys
import unittest
sys.path.append(os.path.abspath('../'))
from dmweb import create_app
class TestDeskMeter(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
dmapp = create_app()
self.client = dmapp.test_client()
self.client.testing = True
def tearDown(self):
pass
# test method
def test_home_status_code(self):
result = self.client.get('/')
# assert the status code of the response
self.assertEqual(result.status_code, 200)
# runs the unit tests in the module
if __name__ == '__main__':
unittest.main()