File size: 1,516 Bytes
c58df45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#/modules/database/chat_db.py
from .mongo_db import insert_document, find_documents
from datetime import datetime, timezone
import logging
from .database_init import get_mongodb  # Asegúrate de que esta importación esté al principio del archivo

logger = logging.getLogger(__name__)

COLLECTION_NAME = 'chat_history-v3'

def store_chat_history(username, messages, analysis_type):
    chat_document = {
        'username': username,
        'timestamp': datetime.now(timezone.utc).isoformat(),
        'messages': messages,
        'analysis_type': analysis_type
    }

    result = insert_document(COLLECTION_NAME, chat_document)
    if result:
        logger.info(f"Historial de chat guardado con ID: {result} para el usuario: {username}")
        return True
    return False

def get_chat_history(username, analysis_type, limit=None):
    query = {"username": username}
    if analysis_type:
        query["analysis_type"] = analysis_type

    db = get_mongodb()
    collection = db['chat_history-v3']
    cursor = collection.find(query).sort("timestamp", -1)
    if limit:
        cursor = cursor.limit(limit)

    return list(cursor)

#def get_chat_history(username, analysis_type=None, limit=10):
#    query = {"username": username}
#    if analysis_type:
#        query["analysis_type"] = analysis_type

#    return find_documents(COLLECTION_NAME, query, sort=[("timestamp", -1)], limit=limit)

# Agregar funciones para actualizar y eliminar chat si es necesario