Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +18 -7
modules/database/sql_db.py
CHANGED
@@ -15,13 +15,16 @@ def get_user(username, role=None):
|
|
15 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
16 |
if role:
|
17 |
query += f" AND c.role = '{role}'"
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
return items[0] if items else None
|
20 |
except Exception as e:
|
21 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
22 |
return None
|
23 |
|
24 |
-
|
25 |
#########################################
|
26 |
def get_admin_user(username):
|
27 |
return get_user(username, role='Administrador')
|
@@ -108,6 +111,7 @@ def record_login(username):
|
|
108 |
return None
|
109 |
|
110 |
#########################################
|
|
|
111 |
def record_logout(username, session_id):
|
112 |
"""Registra el cierre de sesi贸n y calcula la duraci贸n"""
|
113 |
try:
|
@@ -122,9 +126,11 @@ def record_logout(username, session_id):
|
|
122 |
{"name": "@username", "value": username}
|
123 |
]
|
124 |
|
|
|
125 |
items = list(container.query_items(
|
126 |
query=query,
|
127 |
-
parameters=params
|
|
|
128 |
))
|
129 |
|
130 |
if not items:
|
@@ -138,8 +144,7 @@ def record_logout(username, session_id):
|
|
138 |
|
139 |
session.update({
|
140 |
"logoutTime": logout_time.isoformat(),
|
141 |
-
"sessionDuration": duration
|
142 |
-
"partitionKey": username
|
143 |
})
|
144 |
|
145 |
container.upsert_item(body=session)
|
@@ -166,11 +171,14 @@ def get_recent_sessions(limit=10):
|
|
166 |
OFFSET 0 LIMIT @limit
|
167 |
"""
|
168 |
|
|
|
169 |
sessions = list(container.query_items(
|
170 |
query=query,
|
171 |
-
parameters=[{"name": "@limit", "value": limit}]
|
|
|
172 |
))
|
173 |
|
|
|
174 |
clean_sessions = []
|
175 |
for session in sessions:
|
176 |
try:
|
@@ -190,6 +198,7 @@ def get_recent_sessions(limit=10):
|
|
190 |
return []
|
191 |
|
192 |
#########################################
|
|
|
193 |
def get_user_total_time(username):
|
194 |
"""Obtiene el tiempo total que un usuario ha pasado en la plataforma"""
|
195 |
try:
|
@@ -205,9 +214,11 @@ def get_user_total_time(username):
|
|
205 |
AND IS_DEFINED(c.sessionDuration)
|
206 |
"""
|
207 |
|
|
|
208 |
result = list(container.query_items(
|
209 |
query=query,
|
210 |
-
parameters=[{"name": "@username", "value": username}]
|
|
|
211 |
))
|
212 |
|
213 |
return result[0] if result and result[0] is not None else 0
|
|
|
15 |
query = f"SELECT * FROM c WHERE c.id = '{username}'"
|
16 |
if role:
|
17 |
query += f" AND c.role = '{role}'"
|
18 |
+
# Agregar enable_cross_partition_query=True
|
19 |
+
items = list(container.query_items(
|
20 |
+
query=query,
|
21 |
+
enable_cross_partition_query=True
|
22 |
+
))
|
23 |
return items[0] if items else None
|
24 |
except Exception as e:
|
25 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
26 |
return None
|
27 |
|
|
|
28 |
#########################################
|
29 |
def get_admin_user(username):
|
30 |
return get_user(username, role='Administrador')
|
|
|
111 |
return None
|
112 |
|
113 |
#########################################
|
114 |
+
|
115 |
def record_logout(username, session_id):
|
116 |
"""Registra el cierre de sesi贸n y calcula la duraci贸n"""
|
117 |
try:
|
|
|
126 |
{"name": "@username", "value": username}
|
127 |
]
|
128 |
|
129 |
+
# Agregar enable_cross_partition_query=True
|
130 |
items = list(container.query_items(
|
131 |
query=query,
|
132 |
+
parameters=params,
|
133 |
+
enable_cross_partition_query=True
|
134 |
))
|
135 |
|
136 |
if not items:
|
|
|
144 |
|
145 |
session.update({
|
146 |
"logoutTime": logout_time.isoformat(),
|
147 |
+
"sessionDuration": duration
|
|
|
148 |
})
|
149 |
|
150 |
container.upsert_item(body=session)
|
|
|
171 |
OFFSET 0 LIMIT @limit
|
172 |
"""
|
173 |
|
174 |
+
# Agregar enable_cross_partition_query=True
|
175 |
sessions = list(container.query_items(
|
176 |
query=query,
|
177 |
+
parameters=[{"name": "@limit", "value": limit}],
|
178 |
+
enable_cross_partition_query=True
|
179 |
))
|
180 |
|
181 |
+
# Validar y limpiar los datos
|
182 |
clean_sessions = []
|
183 |
for session in sessions:
|
184 |
try:
|
|
|
198 |
return []
|
199 |
|
200 |
#########################################
|
201 |
+
|
202 |
def get_user_total_time(username):
|
203 |
"""Obtiene el tiempo total que un usuario ha pasado en la plataforma"""
|
204 |
try:
|
|
|
214 |
AND IS_DEFINED(c.sessionDuration)
|
215 |
"""
|
216 |
|
217 |
+
# Agregar enable_cross_partition_query=True
|
218 |
result = list(container.query_items(
|
219 |
query=query,
|
220 |
+
parameters=[{"name": "@username", "value": username}],
|
221 |
+
enable_cross_partition_query=True
|
222 |
))
|
223 |
|
224 |
return result[0] if result and result[0] is not None else 0
|