From 706ed2039cef94ec10a1540868e3c1b6bcbbd91e Mon Sep 17 00:00:00 2001 From: buenosairesam Date: Tue, 4 Feb 2025 03:38:21 -0300 Subject: [PATCH] updated calendar, dmmain --- dmapp/dmweb/dm.py | 24 ++++++---- dmapp/dmweb/dmcal.py | 20 ++++---- dmapp/dmweb/get_period_times.py | 85 +++++++++++++++++++++++---------- 3 files changed, 88 insertions(+), 41 deletions(-) diff --git a/dmapp/dmweb/dm.py b/dmapp/dmweb/dm.py index 3303e78..a2f431c 100644 --- a/dmapp/dmweb/dm.py +++ b/dmapp/dmweb/dm.py @@ -2,7 +2,13 @@ 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 +from .get_period_times import ( + get_period_totals, + read_and_extract, + task_file, + task_or_none, + timezone, +) dmbp = Blueprint("deskmeter", __name__, url_prefix="/", template_folder="templates") @@ -18,14 +24,9 @@ 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 + task = task_or_none(task) 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) @@ -34,7 +35,14 @@ def index(task=None): @dmbp.route("/day//") -def oneday(month, day): +@dmbp.route("/day///") +def oneday( + month, + day, + task=None, +): + task = task_or_none(task) + start = datetime(2020, month, day).replace( hour=0, minute=0, second=0, tzinfo=timezone ) diff --git a/dmapp/dmweb/dmcal.py b/dmapp/dmweb/dmcal.py index 811a312..600007a 100644 --- a/dmapp/dmweb/dmcal.py +++ b/dmapp/dmweb/dmcal.py @@ -6,7 +6,13 @@ from pprint import pprint from dmweb.dm import dmbp from flask import Blueprint, render_template -from .get_period_times import get_period_totals, timezone +from .get_period_times import ( + get_period_totals, + read_and_extract, + task_file, + task_or_none, + timezone, +) class DMHTMLCalendar(calendar.HTMLCalendar): @@ -69,7 +75,7 @@ class DMHTMLCalendar(calendar.HTMLCalendar): hour=23, minute=59, second=59, tzinfo=timezone ) - rows = get_period_totals(start, end) + rows = get_period_totals(start, end, self.task) returnstr = "" for row in rows: @@ -112,9 +118,9 @@ class DMHTMLCalendar(calendar.HTMLCalendar): @dmbp.route("/month") @dmbp.route("/month/") @dmbp.route("/month//") -@dmbp.route("/month/task/") -@dmbp.route("/month//task/") -@dmbp.route("/month///task/") +@dmbp.route("/month/") +@dmbp.route("/month//") +@dmbp.route("/month///") def month(month=None, year=None, task=None): usemonth = datetime.today().month useyear = datetime.today().year @@ -127,9 +133,7 @@ def month(month=None, year=None, task=None): cal = DMHTMLCalendar(calendar.SATURDAY) - cal.settask(None) - if task: - cal.settask(task) + cal.settask(task_or_none(task)) cal.setcalmonth(usemonth) cal.setcalyear(useyear) diff --git a/dmapp/dmweb/get_period_times.py b/dmapp/dmweb/get_period_times.py index 28e617b..766ff84 100644 --- a/dmapp/dmweb/get_period_times.py +++ b/dmapp/dmweb/get_period_times.py @@ -1,6 +1,6 @@ -import pprint from collections import Counter, defaultdict from datetime import datetime, timedelta +from pprint import pprint from pymongo import MongoClient from zoneinfo import ZoneInfo @@ -16,6 +16,16 @@ switches = db.switch task_file = "/home/mariano/LETRAS/org/task/main" +def task_or_none(task=None): + if not task: + task = read_and_extract(task_file) + + if task == "all": + task = None + + return task + + def now(): return datetime.now(timezone) @@ -53,6 +63,7 @@ def read_and_extract(file_path): def get_period_totals(start, end, task=None): task_query = {"$in": task.split(",")} if task else {} + match_query = {"date": {"$gte": start, "$lte": end}} if task_query: match_query["task"] = task_query @@ -68,15 +79,27 @@ def get_period_totals(start, end, task=None): "last_doc": {"$last": "$$ROOT"}, } }, - # Lookup to get one document before the first document in the range { "$lookup": { "from": "switch", - "let": {"first_date": "$first_doc.date"}, + "let": {"first_date": "$first_doc.date", "task": "$first_doc.task"}, "pipeline": [ - {"$match": {"$expr": {"$lt": ["$date", "$$first_date"]}}}, - {"$sort": {"date": -1}}, - {"$limit": 1}, + { + "$match": { + "$expr": { + "$and": [ + { + "$lt": ["$date", "$$first_date"] + }, # Only before the first date + { + "$eq": ["$task", "$$task"] + }, # Must have the same task + ] + } + } + }, + {"$sort": {"date": -1}}, # Get the most recent (closest) document + {"$limit": 1}, # Only the immediate previous document ], "as": "before_first", } @@ -85,7 +108,7 @@ def get_period_totals(start, end, task=None): "$project": { "documents": { "$concatArrays": [ - {"$ifNull": ["$before_first", []]}, + {"$ifNull": ["$before_first", []]}, # Add only if found "$documents_in_range", ] } @@ -121,11 +144,24 @@ def get_period_totals(start, end, task=None): { "$lookup": { "from": "switch", - "let": {"first_date": "$first_doc.date"}, + "let": {"first_date": "$first_doc.date", "task": "$first_doc.task"}, "pipeline": [ - {"$match": {"$expr": {"$lt": ["$date", "$$first_date"]}}}, - {"$sort": {"date": -1}}, - {"$limit": 1}, + { + "$match": { + "$expr": { + "$and": [ + { + "$lt": ["$date", "$$first_date"] + }, # Only before the first date + { + "$eq": ["$task", "$$task"] + }, # Must have the same task + ] + } + } + }, + {"$sort": {"date": -1}}, # Get the most recent (closest) document + {"$limit": 1}, # Only the immediate previous document ], "as": "before_first", } @@ -142,32 +178,31 @@ def get_period_totals(start, end, task=None): aux_results = list(switches.aggregate(pipeline_before_after)) + print(aux_results) + bfirst = aux_results[0]["before_first"] + + if bfirst: + bfdate = bfirst["date"].replace(tzinfo=utctz) + start_delta = round((start - bfdate.astimezone(timezone)).total_seconds()) + ldoc = aux_results[0]["last_doc"] - - bfdate = bfirst["date"].replace(tzinfo=utctz) lastdate = ldoc["date"].replace(tzinfo=utctz) - - start_delta = round((start - bfdate.astimezone(timezone)).total_seconds()) end_delta = round((end - lastdate.astimezone(timezone)).total_seconds()) rows = [] active_vs_idle = {"Active": 0, "Idle": 0} - print(results) - for result in results: - if result["_id"] == bfirst["workspace"]: - result["total"] -= start_delta + if bfirst: + if result["_id"] == bfirst["workspace"]: + result["total"] -= start_delta if end < now(): if result["_id"] == ldoc["workspace"]: result["total"] -= ldoc["delta"] - end_delta - print(results) - for result in results: - print(result) if result["total"] > 0: rows.append( {"ws": result["_id"], "total": convert_seconds(result["total"])} @@ -177,7 +212,7 @@ def get_period_totals(start, end, task=None): if result["_id"] in ["Away", "Other"]: active_vs_idle["Idle"] += result["total"] - order = ["Think", "Plan", "Work", "Away", "Other", "Active", "Idle"] + order = ["Plan", "Think", "Work", "Other", "Away", "Active", "Idle"] rows = sorted(rows, key=lambda x: order.index(x["ws"])) @@ -200,7 +235,7 @@ def get_period_totals(start, end, task=None): # print( # get_period_totals( # datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone), -# datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone) -# # "ffbe198e", +# datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone), +# "5fc751ec", # ) # )