-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
409 lines (348 loc) · 14.9 KB
/
database.py
File metadata and controls
409 lines (348 loc) · 14.9 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
"""
数据库模型和数据访问层
"""
import sqlite3
import logging
from datetime import datetime, timedelta
from typing import List, Dict, Optional, Tuple
from config import DATABASE_PATH
logger = logging.getLogger(__name__)
class Database:
"""数据库管理类"""
def __init__(self, db_path: str = DATABASE_PATH):
self.db_path = db_path
self.init_database()
def get_connection(self) -> sqlite3.Connection:
"""获取数据库连接"""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
def init_database(self):
"""初始化数据库表结构"""
conn = self.get_connection()
cursor = conn.cursor()
# 创建活动记录表(每分钟一条记录)
cursor.execute('''
CREATE TABLE IF NOT EXISTS activity_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL,
mouse_distance REAL DEFAULT 0,
mouse_clicks INTEGER DEFAULT 0,
mouse_moves INTEGER DEFAULT 0,
keyboard_presses INTEGER DEFAULT 0,
window_switches INTEGER DEFAULT 0,
active_windows INTEGER DEFAULT 0,
cpu_usage REAL DEFAULT 0,
memory_usage REAL DEFAULT 0,
busy_index REAL DEFAULT 0,
is_idle BOOLEAN DEFAULT 0,
active_window_title TEXT,
UNIQUE(timestamp)
)
''')
# 创建会话表(开机/关机记录)
cursor.execute('''
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_date DATE NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME,
duration_minutes INTEGER,
active_minutes INTEGER,
idle_minutes INTEGER,
nap_minutes INTEGER DEFAULT 0,
total_mouse_clicks INTEGER DEFAULT 0,
total_key_presses INTEGER DEFAULT 0,
average_busy_index REAL DEFAULT 0
)
''')
# 创建每日统计表
cursor.execute('''
CREATE TABLE IF NOT EXISTS daily_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
stat_date DATE UNIQUE NOT NULL,
first_boot_time DATETIME,
last_shutdown_time DATETIME,
total_active_minutes INTEGER DEFAULT 0,
total_idle_minutes INTEGER DEFAULT 0,
nap_minutes INTEGER DEFAULT 0,
total_mouse_clicks INTEGER DEFAULT 0,
total_key_presses INTEGER DEFAULT 0,
total_window_switches INTEGER DEFAULT 0,
total_mouse_distance REAL DEFAULT 0,
average_busy_index REAL DEFAULT 0,
max_busy_index REAL DEFAULT 0,
work_sessions INTEGER DEFAULT 0
)
''')
# 检查并添加 total_mouse_distance 列(兼容旧数据库)
cursor.execute("PRAGMA table_info(daily_stats)")
columns = [col[1] for col in cursor.fetchall()]
if 'total_mouse_distance' not in columns:
cursor.execute('ALTER TABLE daily_stats ADD COLUMN total_mouse_distance REAL DEFAULT 0')
logger.info("已添加 total_mouse_distance 列到 daily_stats 表")
# 创建索引以提高查询性能
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_activity_timestamp
ON activity_records(timestamp)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_session_date
ON sessions(session_date)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_daily_date
ON daily_stats(stat_date)
''')
conn.commit()
conn.close()
logger.info("数据库初始化完成")
def save_activity_record(self, record: Dict) -> bool:
"""保存活动记录"""
try:
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO activity_records (
timestamp, mouse_distance, mouse_clicks, mouse_moves,
keyboard_presses, window_switches, active_windows,
cpu_usage, memory_usage, busy_index, is_idle,
active_window_title
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
record.get('timestamp'),
record.get('mouse_distance', 0),
record.get('mouse_clicks', 0),
record.get('mouse_moves', 0),
record.get('keyboard_presses', 0),
record.get('window_switches', 0),
record.get('active_windows', 0),
record.get('cpu_usage', 0),
record.get('memory_usage', 0),
record.get('busy_index', 0),
record.get('is_idle', False),
record.get('active_window_title', '')
))
conn.commit()
conn.close()
return True
except Exception as e:
logger.error(f"保存活动记录失败: {e}")
return False
def start_session(self) -> int:
"""开始新会话(开机)"""
try:
conn = self.get_connection()
cursor = conn.cursor()
now = datetime.now()
cursor.execute('''
INSERT INTO sessions (session_date, start_time)
VALUES (?, ?)
''', (now.date(), now))
session_id = cursor.lastrowid
conn.commit()
conn.close()
logger.info(f"新会话开始: {session_id}")
return session_id
except Exception as e:
logger.error(f"开始会话失败: {e}")
return -1
def end_session(self, session_id: int) -> bool:
"""结束会话(关机)"""
try:
conn = self.get_connection()
cursor = conn.cursor()
# 获取会话开始时间
cursor.execute('''
SELECT start_time FROM sessions WHERE id = ?
''', (session_id,))
result = cursor.fetchone()
if not result:
return False
start_time = datetime.fromisoformat(result['start_time'])
end_time = datetime.now()
duration = int((end_time - start_time).total_seconds() / 60)
# 统计会话期间的活动
cursor.execute('''
SELECT
COUNT(*) as total_records,
SUM(CASE WHEN is_idle = 0 THEN 1 ELSE 0 END) as active_minutes,
SUM(CASE WHEN is_idle = 1 THEN 1 ELSE 0 END) as idle_minutes,
SUM(mouse_clicks) as total_clicks,
SUM(keyboard_presses) as total_presses,
AVG(busy_index) as avg_busy
FROM activity_records
WHERE timestamp BETWEEN ? AND ?
''', (start_time, end_time))
stats = cursor.fetchone()
# 更新会话记录
cursor.execute('''
UPDATE sessions SET
end_time = ?,
duration_minutes = ?,
active_minutes = ?,
idle_minutes = ?,
total_mouse_clicks = ?,
total_key_presses = ?,
average_busy_index = ?
WHERE id = ?
''', (
end_time,
duration,
stats['active_minutes'] or 0,
stats['idle_minutes'] or 0,
stats['total_clicks'] or 0,
stats['total_presses'] or 0,
stats['avg_busy'] or 0,
session_id
))
conn.commit()
conn.close()
logger.info(f"会话结束: {session_id}, 时长: {duration}分钟")
return True
except Exception as e:
logger.error(f"结束会话失败: {e}")
return False
def update_daily_stats(self, date: datetime.date) -> bool:
"""更新每日统计
注意:将第二天凌晨0:00-2:00的活动也计入当天
"""
try:
conn = self.get_connection()
cursor = conn.cursor()
# 定义工作日的实际时间范围:
# 当天 00:00:00 到第二天 02:00:00
start_of_day = datetime.combine(date, datetime.min.time())
next_day = date + timedelta(days=1)
end_of_day = datetime.combine(next_day, datetime.min.time()) + timedelta(hours=2)
# 获取当天的所有会话(包括跨日的会话)
cursor.execute('''
SELECT
MIN(start_time) as first_boot,
MAX(CASE
WHEN end_time IS NULL THEN NULL
WHEN end_time <= ? THEN end_time
ELSE ?
END) as last_shutdown,
COUNT(*) as session_count
FROM sessions
WHERE (session_date = ? OR
(session_date = ? AND
strftime('%H', start_time) < '02'))
''', (end_of_day, end_of_day, date, next_day))
session_info = cursor.fetchone()
# 获取当天的活动统计(包括第二天凌晨2点前的数据)
cursor.execute('''
SELECT
SUM(CASE WHEN is_idle = 0 THEN 1 ELSE 0 END) as active_minutes,
SUM(CASE WHEN is_idle = 1 THEN 1 ELSE 0 END) as idle_minutes,
SUM(mouse_clicks) as total_clicks,
SUM(keyboard_presses) as total_presses,
SUM(window_switches) as total_switches,
SUM(mouse_distance) as total_distance,
AVG(busy_index) as avg_busy,
MAX(busy_index) as max_busy
FROM activity_records
WHERE timestamp >= ? AND timestamp < ?
''', (start_of_day, end_of_day))
activity_stats = cursor.fetchone()
# 检测午休时间
cursor.execute('''
SELECT
SUM(CASE WHEN is_idle = 1
AND CAST(strftime('%H', timestamp) AS INTEGER) BETWEEN 12 AND 14
THEN 1 ELSE 0 END) as nap_minutes
FROM activity_records
WHERE timestamp BETWEEN ? AND ?
''', (start_of_day, end_of_day))
nap_info = cursor.fetchone()
# 插入或更新每日统计
cursor.execute('''
INSERT OR REPLACE INTO daily_stats (
stat_date, first_boot_time, last_shutdown_time,
total_active_minutes, total_idle_minutes, nap_minutes,
total_mouse_clicks, total_key_presses, total_window_switches,
total_mouse_distance, average_busy_index, max_busy_index, work_sessions
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
date,
session_info['first_boot'],
session_info['last_shutdown'],
activity_stats['active_minutes'] or 0,
activity_stats['idle_minutes'] or 0,
nap_info['nap_minutes'] or 0,
activity_stats['total_clicks'] or 0,
activity_stats['total_presses'] or 0,
activity_stats['total_switches'] or 0,
activity_stats['total_distance'] or 0,
activity_stats['avg_busy'] or 0,
activity_stats['max_busy'] or 0,
session_info['session_count'] or 0
))
conn.commit()
conn.close()
logger.info(f"每日统计已更新: {date}")
return True
except Exception as e:
logger.error(f"更新每日统计失败: {e}")
return False
def get_activity_records(self, start_date: datetime, end_date: datetime) -> List[Dict]:
"""获取指定时间段的活动记录"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM activity_records
WHERE timestamp BETWEEN ? AND ?
ORDER BY timestamp
''', (start_date, end_date))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def get_daily_stats(self, start_date: datetime.date, end_date: datetime.date) -> List[Dict]:
"""获取指定日期范围的每日统计"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM daily_stats
WHERE stat_date BETWEEN ? AND ?
ORDER BY stat_date
''', (start_date, end_date))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def get_current_session_id(self) -> Optional[int]:
"""获取当前未结束的会话ID"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT id FROM sessions
WHERE end_time IS NULL
ORDER BY start_time DESC
LIMIT 1
''')
result = cursor.fetchone()
conn.close()
return result['id'] if result else None
def cleanup_old_data(self, days: int = 365):
"""清理旧数据"""
try:
conn = self.get_connection()
cursor = conn.cursor()
cutoff_date = datetime.now() - timedelta(days=days)
cursor.execute('''
DELETE FROM activity_records WHERE timestamp < ?
''', (cutoff_date,))
cursor.execute('''
DELETE FROM sessions WHERE session_date < ?
''', (cutoff_date.date(),))
cursor.execute('''
DELETE FROM daily_stats WHERE stat_date < ?
''', (cutoff_date.date(),))
conn.commit()
deleted = cursor.rowcount
conn.close()
logger.info(f"清理了 {deleted} 条旧数据")
return True
except Exception as e:
logger.error(f"清理旧数据失败: {e}")
return False