-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
270 lines (228 loc) · 8.54 KB
/
tracker.py
File metadata and controls
270 lines (228 loc) · 8.54 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
"""
TaskTracker - Background Window Monitor
Polls the active window title every N seconds and records process activity.
Records time segments to SQLite database (only process name and time range).
"""
import sqlite3
import json
import time
import threading
import logging
from datetime import datetime
from pathlib import Path
# Windows-specific import
try:
import win32gui
import win32process
import psutil
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
logging.warning("pywin32/psutil not available. Using mock window detection.")
DB_PATH = Path("tasktracker.db")
CONFIG_PATH = Path("config.json")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("tracker.log", encoding="utf-8"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def init_db():
"""Initialize the SQLite database schema - simplified to only store process activity.
Drops the legacy 'segments' table (which stored task_id/task_name) if it still exists.
"""
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS process_segments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
process_name TEXT NOT NULL,
start_time REAL NOT NULL,
end_time REAL,
window_title TEXT
)
""")
conn.commit()
conn.close()
logger.info("Database initialized at %s", DB_PATH)
def load_config():
"""Load task configuration from config.json."""
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
return json.load(f)
def get_active_window_title():
"""Return the title of the currently focused window."""
if HAS_WIN32:
try:
hwnd = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(hwnd)
return title
except Exception as e:
logger.debug("Error getting window title: %s", e)
return ""
else:
return "Unknown Window"
def get_active_process_name():
"""Return the process name of the currently focused window."""
if HAS_WIN32:
try:
hwnd = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(hwnd)
proc = psutil.Process(pid)
return proc.name()
except Exception:
return ""
return ""
def match_task(window_title: str, process_name: str, tasks: list) -> dict | None:
"""
Match the active window to a configured task (for display purposes only).
Returns the matching task dict or None if no match found.
"""
combined = (window_title + " " + process_name).lower()
others_task = None
for task in tasks:
if task.get("id") == "others":
others_task = task
continue
for keyword in task.get("keywords", []):
if keyword.lower() in combined:
return task
return others_task
class TaskTracker:
def __init__(self):
self.config = load_config()
self.tasks = self.config["tasks"]
self.settings = self.config["settings"]
self.poll_interval = self.settings.get("poll_interval_seconds", 3)
self.min_segment = self.settings.get("min_segment_seconds", 5)
self.current_process = None
self.current_segment_id = None
self.current_segment_start = None
self._running = False
self._lock = threading.Lock()
init_db()
def reload_config(self):
"""Hot-reload configuration."""
self.config = load_config()
self.tasks = self.config["tasks"]
self.settings = self.config["settings"]
self.poll_interval = self.settings.get("poll_interval_seconds", 3)
self.min_segment = self.settings.get("min_segment_seconds", 5)
logger.info("Config reloaded.")
def _start_segment(self, process_name: str, window_title: str):
"""Open a new time segment for the given process."""
now = time.time()
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
INSERT INTO process_segments (process_name, start_time, window_title)
VALUES (?, ?, ?)
""", (process_name, now, window_title))
seg_id = c.lastrowid
conn.commit()
conn.close()
self.current_process = process_name
self.current_segment_id = seg_id
self.current_segment_start = now
logger.info("▶ Started segment #%d for process '%s' | window: %s",
seg_id, process_name, window_title[:80])
def _end_segment(self):
"""Close the current open segment."""
if self.current_segment_id is None:
return
now = time.time()
duration = now - (self.current_segment_start or now)
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
if duration < self.min_segment:
c.execute("DELETE FROM process_segments WHERE id = ?", (self.current_segment_id,))
logger.debug("Deleted short segment #%d (%.1fs)", self.current_segment_id, duration)
else:
c.execute("UPDATE process_segments SET end_time = ? WHERE id = ?",
(now, self.current_segment_id))
logger.info("■ Ended segment #%d for process '%s' (%.0fs)",
self.current_segment_id,
self.current_process or "?",
duration)
conn.commit()
conn.close()
self.current_process = None
self.current_segment_id = None
self.current_segment_start = None
def _poll(self):
"""Single poll cycle: detect active window and update segments."""
window_title = get_active_window_title()
process_name = get_active_process_name()
if not process_name:
return
with self._lock:
if self.current_process is None:
# Start new segment
self._start_segment(process_name, window_title)
elif process_name != self.current_process:
# Process changed, end current and start new
self._end_segment()
self._start_segment(process_name, window_title)
else:
# Same process, update end_time (heartbeat)
if self.current_segment_id is not None:
now = time.time()
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("UPDATE process_segments SET end_time = ? WHERE id = ?",
(now, self.current_segment_id))
conn.commit()
conn.close()
def start(self):
"""Start the background polling loop."""
self._running = True
logger.info("TaskTracker started. Poll interval: %ds", self.poll_interval)
while self._running:
try:
self._poll()
except Exception as e:
logger.error("Poll error: %s", e)
time.sleep(self.poll_interval)
# Clean up on stop
with self._lock:
self._end_segment()
logger.info("TaskTracker stopped.")
def stop(self):
self._running = False
def get_status(self) -> dict:
"""Return current tracking status."""
with self._lock:
title = get_active_window_title()
process = get_active_process_name()
matched = match_task(title, process, self.tasks)
return {
"running": self._running,
"current_process": self.current_process,
"segment_start": self.current_segment_start,
"window_title": title,
"process_name": process,
"matched_task": matched
}
# Singleton tracker instance (used by app.py)
_tracker_instance: TaskTracker | None = None
_tracker_thread: threading.Thread | None = None
def get_tracker() -> TaskTracker:
global _tracker_instance
if _tracker_instance is None:
_tracker_instance = TaskTracker()
return _tracker_instance
def start_tracker_thread() -> threading.Thread:
global _tracker_thread, _tracker_instance
_tracker_instance = TaskTracker()
t = threading.Thread(target=_tracker_instance.start, daemon=True, name="TaskTrackerThread")
t.start()
_tracker_thread = t
return t
if __name__ == "__main__":
tracker = TaskTracker()
try:
tracker.start()
except KeyboardInterrupt:
tracker.stop()