-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
702 lines (574 loc) · 23 KB
/
Copy pathdatabase.py
File metadata and controls
702 lines (574 loc) · 23 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
"""
TutorOps Pro — Database Module
==============================
SQLite-based persistence for message logging, analytics, and conversation history.
Thread-safe and async-ready.
"""
import sqlite3
import threading
from pathlib import Path
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from contextlib import contextmanager
# Database file location
DB_PATH = Path(__file__).parent / "tutorops.db"
# Thread-local storage for connections
_local = threading.local()
def get_connection() -> sqlite3.Connection:
"""Get thread-local database connection."""
if not hasattr(_local, 'connection') or _local.connection is None:
_local.connection = sqlite3.connect(str(DB_PATH), check_same_thread=False)
_local.connection.row_factory = sqlite3.Row
return _local.connection
@contextmanager
def get_cursor():
"""Context manager for database operations."""
conn = get_connection()
cursor = conn.cursor()
try:
yield cursor
conn.commit()
except Exception as e:
conn.rollback()
raise e
def init_database():
"""Create tables if they don't exist."""
with get_cursor() as cursor:
# Messages table
cursor.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone TEXT NOT NULL,
content TEXT NOT NULL,
response TEXT,
response_time_ms INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
is_from_user BOOLEAN DEFAULT TRUE
)
""")
# Conversations table (tracks active conversations)
cursor.execute("""
CREATE TABLE IF NOT EXISTS conversations (
phone TEXT PRIMARY KEY,
last_activity DATETIME DEFAULT CURRENT_TIMESTAMP,
message_count INTEGER DEFAULT 0,
first_contact DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# Daily stats table
cursor.execute("""
CREATE TABLE IF NOT EXISTS daily_stats (
date TEXT PRIMARY KEY,
total_messages INTEGER DEFAULT 0,
total_responses INTEGER DEFAULT 0,
avg_response_time_ms REAL DEFAULT 0,
unique_users INTEGER DEFAULT 0
)
""")
# Create indexes for performance
cursor.execute("CREATE INDEX IF NOT EXISTS idx_messages_phone ON messages(phone)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_messages_timestamp ON messages(timestamp)")
# Students table (Phase 2)
cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone TEXT UNIQUE NOT NULL,
name TEXT,
balance REAL DEFAULT 0,
notes TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# Payments table (Phase 2)
cursor.execute("""
CREATE TABLE IF NOT EXISTS payments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id INTEGER,
phone TEXT,
amount REAL NOT NULL,
transaction_id TEXT,
status TEXT DEFAULT 'pending',
receipt_path TEXT,
notes TEXT,
verified_by TEXT,
verified_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(id)
)
""")
# Create indexes for payments
cursor.execute("CREATE INDEX IF NOT EXISTS idx_payments_status ON payments(status)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_payments_phone ON payments(phone)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_students_phone ON students(phone)")
# Scheduled messages table (Phase 4)
cursor.execute("""
CREATE TABLE IF NOT EXISTS scheduled_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone TEXT,
message TEXT NOT NULL,
send_at DATETIME NOT NULL,
status TEXT DEFAULT 'pending',
sent_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# Add label column to conversations if not exists
try:
cursor.execute("ALTER TABLE conversations ADD COLUMN label TEXT")
except:
pass # Column already exists
print("[Database] Initialized successfully")
# ============== MESSAGE OPERATIONS ==============
def log_message(
phone: str,
content: str,
response: Optional[str] = None,
response_time_ms: Optional[int] = None
) -> int:
"""Log a message and update conversation stats."""
with get_cursor() as cursor:
# Insert message
cursor.execute("""
INSERT INTO messages (phone, content, response, response_time_ms)
VALUES (?, ?, ?, ?)
""", (phone, content, response, response_time_ms))
message_id = cursor.lastrowid
# Update conversation
cursor.execute("""
INSERT INTO conversations (phone, message_count)
VALUES (?, 1)
ON CONFLICT(phone) DO UPDATE SET
last_activity = CURRENT_TIMESTAMP,
message_count = message_count + 1
""", (phone,))
# Update daily stats
today = datetime.now().strftime("%Y-%m-%d")
cursor.execute("""
INSERT INTO daily_stats (date, total_messages, total_responses, unique_users)
VALUES (?, 1, ?, 1)
ON CONFLICT(date) DO UPDATE SET
total_messages = total_messages + 1,
total_responses = total_responses + ?
""", (today, 1 if response else 0, 1 if response else 0))
return message_id
def get_conversation_history(phone: str, limit: int = 5) -> List[Dict[str, Any]]:
"""Get recent messages for a phone number."""
with get_cursor() as cursor:
cursor.execute("""
SELECT content, response, timestamp
FROM messages
WHERE phone = ?
ORDER BY timestamp DESC
LIMIT ?
""", (phone, limit))
rows = cursor.fetchall()
return [dict(row) for row in reversed(rows)]
def get_conversation_context(phone: str, max_age_minutes: int = 30) -> List[Dict[str, str]]:
"""Get recent conversation for AI context (within time limit)."""
cutoff = datetime.now() - timedelta(minutes=max_age_minutes)
with get_cursor() as cursor:
cursor.execute("""
SELECT content, response
FROM messages
WHERE phone = ? AND timestamp > ?
ORDER BY timestamp DESC
LIMIT 5
""", (phone, cutoff.isoformat()))
rows = cursor.fetchall()
context = []
for row in reversed(rows):
if row['content']:
context.append({"role": "user", "text": row['content']})
if row['response']:
context.append({"role": "assistant", "text": row['response']})
return context
# ============== ANALYTICS ==============
def get_stats() -> Dict[str, Any]:
"""Get overall statistics."""
with get_cursor() as cursor:
# Total messages
cursor.execute("SELECT COUNT(*) as count FROM messages")
total_messages = cursor.fetchone()['count']
# Today's messages
today = datetime.now().strftime("%Y-%m-%d")
cursor.execute("""
SELECT total_messages, avg_response_time_ms
FROM daily_stats WHERE date = ?
""", (today,))
today_row = cursor.fetchone()
# Unique users
cursor.execute("SELECT COUNT(*) as count FROM conversations")
unique_users = cursor.fetchone()['count']
# Active conversations (last 30 mins)
cutoff = (datetime.now() - timedelta(minutes=30)).isoformat()
cursor.execute("""
SELECT COUNT(*) as count FROM conversations
WHERE last_activity > ?
""", (cutoff,))
active_convos = cursor.fetchone()['count']
return {
"total_messages": total_messages,
"messages_today": today_row['total_messages'] if today_row else 0,
"avg_response_time_ms": round(today_row['avg_response_time_ms'], 2) if today_row else 0,
"unique_users": unique_users,
"active_conversations": active_convos,
"database_size_kb": round(DB_PATH.stat().st_size / 1024, 2) if DB_PATH.exists() else 0
}
def update_response_time(date: str, response_time_ms: int):
"""Update average response time for a date."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE daily_stats SET
avg_response_time_ms = (
(avg_response_time_ms * (total_responses - 1) + ?) / total_responses
)
WHERE date = ?
""", (response_time_ms, date))
# ============== HEALTH CHECK ==============
def check_database_health() -> Dict[str, Any]:
"""Check database connectivity and integrity."""
try:
with get_cursor() as cursor:
cursor.execute("SELECT 1")
cursor.execute("PRAGMA integrity_check")
integrity = cursor.fetchone()[0]
return {
"status": "healthy" if integrity == "ok" else "degraded",
"integrity": integrity,
"path": str(DB_PATH),
"size_kb": round(DB_PATH.stat().st_size / 1024, 2) if DB_PATH.exists() else 0
}
except Exception as e:
return {
"status": "unhealthy",
"error": str(e)
}
# ============== STUDENT OPERATIONS (Phase 2) ==============
def get_or_create_student(phone: str, name: str = None) -> Dict[str, Any]:
"""Get existing student or create new one."""
with get_cursor() as cursor:
# Try to find existing
cursor.execute("SELECT * FROM students WHERE phone = ?", (phone,))
row = cursor.fetchone()
if row:
return dict(row)
# Create new student
cursor.execute("""
INSERT INTO students (phone, name) VALUES (?, ?)
""", (phone, name or "Unknown"))
cursor.execute("SELECT * FROM students WHERE id = ?", (cursor.lastrowid,))
return dict(cursor.fetchone())
def get_all_students() -> List[Dict[str, Any]]:
"""Get all students."""
with get_cursor() as cursor:
cursor.execute("""
SELECT s.*,
(SELECT COUNT(*) FROM payments WHERE student_id = s.id) as payment_count
FROM students s
ORDER BY s.created_at DESC
""")
return [dict(row) for row in cursor.fetchall()]
def update_student(student_id: int, name: str = None, notes: str = None) -> bool:
"""Update student details."""
with get_cursor() as cursor:
updates = []
params = []
if name is not None:
updates.append("name = ?")
params.append(name)
if notes is not None:
updates.append("notes = ?")
params.append(notes)
if not updates:
return False
updates.append("updated_at = CURRENT_TIMESTAMP")
params.append(student_id)
cursor.execute(f"""
UPDATE students SET {', '.join(updates)} WHERE id = ?
""", params)
return cursor.rowcount > 0
def update_student_balance(student_id: int, amount: float) -> float:
"""Add to student balance. Returns new balance."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE students SET
balance = balance + ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (amount, student_id))
cursor.execute("SELECT balance FROM students WHERE id = ?", (student_id,))
row = cursor.fetchone()
return row['balance'] if row else 0
# ============== PAYMENT OPERATIONS (Phase 2) ==============
def create_payment(
phone: str,
amount: float,
transaction_id: str = None,
receipt_path: str = None,
status: str = "pending"
) -> int:
"""Create a new payment record."""
with get_cursor() as cursor:
# Get or create student
student = get_or_create_student(phone)
cursor.execute("""
INSERT INTO payments (student_id, phone, amount, transaction_id, receipt_path, status)
VALUES (?, ?, ?, ?, ?, ?)
""", (student['id'], phone, amount, transaction_id, receipt_path, status))
return cursor.lastrowid
def get_pending_payments() -> List[Dict[str, Any]]:
"""Get all pending payments for review."""
with get_cursor() as cursor:
cursor.execute("""
SELECT p.*, s.name as student_name, s.balance as student_balance
FROM payments p
LEFT JOIN students s ON p.student_id = s.id
WHERE p.status = 'pending'
ORDER BY p.created_at DESC
""")
return [dict(row) for row in cursor.fetchall()]
def get_all_payments(limit: int = 50) -> List[Dict[str, Any]]:
"""Get recent payments."""
with get_cursor() as cursor:
cursor.execute("""
SELECT p.*, s.name as student_name
FROM payments p
LEFT JOIN students s ON p.student_id = s.id
ORDER BY p.created_at DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
def verify_payment(payment_id: int, verified_by: str = "admin") -> bool:
"""Mark payment as verified and update student balance."""
with get_cursor() as cursor:
# Get payment details
cursor.execute("SELECT * FROM payments WHERE id = ?", (payment_id,))
payment = cursor.fetchone()
if not payment or payment['status'] != 'pending':
return False
# Update payment status
cursor.execute("""
UPDATE payments SET
status = 'verified',
verified_by = ?,
verified_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (verified_by, payment_id))
# Update student balance
if payment['student_id']:
update_student_balance(payment['student_id'], payment['amount'])
return True
def reject_payment(payment_id: int, reason: str = None) -> bool:
"""Mark payment as rejected."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE payments SET
status = 'rejected',
notes = ?,
verified_at = CURRENT_TIMESTAMP
WHERE id = ? AND status = 'pending'
""", (reason, payment_id))
return cursor.rowcount > 0
def get_payment_stats() -> Dict[str, Any]:
"""Get payment statistics."""
with get_cursor() as cursor:
# Total verified
cursor.execute("""
SELECT COALESCE(SUM(amount), 0) as total
FROM payments WHERE status = 'verified'
""")
total_verified = cursor.fetchone()['total']
# Pending count
cursor.execute("SELECT COUNT(*) as count FROM payments WHERE status = 'pending'")
pending_count = cursor.fetchone()['count']
# Today's payments
today = datetime.now().strftime("%Y-%m-%d")
cursor.execute("""
SELECT COALESCE(SUM(amount), 0) as total
FROM payments
WHERE status = 'verified' AND DATE(verified_at) = ?
""", (today,))
today_total = cursor.fetchone()['total']
return {
"total_verified": total_verified,
"pending_count": pending_count,
"today_total": today_total
}
# ============== ADMIN OPERATIONS (Phase 2) ==============
def get_all_messages(limit: int = 100, offset: int = 0) -> List[Dict[str, Any]]:
"""Get all messages with pagination."""
with get_cursor() as cursor:
cursor.execute("""
SELECT * FROM messages
ORDER BY timestamp DESC
LIMIT ? OFFSET ?
""", (limit, offset))
return [dict(row) for row in cursor.fetchall()]
def search_messages(query: str, limit: int = 50) -> List[Dict[str, Any]]:
"""Search messages by content or phone."""
with get_cursor() as cursor:
search = f"%{query}%"
cursor.execute("""
SELECT * FROM messages
WHERE content LIKE ? OR phone LIKE ? OR response LIKE ?
ORDER BY timestamp DESC
LIMIT ?
""", (search, search, search, limit))
return [dict(row) for row in cursor.fetchall()]
def get_daily_stats_range(days: int = 7) -> List[Dict[str, Any]]:
"""Get daily stats for the last N days."""
with get_cursor() as cursor:
cursor.execute("""
SELECT * FROM daily_stats
ORDER BY date DESC
LIMIT ?
""", (days,))
return [dict(row) for row in cursor.fetchall()]
def export_all_data() -> Dict[str, Any]:
"""Export all data as JSON-serializable dict."""
return {
"exported_at": datetime.now().isoformat(),
"messages": get_all_messages(limit=10000),
"students": get_all_students(),
"payments": get_all_payments(limit=10000),
"stats": get_stats(),
"daily_stats": get_daily_stats_range(days=30)
}
# Initialize on import
init_database()
if __name__ == "__main__":
# Test database
print("\n=== Database Test ===")
# Log a test message
msg_id = log_message(
phone="+201234567890",
content="Test message",
response="Test response",
response_time_ms=150
)
print(f"Logged message ID: {msg_id}")
# Get history
history = get_conversation_history("+201234567890")
print(f"History: {history}")
# Get stats
stats = get_stats()
print(f"Stats: {stats}")
# Health check
health = check_database_health()
print(f"Health: {health}")
# ============== SCHEDULED MESSAGES (Phase 4) ==============
def create_scheduled_message(message: str, send_at: datetime, phone: str = None) -> int:
"""
Create a scheduled message.
If phone is None, message will be broadcast to all students.
"""
with get_cursor() as cursor:
cursor.execute("""
INSERT INTO scheduled_messages (phone, message, send_at, status)
VALUES (?, ?, ?, 'pending')
""", (phone, message, send_at.strftime("%Y-%m-%d %H:%M:%S")))
return cursor.lastrowid
def get_due_scheduled_messages() -> List[Dict]:
"""Get all scheduled messages that should be sent now."""
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with get_cursor() as cursor:
cursor.execute("""
SELECT id, phone, message, send_at, status
FROM scheduled_messages
WHERE status = 'pending' AND send_at <= ?
ORDER BY send_at ASC
""", (now,))
return [dict(row) for row in cursor.fetchall()]
def get_pending_scheduled_messages() -> List[Dict]:
"""Get all pending scheduled messages."""
with get_cursor() as cursor:
cursor.execute("""
SELECT id, phone, message, send_at, status, created_at
FROM scheduled_messages
WHERE status = 'pending'
ORDER BY send_at ASC
""")
return [dict(row) for row in cursor.fetchall()]
def mark_scheduled_message_sent(msg_id: int) -> bool:
"""Mark a scheduled message as sent."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE scheduled_messages
SET status = 'sent', sent_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (msg_id,))
return cursor.rowcount > 0
def cancel_scheduled_message(msg_id: int) -> bool:
"""Cancel a scheduled message."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE scheduled_messages
SET status = 'cancelled'
WHERE id = ? AND status = 'pending'
""", (msg_id,))
return cursor.rowcount > 0
def get_all_scheduled_messages(limit: int = 50) -> List[Dict]:
"""Get all scheduled messages (for admin)."""
with get_cursor() as cursor:
cursor.execute("""
SELECT id, phone, message, send_at, status, sent_at, created_at
FROM scheduled_messages
ORDER BY created_at DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
# ============== CONVERSATION LABELS (Phase 4) ==============
def label_conversation(phone: str, label: str) -> bool:
"""Add a label to a conversation."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE conversations SET label = ? WHERE phone = ?
""", (label, phone))
return cursor.rowcount > 0
def get_conversations_by_label(label: str) -> List[Dict]:
"""Get all conversations with a specific label."""
with get_cursor() as cursor:
cursor.execute("""
SELECT phone, last_activity, message_count, label
FROM conversations
WHERE label = ?
ORDER BY last_activity DESC
""", (label,))
return [dict(row) for row in cursor.fetchall()]
# ============== STUDENT NOTES (Phase 4) ==============
def add_student_note(phone: str, note: str) -> bool:
"""Add a note to a student's profile."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
with get_cursor() as cursor:
# Get existing notes
cursor.execute("SELECT notes FROM students WHERE phone = ?", (phone,))
row = cursor.fetchone()
if row:
existing = row['notes'] or ''
new_notes = f"{existing}\n[{timestamp}] {note}" if existing else f"[{timestamp}] {note}"
cursor.execute("""
UPDATE students SET notes = ?, updated_at = CURRENT_TIMESTAMP
WHERE phone = ?
""", (new_notes, phone))
return True
return False
def update_student_name(phone: str, name: str) -> bool:
"""Update a student's name."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE students SET name = ?, updated_at = CURRENT_TIMESTAMP
WHERE phone = ?
""", (name, phone))
return cursor.rowcount > 0
def adjust_student_balance(phone: str, amount: float, reason: str = None) -> bool:
"""Adjust a student's balance (positive or negative)."""
with get_cursor() as cursor:
cursor.execute("""
UPDATE students
SET balance = balance + ?, updated_at = CURRENT_TIMESTAMP
WHERE phone = ?
""", (amount, phone))
if cursor.rowcount > 0 and reason:
add_student_note(phone, f"Balance adjusted by {amount}: {reason}")
return cursor.rowcount > 0