yougandar commited on
Commit
8d2078f
·
verified ·
1 Parent(s): 703612f

Update db.py

Browse files
Files changed (1) hide show
  1. db.py +36 -13
db.py CHANGED
@@ -3,6 +3,7 @@ import psycopg2
3
 
4
  DB_URL = os.getenv("DATABASE_URL") # Set this in Hugging Face Space Secrets
5
 
 
6
  def get_connection():
7
  """
8
  Returns a Postgres connection if DATABASE_URL is set,
@@ -13,39 +14,61 @@ def get_connection():
13
  return psycopg2.connect(DB_URL)
14
 
15
 
16
- def get_alerts():
17
  """
18
- Fetch alerts from DB if available, otherwise return mock data.
 
19
  """
20
  try:
21
  conn = get_connection()
22
  cur = conn.cursor()
23
- cur.execute("SELECT id, message FROM alerts ORDER BY id DESC LIMIT 10;")
24
  rows = cur.fetchall()
25
  cur.close()
26
  conn.close()
27
- return [{"id": r[0], "message": r[1]} for r in rows]
28
 
29
  except Exception as e:
30
- # Fallback: return mock alerts when DB is unavailable
31
- print("⚠️ DB not available, using mock alerts:", e)
32
  return [
33
- {"id": 1, "message": "Mock Alert: No DB connection"},
34
- {"id": 2, "message": "System running in fallback mode"},
 
35
  ]
36
 
37
 
38
- def log_event(event_message: str):
39
  """
40
- Insert event into DB if available, otherwise print it.
41
  """
42
  try:
43
  conn = get_connection()
44
  cur = conn.cursor()
45
- cur.execute("INSERT INTO events (message) VALUES (%s);", (event_message,))
46
  conn.commit()
47
  cur.close()
48
  conn.close()
49
  except Exception as e:
50
- # Fallback: just print to console
51
- print(f"⚠️ Could not log event to DB ({e}). Event:", event_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  DB_URL = os.getenv("DATABASE_URL") # Set this in Hugging Face Space Secrets
5
 
6
+
7
  def get_connection():
8
  """
9
  Returns a Postgres connection if DATABASE_URL is set,
 
14
  return psycopg2.connect(DB_URL)
15
 
16
 
17
+ def get_table_status():
18
  """
19
+ Example: Fetch current table occupancy status.
20
+ Returns mock data if DB not available.
21
  """
22
  try:
23
  conn = get_connection()
24
  cur = conn.cursor()
25
+ cur.execute("SELECT table_id, occupied FROM tables ORDER BY table_id;")
26
  rows = cur.fetchall()
27
  cur.close()
28
  conn.close()
29
+ return [{"table_id": r[0], "occupied": r[1]} for r in rows]
30
 
31
  except Exception as e:
32
+ print("⚠️ DB not available, using mock table status:", e)
33
+ # fallback mock data
34
  return [
35
+ {"table_id": 1, "occupied": True},
36
+ {"table_id": 2, "occupied": False},
37
+ {"table_id": 3, "occupied": True},
38
  ]
39
 
40
 
41
+ def log_customer_visit(customer_name: str):
42
  """
43
+ Log when a customer is detected/recognized.
44
  """
45
  try:
46
  conn = get_connection()
47
  cur = conn.cursor()
48
+ cur.execute("INSERT INTO visits (customer_name) VALUES (%s);", (customer_name,))
49
  conn.commit()
50
  cur.close()
51
  conn.close()
52
  except Exception as e:
53
+ print(f"⚠️ Could not log visit to DB ({e}). Customer:", customer_name)
54
+
55
+
56
+ def get_alerts():
57
+ """
58
+ Fetch alerts from DB if available, otherwise return mock data.
59
+ """
60
+ try:
61
+ conn = get_connection()
62
+ cur = conn.cursor()
63
+ cur.execute("SELECT id, message FROM alerts ORDER BY id DESC LIMIT 10;")
64
+ rows = cur.fetchall()
65
+ cur.close()
66
+ conn.close()
67
+ return [{"id": r[0], "message": r[1]} for r in rows]
68
+
69
+ except Exception as e:
70
+ print("⚠️ DB not available, using mock alerts:", e)
71
+ return [
72
+ {"id": 1, "message": "Mock Alert: No DB connection"},
73
+ {"id": 2, "message": "System running in fallback mode"},
74
+ ]