-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
1248 lines (1017 loc) · 42 KB
/
tracker.py
File metadata and controls
1248 lines (1017 loc) · 42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# tracker.py - Optimized version
import win32gui
import win32process
import psutil
import time
import json
import threading
from datetime import datetime
from pynput import keyboard, mouse
import tkinter as tk
import os
import subprocess
import winreg
from pathlib import Path
from win10toast import ToastNotifier
from browser_tracker import update_browser_tracking, get_browser_status
from insights import give_timer_suggestions
LOG_FILE = "data/logs.json"
STATUS_FILE = "data/status.json"
SESSIONS_FILE = "data/sessions.json"
ALERT_CONFIG_FILE = "data/alert_config.json"
CUSTOM_ALERTS_FILE = "data/custom_alerts.json"
# SnapAlert App ID for Windows notifications
SNAPALERT_APP_ID = "SnapAlert.ProductivityMonitor"
# Alert configuration
ALERT_LEVELS = [
{"minutes": 3, "title": "Idle App Alert", "severity": "info"},
{"minutes": 10, "title": "Long Idle App", "severity": "warning"},
{"minutes": 30, "title": "Very Long Idle App", "severity": "critical"},
]
# Break reminder configuration
BREAK_REMINDER_INTERVAL = 180 # 3 minutes in seconds
# Performance optimization settings
MAIN_LOOP_INTERVAL = 5 # Increased from 3 to 5 seconds for better performance
WINDOW_ENUM_INTERVAL = 10 # Increased to 10 seconds
RESOURCE_CHECK_INTERVAL = 60 # Increased to 60 seconds (1 minute)
BROWSER_UPDATE_INTERVAL = 15 # Increased to 15 seconds
# Apps to exclude from alerts (system apps, etc.)
EXCLUDED_APPS = {
"dwm.exe",
"explorer.exe",
"winlogon.exe",
"csrss.exe",
"wininit.exe",
"services.exe",
"lsass.exe",
"svchost.exe",
"taskhost.exe",
"dllhost.exe",
"conhost.exe",
"RuntimeBroker.exe",
"ApplicationFrameHost.exe",
"ShellExperienceHost.exe",
"StartMenuExperienceHost.exe",
"SearchApp.exe",
"UserOOBEBroker.exe",
"SettingsApp.exe",
"SystemSettings.exe",
}
# Global variables
log_buffer = []
keystroke_count = 0
session_start_time = time.time()
current_app = None
current_title = None
start_time = time.time()
open_apps = {}
sessions = []
notifier = None
last_mouse_move_time = time.time()
last_break_reminder_time = time.time()
alert_config = {}
last_activity_time = time.time()
# Performance tracking
last_window_enum_time = 0
last_resource_check_time = 0
last_browser_update_time = 0
resource_usage_cache = {}
keyboard_listener = None
mouse_listener = None
# Ensure data directory exists
os.makedirs("data", exist_ok=True)
# Initialize notifier with error handling
try:
notifier = ToastNotifier()
print("[Tracker] Toast notifications initialized successfully")
except Exception as e:
print(f"[Tracker] Toast notification initialization failed: {e}")
notifier = None
def ensure_app_id_registered():
"""Automatically register SnapAlert app ID with Windows on startup"""
try:
# Check if already registered
key_path = f"SOFTWARE\\Classes\\AppUserModelId\\{SNAPALERT_APP_ID}"
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path):
print("[Tracker] SnapAlert app ID already registered")
return True
except FileNotFoundError:
pass # Not registered yet
print("[Tracker] Registering SnapAlert app ID...")
# Get icon path
script_dir = Path(__file__).parent.absolute()
icon_path = script_dir / "icons" / "snapalert.ico"
# Register the app ID
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_path) as key:
winreg.SetValueEx(key, "DisplayName", 0, winreg.REG_SZ, "SnapAlert")
if icon_path.exists():
winreg.SetValueEx(key, "IconUri", 0, winreg.REG_SZ, str(icon_path))
winreg.SetValueEx(
key, "IconBackgroundColor", 0, winreg.REG_SZ, "#E31E24"
)
print(f"[Tracker] Icon registered: {icon_path}")
winreg.SetValueEx(key, "ShowInSettings", 0, winreg.REG_DWORD, 1)
print("[Tracker] ✅ SnapAlert app ID registered successfully")
return True
except Exception as e:
print(f"[Tracker] ⚠️ App ID registration failed: {e}")
return False
def load_alert_config():
"""Load alert configuration from file"""
global alert_config
try:
if os.path.exists(ALERT_CONFIG_FILE):
with open(ALERT_CONFIG_FILE, "r") as f:
alert_config = json.load(f)
else:
alert_config = {
"enabled": True,
"whitelist": [],
"snooze_until": {},
"alert_levels_enabled": [True, True, True],
"show_resource_usage": True,
"smart_filtering": True,
"break_reminders_enabled": True,
"break_reminder_interval": 180,
}
save_alert_config()
except Exception as e:
print(f"Error loading alert config: {e}")
alert_config = {
"enabled": True,
"whitelist": [],
"snooze_until": {},
"alert_levels_enabled": [True, True, True],
"show_resource_usage": True,
"smart_filtering": True,
"break_reminders_enabled": True,
"break_reminder_interval": 180,
}
def save_alert_config():
"""Save alert configuration to file"""
try:
with open(ALERT_CONFIG_FILE, "w") as f:
json.dump(alert_config, f, indent=2)
except Exception as e:
print(f"Error saving alert config: {e}")
def load_custom_alerts():
"""Load custom alerts from file"""
try:
if os.path.exists(CUSTOM_ALERTS_FILE):
with open(CUSTOM_ALERTS_FILE, "r") as f:
return json.load(f)
return []
except Exception as e:
print(f"Error loading custom alerts: {e}")
return []
def save_custom_alerts(alerts):
"""Save custom alerts to file"""
try:
with open(CUSTOM_ALERTS_FILE, "w") as f:
json.dump(alerts, f, indent=2)
return True
except Exception as e:
print(f"Error saving custom alerts: {e}")
return False
def get_process_resource_usage_cached(app_name):
"""Disabled resource usage checking to prevent system crashes"""
# Return static data to prevent psutil crashes
return {"memory_mb": 0, "cpu_percent": 0, "process_count": 1}
def should_alert_for_app(app_name, current_time):
"""Check if we should alert for this app based on configuration"""
if not alert_config.get("enabled", True):
return False
if app_name in alert_config.get("whitelist", []):
return False
if alert_config.get("smart_filtering", True) and app_name in EXCLUDED_APPS:
return False
snooze_until = alert_config.get("snooze_until", {}).get(app_name, 0)
if current_time < snooze_until:
return False
return True
def show_notification_powershell(title, message, duration=8):
"""Show Windows notification using PowerShell with proper SnapAlert branding"""
try:
# Escape quotes in the message
escaped_title = title.replace('"', '""').replace("'", "''")
escaped_message = message.replace('"', '""').replace("'", "''")
# PowerShell command using Windows Toast notifications with registered app ID
powershell_cmd = f"""
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
# Create toast XML template
$template = @"
<toast>
<visual>
<binding template="ToastGeneric">
<text>{escaped_title}</text>
<text>{escaped_message}</text>
</binding>
</visual>
</toast>
"@
# Create XML document and toast notification
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
# Create toast notification with registered SnapAlert app ID
$toast = New-Object Windows.UI.Notifications.ToastNotification($xml)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("{SNAPALERT_APP_ID}")
$notifier.Show($toast)
"""
result = subprocess.run(
["powershell.exe", "-Command", powershell_cmd],
capture_output=True,
text=True,
timeout=10,
creationflags=subprocess.CREATE_NO_WINDOW, # Hide PowerShell window
)
if result.returncode == 0:
print(f"[Tracker] PowerShell notification sent successfully: {title}")
return True
else:
print(f"[Tracker] PowerShell failed: {result.stderr}")
return False
except Exception as e:
print(f"[Tracker] PowerShell notification error: {e}")
return False
def show_notification_fallback(title, message, duration=8):
"""Fallback notification using win10toast_click"""
try:
from win10toast_click import ToastNotifier
notifier = ToastNotifier()
notifier.show_toast(
title=title,
msg=message,
duration=duration,
icon_path=None,
threaded=True,
)
print(f"[Tracker] win10toast_click notification sent: {title}")
return True
except Exception as e:
print(f"[Tracker] win10toast_click error: {e}")
return False
def show_notification(title, message, duration=8):
"""Improved Windows notification function with multiple fallback methods"""
try:
print(f"[Tracker] Attempting to show: {title}")
# Add SnapAlert branding to title if not already present
if not title.startswith("🔺 SnapAlert"):
branded_title = f"🔺 SnapAlert: {title}"
else:
branded_title = title
# Method 1: Try win10toast_click first (we know this works for you)
if show_notification_fallback(branded_title, message, duration):
return True
# Method 2: Try PowerShell method as backup (with registered app ID)
print("[Tracker] win10toast_click failed, trying PowerShell...")
if show_notification_powershell(branded_title, message, duration):
return True
# Method 3: Try plyer as fallback
print("[Tracker] PowerShell failed, trying plyer...")
try:
from plyer import notification
notification.notify(
title=branded_title,
message=message,
app_name="SnapAlert",
timeout=duration,
)
print(f"[Tracker] Plyer notification sent successfully")
return True
except Exception as e:
print(f"[Tracker] Plyer notification failed: {e}")
# Method 4: Try original win10toast as final fallback
print("[Tracker] Trying original win10toast as final fallback...")
try:
from win10toast import ToastNotifier
notifier = ToastNotifier()
notifier.show_toast(
title=branded_title,
msg=message,
duration=duration,
icon_path=None,
threaded=True,
)
print(f"[Tracker] win10toast notification sent successfully")
return True
except Exception as e:
print(f"[Tracker] win10toast failed: {e}")
# If all methods fail, at least log it
print(
f"[Tracker] All notification methods failed. Title: {branded_title}, Message: {message}"
)
return False
except Exception as e:
print(f"[Tracker] System error: {e}")
return False
def snooze_app_alerts(app_name, minutes=30):
"""Snooze alerts for an app for specified minutes"""
snooze_until = time.time() + (minutes * 60)
alert_config["snooze_until"][app_name] = snooze_until
save_alert_config()
print(f"[Alert] Snoozed alerts for {app_name} for {minutes} minutes")
def check_custom_alerts(current_time):
"""Check custom alerts and trigger them when conditions are met"""
global session_start_time, keystroke_count, current_app, open_apps
try:
# Load custom alerts
custom_alerts = load_custom_alerts()
alerts_modified = False
for alert in custom_alerts:
# Skip disabled alerts
if not alert.get("enabled", True):
continue
alert_type = alert.get("type", "")
condition = alert.get("condition", "greater_than")
threshold = alert.get("threshold", 0)
app_filter = alert.get("app_filter", "")
# Check if alert should be triggered
should_trigger = False
current_value = 0
if alert_type == "keystroke_count":
current_value = keystroke_count
should_trigger = evaluate_condition(current_value, condition, threshold)
elif alert_type == "session_time":
current_value = int((current_time - session_start_time) / 60) # minutes
should_trigger = evaluate_condition(current_value, condition, threshold)
elif alert_type == "app_time":
if (
app_filter
and app_filter == current_app
and current_app in open_apps
):
app_start_time = open_apps[current_app].get(
"start_time", current_time
)
current_value = int((current_time - app_start_time) / 60) # minutes
should_trigger = evaluate_condition(
current_value, condition, threshold
)
elif alert_type == "idle_time":
if current_app and current_app in open_apps:
last_used = open_apps[current_app].get(
"last_used_time", current_time
)
current_value = int((current_time - last_used) / 60) # minutes
should_trigger = evaluate_condition(
current_value, condition, threshold
)
# Trigger alert if conditions are met
if should_trigger:
# Check if alert was recently triggered (prevent spam)
last_triggered = alert.get("last_triggered")
if last_triggered:
time_since_last = current_time - last_triggered
# Don't trigger same alert within 5 minutes
if time_since_last < 300:
continue
# Format message with placeholders
message = alert.get("message", "Custom alert triggered")
message = message.replace("{threshold}", str(threshold))
message = message.replace(
"{app}", app_filter or current_app or "application"
)
message = message.replace("{value}", str(current_value))
# Show notification
alert_name = alert.get("name", "Custom Alert")
success = show_notification(alert_name, message, duration=8)
if success:
# Update alert statistics
alert["last_triggered"] = current_time
alert["trigger_count"] = alert.get("trigger_count", 0) + 1
alerts_modified = True
print(
f"[Custom Alert] Triggered '{alert_name}' - {alert_type}: {current_value} {condition} {threshold}"
)
# Save updated alerts if any were modified
if alerts_modified:
save_custom_alerts(custom_alerts)
except Exception as e:
print(f"[Custom Alert Error] {e}")
def evaluate_condition(current_value, condition, threshold):
"""Evaluate if current value meets the condition against threshold"""
if condition == "greater_than":
return current_value > threshold
elif condition == "less_than":
return current_value < threshold
elif condition == "equal_to":
return current_value == threshold
return False
def check_break_reminder(current_time):
"""Check if it's time to send a break reminder"""
global last_break_reminder_time
if not alert_config.get("break_reminders_enabled", True):
return
reminder_interval = alert_config.get("break_reminder_interval", 180)
time_since_last_reminder = current_time - last_break_reminder_time
if time_since_last_reminder >= reminder_interval:
minutes_elapsed = int(time_since_last_reminder / 60)
show_notification(
"💪 Break Time!",
f"Hey! You've been working for {minutes_elapsed} minutes. "
f"Time to take a break! 🚶♂️\n"
f"• Stand up and stretch\n"
f"• Look away from the screen\n"
f"• Take a deep breath",
duration=10,
)
last_break_reminder_time = current_time
print(
f"[Break Reminder] Sent reminder after {minutes_elapsed} minutes of activity"
)
# Keystroke listener with better error handling
def on_key_press(key):
global \
keystroke_count, \
last_mouse_move_time, \
last_break_reminder_time, \
last_activity_time
try:
keystroke_count += 1
current_time = time.time()
last_mouse_move_time = current_time
last_activity_time = current_time
# Debug logging for keystroke issues
if keystroke_count % 100 == 0: # Every 100 keystrokes
print(f"[Keystroke Debug] Count: {keystroke_count}")
except Exception as e:
print(f"[Keystroke Error] {e}")
def on_mouse_move(x, y):
global last_mouse_move_time, last_break_reminder_time, last_activity_time
try:
current_time = time.time()
last_mouse_move_time = current_time
last_activity_time = current_time
if current_time - last_break_reminder_time > 60:
last_break_reminder_time = current_time
except Exception as e:
print(f"[Mouse Error] {e}")
def start_input_listeners():
"""Start keyboard and mouse listeners with error handling"""
global keyboard_listener, mouse_listener
try:
# Stop existing listeners if they exist
if keyboard_listener:
keyboard_listener.stop()
if mouse_listener:
mouse_listener.stop()
# Start new listeners
keyboard_listener = keyboard.Listener(on_press=on_key_press)
keyboard_listener.start()
print("[Tracker] Keyboard listener started successfully")
mouse_listener = mouse.Listener(on_move=on_mouse_move)
mouse_listener.start()
print("[Tracker] Mouse listener started successfully")
return True
except Exception as e:
print(f"[Input Listener Error] Failed to start listeners: {e}")
return False
def load_existing_sessions():
"""Load existing sessions from file on startup"""
global sessions
try:
if os.path.exists(SESSIONS_FILE):
with open(SESSIONS_FILE, "r") as f:
sessions = json.load(f)
print(f"[Tracker] Loaded {len(sessions)} existing sessions")
else:
sessions = []
print("[Tracker] No existing sessions file found, starting fresh")
except Exception as e:
print(f"[Tracker] Error loading sessions: {e}")
sessions = []
def load_current_session_state():
"""Load current session state from status file to resume session"""
global \
session_start_time, \
keystroke_count, \
last_break_reminder_time, \
last_activity_time
try:
if os.path.exists(STATUS_FILE):
with open(STATUS_FILE, "r") as f:
status = json.load(f)
if "last_updated" in status:
last_updated = datetime.fromisoformat(status["last_updated"])
time_since_update = (datetime.now() - last_updated).total_seconds()
if time_since_update < 600: # 10 minutes
if "session_start_time" in status:
session_start_from_file = datetime.fromisoformat(
status["session_start_time"]
)
session_start_time = session_start_from_file.timestamp()
keystroke_count = status.get("keystrokes", 0)
last_break_reminder_time = session_start_time
last_activity_time = time.time()
resumed_duration = time.time() - session_start_time
print(
f"[Tracker] Resumed existing session (running for {resumed_duration:.0f} seconds)"
)
return True
print(
"[Tracker] Starting new session (previous session too old or invalid)"
)
else:
print("[Tracker] No status file found, starting new session")
except Exception as e:
print(f"[Tracker] Error loading session state: {e}")
# Start fresh session
current_time = time.time()
session_start_time = current_time
keystroke_count = 0
last_break_reminder_time = current_time
last_activity_time = current_time
return False
def get_active_window():
"""Get currently active window with better error handling"""
try:
hwnd = win32gui.GetForegroundWindow()
if not hwnd:
return None, None
_, pid = win32process.GetWindowThreadProcessId(hwnd)
try:
proc = psutil.Process(pid)
return proc.name(), win32gui.GetWindowText(hwnd)
except:
# If we can't get process info, just return the window title
try:
return "Unknown", win32gui.GetWindowText(hwnd)
except:
return None, None
except Exception as e:
return None, None
def get_open_windows_cached():
"""Cached version of window enumeration - only updates every 5 seconds"""
global last_window_enum_time
current_time = time.time()
# Only enumerate windows every 5 seconds
if current_time - last_window_enum_time < WINDOW_ENUM_INTERVAL:
return {}
last_window_enum_time = current_time
windows = {}
def callback(hwnd, extra):
try:
if not win32gui.IsWindowVisible(hwnd):
return
try:
_, pid = win32process.GetWindowThreadProcessId(hwnd)
try:
proc = psutil.Process(pid)
name = proc.name()
except:
# If we can't get process info, skip this window entirely
return
except Exception:
return
try:
title = win32gui.GetWindowText(hwnd)
except:
title = "Unknown"
if name and title:
if name not in windows:
windows[name] = {
"instances": [],
"count": 0,
"start_time": time.time(),
}
window_exists = any(
instance["title"] == title and instance["hwnd"] == hwnd
for instance in windows[name]["instances"]
)
if not window_exists:
windows[name]["instances"].append(
{"hwnd": hwnd, "title": title, "pid": pid}
)
windows[name]["count"] = len(windows[name]["instances"])
if name in open_apps:
windows[name]["start_time"] = open_apps[name].get(
"start_time", time.time()
)
except Exception:
# Silent failure for window enumeration errors
pass
try:
win32gui.EnumWindows(callback, None)
except Exception as e:
print(f"[Window Enum Error] {e}")
return windows
def check_idle_apps(current_time):
"""Enhanced idle app checking with performance optimization"""
global last_resource_check_time
for app, info in list(open_apps.items()):
if not should_alert_for_app(app, current_time):
continue
last_used = info.get("last_used_time", info["start_time"])
duration_since_used = current_time - last_used
minutes_unused = duration_since_used / 60
alert_history = info.get("alert_history", [])
for level_index, level in enumerate(ALERT_LEVELS):
if not alert_config.get("alert_levels_enabled", [True, True, True])[
level_index
]:
continue
if (
minutes_unused >= level["minutes"]
and level["minutes"] not in alert_history
):
# Resource usage disabled to prevent system crashes
resource_info = ""
instance_count = info.get("instance_count", 1)
instance_text = (
f" ({instance_count} instances)" if instance_count > 1 else ""
)
severity_emoji = {"info": "💡", "warning": "⚠️", "critical": "🚨"}
emoji = severity_emoji.get(level["severity"], "💡")
message = (
f"{emoji} {app}{instance_text} has been idle for "
f"{int(minutes_unused)} minutes.{resource_info}\n"
f"Consider closing it to save resources."
)
show_notification(
level["title"],
message,
duration=8 + (level_index * 2),
)
alert_history.append(level["minutes"])
open_apps[app]["alert_history"] = alert_history
print(
f"[Alert] {level['title']} - {app} idle for {int(minutes_unused)} minutes"
)
if level["minutes"] >= 30:
snooze_app_alerts(app, 60)
break
# Update resource check time
if current_time - last_resource_check_time >= RESOURCE_CHECK_INTERVAL:
last_resource_check_time = current_time
def save_log():
"""Save log buffer to file"""
try:
with open(LOG_FILE, "a") as f:
for entry in log_buffer:
f.write(json.dumps(entry) + "\n")
log_buffer.clear()
except Exception as e:
print(f"[Log Save Error] {e}")
def save_sessions():
"""Save sessions to file"""
try:
with open(SESSIONS_FILE, "w") as f:
json.dump(sessions, f, indent=2)
print(f"[Tracker] Saved {len(sessions)} sessions to file")
except Exception as e:
print(f"[Tracker] Error saving sessions: {e}")
def save_insights(insights):
"""Save insights to insights.json"""
try:
script_dir = Path(__file__).parent.absolute()
insights_file = script_dir / "data" / "session_insights.json"
# Load existing insights
existing_insights = []
if insights_file.exists():
try:
with open(insights_file, "r", encoding="utf-8") as f:
existing_insights = json.load(f)
except:
existing_insights = []
# Add new insight
existing_insights.append(insights)
# Keep only last 100 insights
if len(existing_insights) > 100:
existing_insights = existing_insights[-100:]
# Save back to file
with open(insights_file, "w", encoding="utf-8") as f:
json.dump(existing_insights, f, indent=2, ensure_ascii=False)
print(f"[AI Analysis] Insights saved to {insights_file}")
return True
except Exception as e:
print(f"[AI Analysis] Error saving insights: {e}")
return False
def update_alerts(llm_suggestion):
print(llm_suggestion)
parsed_suggestions = json.loads(llm_suggestion)
alert_config["break_reminder_interval"] = parsed_suggestions["break_threshold"] * 60
save_alert_config()
ALERT_LEVELS[0]["minutes"] = parsed_suggestions["idle_app_threshold_1"]
ALERT_LEVELS[1]["minutes"] = parsed_suggestions["idle_app_threshold_2"]
ALERT_LEVELS[2]["minutes"] = parsed_suggestions["idle_app_threshold_3"]
save_insights(parsed_suggestions)
def update_status_file(session_time, keystrokes):
"""Update status file with current data"""
try:
open_apps_details = {}
current_time = time.time()
for app_name, app_info in open_apps.items():
start_time = app_info.get("start_time", current_time)
last_used = app_info.get("last_used_time", start_time)
duration_open = current_time - start_time
duration_since_used = current_time - last_used
# Resource usage disabled to prevent system crashes
resource_usage = None
open_apps_details[app_name] = {
"title": app_info.get("title", ""),
"start_time": datetime.fromtimestamp(start_time).isoformat(),
"duration_open_sec": round(duration_open, 2),
"last_used_time": datetime.fromtimestamp(last_used).isoformat(),
"duration_since_used_sec": round(duration_since_used, 2),
"alert_history": app_info.get("alert_history", []),
"is_current": app_name == current_app,
"instance_count": app_info.get("instance_count", 1),
"instances": app_info.get("instances", []),
"resource_usage": resource_usage,
}
# Get browser data with error handling
browser_data = {}
try:
browser_data = get_browser_status()
except Exception as e:
print(f"Error getting browser data: {e}")
browser_data = {}
status_data = {
"session_time": session_time,
"session_start_time": datetime.fromtimestamp(
session_start_time
).isoformat(),
"keystrokes": keystrokes,
"last_updated": datetime.now().isoformat(),
"open_apps": list(open_apps.keys()),
"open_apps_details": open_apps_details,
"current_app": current_app,
"sessions": sessions[-5:],
"browser_data": browser_data,
"alert_config": alert_config,
"custom_alerts": load_custom_alerts(),
}
with open(STATUS_FILE, "w") as f:
json.dump(status_data, f, indent=2)
print(f"[Status Debug] Session time: {session_time}s, Keystrokes: {keystrokes}")
except Exception as e:
print(f"[Status Update Error] {e}")
def update_tracker():
"""Optimized main tracking loop with reduced system load"""
global \
current_app, \
current_title, \
start_time, \
open_apps, \
session_start_time, \
keystroke_count, \
last_activity_time, \
last_browser_update_time
try:
session_ended_recently = False
loop_count = 0
print("[Tracker] Starting main tracking loop...")
while True:
loop_start_time = time.time()
now = time.time()
# Performance monitoring every 20 loops (now ~100 seconds)
if loop_count % 20 == 0:
print(
f"[Performance] Loop {loop_count}, Session: {(now - session_start_time) / 60:.1f}min, "
f"Keys: {keystroke_count}, Apps: {len(open_apps)}"
)
# Session management
time_since_last_activity = now - last_activity_time
session_duration = now - session_start_time
# End session after 10 minutes of inactivity
if (
time_since_last_activity > 600
and session_duration > 60
and not session_ended_recently
):
new_session = {
"start": datetime.fromtimestamp(session_start_time).isoformat(),
"end": datetime.fromtimestamp(now).isoformat(),
"duration_sec": round(session_duration, 2),
}
sessions.append(new_session)
save_sessions()
print(
f"[Tracker] Session ended after {time_since_last_activity / 60:.1f} minutes of inactivity"
)
show_notification(
"Session Ended",
f"No activity for {int(time_since_last_activity / 60)} minutes. "
f"Session recorded ({session_duration / 60:.1f} min). Total sessions: {len(sessions)}",
duration=5,
)
save_log()
# Start new session
session_start_time = now
start_time = now
keystroke_count = 0
last_break_reminder_time = now
last_activity_time = now
session_ended_recently = True
def reset_session_flag():
global session_ended_recently
time.sleep(30)
session_ended_recently = False
llm_suggestions = give_timer_suggestions()
update_alerts(llm_suggestions)
threading.Thread(target=reset_session_flag, daemon=True).start()
continue
session_ended_recently = False
# Update open windows (cached - only every 10 seconds now)
try: