Create database.py
Browse files- modules/database.py +83 -0
modules/database.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# database.py
|
| 2 |
+
import logging
|
| 3 |
+
from pymongo import MongoClient
|
| 4 |
+
import certifi
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
# Variables globales
|
| 10 |
+
mongo_client = None
|
| 11 |
+
db = None
|
| 12 |
+
analysis_collection = None
|
| 13 |
+
|
| 14 |
+
def initialize_mongodb_connection():
|
| 15 |
+
global mongo_client, db, analysis_collection
|
| 16 |
+
try:
|
| 17 |
+
cosmos_mongodb_connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
| 18 |
+
if not cosmos_mongodb_connection_string:
|
| 19 |
+
logger.error("La variable de entorno MONGODB_CONNECTION_STRING no est谩 configurada")
|
| 20 |
+
return False
|
| 21 |
+
|
| 22 |
+
mongo_client = MongoClient(cosmos_mongodb_connection_string,
|
| 23 |
+
tls=True,
|
| 24 |
+
tlsCAFile=certifi.where(),
|
| 25 |
+
retryWrites=False,
|
| 26 |
+
serverSelectionTimeoutMS=5000,
|
| 27 |
+
connectTimeoutMS=10000,
|
| 28 |
+
socketTimeoutMS=10000)
|
| 29 |
+
|
| 30 |
+
mongo_client.admin.command('ping')
|
| 31 |
+
|
| 32 |
+
db = mongo_client['aideatext_db']
|
| 33 |
+
analysis_collection = db['text_analysis']
|
| 34 |
+
|
| 35 |
+
logger.info("Conexi贸n a Cosmos DB MongoDB API exitosa")
|
| 36 |
+
return True
|
| 37 |
+
except Exception as e:
|
| 38 |
+
logger.error(f"Error al conectar con Cosmos DB MongoDB API: {str(e)}", exc_info=True)
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
def get_student_data(username):
|
| 42 |
+
if analysis_collection is None:
|
| 43 |
+
logger.error("La conexi贸n a MongoDB no est谩 inicializada")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Buscar los datos del estudiante
|
| 48 |
+
cursor = analysis_collection.find({"username": username}).sort("timestamp", -1)
|
| 49 |
+
|
| 50 |
+
# Formatear los datos
|
| 51 |
+
formatted_data = {
|
| 52 |
+
"username": username,
|
| 53 |
+
"entries": [],
|
| 54 |
+
"entries_count": 0,
|
| 55 |
+
"word_count": {},
|
| 56 |
+
"arc_diagrams": [],
|
| 57 |
+
"network_diagrams": []
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
for entry in cursor:
|
| 61 |
+
formatted_data["entries"].append({
|
| 62 |
+
"timestamp": entry["timestamp"].isoformat(),
|
| 63 |
+
"text": entry["text"]
|
| 64 |
+
})
|
| 65 |
+
formatted_data["entries_count"] += 1
|
| 66 |
+
|
| 67 |
+
# Agregar conteo de palabras
|
| 68 |
+
for category, count in entry.get("word_count", {}).items():
|
| 69 |
+
if category in formatted_data["word_count"]:
|
| 70 |
+
formatted_data["word_count"][category] += count
|
| 71 |
+
else:
|
| 72 |
+
formatted_data["word_count"][category] = count
|
| 73 |
+
|
| 74 |
+
# Agregar diagramas
|
| 75 |
+
formatted_data["arc_diagrams"].extend(entry.get("arc_diagrams", []))
|
| 76 |
+
formatted_data["network_diagrams"].append(entry.get("network_diagram", ""))
|
| 77 |
+
|
| 78 |
+
return formatted_data if formatted_data["entries_count"] > 0 else None
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
# Aqu铆 ir铆an las dem谩s funciones relacionadas con la base de datos...
|