Update modules/database/chat_mongo_db.py
Browse files
modules/database/chat_mongo_db.py
CHANGED
@@ -7,15 +7,40 @@ from .database_init import get_mongodb # Aseg煤rate de que esta importaci贸n es
|
|
7 |
logger = logging.getLogger(__name__)
|
8 |
COLLECTION_NAME = 'chat_history-v3'
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def store_chat_history(username, messages, analysis_type='sidebar'):
|
11 |
"""
|
12 |
Guarda el historial del chat
|
13 |
-
Args:
|
14 |
-
username: nombre del usuario
|
15 |
-
messages: lista de mensajes
|
16 |
-
analysis_type: tipo de chat ('sidebar' por defecto)
|
17 |
"""
|
18 |
try:
|
|
|
|
|
|
|
|
|
|
|
19 |
chat_document = {
|
20 |
'username': username,
|
21 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
@@ -23,46 +48,21 @@ def store_chat_history(username, messages, analysis_type='sidebar'):
|
|
23 |
'analysis_type': analysis_type
|
24 |
}
|
25 |
|
26 |
-
result =
|
27 |
-
if result:
|
28 |
-
logger.info(f"Historial de chat guardado con ID: {result} para el usuario: {username}")
|
29 |
return True
|
|
|
|
|
30 |
return False
|
|
|
31 |
except Exception as e:
|
32 |
logger.error(f"Error al guardar historial de chat: {str(e)}")
|
33 |
return False
|
34 |
|
35 |
-
def get_chat_history(username, analysis_type='sidebar', limit=None):
|
36 |
-
"""
|
37 |
-
Recupera el historial del chat
|
38 |
-
Args:
|
39 |
-
username: nombre del usuario
|
40 |
-
analysis_type: tipo de chat ('sidebar' por defecto)
|
41 |
-
limit: l铆mite de mensajes a recuperar
|
42 |
-
Returns:
|
43 |
-
list: Lista de conversaciones
|
44 |
-
"""
|
45 |
-
try:
|
46 |
-
query = {
|
47 |
-
"username": username,
|
48 |
-
"analysis_type": analysis_type
|
49 |
-
}
|
50 |
|
51 |
-
db = get_mongodb()
|
52 |
-
if not db:
|
53 |
-
raise Exception("No se pudo conectar a la base de datos")
|
54 |
-
|
55 |
-
collection = db[COLLECTION_NAME]
|
56 |
|
57 |
-
|
58 |
-
if limit:
|
59 |
-
cursor = cursor.limit(limit)
|
60 |
-
|
61 |
-
return list(cursor)
|
62 |
-
except Exception as e:
|
63 |
-
logger.error(f"Error al recuperar historial de chat: {str(e)}")
|
64 |
-
return []
|
65 |
-
#def get_chat_history(username, analysis_type=None, limit=10):
|
66 |
# query = {"username": username}
|
67 |
# if analysis_type:
|
68 |
# query["analysis_type"] = analysis_type
|
|
|
7 |
logger = logging.getLogger(__name__)
|
8 |
COLLECTION_NAME = 'chat_history-v3'
|
9 |
|
10 |
+
def get_chat_history(username, analysis_type='sidebar', limit=None):
|
11 |
+
"""
|
12 |
+
Recupera el historial del chat
|
13 |
+
"""
|
14 |
+
try:
|
15 |
+
query = {
|
16 |
+
"username": username,
|
17 |
+
"analysis_type": analysis_type
|
18 |
+
}
|
19 |
+
|
20 |
+
collection = get_collection(COLLECTION_NAME) # Usar get_collection en lugar de get_mongodb
|
21 |
+
if collection is None:
|
22 |
+
logger.error("No se pudo obtener la colecci贸n de chat")
|
23 |
+
return []
|
24 |
+
|
25 |
+
cursor = collection.find(query).sort("timestamp", -1)
|
26 |
+
if limit:
|
27 |
+
cursor = cursor.limit(limit)
|
28 |
+
|
29 |
+
return list(cursor)
|
30 |
+
except Exception as e:
|
31 |
+
logger.error(f"Error al recuperar historial de chat: {str(e)}")
|
32 |
+
return []
|
33 |
+
|
34 |
def store_chat_history(username, messages, analysis_type='sidebar'):
|
35 |
"""
|
36 |
Guarda el historial del chat
|
|
|
|
|
|
|
|
|
37 |
"""
|
38 |
try:
|
39 |
+
collection = get_collection(COLLECTION_NAME)
|
40 |
+
if collection is None:
|
41 |
+
logger.error("No se pudo obtener la colecci贸n de chat")
|
42 |
+
return False
|
43 |
+
|
44 |
chat_document = {
|
45 |
'username': username,
|
46 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
|
|
48 |
'analysis_type': analysis_type
|
49 |
}
|
50 |
|
51 |
+
result = collection.insert_one(chat_document)
|
52 |
+
if result.inserted_id:
|
53 |
+
logger.info(f"Historial de chat guardado con ID: {result.inserted_id} para el usuario: {username}")
|
54 |
return True
|
55 |
+
|
56 |
+
logger.error("No se pudo insertar el documento")
|
57 |
return False
|
58 |
+
|
59 |
except Exception as e:
|
60 |
logger.error(f"Error al guardar historial de chat: {str(e)}")
|
61 |
return False
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
#def get_chat_history(username, analysis_type=None, limit=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# query = {"username": username}
|
67 |
# if analysis_type:
|
68 |
# query["analysis_type"] = analysis_type
|