from datetime import datetime, timedelta from flask import Blueprint, render_template from .get_period_times import get_period_totals, read_and_extract, task_file, timezone dmbp = Blueprint("deskmeter", __name__, url_prefix="/", template_folder="templates") @dmbp.route("/favicon.ico") def favicon(): return "", 204 # No Content @dmbp.route("/") @dmbp.route("/") def index(task=None): """ Show total time used in each desktop for today """ if not task: task = read_and_extract(task_file) if task == "all": task = None start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone) end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone) rows = get_period_totals(start, end, task) return render_template("main.html", rows=rows) @dmbp.route("/day//") def oneday(month, day): start = datetime(2020, month, day).replace( hour=0, minute=0, second=0, tzinfo=timezone ) end = datetime(2020, month, day).replace( hour=23, minute=59, second=59, tzinfo=timezone ) rows = get_period_totals(start, end) return render_template("pages.html", rows=rows) @dmbp.route("/period//") def period(start, end): start = datetime(*map(int, start.split("-"))).replace( hour=0, minute=0, second=0, tzinfo=timezone ) end = datetime(*map(int, end.split("-"))).replace( hour=23, minute=59, second=59, tzinfo=timezone ) rows = get_period_totals(start, end) return render_template("pages.html", rows=rows) @dmbp.route("/totals") def totals(): """ Show total time used in each desktop for all time """ pipe = [ {"$group": {"_id": "$workspace", "totals": {"$sum": "$delta"}}}, {"$sort": {"_id": 1}}, ] rows = [] for total in switches.aggregate(pipeline=pipe): rows.append( { "ws": total["_id"], "total": str(timedelta(seconds=total["totals"])), } ) return render_template("pages.html", rows=rows)