File size: 16,240 Bytes
469692c | 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 | """
Database Models and Connection Management
SQLite database for users, predictions, and feedback
"""
import os
import sqlite3
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple
from contextlib import contextmanager
import json
class Database:
"""Database management class for SQLite"""
def __init__(self, db_path: str = '../data/app.db'):
self.db_path = os.path.abspath(db_path)
db_dir = os.path.dirname(self.db_path)
os.makedirs(db_dir, exist_ok=True)
self._init_database()
@contextmanager
def get_connection(self):
"""Get database connection with automatic commit/rollback"""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row # Return rows as dictionaries
try:
yield conn
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
def _init_database(self):
"""Initialize database tables"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT UNIQUE NOT NULL,
email TEXT,
phone TEXT,
baby_name TEXT NOT NULL,
baby_gender TEXT,
baby_birthday TEXT,
platform TEXT DEFAULT 'web',
language TEXT DEFAULT 'ar',
registration_date TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
)
''')
# Predictions table (recordings)
cursor.execute('''
CREATE TABLE IF NOT EXISTS predictions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT,
prediction TEXT NOT NULL,
confidence REAL NOT NULL,
audio_id TEXT UNIQUE NOT NULL,
model_type TEXT,
timestamp TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id)
)
''')
# Feedback table
cursor.execute('''
CREATE TABLE IF NOT EXISTS feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT,
audio_id TEXT NOT NULL,
user_id TEXT,
predicted_label TEXT NOT NULL,
correct_label TEXT NOT NULL,
is_correct INTEGER NOT NULL,
confidence REAL,
category TEXT,
timestamp TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id)
)
''')
# Analytics events table
cursor.execute('''
CREATE TABLE IF NOT EXISTS analytics_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event_name TEXT NOT NULL,
user_id TEXT,
event_data TEXT,
timestamp TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
''')
# Create indexes for better query performance
cursor.execute('CREATE INDEX IF NOT EXISTS idx_users_user_id ON users(user_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_predictions_user_id ON predictions(user_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_predictions_timestamp ON predictions(timestamp)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_feedback_user_id ON feedback(user_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_feedback_audio_id ON feedback(audio_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_feedback_timestamp ON feedback(timestamp)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_analytics_user_id ON analytics_events(user_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_analytics_timestamp ON analytics_events(timestamp)')
conn.commit()
# ==================== USERS CRUD ====================
def create_user(self, user_data: Dict) -> str:
"""Create a new user"""
with self.get_connection() as conn:
cursor = conn.cursor()
registration_date = user_data.get('registration_date', datetime.now().isoformat())
cursor.execute('''
INSERT INTO users (
user_id, email, phone, baby_name, baby_gender, baby_birthday,
platform, language, registration_date
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
user_data.get('user_id'),
user_data.get('email'),
user_data.get('phone'),
user_data.get('baby_name'),
user_data.get('baby_gender'),
user_data.get('baby_birthday'),
user_data.get('platform', 'web'),
user_data.get('language', 'ar'),
registration_date
))
return user_data.get('user_id')
def get_user(self, user_id: str) -> Optional[Dict]:
"""Get user by user_id"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE user_id = ?', (user_id,))
row = cursor.fetchone()
return dict(row) if row else None
def get_all_users(self, limit: Optional[int] = None, offset: int = 0) -> List[Dict]:
"""Get all users with pagination"""
with self.get_connection() as conn:
cursor = conn.cursor()
if limit:
cursor.execute('SELECT * FROM users ORDER BY registration_date DESC LIMIT ? OFFSET ?', (limit, offset))
else:
cursor.execute('SELECT * FROM users ORDER BY registration_date DESC')
return [dict(row) for row in cursor.fetchall()]
def update_user(self, user_id: str, user_data: Dict) -> bool:
"""Update user information"""
with self.get_connection() as conn:
cursor = conn.cursor()
updates = []
values = []
for key in ['email', 'phone', 'baby_name', 'baby_gender', 'baby_birthday', 'platform', 'language']:
if key in user_data:
updates.append(f'{key} = ?')
values.append(user_data[key])
if not updates:
return False
updates.append('updated_at = ?')
values.append(datetime.now().isoformat())
values.append(user_id)
cursor.execute(f'''
UPDATE users SET {", ".join(updates)}
WHERE user_id = ?
''', values)
return cursor.rowcount > 0
def delete_user(self, user_id: str) -> bool:
"""Delete user"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('DELETE FROM users WHERE user_id = ?', (user_id,))
return cursor.rowcount > 0
def count_users(self) -> int:
"""Count total users"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) as count FROM users')
return cursor.fetchone()['count']
# ==================== PREDICTIONS ====================
def create_prediction(self, prediction_data: Dict):
"""Save a prediction (recording)"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO predictions (
user_id, prediction, confidence, audio_id, model_type, timestamp
) VALUES (?, ?, ?, ?, ?, ?)
''', (
prediction_data.get('user_id'),
prediction_data.get('prediction'),
prediction_data.get('confidence', 0),
prediction_data.get('audio_id'),
prediction_data.get('model_type'),
prediction_data.get('timestamp', datetime.now().isoformat())
))
def count_predictions(self, user_id: Optional[str] = None) -> int:
"""Count total predictions/recordings"""
with self.get_connection() as conn:
cursor = conn.cursor()
if user_id:
cursor.execute('SELECT COUNT(*) as count FROM predictions WHERE user_id = ?', (user_id,))
else:
cursor.execute('SELECT COUNT(*) as count FROM predictions')
return cursor.fetchone()['count']
# ==================== FEEDBACK ====================
def create_feedback(self, feedback_data: Dict):
"""Save feedback"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO feedback (
audio_id, user_id, predicted_label, correct_label,
is_correct, confidence, category, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (
feedback_data.get('audio_id'),
feedback_data.get('user_id'),
feedback_data.get('predicted_label'),
feedback_data.get('correct_label'),
1 if feedback_data.get('is_correct', False) else 0,
feedback_data.get('confidence'),
feedback_data.get('correct_label'), # category
feedback_data.get('timestamp', datetime.now().isoformat())
))
def count_feedback(self, is_correct: Optional[bool] = None) -> int:
"""Count feedback (optionally filtered by is_correct)"""
with self.get_connection() as conn:
cursor = conn.cursor()
if is_correct is not None:
cursor.execute(
'SELECT COUNT(*) as count FROM feedback WHERE is_correct = ?',
(1 if is_correct else 0,)
)
else:
cursor.execute('SELECT COUNT(*) as count FROM feedback')
return cursor.fetchone()['count']
# ==================== STATISTICS ====================
def get_user_stats(self) -> Dict:
"""Get comprehensive user statistics"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Total users
cursor.execute('SELECT COUNT(*) as count FROM users')
total_users = cursor.fetchone()['count']
# Active users (last 7/30 days) - users who made predictions
seven_days_ago = (datetime.now() - timedelta(days=7)).isoformat()
thirty_days_ago = (datetime.now() - timedelta(days=30)).isoformat()
cursor.execute('''
SELECT COUNT(DISTINCT user_id) as count FROM predictions
WHERE timestamp >= ? AND user_id IS NOT NULL
''', (seven_days_ago,))
active_7d = cursor.fetchone()['count']
cursor.execute('''
SELECT COUNT(DISTINCT user_id) as count FROM predictions
WHERE timestamp >= ? AND user_id IS NOT NULL
''', (thirty_days_ago,))
active_30d = cursor.fetchone()['count']
# New users
cursor.execute('''
SELECT COUNT(*) as count FROM users
WHERE registration_date >= ?
''', (seven_days_ago,))
new_users_7d = cursor.fetchone()['count']
cursor.execute('''
SELECT COUNT(*) as count FROM users
WHERE registration_date >= ?
''', (thirty_days_ago,))
new_users_30d = cursor.fetchone()['count']
return {
'total_users': total_users,
'active_users_7d': active_7d,
'active_users_30d': active_30d,
'new_users_7d': new_users_7d,
'new_users_30d': new_users_30d
}
def get_feedback_stats(self) -> Dict:
"""Get feedback statistics"""
with self.get_connection() as conn:
cursor = conn.cursor()
# Total feedback
total_feedback = self.count_feedback()
# Positive feedback (is_correct = 1)
positive_feedback = self.count_feedback(is_correct=True)
# Negative feedback (is_correct = 0)
negative_feedback = self.count_feedback(is_correct=False)
# Feedback by category
cursor.execute('''
SELECT category, COUNT(*) as count
FROM feedback
GROUP BY category
''')
category_counts = {row['category']: row['count'] for row in cursor.fetchall()}
# Total recordings/predictions
total_recordings = self.count_predictions()
return {
'total_feedback': total_feedback,
'positive_feedback': positive_feedback,
'negative_feedback': negative_feedback,
'category_counts': category_counts,
'total_recordings': total_recordings
}
def get_platform_distribution(self) -> Dict:
"""Get platform distribution"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT platform, COUNT(*) as count
FROM users
GROUP BY platform
''')
return {row['platform']: row['count'] for row in cursor.fetchall()}
def get_language_distribution(self) -> Dict:
"""Get language distribution"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT language, COUNT(*) as count
FROM users
GROUP BY language
''')
return {row['language']: row['count'] for row in cursor.fetchall()}
def get_user_timeline(self, days: int = 30) -> List[Dict]:
"""Get user registration timeline"""
with self.get_connection() as conn:
cursor = conn.cursor()
from datetime import timedelta
days_ago = (datetime.now() - timedelta(days=days)).isoformat()
cursor.execute('''
SELECT DATE(registration_date) as date, COUNT(*) as count
FROM users
WHERE registration_date >= ?
GROUP BY DATE(registration_date)
ORDER BY date ASC
''', (days_ago,))
return [{'date': row['date'], 'count': row['count']} for row in cursor.fetchall()]
def get_feedback_timeline(self, days: int = 30) -> List[Dict]:
"""Get feedback submission timeline"""
with self.get_connection() as conn:
cursor = conn.cursor()
from datetime import timedelta
days_ago = (datetime.now() - timedelta(days=days)).isoformat()
cursor.execute('''
SELECT DATE(timestamp) as date, COUNT(*) as count
FROM feedback
WHERE timestamp >= ?
GROUP BY DATE(timestamp)
ORDER BY date ASC
''', (days_ago,))
return [{'date': row['date'], 'count': row['count']} for row in cursor.fetchall()]
|