diff --git a/dmmain.py b/dmmain.py index d29c8a3..2601728 100644 --- a/dmmain.py +++ b/dmmain.py @@ -5,10 +5,14 @@ import time from pprint import pprint from pymongo import MongoClient +from zoneinfo import ZoneInfo client = MongoClient() -now = datetime.datetime.utcnow + +def now(): + return datetime.datetime.now(ZoneInfo("America/Argentina/Buenos_Aires")) + db = client.deskmeter switches = db.switch @@ -72,25 +76,41 @@ def desktop(workspace_index): current_workspace = active_workspace() current_task = read_and_extract(task_file) - last_switch_time = now() -while True: - if current_workspace != active_workspace(): - delta = round((now() - last_switch_time).total_seconds()) +switch = { + "workspace": desktop(current_workspace), + "date": now(), + "delta": 0, + "task": current_task, +} + +switches.insert_one(switch) + +while True: + current_task = read_and_extract(task_file) + current_workspace = active_workspace() + + last_doc = switches.find_one(sort=[("_id", -1)]) + if ( + last_doc["workspace"] == desktop(current_workspace) + and last_doc["task"] == current_task + ): + delta = round((now() - last_switch_time).total_seconds()) + switches.update_one( + {"_id": last_doc["_id"]}, {"$set": {"delta": delta, "task": current_task}} + ) + else: + time.sleep(2) + current_workspace = active_workspace() - # task = "" - # if current_workspace in [0, 1, 2]: switch = { "workspace": desktop(current_workspace), - "date": last_switch_time, - "delta": delta, + "date": now(), + "delta": 0, "task": current_task, } - switches.insert_one(switch) - current_workspace = active_workspace() - current_task = read_and_extract(task_file) last_switch_time = now() time.sleep(2)