🔍 Code Extractor

function smartstat_get_progress

Maturity: 46

Flask API endpoint that retrieves the progress status of a SmartStat analysis job by job_id, returning progress data and completed results if available.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
5384 - 5397
Complexity:
simple

Purpose

This endpoint allows clients to poll for the status of long-running SmartStat statistical analysis jobs. It checks a global progress dictionary for the job status and returns progress information. When a job is completed, it includes the full analysis result with cleaned JSON-safe data (removing NaN/Inf values). This is part of an asynchronous job processing pattern where jobs are initiated separately and clients poll for completion.

Source Code

def smartstat_get_progress(job_id):
    """Get progress of SmartStat analysis job"""
    if job_id not in smartstat_progress:
        return jsonify({'error': 'Job not found'}), 404
    
    progress_data = smartstat_progress[job_id].copy()
    
    # If completed, include full result
    if progress_data['status'] == 'completed' and 'result' in progress_data:
        result = progress_data.pop('result')
        # Clean NaN/Inf values before returning
        progress_data['result'] = clean_for_json(result)
    
    return jsonify(progress_data)

Parameters

Name Type Default Kind
job_id - - positional_or_keyword

Parameter Details

job_id: String identifier for the SmartStat analysis job. This ID is used to look up the job's progress in the smartstat_progress dictionary. Expected to be a UUID or unique string generated when the job was created.

Return Value

Returns a Flask JSON response. On success (200): a dictionary containing progress data with keys like 'status', 'progress', and potentially 'result' if completed. The 'result' field contains cleaned statistical analysis data with NaN/Inf values removed. On error (404): returns {'error': 'Job not found'} if the job_id doesn't exist in the progress tracker.

Dependencies

  • flask
  • json

Required Imports

from flask import jsonify

Usage Example

# Assuming Flask app is set up with authentication and progress tracking
# Client-side usage:
import requests

job_id = 'abc-123-def-456'  # Job ID from initial analysis request
response = requests.get(
    f'http://localhost:5000/api/smartstat/progress/{job_id}',
    headers={'Authorization': 'Bearer YOUR_TOKEN'}
)

if response.status_code == 200:
    progress = response.json()
    print(f"Status: {progress['status']}")
    if progress['status'] == 'completed':
        result = progress['result']
        print(f"Analysis complete: {result}")
    elif progress['status'] == 'running':
        print(f"Progress: {progress.get('progress', 0)}%")
else:
    print(f"Error: {response.json()['error']}")

Best Practices

  • This endpoint should be polled at reasonable intervals (e.g., every 2-5 seconds) to avoid overwhelming the server
  • The smartstat_progress dictionary should be thread-safe if jobs run in background threads
  • Consider implementing job expiration to clean up old completed jobs from the progress dictionary
  • The clean_for_json function is critical for preventing JSON serialization errors with pandas/numpy data
  • Authentication is required via the require_auth decorator - ensure valid credentials are provided
  • Handle 404 responses gracefully in client code as jobs may expire or be invalid
  • Consider adding rate limiting to prevent abuse of the polling endpoint
  • The progress_data.copy() ensures the original progress data isn't modified when popping the result

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function smartstat_run_analysis 78.0% similar

    Flask API endpoint that initiates a SmartStat statistical analysis in a background thread, tracking progress and persisting results to a data section.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function text_chat_get_progress 72.8% similar

    Flask API endpoint that retrieves the progress status of an asynchronous text chat job, particularly for multi-cycle web search operations.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_analysis_progress 70.9% similar

    Flask route handler that retrieves the progress status of a running analysis task and performs cleanup of completed/failed analyses after a timeout period.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function api_index_progress 68.2% similar

    Flask API endpoint that retrieves the current progress status of an asynchronous indexing task by its task ID.

    From: /tf/active/vicechatdev/docchat/app.py
  • function smartstat_get_history 67.4% similar

    Flask API endpoint that retrieves analysis history for a SmartStat session, with automatic session recovery from saved data if the session is not found in memory.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse