function get_results_v1
Flask route handler that retrieves and returns statistical analysis results, generated files, and AI-generated interpretations for a given session ID.
/tf/active/vicechatdev/smartstat/app.py
459 - 483
moderate
Purpose
This endpoint serves as the primary interface for fetching completed analysis results from a statistical analysis service. It retrieves session summaries, generated files (like plots or reports), and AI-generated interpretations of the analysis results. The function handles error cases gracefully, returning appropriate HTTP status codes and error messages when sessions are not found or when exceptions occur.
Source Code
def get_results(session_id):
"""Get analysis results for session"""
try:
summary = analysis_service.get_session_summary(session_id)
if not summary['success']:
return jsonify({'success': False, 'error': 'Session not found'}), 404
# Get interpretation if available
interpretation = analysis_service.generate_interpretation(session_id)
return jsonify({
'success': True,
'results': summary['results'],
'generated_files': summary.get('generated_files', []), # Include generated files
'interpretation': interpretation if interpretation['success'] else None,
'summary': summary['summary']
})
except Exception as e:
logger.error(f"Error getting results: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
session_id |
- | - | positional_or_keyword |
Parameter Details
session_id: Unique identifier (string) for the analysis session. This ID is used to retrieve the corresponding analysis results from the analysis_service. Expected to be a valid session identifier that was previously created during an analysis request. Typically a UUID or similar unique string.
Return Value
Returns a Flask JSON response tuple containing (response_object, status_code). On success (200): returns a dictionary with 'success': True, 'results': analysis results data, 'generated_files': list of file paths/URLs for generated outputs, 'interpretation': AI-generated interpretation object or None if unavailable, and 'summary': session summary data. On session not found (404): returns {'success': False, 'error': 'Session not found'}. On exception (500): returns {'success': False, 'error': error message string}.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# Assuming Flask app setup and analysis_service initialized
from flask import Flask, jsonify
import logging
logger = logging.getLogger(__name__)
app = Flask(__name__)
# analysis_service should be initialized elsewhere
# from services import StatisticalAnalysisService
# analysis_service = StatisticalAnalysisService()
@app.route('/results/<session_id>')
def get_results(session_id):
try:
summary = analysis_service.get_session_summary(session_id)
if not summary['success']:
return jsonify({'success': False, 'error': 'Session not found'}), 404
interpretation = analysis_service.generate_interpretation(session_id)
return jsonify({
'success': True,
'results': summary['results'],
'generated_files': summary.get('generated_files', []),
'interpretation': interpretation if interpretation['success'] else None,
'summary': summary['summary']
})
except Exception as e:
logger.error(f"Error getting results: {str(e)}")
return jsonify({'success': False, 'error': str(e)}), 500
# Client usage:
# GET request to: http://your-server/results/abc-123-session-id
# Response: {"success": true, "results": {...}, "generated_files": [...], "interpretation": {...}, "summary": {...}}
Best Practices
- Ensure the analysis_service is properly initialized before the Flask app starts accepting requests
- The session_id should be validated or sanitized if there are security concerns about injection attacks
- Consider implementing rate limiting on this endpoint to prevent abuse
- The function assumes analysis_service methods return dictionaries with 'success' keys - ensure this contract is maintained
- Error logging is implemented but consider adding more detailed logging for debugging (e.g., log session_id on errors)
- The interpretation generation might be computationally expensive - consider caching results or implementing async processing
- Consider adding authentication/authorization checks to ensure users can only access their own session results
- The 500 error response exposes the raw exception message - in production, consider sanitizing error messages to avoid information leakage
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_results 95.2% similar
-
function get_analysis_files 81.3% similar
-
function get_analysis_session 78.7% similar
-
function get_session_status 78.6% similar
-
function view_session 78.4% similar