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 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") 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 Show total time used in each desktop for today
""" """
if not task: task = task_or_none(task)
task = read_and_extract(task_file)
if task == "all":
task = None
start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone) start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone)
end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone) end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone)
rows = get_period_totals(start, end, task) rows = get_period_totals(start, end, task)
@@ -34,7 +35,14 @@ def index(task=None):
@dmbp.route("/day/<int:month>/<int:day>") @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( start = datetime(2020, month, day).replace(
hour=0, minute=0, second=0, tzinfo=timezone hour=0, minute=0, second=0, tzinfo=timezone
) )

View File

@@ -6,7 +6,13 @@ from pprint import pprint
from dmweb.dm import dmbp from dmweb.dm import dmbp
from flask import Blueprint, render_template 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): class DMHTMLCalendar(calendar.HTMLCalendar):
@@ -69,7 +75,7 @@ class DMHTMLCalendar(calendar.HTMLCalendar):
hour=23, minute=59, second=59, tzinfo=timezone 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'>" returnstr = "<table class='totaltable'>"
for row in rows: for row in rows:
@@ -112,9 +118,9 @@ class DMHTMLCalendar(calendar.HTMLCalendar):
@dmbp.route("/month") @dmbp.route("/month")
@dmbp.route("/month/<int:month>") @dmbp.route("/month/<int:month>")
@dmbp.route("/month/<int:month>/<int:year>") @dmbp.route("/month/<int:month>/<int:year>")
@dmbp.route("/month/task/<string:task>") @dmbp.route("/month/<string:task>")
@dmbp.route("/month/<int:month>/task/<string:task>") @dmbp.route("/month/<string:task>/<int:month>")
@dmbp.route("/month/<int:month>/<int:year>/task/<string:task>") @dmbp.route("/month/<string:task>/<int:month>/<int:year>")
def month(month=None, year=None, task=None): def month(month=None, year=None, task=None):
usemonth = datetime.today().month usemonth = datetime.today().month
useyear = datetime.today().year useyear = datetime.today().year
@@ -127,9 +133,7 @@ def month(month=None, year=None, task=None):
cal = DMHTMLCalendar(calendar.SATURDAY) cal = DMHTMLCalendar(calendar.SATURDAY)
cal.settask(None) cal.settask(task_or_none(task))
if task:
cal.settask(task)
cal.setcalmonth(usemonth) cal.setcalmonth(usemonth)
cal.setcalyear(useyear) cal.setcalyear(useyear)

View File

@@ -1,6 +1,6 @@
import pprint
from collections import Counter, defaultdict from collections import Counter, defaultdict
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pprint import pprint
from pymongo import MongoClient from pymongo import MongoClient
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
@@ -16,6 +16,16 @@ switches = db.switch
task_file = "/home/mariano/LETRAS/org/task/main" 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(): def now():
return datetime.now(timezone) return datetime.now(timezone)
@@ -53,6 +63,7 @@ def read_and_extract(file_path):
def get_period_totals(start, end, task=None): def get_period_totals(start, end, task=None):
task_query = {"$in": task.split(",")} if task else {} task_query = {"$in": task.split(",")} if task else {}
match_query = {"date": {"$gte": start, "$lte": end}} match_query = {"date": {"$gte": start, "$lte": end}}
if task_query: if task_query:
match_query["task"] = task_query match_query["task"] = task_query
@@ -68,15 +79,27 @@ def get_period_totals(start, end, task=None):
"last_doc": {"$last": "$$ROOT"}, "last_doc": {"$last": "$$ROOT"},
} }
}, },
# Lookup to get one document before the first document in the range
{ {
"$lookup": { "$lookup": {
"from": "switch", "from": "switch",
"let": {"first_date": "$first_doc.date"}, "let": {"first_date": "$first_doc.date", "task": "$first_doc.task"},
"pipeline": [ "pipeline": [
{"$match": {"$expr": {"$lt": ["$date", "$$first_date"]}}}, {
{"$sort": {"date": -1}}, "$match": {
{"$limit": 1}, "$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", "as": "before_first",
} }
@@ -85,7 +108,7 @@ def get_period_totals(start, end, task=None):
"$project": { "$project": {
"documents": { "documents": {
"$concatArrays": [ "$concatArrays": [
{"$ifNull": ["$before_first", []]}, {"$ifNull": ["$before_first", []]}, # Add only if found
"$documents_in_range", "$documents_in_range",
] ]
} }
@@ -121,11 +144,24 @@ def get_period_totals(start, end, task=None):
{ {
"$lookup": { "$lookup": {
"from": "switch", "from": "switch",
"let": {"first_date": "$first_doc.date"}, "let": {"first_date": "$first_doc.date", "task": "$first_doc.task"},
"pipeline": [ "pipeline": [
{"$match": {"$expr": {"$lt": ["$date", "$$first_date"]}}}, {
{"$sort": {"date": -1}}, "$match": {
{"$limit": 1}, "$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", "as": "before_first",
} }
@@ -142,32 +178,31 @@ def get_period_totals(start, end, task=None):
aux_results = list(switches.aggregate(pipeline_before_after)) aux_results = list(switches.aggregate(pipeline_before_after))
print(aux_results)
bfirst = aux_results[0]["before_first"] 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"] ldoc = aux_results[0]["last_doc"]
bfdate = bfirst["date"].replace(tzinfo=utctz)
lastdate = ldoc["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()) end_delta = round((end - lastdate.astimezone(timezone)).total_seconds())
rows = [] rows = []
active_vs_idle = {"Active": 0, "Idle": 0} active_vs_idle = {"Active": 0, "Idle": 0}
print(results)
for result in results: for result in results:
if result["_id"] == bfirst["workspace"]: if bfirst:
result["total"] -= start_delta if result["_id"] == bfirst["workspace"]:
result["total"] -= start_delta
if end < now(): if end < now():
if result["_id"] == ldoc["workspace"]: if result["_id"] == ldoc["workspace"]:
result["total"] -= ldoc["delta"] - end_delta result["total"] -= ldoc["delta"] - end_delta
print(results)
for result in results: for result in results:
print(result)
if result["total"] > 0: if result["total"] > 0:
rows.append( rows.append(
{"ws": result["_id"], "total": convert_seconds(result["total"])} {"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"]: if result["_id"] in ["Away", "Other"]:
active_vs_idle["Idle"] += result["total"] 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"])) rows = sorted(rows, key=lambda x: order.index(x["ws"]))
@@ -200,7 +235,7 @@ def get_period_totals(start, end, task=None):
# print( # print(
# get_period_totals( # get_period_totals(
# datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone), # datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone),
# datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone) # datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone),
# # "ffbe198e", # "5fc751ec",
# ) # )
# ) # )