-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
91 lines (77 loc) · 3.53 KB
/
Copy pathdatabase.py
File metadata and controls
91 lines (77 loc) · 3.53 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
import sqlite3
import time
import os
# Resolve the database path relative to this file's location so it always
# points to the project root regardless of the working directory at launch.
_SRC_DIR = os.path.dirname(os.path.abspath(__file__))
_PROJECT_ROOT = os.path.dirname(_SRC_DIR)
DB_PATH = os.path.join(_PROJECT_ROOT, "workplace.db")
def init_db():
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) if os.path.dirname(DB_PATH) else None
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
track_id INTEGER NOT NULL,
camera_id TEXT NOT NULL DEFAULT '0',
role TEXT NOT NULL,
entry_time REAL NOT NULL,
exit_time REAL
)
''')
# Check if camera_id exists, if not, add it for backwards compatibility
cursor.execute("PRAGMA table_info(events)")
columns = [col[1] for col in cursor.fetchall()]
if 'camera_id' not in columns:
cursor.execute("ALTER TABLE events ADD COLUMN camera_id TEXT NOT NULL DEFAULT '0'")
# CLEANUP: Close any active sessions left open from the previous run abruptly stopping
cursor.execute("UPDATE events SET exit_time = entry_time WHERE exit_time IS NULL")
conn.commit()
def log_entry(track_id, role="manager", current_time=None, camera_id="0"):
"""
Handles ENTRY events with stateless session merging.
If the last EXIT for this track_id on this camera was < 60 seconds ago, it resumes the session.
"""
if current_time is None:
current_time = time.time()
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
# Check the most recent event for this track
cursor.execute('''
SELECT id, exit_time FROM events
WHERE track_id = ? AND camera_id = ?
ORDER BY entry_time DESC LIMIT 1
''', (track_id, camera_id))
row = cursor.fetchone()
if row and row[1] is not None:
last_event_id = row[0]
last_exit_time = row[1]
# Session Cooldown Logic: If they exited less than 60s ago, merge the session
if (current_time - last_exit_time) < 60:
cursor.execute('UPDATE events SET exit_time = NULL WHERE id = ?', (last_event_id,))
conn.commit()
return
# Otherwise, insert a new entry
cursor.execute('INSERT INTO events (track_id, role, entry_time, camera_id) VALUES (?, ?, ?, ?)',
(track_id, role, current_time, camera_id))
conn.commit()
def log_exit(track_id, current_time=None, camera_id="0"):
"""
Handles EXIT events by updating the most recent open session for this track on this camera.
"""
if current_time is None:
current_time = time.time()
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
# Find the most recent open session for this track
cursor.execute('''
SELECT id FROM events
WHERE track_id = ? AND camera_id = ? AND exit_time IS NULL
ORDER BY entry_time DESC LIMIT 1
''', (track_id, camera_id))
row = cursor.fetchone()
if row:
event_id = row[0]
cursor.execute('UPDATE events SET exit_time = ? WHERE id = ?', (current_time, event_id))
conn.commit()