🔍 Code Extractor

function smartstat_workspace

Maturity: 51

Flask route handler that opens a SmartStat statistical analysis workspace for a specific data section, managing session creation, data restoration, and access control.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
4663 - 4780
Complexity:
complex

Purpose

This function serves as the entry point for the SmartStat analysis workspace. It verifies user access to a data section, creates or retrieves an existing SmartStat analysis session, restores previously saved datasets and analysis history from disk if the session is not in memory, and renders the workspace interface. It supports multi-dataset analysis, information sheets, and maintains analysis history across sessions.

Source Code

def smartstat_workspace(section_id):
    """Open SmartStat analysis workspace for a data section"""
    user_email = get_current_user()
    
    # Verify data section exists and user has access
    data_section = data_section_service.get_data_section(section_id)
    if not data_section or data_section.owner != user_email:
        flash('Data section not found or access denied')
        return redirect(url_for('new_workspace'))
    
    # Find which document contains this data section
    document_id = None
    conn = db_manager._get_connection()
    cursor = conn.execute(
        "SELECT document_id FROM document_sections WHERE section_id = ? AND section_type = 'data'",
        (section_id,)
    )
    row = cursor.fetchone()
    if row:
        document_id = row[0]
    
    # Create or retrieve SmartStat session
    smartstat_session_id = data_section.analysis_session_id
    
    logger.info(f"Opening SmartStat for data section {section_id}")
    logger.info(f"Existing session ID: {smartstat_session_id}")
    
    if not smartstat_session_id:
        # Create new SmartStat session
        smartstat_session_id = smartstat_service.create_session(
            data_section_id=section_id,
            title=data_section.title
        )
        logger.info(f"Created new SmartStat session: {smartstat_session_id}")
        
        # Link session to data section
        data_section.analysis_session_id = smartstat_session_id
        data_section_service.update_data_section(data_section)
        logger.info(f"Updated data section with session ID")
    
    # Get or recover session info
    session_info = smartstat_service.get_session(smartstat_session_id)
    
    if not session_info:
        # Session not in memory - recreate it
        logger.info(f"Session {smartstat_session_id} not in memory, recreating")
        session_info = SmartStatSession(smartstat_session_id, section_id, data_section.title)
        smartstat_service.sessions[smartstat_session_id] = session_info
        
        # Try to restore dataframes and datasets from saved project
        from pathlib import Path
        import json
        project_dirs = list(Path(smartstat_config.GENERATED_SCRIPTS_FOLDER).glob(f"{smartstat_session_id}/project_*"))
        if project_dirs:
            latest_project = max(project_dirs, key=lambda p: p.stat().st_mtime)
            logger.info(f"Found project directory: {latest_project}")
            
            # Load individual dataset CSV files (multi-dataset support)
            dataset_files = list(latest_project.glob("*.csv"))
            logger.info(f"Found {len(dataset_files)} CSV files")
            
            for csv_file in dataset_files:
                dataset_name = csv_file.stem
                if dataset_name == 'data':
                    # Skip the legacy single data.csv for now
                    continue
                
                try:
                    from smartstat_service import smart_read_csv
                    df = smart_read_csv(str(csv_file))
                    session_info.datasets[dataset_name] = df
                    logger.info(f"Loaded dataset '{dataset_name}' with {len(df)} rows, {len(df.columns)} columns")
                except Exception as e:
                    logger.error(f"Error loading dataset CSV {csv_file.name}: {e}")
            
            # Load information sheets from JSON if available
            info_sheets_file = latest_project / "info_sheets.json"
            if info_sheets_file.exists():
                try:
                    with open(info_sheets_file, 'r') as f:
                        info_sheets_data = json.load(f)
                        session_info.info_sheets = info_sheets_data
                        logger.info(f"Loaded {len(info_sheets_data)} information sheets")
                except Exception as e:
                    logger.error(f"Error loading info sheets: {e}")
            
            # If no datasets were loaded, fall back to single data.csv
            if len(session_info.datasets) == 0:
                data_file = latest_project / "data.csv"
                if data_file.exists():
                    try:
                        from smartstat_service import smart_read_csv
                        df = smart_read_csv(str(data_file))
                        session_info.dataframe = df
                        logger.info(f"Fell back to single dataframe with {len(df)} rows")
                    except Exception as e:
                        logger.error(f"Error loading data.csv: {e}")
            else:
                logger.info(f"Successfully loaded {len(session_info.datasets)} datasets")
        
        # Restore history from metadata
        if data_section.metadata and 'analysis_history' in data_section.metadata:
            session_info.analysis_history = data_section.metadata['analysis_history']
            logger.info(f"Restored {len(session_info.analysis_history)} analyses from metadata")
    
    logger.info(f"Session ready: has_data={session_info.dataframe is not None or len(session_info.datasets) > 0}, history_count={len(session_info.analysis_history)}")
    logger.info(f"Session datasets: {len(session_info.datasets)}, info_sheets: {len(session_info.info_sheets)}")
    logger.info(f"All sessions in service: {list(smartstat_service.sessions.keys())}")
    
    return render_template(
        'smartstat_workspace_simple.html',
        session_id=smartstat_session_id,
        data_section_id=section_id,
        document_id=document_id,
        title=data_section.title,
        has_data=session_info.dataframe is not None if session_info else False,
        user_email=user_email
    )

Parameters

Name Type Default Kind
section_id - - positional_or_keyword

Parameter Details

section_id: String identifier for the data section to be analyzed. Must correspond to an existing DataSection in the database that the current user owns. Used to retrieve section metadata, verify access, and link to SmartStat sessions.

Return Value

Returns a Flask Response object. On success, renders 'smartstat_workspace_simple.html' template with context including session_id, data_section_id, document_id, title, has_data flag, and user_email. On failure (section not found or access denied), redirects to 'new_workspace' route with a flash message.

Dependencies

  • flask
  • pathlib
  • json
  • logging
  • pandas
  • models
  • services
  • smartstat_service
  • smartstat_config

Required Imports

from flask import render_template, redirect, url_for, flash
from models import DatabaseManager
from services import DataSectionService
from smartstat_service import SmartStatService, SmartStatSession
import logging
from pathlib import Path
import json

Conditional/Optional Imports

These imports are only needed under specific conditions:

from smartstat_service import smart_read_csv

Condition: only when restoring datasets from CSV files in saved project directories

Required (conditional)
import pandas

Condition: implicitly required by smart_read_csv for dataframe operations

Required (conditional)

Usage Example

# This is a Flask route handler, typically called via HTTP request
# Example: GET /smartstat/abc123-section-id

# Setup (in main Flask app):
from flask import Flask
from auth.azure_auth import require_auth
from services import DataSectionService
from smartstat_service import SmartStatService

app = Flask(__name__)
data_section_service = DataSectionService()
smartstat_service = SmartStatService()

@app.route('/smartstat/<section_id>')
@require_auth
def smartstat_workspace(section_id):
    # Function implementation as shown
    pass

# Access via browser or HTTP client:
# GET http://localhost:5000/smartstat/abc123-section-id
# User must be authenticated and own the data section

# Programmatic redirect example:
from flask import redirect, url_for
return redirect(url_for('smartstat_workspace', section_id='abc123-section-id'))

Best Practices

  • Ensure the require_auth decorator is properly configured before using this route to prevent unauthorized access
  • The function performs database queries directly using db_manager - consider refactoring to use service layer for better separation of concerns
  • Session restoration from disk is performed synchronously and may be slow for large datasets - consider adding loading indicators or async processing
  • The function modifies global state (smartstat_service.sessions) which may cause issues in multi-threaded environments - ensure proper thread safety
  • Error handling for CSV loading is logged but doesn't prevent the workspace from opening - verify that appropriate user feedback is provided
  • The fallback to single 'data.csv' maintains backward compatibility but should be documented for users migrating from older versions
  • Consider implementing session timeout or cleanup mechanisms to prevent memory leaks from abandoned sessions
  • The document_id lookup could fail silently if the section is not linked to a document - ensure this is acceptable behavior
  • Log messages are extensive which is good for debugging but may need adjustment for production environments
  • The function assumes smartstat_config.GENERATED_SCRIPTS_FOLDER exists and is writable - validate this during application startup

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function smartstat_save_to_document 77.0% similar

    Flask route handler that saves SmartStat statistical analysis results back to a data section document, generating a final report with queries, results, and plots.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function smartstat_save_selective 76.4% similar

    Flask route handler that saves selected statistical analysis rounds and their associated plots from a SmartStat session to a data section, with options to replace or append to existing content.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function analysis_workspace 74.9% similar

    Flask route handler that renders an analysis workspace interface for a given session, displaying session summary and generated files in a VS Code-like environment.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function smartstat_upload_data 73.5% similar

    Flask route handler that uploads CSV or Excel data files to a SmartStat analysis session, with support for multi-sheet Excel files and session recovery.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function smartstat_get_history 73.0% 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