Spaces:
Sleeping
Sleeping
File size: 22,505 Bytes
3cd2ab5 | 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 | import psycopg2
from psycopg2 import pool
from datetime import datetime, timedelta
import os
import json
import secrets
import time
from functools import wraps
# Load and parse DATABASE_CONFIG from environment variable
database_config_json = os.getenv("neon_db")
DB_CONFIG = json.loads(database_config_json)
# Initialize connection pool
db_pool = psycopg2.pool.SimpleConnectionPool(1, 20, **DB_CONFIG)
# Retry decorator for handling EOF errors
def retry_on_eof(max_attempts=2, delay=3):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
while attempts < max_attempts:
try:
return func(*args, **kwargs)
except psycopg2.OperationalError as e:
if "SSL SYSCALL error: EOF detected" in str(e):
print(f"EOF detected in {func.__name__}, retrying {attempts + 1}/{max_attempts} after {delay}s")
attempts += 1
if attempts == max_attempts:
raise # Re-raise the exception if max attempts reached
time.sleep(delay) # Wait before retrying
# Re-establish connection if closed
conn = db_pool.getconn()
if conn.closed:
db_pool.putconn(conn, close=True)
conn = db_pool.getconn()
else:
raise # Re-raise other OperationalErrors
return func(*args, **kwargs)
return wrapper
return decorator
@retry_on_eof(max_attempts=2, delay=3)
def init_db():
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("CREATE TABLE IF NOT EXISTS users (user_id SERIAL PRIMARY KEY, email TEXT UNIQUE, password TEXT)")
c.execute("""
CREATE TABLE IF NOT EXISTS predictions (
pred_id SERIAL PRIMARY KEY,
user_id INT,
timestamp TEXT,
audio_name TEXT,
result TEXT,
audio BYTEA,
hive_id INT,
FOREIGN KEY (hive_id) REFERENCES Hive(hive_id)
)
""")
c.execute("CREATE TABLE IF NOT EXISTS farm (farm_id SERIAL PRIMARY KEY, user_id INT UNIQUE, fullname TEXT, country TEXT, city TEXT, zip TEXT, hives INT DEFAULT 0)")
c.execute("""
CREATE TABLE IF NOT EXISTS Hive (
hive_id SERIAL PRIMARY KEY,
farm_id INT,
hive_number INT,
bee_type TEXT,
number_of_frames INT,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
health_status TEXT,
notes TEXT,
FOREIGN KEY (farm_id) REFERENCES farm(farm_id)
)
""")
conn.commit()
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def generate_reset_code(email):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Check if email exists
c.execute("SELECT user_id FROM users WHERE email = %s", (email,))
user = c.fetchone()
if not user:
return None
# Generate 6-digit code
code = ''.join(str(secrets.randbelow(10)) for _ in range(6))
# Set expiration (15 minutes from now)
expires_at = datetime.now() + timedelta(minutes=15)
# Store code
c.execute(
"""
INSERT INTO password_reset_codes (user_id, code, expires_at)
VALUES (%s, %s, %s)
ON CONFLICT (user_id) DO UPDATE
SET code = %s, expires_at = %s
""",
(user[0], code, expires_at, code, expires_at)
)
conn.commit()
return code
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def verify_reset_code(email, code):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"""
SELECT code, expires_at
FROM password_reset_codes prc
JOIN users u ON prc.user_id = u.user_id
WHERE u.email = %s AND prc.code = %s
""",
(email, code)
)
result = c.fetchone()
if result and result[1] > datetime.now():
# Delete code after verification
c.execute("DELETE FROM password_reset_codes WHERE code = %s", (code,))
conn.commit()
return True
return False
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_user_password(email, new_password):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"UPDATE users SET password = %s WHERE email = %s RETURNING user_id",
(new_password, email)
)
result = c.fetchone()
conn.commit()
return bool(result)
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def change_user_password(user_id, current_password, new_password):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Verify user exists and current password matches
c.execute(
"SELECT user_id FROM users WHERE user_id = %s AND password = %s",
(user_id, current_password)
)
user = c.fetchone()
if not user:
return False
# Update password
c.execute(
"UPDATE users SET password = %s WHERE user_id = %s",
(new_password, user_id)
)
conn.commit()
return True
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def add_hive_to_db(farm_id, hive_number, bee_type, number_of_frames, health_status, notes):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
INSERT INTO Hive (farm_id, hive_number, bee_type, number_of_frames, health_status, notes)
VALUES (%s, %s, %s, %s, %s, %s)
RETURNING hive_id
""", (farm_id, hive_number, bee_type, number_of_frames, health_status, notes))
hive_id = c.fetchone()[0]
c.execute("UPDATE farm SET hives = hives + 1 WHERE farm_id = %s", (farm_id,))
conn.commit()
return hive_id
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def delete_hive_from_db(hive_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT farm_id FROM Hive WHERE hive_id = %s", (hive_id,))
farm = c.fetchone()
if farm:
farm_id = farm[0]
c.execute("DELETE FROM Hive WHERE hive_id = %s", (hive_id,))
if c.rowcount > 0:
c.execute("UPDATE farm SET hives = hives - 1 WHERE farm_id = %s", (farm_id,))
conn.commit()
return True
return False
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def register_user(fullname, email, password=None, google_id=None):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Check if email or google_id already exists
c.execute("SELECT email, google_id FROM users WHERE email = %s OR google_id = %s", (email, google_id))
if c.fetchone():
return "email already exist"
# Insert new user with default values
c.execute(
"INSERT INTO users (fullname, email, password, google_id, country, city, gender, phone_number) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING user_id",
(fullname, email, password, google_id, "Pakistan", "Karachi", "Male", "0")
)
user_id = c.fetchone()[0]
# Create farm record for the new user with default values
c.execute(
"INSERT INTO farm (user_id, fullname, country, city, zip, hives) VALUES (%s, %s, %s, %s, %s, %s)",
(user_id, "user farm", "Pakistan", "Karachi", "24700", 0)
)
conn.commit()
return "Signup successful"
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def authenticate_google_user(google_id, email):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Get user_id and farm_id by google_id or email
c.execute("""
SELECT u.user_id, f.farm_id
FROM users u
LEFT JOIN farm f ON u.user_id = f.user_id
WHERE u.google_id = %s OR u.email = %s
""", (google_id, email))
result = c.fetchone()
return {"user_id": result[0], "farm_id": result[1]} if result else {"error": "User not found"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def authenticate_user(email, password):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Get user_id and farm_id
c.execute("""
SELECT u.user_id, f.farm_id
FROM users u
LEFT JOIN farm f ON u.user_id = f.user_id
WHERE u.email = %s AND u.password = %s
""", (email, password))
result = c.fetchone()
return {"user_id": result[0], "farm_id": result[1]} if result else {"error": "Invalid credentials"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def save_prediction(user_id, audio_name, result, file_id, hive_id=None, user_predict=None):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
# Add 5 hours to server time
timestamp = (datetime.now() + timedelta(hours=5)).strftime("%Y-%m-%d %H:%M:%S")
c.execute(
"INSERT INTO predictions (user_id, timestamp, audio_name, result, file_id, hive_id, user_predict) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
(user_id, timestamp, audio_name, result, file_id, hive_id, user_predict)
)
conn.commit()
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_history(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT p.timestamp, p.audio_name, p.result, p.hive_id, h.hive_number
FROM predictions p
LEFT JOIN Hive h ON p.hive_id = h.hive_id
WHERE p.user_id = %s
ORDER BY p.timestamp DESC
""", (user_id,))
return [{
"timestamp": row[0],
"audio_name": row[1],
"result": row[2],
"hive_number": row[4] if row[3] else None
} for row in c.fetchall()]
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_user_profile(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT email, fullname, country, city, gender, phone_number FROM users WHERE user_id = %s", (user_id,))
user = c.fetchone()
return {
"email": user[0], "fullname": user[1], "country": user[2],
"city": user[3], "gender": user[4], "phone_number": user[5]
} if user else {"error": "User not found"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_user_profile(user_id, fullname, country, city, gender, phone_number):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
UPDATE users
SET fullname = %s, country = %s, city = %s, gender = %s, phone_number = %s
WHERE user_id = %s
""", (fullname, country, city, gender, phone_number, user_id))
conn.commit()
return {"message": "Profile updated successfully"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_farm_details_from_db(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT farm_id, fullname, country, city, zip, hives FROM farm WHERE user_id = %s", (user_id,))
farm = c.fetchone()
return {
"farm_id": farm[0],
"fullname": farm[1],
"country": farm[2],
"city": farm[3],
"zip": farm[4]
} if farm else None
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_farm_details_in_db(user_id, fullname, country, city, zip_code):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT farm_id FROM farm WHERE user_id = %s", (user_id,))
existing_farm = c.fetchone()
if existing_farm:
c.execute("""
UPDATE farm
SET fullname = %s, country = %s, city = %s, zip = %s
WHERE user_id = %s
""", (fullname, country, city, zip_code, user_id))
else:
c.execute("""
INSERT INTO farm (user_id, fullname, country, city, zip)
VALUES (%s, %s, %s, %s, %s)
""", (user_id, fullname, country, city, zip_code))
conn.commit()
return {"message": "Farm details updated successfully"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_farm_detailss_from_db(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT farm_id FROM farm WHERE user_id = %s", (user_id,))
farm = c.fetchone()
return {"farm_id": farm[0]} if farm else None
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_hives_from_db(farm_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT hive_id, hive_number, health_status FROM Hive WHERE farm_id = %s ORDER BY hive_number", (farm_id,))
return [{"hive_id": row[0], "hive_number": row[1], "health_status": row[2]} for row in c.fetchall()]
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_hive_detail_from_db(hive_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"SELECT hive_number, bee_type, number_of_frames, creation_date, health_status, notes FROM Hive WHERE hive_id = %s",
(hive_id,)
)
row = c.fetchone()
if row:
return {
"hive_number": row[0],
"bee_type": row[1],
"number_of_frames": row[2],
"creation_date": row[3],
"health_status": row[4],
"notes": row[5]
}
return {"error": "Hive not found"}
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_hive_health_in_db(hive_id, health_status):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("UPDATE hive SET health_status = %s WHERE hive_id = %s", (health_status, hive_id))
if c.rowcount == 0:
raise Exception("Hive not found")
conn.commit()
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_hive_in_db(hive_id, number_of_frames, bee_type, notes):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"""
UPDATE Hive
SET number_of_frames = %s, bee_type = %s, notes = %s
WHERE hive_id = %s
RETURNING hive_id
""",
(number_of_frames, bee_type, notes, hive_id)
)
result = c.fetchone()
conn.commit()
return bool(result)
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def add_task_to_db(user_id, task_name, hive_id, description, deadline_date, status):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
INSERT INTO Task (user_id, task_name, hive_id, description, deadline_date, status)
VALUES (%s, %s, %s, %s, %s, %s)
RETURNING task_id
""", (user_id, task_name, None if hive_id is None else hive_id, description, deadline_date, status))
task_id = c.fetchone()[0]
conn.commit()
return task_id
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_user_tasks_from_db(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT task_id, task_name, status, deadline_date
FROM Task
WHERE user_id = %s
ORDER BY status = 'pending' DESC, deadline_date ASC NULLS LAST, task_id
""", (user_id,))
return [{
"task_id": row[0],
"task_name": row[1],
"status": row[2],
"deadline_date": row[3].strftime("%Y-%m-%d") if row[3] else None
} for row in c.fetchall()]
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_task_detail_from_db(task_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT t.task_id, t.user_id, t.task_name, h.hive_number, t.description,
t.deadline_date, t.status
FROM Task t
LEFT JOIN Hive h ON t.hive_id = h.hive_id
WHERE t.task_id = %s
""", (task_id,))
row = c.fetchone()
if row:
return {
"task_id": row[0],
"task_name": row[2],
"hive_number": row[3],
"description": row[4],
"deadline_date": row[5].strftime("%Y-%m-%d") if row[5] else None,
"status": row[6]
}
return None
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def update_task_status_to_completed(task_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
UPDATE Task
SET status = 'completed'
WHERE task_id = %s
""", (task_id,))
if c.rowcount > 0:
conn.commit()
return True
return False
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def delete_task_from_db(task_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("DELETE FROM Task WHERE task_id = %s", (task_id,))
if c.rowcount > 0:
conn.commit()
return True
return False
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_all_notifications(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT notification_id, text, created_at, read_status
FROM notifications
WHERE user_id = %s
ORDER BY read_status = 'unread' DESC, created_at DESC
""", (user_id,))
return [{
"notification_id": row[0],
"text": row[1],
"created_at": row[2].strftime("%Y-%m-%d %H:%M:%S"),
"read_status": row[3]
} for row in c.fetchall()]
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def add_notification(user_id, text):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
INSERT INTO notifications (user_id, text, read_status, created_at)
VALUES (%s, %s, %s, CURRENT_TIMESTAMP + INTERVAL '5 hours')
RETURNING notification_id
""", (user_id, text, 'unread'))
notification_id = c.fetchone()[0]
conn.commit()
return notification_id
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def mark_notification_as_read(notification_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
UPDATE notifications
SET read_status = 'read'
WHERE notification_id = %s
""", (notification_id,))
conn.commit()
finally:
db_pool.putconn(conn)
@retry_on_eof(max_attempts=2, delay=3)
def get_user_cities():
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT user_id, city FROM farm WHERE city IS NOT NULL AND city != ''")
return {row[0]: row[1] for row in c.fetchall()}
finally:
db_pool.putconn(conn) |