| |
| import logging |
| import base64 |
| from datetime import datetime, timezone |
| from pymongo.errors import PyMongoError |
|
|
| |
| from .mongo_db import ( |
| get_collection, |
| insert_document, |
| find_documents, |
| update_document, |
| delete_document |
| ) |
|
|
| |
| logger = logging.getLogger(__name__) |
| COLLECTION_NAME = 'student_semantic_live_analysis' |
|
|
| |
| |
|
|
| def store_student_semantic_live_result(username, text, analysis_result, lang_code='en'): |
| """ |
| Versi贸n corregida con: |
| - Verificaci贸n correcta de colecci贸n |
| - Manejo de proyecci贸n alternativo |
| - Mejor manejo de errores |
| """ |
| try: |
| |
| collection = get_collection(COLLECTION_NAME) |
| if collection is None: |
| logger.error(f"No se pudo obtener la colecci贸n {COLLECTION_NAME}") |
| return False |
|
|
| |
| if not all([username, text, analysis_result]): |
| logger.error("Par谩metros incompletos para guardar an谩lisis") |
| return False |
|
|
| |
| analysis_document = { |
| 'username': username, |
| 'timestamp': datetime.now(timezone.utc).isoformat(), |
| 'text': text[:50000], |
| 'analysis_type': 'semantic_live', |
| 'language': lang_code, |
| 'key_concepts': analysis_result.get('key_concepts', [])[:50], |
| 'concept_centrality': analysis_result.get('concept_centrality', {}) |
| } |
|
|
| |
| if 'concept_graph' in analysis_result and analysis_result['concept_graph']: |
| try: |
| if isinstance(analysis_result['concept_graph'], bytes): |
| analysis_document['concept_graph'] = base64.b64encode( |
| analysis_result['concept_graph']).decode('utf-8') |
| except Exception as e: |
| logger.error(f"Error procesando gr谩fico: {str(e)}") |
|
|
| |
| try: |
| result = collection.insert_one(analysis_document) |
| if result.inserted_id: |
| logger.info(f"An谩lisis guardado. ID: {result.inserted_id}") |
| return True |
| logger.error("Inserci贸n fallida - Sin ID devuelto") |
| return False |
| except PyMongoError as e: |
| logger.error(f"Error de MongoDB: {str(e)}") |
| return False |
|
|
| except Exception as e: |
| logger.error(f"Error inesperado: {str(e)}", exc_info=True) |
| return False |
|
|
| |
| |
| def get_student_semantic_live_analysis(username, limit=10): |
| """ |
| Versi贸n corregida sin usar projection |
| """ |
| try: |
| collection = get_collection(COLLECTION_NAME) |
| if collection is None: |
| logger.error("No se pudo obtener la colecci贸n") |
| return [] |
|
|
| query = {"username": username, "analysis_type": "semantic_live"} |
| |
| |
| cursor = collection.find(query, { |
| "timestamp": 1, |
| "text": 1, |
| "key_concepts": 1, |
| "concept_graph": 1, |
| "_id": 1 |
| }).sort("timestamp", -1).limit(limit) |
| |
| results = list(cursor) |
| logger.info(f"Recuperados {len(results)} an谩lisis para {username}") |
| return results |
| |
| except PyMongoError as e: |
| logger.error(f"Error de MongoDB: {str(e)}") |
| return [] |
| except Exception as e: |
| logger.error(f"Error inesperado: {str(e)}") |
| return [] |
|
|
| |
| |
| def update_student_semantic_live_analysis(analysis_id, update_data): |
| """Actualiza un an谩lisis existente con manejo de errores""" |
| try: |
| query = {"_id": analysis_id} |
| update = {"$set": update_data} |
| return update_document(COLLECTION_NAME, query, update) > 0 |
| except PyMongoError as e: |
| logger.error(f"Error al actualizar: {str(e)}") |
| return False |
|
|
| |
| |
| def delete_student_semantic_live_analysis(analysis_id): |
| """Elimina un an谩lisis con manejo de errores""" |
| try: |
| query = {"_id": analysis_id} |
| return delete_document(COLLECTION_NAME, query) > 0 |
| except PyMongoError as e: |
| logger.error(f"Error al eliminar: {str(e)}") |
| return False |
|
|
| |
| |
| def get_student_semantic_live_data(username): |
| """ |
| Obtiene todos los an谩lisis sem谩nticos en vivo de un estudiante. |
| Versi贸n corregida que usa la funci贸n _live. |
| """ |
| try: |
| analyses = get_student_semantic_live_analysis(username, limit=None) |
| |
| formatted_analyses = [] |
| for analysis in analyses: |
| formatted_analysis = { |
| 'timestamp': analysis.get('timestamp'), |
| 'text': analysis.get('text', ''), |
| 'key_concepts': analysis.get('key_concepts', []), |
| 'concept_graph': analysis.get('concept_graph') |
| } |
| formatted_analyses.append(formatted_analysis) |
| |
| return { |
| 'username': username, |
| 'entries': formatted_analyses, |
| 'count': len(formatted_analyses), |
| 'status': 'success' |
| } |
| |
| except Exception as e: |
| logger.error(f"Error al obtener datos: {str(e)}") |
| return { |
| 'username': username, |
| 'entries': [], |
| 'count': 0, |
| 'status': 'error', |
| 'error': str(e) |
| } |
|
|
|
|
| |
| |
|
|
| __all__ = [ |
| 'store_student_semantic_live_result', |
| 'get_student_semantic_live_analysis', |
| 'update_student_semantic_live_analysis', |
| 'delete_student_semantic_live_analysis', |
| 'get_student_semantic_live_data' |
| ] |