Update database.py
Browse files- database.py +29 -7
database.py
CHANGED
|
@@ -85,7 +85,8 @@ def load_db(user_id=None):
|
|
| 85 |
if conn:
|
| 86 |
try:
|
| 87 |
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
| 88 |
-
|
|
|
|
| 89 |
rows = cur.fetchall()
|
| 90 |
for r in rows:
|
| 91 |
upcoming = r["upcoming_tasks"]
|
|
@@ -118,6 +119,26 @@ def load_db(user_id=None):
|
|
| 118 |
merged_machines = []
|
| 119 |
has_changes = False
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
for om in openremote_machines:
|
| 122 |
m_id = om["id"]
|
| 123 |
if m_id in supabase_lookup:
|
|
@@ -129,7 +150,7 @@ def load_db(user_id=None):
|
|
| 129 |
om["history_logs"] = sm.get("history_logs", [])
|
| 130 |
om["safety_alerts"] = sm.get("safety_alerts", [])
|
| 131 |
else:
|
| 132 |
-
# Found a new machine from OpenRemote
|
| 133 |
om["status"] = "Operational"
|
| 134 |
om["health_score"] = 90
|
| 135 |
om["last_maintenance"] = None
|
|
@@ -142,11 +163,11 @@ def load_db(user_id=None):
|
|
| 142 |
|
| 143 |
# Save the updated list to Supabase to preserve the initial states
|
| 144 |
if has_changes:
|
| 145 |
-
save_db({"machines": merged_machines})
|
| 146 |
|
| 147 |
return {"machines": merged_machines}
|
| 148 |
|
| 149 |
-
def save_db(data):
|
| 150 |
"""Save dynamic modifications (logs, tasks, health score, etc.) back to Supabase PostgreSQL."""
|
| 151 |
conn = get_supabase_connection()
|
| 152 |
if not conn:
|
|
@@ -156,9 +177,9 @@ def save_db(data):
|
|
| 156 |
with conn.cursor() as cur:
|
| 157 |
for m in data.get("machines", []):
|
| 158 |
cur.execute("""
|
| 159 |
-
INSERT INTO machine_states (id, status, health_score, last_maintenance, upcoming_tasks, history_logs, safety_alerts)
|
| 160 |
-
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
| 161 |
-
ON CONFLICT (id)
|
| 162 |
DO UPDATE SET
|
| 163 |
status = EXCLUDED.status,
|
| 164 |
health_score = EXCLUDED.health_score,
|
|
@@ -168,6 +189,7 @@ def save_db(data):
|
|
| 168 |
safety_alerts = EXCLUDED.safety_alerts
|
| 169 |
""", (
|
| 170 |
m["id"],
|
|
|
|
| 171 |
m.get("status", "Operational"),
|
| 172 |
m.get("health_score", 90),
|
| 173 |
m.get("last_maintenance"),
|
|
|
|
| 85 |
if conn:
|
| 86 |
try:
|
| 87 |
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
| 88 |
+
# Query strictly using the active user_id
|
| 89 |
+
cur.execute("SELECT * FROM machine_states WHERE user_id = %s;", (user_id,))
|
| 90 |
rows = cur.fetchall()
|
| 91 |
for r in rows:
|
| 92 |
upcoming = r["upcoming_tasks"]
|
|
|
|
| 119 |
merged_machines = []
|
| 120 |
has_changes = False
|
| 121 |
|
| 122 |
+
# 1. Identify deleted or inaccessible machines for this specific user
|
| 123 |
+
openremote_ids = {om["id"] for om in openremote_machines}
|
| 124 |
+
deleted_ids = [s_id for s_id in supabase_lookup if s_id not in openremote_ids]
|
| 125 |
+
|
| 126 |
+
# 2. Sync deletion (remove machines from Supabase that this user no longer has access to)
|
| 127 |
+
if deleted_ids:
|
| 128 |
+
try:
|
| 129 |
+
conn_del = get_supabase_connection()
|
| 130 |
+
if conn_del:
|
| 131 |
+
with conn_del.cursor() as cur:
|
| 132 |
+
cur.execute(
|
| 133 |
+
"DELETE FROM machine_states WHERE id = ANY(%s) AND user_id = %s;",
|
| 134 |
+
(deleted_ids, user_id)
|
| 135 |
+
)
|
| 136 |
+
conn_del.commit()
|
| 137 |
+
conn_del.close()
|
| 138 |
+
print(f"[Supabase Sync] Deleted obsolete/unauthorized machines for user {user_id}: {deleted_ids}")
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"[Supabase Sync] Failed to delete obsolete machines: {e}")
|
| 141 |
+
|
| 142 |
for om in openremote_machines:
|
| 143 |
m_id = om["id"]
|
| 144 |
if m_id in supabase_lookup:
|
|
|
|
| 150 |
om["history_logs"] = sm.get("history_logs", [])
|
| 151 |
om["safety_alerts"] = sm.get("safety_alerts", [])
|
| 152 |
else:
|
| 153 |
+
# Found a new machine from OpenRemote for this user
|
| 154 |
om["status"] = "Operational"
|
| 155 |
om["health_score"] = 90
|
| 156 |
om["last_maintenance"] = None
|
|
|
|
| 163 |
|
| 164 |
# Save the updated list to Supabase to preserve the initial states
|
| 165 |
if has_changes:
|
| 166 |
+
save_db({"machines": merged_machines}, user_id)
|
| 167 |
|
| 168 |
return {"machines": merged_machines}
|
| 169 |
|
| 170 |
+
def save_db(data, user_id=None):
|
| 171 |
"""Save dynamic modifications (logs, tasks, health score, etc.) back to Supabase PostgreSQL."""
|
| 172 |
conn = get_supabase_connection()
|
| 173 |
if not conn:
|
|
|
|
| 177 |
with conn.cursor() as cur:
|
| 178 |
for m in data.get("machines", []):
|
| 179 |
cur.execute("""
|
| 180 |
+
INSERT INTO machine_states (id, user_id, status, health_score, last_maintenance, upcoming_tasks, history_logs, safety_alerts)
|
| 181 |
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
| 182 |
+
ON CONFLICT (id, user_id)
|
| 183 |
DO UPDATE SET
|
| 184 |
status = EXCLUDED.status,
|
| 185 |
health_score = EXCLUDED.health_score,
|
|
|
|
| 189 |
safety_alerts = EXCLUDED.safety_alerts
|
| 190 |
""", (
|
| 191 |
m["id"],
|
| 192 |
+
user_id,
|
| 193 |
m.get("status", "Operational"),
|
| 194 |
m.get("health_score", 90),
|
| 195 |
m.get("last_maintenance"),
|