updated calendar, dmmain
This commit is contained in:
@@ -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",
|
||||
# )
|
||||
# )
|
||||
|
||||
Reference in New Issue
Block a user