updated calendar, dmmain

This commit is contained in:
buenosairesam
2025-02-04 03:38:21 -03:00
parent d0f1047b8b
commit 706ed2039c
3 changed files with 88 additions and 41 deletions

View File

@@ -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/<int:month>/<int:day>")
def oneday(month, day):
@dmbp.route("/day/<string:task>/<int:month>/<int: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
)

View File

@@ -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 = "<table class='totaltable'>"
for row in rows:
@@ -112,9 +118,9 @@ class DMHTMLCalendar(calendar.HTMLCalendar):
@dmbp.route("/month")
@dmbp.route("/month/<int:month>")
@dmbp.route("/month/<int:month>/<int:year>")
@dmbp.route("/month/task/<string:task>")
@dmbp.route("/month/<int:month>/task/<string:task>")
@dmbp.route("/month/<int:month>/<int:year>/task/<string:task>")
@dmbp.route("/month/<string:task>")
@dmbp.route("/month/<string:task>/<int:month>")
@dmbp.route("/month/<string:task>/<int:month>/<int:year>")
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)

View File

@@ -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,21 +178,23 @@ 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"]
ldoc = aux_results[0]["last_doc"]
if bfirst:
bfdate = bfirst["date"].replace(tzinfo=utctz)
lastdate = ldoc["date"].replace(tzinfo=utctz)
start_delta = round((start - bfdate.astimezone(timezone)).total_seconds())
ldoc = aux_results[0]["last_doc"]
lastdate = ldoc["date"].replace(tzinfo=utctz)
end_delta = round((end - lastdate.astimezone(timezone)).total_seconds())
rows = []
active_vs_idle = {"Active": 0, "Idle": 0}
print(results)
for result in results:
if bfirst:
if result["_id"] == bfirst["workspace"]:
result["total"] -= start_delta
@@ -164,10 +202,7 @@ def get_period_totals(start, end, task=None):
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",
# )
# )