🔍 Code Extractor

class StatisticalSession

Maturity: 46

A dataclass representing a statistical analysis session that tracks metadata, configuration, and status of data analysis operations.

File:
/tf/active/vicechatdev/vice_ai/smartstat_models.py
Lines:
113 - 147
Complexity:
moderate

Purpose

StatisticalSession serves as a container for managing the complete lifecycle of a statistical analysis workflow. It stores session identification, user information, timestamps, data source configuration, analysis parameters, execution status, and SQL queries. This class is designed to persist and track analysis sessions, enabling users to save, load, and resume statistical analysis work. It provides serialization capabilities for storage and retrieval from databases or file systems.

Source Code

class StatisticalSession:
    """Statistical analysis session"""
    session_id: str
    user_id: str = "default"
    created_at: datetime = None
    updated_at: datetime = None
    title: str = ""
    description: str = ""
    data_source: DataSource = None
    analysis_config: AnalysisConfiguration = None
    status: AnalysisStatus = AnalysisStatus.PENDING
    sql_query: str = ""
    
    def __post_init__(self):
        if self.created_at is None:
            self.created_at = datetime.now()
        if self.updated_at is None:
            self.updated_at = datetime.now()
        # Don't create default objects here to avoid initialization issues
        # They will be set when data is actually loaded
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary with proper serialization"""
        return {
            'session_id': self.session_id,
            'user_id': self.user_id,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'title': self.title,
            'description': self.description,
            'data_source': self.data_source.to_dict() if self.data_source else None,
            'analysis_config': self.analysis_config.to_dict() if self.analysis_config else None,
            'status': self.status.value,
            'sql_query': self.sql_query
        }

Parameters

Name Type Default Kind
bases - -

Parameter Details

session_id: Unique identifier for the statistical analysis session. Required field that distinguishes this session from others.

user_id: Identifier for the user who owns this session. Defaults to 'default' if not specified.

created_at: Timestamp when the session was created. Automatically set to current datetime in __post_init__ if not provided.

updated_at: Timestamp when the session was last modified. Automatically set to current datetime in __post_init__ if not provided.

title: Human-readable title for the session. Defaults to empty string.

description: Detailed description of the analysis session's purpose or contents. Defaults to empty string.

data_source: DataSource object containing information about where the data comes from. Can be None initially.

analysis_config: AnalysisConfiguration object containing parameters and settings for the statistical analysis. Can be None initially.

status: Current status of the analysis session using AnalysisStatus enum. Defaults to AnalysisStatus.PENDING.

sql_query: SQL query string associated with this analysis session. Defaults to empty string.

Return Value

Instantiation returns a StatisticalSession object with all specified attributes. The to_dict() method returns a dictionary with string keys and serialized values suitable for JSON storage, with datetime objects converted to ISO format strings and nested objects converted to dictionaries.

Class Interface

Methods

__post_init__(self) -> None

Purpose: Post-initialization hook that sets default values for created_at and updated_at timestamps if they were not provided during instantiation

Returns: None - modifies instance attributes in place

to_dict(self) -> Dict[str, Any]

Purpose: Serializes the StatisticalSession object to a dictionary with proper handling of datetime objects and nested objects for storage or JSON serialization

Returns: Dictionary containing all session attributes with datetime objects converted to ISO format strings, nested objects converted to dictionaries via their to_dict() methods, and enum values converted to their string values

Attributes

Name Type Description Scope
session_id str Unique identifier for this statistical analysis session instance
user_id str Identifier for the user who owns this session, defaults to 'default' instance
created_at datetime Timestamp when the session was created, automatically set if None instance
updated_at datetime Timestamp when the session was last updated, automatically set if None instance
title str Human-readable title for the analysis session instance
description str Detailed description of the analysis session's purpose instance
data_source DataSource DataSource object containing information about the data origin, can be None instance
analysis_config AnalysisConfiguration Configuration object containing analysis parameters and settings, can be None instance
status AnalysisStatus Current status of the analysis session using AnalysisStatus enum, defaults to PENDING instance
sql_query str SQL query string associated with this analysis session instance

Dependencies

  • datetime
  • typing
  • dataclasses

Required Imports

from datetime import datetime
from typing import Dict, Any
from dataclasses import dataclass

Usage Example

from datetime import datetime
from dataclasses import dataclass

# Assuming DataSource, AnalysisConfiguration, and AnalysisStatus are defined
session = StatisticalSession(
    session_id='sess_12345',
    user_id='user_001',
    title='Sales Analysis Q4',
    description='Quarterly sales performance analysis',
    status=AnalysisStatus.PENDING,
    sql_query='SELECT * FROM sales WHERE quarter = 4'
)

# Access attributes
print(session.session_id)  # 'sess_12345'
print(session.created_at)  # datetime object

# Convert to dictionary for serialization
session_dict = session.to_dict()
print(session_dict['created_at'])  # ISO format string

# Update session
session.status = AnalysisStatus.COMPLETED
session.updated_at = datetime.now()

# Serialize for storage
import json
json_data = json.dumps(session.to_dict())

Best Practices

  • Always provide a unique session_id when creating a new session to avoid conflicts
  • The created_at and updated_at timestamps are automatically set if not provided, but should be manually updated when the session state changes
  • Set data_source and analysis_config objects after instantiation when actual data is loaded to avoid initialization issues
  • Use the to_dict() method for serialization before storing in databases or JSON files
  • Update the status attribute as the analysis progresses through different stages
  • The __post_init__ method intentionally does not create default DataSource or AnalysisConfiguration objects to avoid initialization issues - set these explicitly when needed
  • When deserializing from storage, convert ISO format strings back to datetime objects for created_at and updated_at
  • The sql_query field should be populated when the analysis involves database queries

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class StatisticalSession_v1 95.6% similar

    A dataclass representing a statistical analysis session that tracks user data analysis workflows, including data sources, configurations, and execution status.

    From: /tf/active/vicechatdev/smartstat/models.py
  • class DataAnalysisSession_v1 88.2% similar

    A dataclass representing a statistical analysis session that is linked to specific document sections, managing analysis state, messages, plots, and configuration.

    From: /tf/active/vicechatdev/vice_ai/models.py
  • class DataAnalysisSession 82.6% similar

    A dataclass representing a data analysis session that is linked to a specific text section within a document, managing conversation messages, analysis results, plots, and configuration.

    From: /tf/active/vicechatdev/vice_ai/models.py
  • class SmartStatSession 80.7% similar

    A session management class that encapsulates a SmartStat statistical analysis session, tracking data, analysis history, plots, and reports for a specific data section.

    From: /tf/active/vicechatdev/vice_ai/smartstat_service.py
  • class AnalysisStep 68.8% similar

    A dataclass representing an individual step in an analysis process, tracking execution details, scripts, outputs, and metadata for each step in a data analysis workflow.

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