🔍 Code Extractor

function get_session_status

Maturity: 46

Flask API endpoint that retrieves the current status and summary of an analysis session by its ID.

File:
/tf/active/vicechatdev/full_smartstat/app.py
Lines:
1487 - 1506
Complexity:
simple

Purpose

This function serves as a REST API endpoint to check the status of an ongoing or completed analysis session. It queries the analysis service for session information and returns the session status along with a summary. This is typically used for polling session progress, checking completion status, or retrieving analysis results. The endpoint handles error cases including session not found (404) and internal server errors (500).

Source Code

def get_session_status(session_id):
    """Get current session status"""
    try:
        summary = analysis_service.get_session_summary(session_id)
        
        if not summary['success']:
            return jsonify({'success': False, 'error': 'Session not found'}), 404
        
        return jsonify({
            'success': True,
            'status': summary['session']['status'],
            'summary': summary['summary']
        })
        
    except Exception as e:
        logger.error(f"Error getting session status: {str(e)}")
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500

Parameters

Name Type Default Kind
session_id - - positional_or_keyword

Parameter Details

session_id: String identifier for the analysis session. This is typically a UUID or unique string that was generated when the session was created. Used to look up the specific session in the analysis service.

Return Value

Returns a Flask JSON response tuple. On success (200): {'success': True, 'status': <session_status_string>, 'summary': <summary_dict>}. On session not found (404): {'success': False, 'error': 'Session not found'}. On internal error (500): {'success': False, 'error': <error_message>}. The status field indicates the current state of the session (e.g., 'pending', 'running', 'completed', 'failed'). The summary field contains detailed analysis results or progress information.

Dependencies

  • flask
  • logging

Required Imports

from flask import jsonify
import logging

Usage Example

# Assuming Flask app and analysis_service are set up
# Client-side usage (e.g., JavaScript fetch or curl):
# GET /api/session/abc-123-def-456/status

# Server-side setup:
from flask import Flask, jsonify
import logging
from services import StatisticalAnalysisService

app = Flask(__name__)
logger = logging.getLogger(__name__)
analysis_service = StatisticalAnalysisService()

@app.route('/api/session/<session_id>/status')
def get_session_status(session_id):
    try:
        summary = analysis_service.get_session_summary(session_id)
        if not summary['success']:
            return jsonify({'success': False, 'error': 'Session not found'}), 404
        return jsonify({
            'success': True,
            'status': summary['session']['status'],
            'summary': summary['summary']
        })
    except Exception as e:
        logger.error(f"Error getting session status: {str(e)}")
        return jsonify({'success': False, 'error': str(e)}), 500

# Client usage example:
import requests
response = requests.get('http://localhost:5000/api/session/abc-123-def-456/status')
data = response.json()
if data['success']:
    print(f"Session status: {data['status']}")
    print(f"Summary: {data['summary']}")

Best Practices

  • This endpoint should be called periodically to poll for session status updates rather than blocking
  • The session_id parameter is extracted from the URL path by Flask's routing mechanism
  • Always check the 'success' field in the response before accessing 'status' or 'summary' fields
  • Handle both 404 (session not found) and 500 (server error) HTTP status codes appropriately in client code
  • The analysis_service.get_session_summary() method must return a dictionary with 'success', 'session', and 'summary' keys
  • Consider implementing rate limiting on this endpoint to prevent excessive polling
  • The logger should be properly configured before using this endpoint to capture error information
  • This function depends on the analysis_service being properly initialized in the module scope

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_analysis_session 85.0% similar

    Flask API endpoint that retrieves details of a specific data analysis session for an authenticated user, ensuring the user has access to the requested session.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_results_v1 78.6% similar

    Flask route handler that retrieves and returns statistical analysis results, generated files, and AI-generated interpretations for a given session ID.

    From: /tf/active/vicechatdev/smartstat/app.py
  • function view_session 77.2% similar

    Flask route handler that retrieves and displays an analysis session by its ID, rendering the session details in a template or redirecting on error.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function get_results 76.8% similar

    Flask route handler that retrieves and returns comprehensive analysis results for a given session, including summary data, generated files, interpretations, and execution tracking information.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function list_sessions 75.6% similar

    Flask route handler that retrieves and displays a list of recent analysis sessions from the analysis service.

    From: /tf/active/vicechatdev/full_smartstat/app.py
← Back to Browse