major redo of period totals function
This commit is contained in:
@@ -1,16 +1,9 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from dmweb import dm
|
from dmweb import dm
|
||||||
|
|
||||||
def create_app(test_config=None):
|
def create_app():
|
||||||
|
|
||||||
app = Flask("deskmeter") #instance_relative_config=True
|
app = Flask("deskmeter")
|
||||||
|
|
||||||
try:
|
|
||||||
os.makedirs(app.instance_path)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
app.register_blueprint(dm.dmbp)
|
app.register_blueprint(dm.dmbp)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
from collections import Counter
|
||||||
|
import datetime
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
from flask import Blueprint, render_template
|
from flask import Blueprint, render_template
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
from pprint import pprint
|
|
||||||
import datetime
|
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
client = MongoClient()
|
client = MongoClient()
|
||||||
@@ -24,11 +26,31 @@ def index():
|
|||||||
minute=59,
|
minute=59,
|
||||||
second=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)
|
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>")
|
@dmbp.route("/period/<start>/<end>")
|
||||||
def period(start,end):
|
def period(start,end):
|
||||||
|
|
||||||
@@ -40,7 +62,8 @@ def period(start,end):
|
|||||||
minute=59,
|
minute=59,
|
||||||
second=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)
|
return render_template("pages.html", rows=rows)
|
||||||
|
|
||||||
@@ -64,55 +87,82 @@ def totals():
|
|||||||
|
|
||||||
|
|
||||||
def get_period_totals(start, end):
|
def get_period_totals(start, end):
|
||||||
"""
|
"""
|
||||||
TODOs: refactor to have available the ws array and the active ws function in order to remove
|
TODOs: refactor to have available the ws array and the active ws function in order to remove
|
||||||
the delta = 0 thing
|
the delta = 0 thing
|
||||||
refactor to pass in the timezone and make the variable names clearer
|
refactor to pass in the timezone and make the variable names clearer
|
||||||
"""
|
"""
|
||||||
|
|
||||||
bsas = pytz.timezone("Etc/GMT+3")
|
queries = {
|
||||||
local_start = start.replace(tzinfo=bsas)
|
"period" : {"date": {"$gt" : start, "$lt" : end }},
|
||||||
local_end = end.replace(tzinfo=bsas)
|
"previous_doc" : {"date" : {"$lt" : start }}
|
||||||
|
#"next_doc" : {"date":{"$lt":start}}
|
||||||
|
}
|
||||||
|
|
||||||
queries = {
|
length = switches.count_documents(queries["period"])
|
||||||
"period" : {"date":{"$gt":local_start,"$lt":local_end}},
|
docs = switches.find(queries["period"]).sort([("date", 1)])
|
||||||
"previous_doc" : {"date":{"$lt":local_start}}
|
|
||||||
#"next_doc" : {"date":{"$lt":local_start}}
|
#next_doc = switches.find_one(queries["next_doc"])
|
||||||
}
|
last_doc = docs[length-1]
|
||||||
|
|
||||||
length = switches.count_documents(queries["period"])
|
first_date = local_date(docs[0]["date"], True)
|
||||||
docs = switches.find(queries["period"]).sort([("date", 1)])
|
last_date = local_date(last_doc["date"], True)
|
||||||
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]
|
|
||||||
|
|
||||||
local_first_date = docs[0]["date"].replace(tzinfo=pytz.utc).astimezone(bsas)
|
pipe = [ {'$match': queries["period"] },
|
||||||
local_last_date = last_doc["date"].replace(tzinfo=pytz.utc).astimezone(bsas)
|
{'$group': { '_id':"$workspace",'totals': {'$sum': '$delta'}}},
|
||||||
|
{'$sort': { "_id": 1}}]
|
||||||
|
|
||||||
delta_to_start = (local_first_date - local_start).total_seconds()
|
pre_rows = Counter({})
|
||||||
delta_to_end = (local_end - local_last_date).total_seconds()
|
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 = {}
|
time_corrections = []
|
||||||
for total in switches.aggregate(pipeline=pipe):
|
if first_date > start:
|
||||||
pre_rows[total["_id"]] = total["totals"]
|
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:
|
if end > now:
|
||||||
delta_to_end = 0
|
time_corrections.append( Counter({ last_doc["workspace"] : split_entry(last_doc, now, after=False)}))
|
||||||
|
|
||||||
if previous_doc["workspace"] not in pre_rows:
|
if end > last_date and now > end:
|
||||||
pre_rows[previous_doc["workspace"]] = 0
|
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 correction in time_corrections:
|
||||||
for ws, delta in pre_rows.items():
|
pre_rows += correction
|
||||||
rows.append( {"ws": ws,
|
|
||||||
"total" : str(datetime.timedelta(seconds=delta))})
|
|
||||||
|
|
||||||
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
38
dmapp/tests/test_dmapp.py
Normal 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()
|
||||||
Reference in New Issue
Block a user