File size: 9,888 Bytes
b561839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sqlite3
from datetime import datetime
import numpy as np

DB_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "attendance.db")

def get_db_connection():
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def init_db():
    conn = get_db_connection()
    cursor = conn.cursor()
    
    # 1. Create Employees table
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS employees (
        id TEXT PRIMARY KEY,
        name TEXT NOT NULL,
        email TEXT UNIQUE NOT NULL,
        role TEXT NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )
    """)
    
    # 2. Create Face Embeddings table (multi-angle storage)
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS face_embeddings (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        employee_id TEXT NOT NULL,
        angle TEXT NOT NULL, -- 'center', 'left', 'right', 'up', 'down'
        embedding BLOB NOT NULL, -- Binary float array (128 floats)
        FOREIGN KEY (employee_id) REFERENCES employees(id) ON DELETE CASCADE,
        UNIQUE(employee_id, angle)
    )
    """)
    
    # 3. Create Attendance Logs table
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS attendance_logs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        employee_id TEXT NOT NULL,
        timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        event_type TEXT NOT NULL, -- 'check_in', 'check_out'
        similarity_score REAL NOT NULL,
        FOREIGN KEY (employee_id) REFERENCES employees(id) ON DELETE CASCADE
    )
    """)
    
    conn.commit()
    conn.close()
    print("[+] SQLite Database initialized successfully!")

# Initialize immediately upon module load
init_db()

# CRUD: Employees
def add_employee(emp_id: str, name: str, email: str, role: str):
    conn = get_db_connection()
    cursor = conn.cursor()
    try:
        cursor.execute(
            "INSERT INTO employees (id, name, email, role) VALUES (?, ?, ?, ?)",
            (emp_id, name, email, role)
        )
        conn.commit()
        return True
    except sqlite3.IntegrityError as e:
        print(f"[-] Database Integrity Error: {e}")
        return False
    finally:
        conn.close()

def delete_employee(emp_id: str):
    conn = get_db_connection()
    cursor = conn.cursor()
    try:
        # Cascade deletes will clean up embeddings and logs due to CASCADE setup
        cursor.execute("PRAGMA foreign_keys = ON")
        cursor.execute("DELETE FROM employees WHERE id = ?", (emp_id,))
        conn.commit()
        return True
    except Exception as e:
        print(f"[-] Failed to delete employee {emp_id}: {e}")
        return False
    finally:
        conn.close()

def get_employee(emp_id: str):
    conn = get_db_connection()
    row = conn.execute("SELECT * FROM employees WHERE id = ?", (emp_id,)).fetchone()
    conn.close()
    return dict(row) if row else None

def get_all_employees():
    conn = get_db_connection()
    rows = conn.execute("SELECT * FROM employees ORDER BY created_at DESC").fetchall()
    conn.close()
    return [dict(r) for r in rows]

# Face Embeddings Operations
def save_face_embedding(emp_id: str, angle: str, embedding: np.ndarray):
    conn = get_db_connection()
    cursor = conn.cursor()
    try:
        # SFace embedding is a 1x128 array of float32
        # Convert numpy array to raw bytes for binary storage
        embedding_bytes = embedding.astype(np.float32).tobytes()
        cursor.execute(
            """
            INSERT OR REPLACE INTO face_embeddings (employee_id, angle, embedding)
            VALUES (?, ?, ?)
            """,
            (emp_id, angle, embedding_bytes)
        )
        conn.commit()
        return True
    except Exception as e:
        print(f"[-] Error saving embedding: {e}")
        return False
    finally:
        conn.close()

def get_all_embeddings():
    conn = get_db_connection()
    rows = conn.execute(
        """
        SELECT fe.employee_id, fe.angle, fe.embedding, e.name
        FROM face_embeddings fe
        JOIN employees e ON fe.employee_id = e.id
        """
    ).fetchall()
    conn.close()
    
    embeddings_list = []
    for r in rows:
        # Deserialize bytes back to float32 numpy array and reshape to (1, 128) for SFace
        emb_arr = np.frombuffer(r['embedding'], dtype=np.float32).reshape(1, 128)
        embeddings_list.append({
            "employee_id": r['employee_id'],
            "name": r['name'],
            "angle": r['angle'],
            "embedding": emb_arr
        })
    return embeddings_list

def has_all_embeddings(emp_id: str):
    conn = get_db_connection()
    count = conn.execute(
        "SELECT COUNT(*) as count FROM face_embeddings WHERE employee_id = ?",
        (emp_id,)
    ).fetchone()['count']
    conn.close()
    return count >= 5  # center, left, right, up, down

# Attendance Logs Operations
def log_attendance(emp_id: str, event_type: str, score: float):
    conn = get_db_connection()
    cursor = conn.cursor()
    try:
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        cursor.execute(
            """
            INSERT INTO attendance_logs (employee_id, event_type, similarity_score, timestamp)
            VALUES (?, ?, ?, ?)
            """,
            (emp_id, event_type, score, timestamp)
        )
        conn.commit()
        return True
    except Exception as e:
        print(f"[-] Error logging attendance: {e}")
        return False
    finally:
        conn.close()

def get_last_attendance_log(emp_id: str):
    conn = get_db_connection()
    row = conn.execute(
        """
        SELECT * FROM attendance_logs 
        WHERE employee_id = ? 
        ORDER BY timestamp DESC LIMIT 1
        """,
        (emp_id,)
    ).fetchone()
    conn.close()
    return dict(row) if row else None

def get_todays_logs():
    conn = get_db_connection()
    today_str = datetime.now().strftime("%Y-%m-%d")
    rows = conn.execute(
        """
        SELECT al.id, al.employee_id, al.event_type, al.similarity_score, al.timestamp, e.name, e.role
        FROM attendance_logs al
        JOIN employees e ON al.employee_id = e.id
        WHERE al.timestamp LIKE ?
        ORDER BY al.timestamp DESC
        """,
        (f"{today_str}%",)
    ).fetchall()
    conn.close()
    return [dict(r) for r in rows]

def get_attendance_history(limit: int = 100):
    conn = get_db_connection()
    rows = conn.execute(
        """
        SELECT al.id, al.employee_id, al.event_type, al.similarity_score, al.timestamp, e.name, e.role, e.email
        FROM attendance_logs al
        JOIN employees e ON al.employee_id = e.id
        ORDER BY al.timestamp DESC LIMIT ?
        """,
        (limit,)
    ).fetchall()
    conn.close()
    return [dict(r) for r in rows]

def get_todays_stats():
    conn = get_db_connection()
    today_str = datetime.now().strftime("%Y-%m-%d")
    
    # 1. Total employees
    total_emp = conn.execute("SELECT COUNT(*) as count FROM employees").fetchone()['count']
    
    # 2. Total active logs today
    today_logs = conn.execute(
        "SELECT COUNT(*) as count FROM attendance_logs WHERE timestamp LIKE ?",
        (f"{today_str}%",)
    ).fetchone()['count']
    
    # 3. Unique present employees today
    present_today = conn.execute(
        """
        SELECT COUNT(DISTINCT employee_id) as count 
        FROM attendance_logs 
        WHERE timestamp LIKE ? AND event_type = 'check_in'
        """,
        (f"{today_str}%",)
    ).fetchone()['count']
    
    # 4. Checked out today
    checked_out_today = conn.execute(
        """
        SELECT COUNT(DISTINCT employee_id) as count 
        FROM attendance_logs 
        WHERE timestamp LIKE ? AND event_type = 'check_out'
        """,
        (f"{today_str}%",)
    ).fetchone()['count']
    
    conn.close()
    return {
        "total_employees": total_emp,
        "today_logs_count": today_logs,
        "present_count": present_today,
        "checked_out_count": checked_out_today,
        "absent_count": max(0, total_emp - present_today)
    }

def get_weekly_attendance():
    conn = get_db_connection()
    cursor = conn.cursor()
    
    # Generate past 7 days (including today)
    import datetime as dt
    dates = []
    for i in range(6, -1, -1):
        d = (dt.datetime.now() - dt.timedelta(days=i)).strftime("%Y-%m-%d")
        dates.append(d)
        
    stats = []
    for d in dates:
        cursor.execute(
            """
            SELECT COUNT(DISTINCT employee_id) as count 
            FROM attendance_logs 
            WHERE timestamp LIKE ? AND event_type = 'check_in'
            """,
            (f"{d}%",)
        )
        row = cursor.fetchone()
        count = row['count'] if row else 0
        
        date_obj = dt.datetime.strptime(d, "%Y-%m-%d")
        day_label = date_obj.strftime("%a")
        stats.append({"date": d, "label": day_label, "count": count})
        
    conn.close()
    return stats

def get_hourly_attendance():
    conn = get_db_connection()
    cursor = conn.cursor()
    today_str = datetime.now().strftime("%Y-%m-%d")
    
    hours = ["09", "10", "11", "12", "13", "14", "15", "16", "17"]
    stats = []
    
    for h in hours:
        cursor.execute(
            """
            SELECT COUNT(*) as count 
            FROM attendance_logs 
            WHERE timestamp LIKE ? AND strftime('%H', timestamp) = ? AND event_type = 'check_in'
            """,
            (f"{today_str}%", h)
        )
        row = cursor.fetchone()
        count = row['count'] if row else 0
        
        h_int = int(h)
        ampm = "AM" if h_int < 12 else "PM"
        disp_h = h_int if h_int <= 12 else h_int - 12
        if disp_h == 0:
            disp_h = 12
        stats.append({"hour": h, "label": f"{disp_h} {ampm}", "count": count})
        
    conn.close()
    return stats