🔍 Code Extractor

class DataAnalysisSession_v1

Maturity: 49

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

File:
/tf/active/vicechatdev/vice_ai/models.py
Lines:
1783 - 1841
Complexity:
moderate

Purpose

DataAnalysisSession serves as a comprehensive container for managing the entire lifecycle of a data analysis workflow. It tracks the analysis state, stores conversation messages between users and the system, maintains references to data sources and analysis configurations, records generated plots, and preserves SQL queries and conclusions. This class is designed to be persisted and retrieved, enabling resumable analysis sessions tied to specific document sections.

Source Code

class DataAnalysisSession:
    """Statistical analysis session linked to document sections"""
    session_id: str
    section_id: str
    document_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 = ""
    messages: List[Dict] = None
    generated_plots: List[str] = None
    conclusions: 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()
        if self.messages is None:
            self.messages = []
        if self.generated_plots is None:
            self.generated_plots = []
    
    def add_message(self, role: str, content: str, analysis_data: Dict = None):
        """Add a message to the analysis session"""
        message = {
            'id': str(uuid.uuid4()),
            'role': role,
            'content': content,
            'timestamp': datetime.now().isoformat(),
            'analysis_data': analysis_data or {}
        }
        self.messages.append(message)
        self.updated_at = datetime.now()
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary with proper serialization"""
        return {
            'session_id': self.session_id,
            'section_id': self.section_id,
            'document_id': self.document_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,
            'messages': self.messages,
            'generated_plots': self.generated_plots,
            'conclusions': self.conclusions
        }

Parameters

Name Type Default Kind
bases - -

Parameter Details

session_id: Unique identifier for this analysis session, typically a UUID string

section_id: Identifier linking this session to a specific document section

document_id: Identifier of the parent document containing the section

user_id: Identifier of the user who owns this session, defaults to 'default'

created_at: Timestamp when the session was created, auto-set to current time if None

updated_at: Timestamp of last modification, auto-set to current time if None

title: Human-readable title for the analysis session

description: Detailed description of what this analysis session covers

data_source: DataSource object containing information about the data being analyzed

analysis_config: AnalysisConfiguration object with settings for the analysis

status: Current status of the analysis (AnalysisStatus enum), defaults to PENDING

sql_query: SQL query string used to retrieve or filter data for analysis

messages: List of message dictionaries representing the conversation history, auto-initialized to empty list

generated_plots: List of plot identifiers or paths generated during analysis, auto-initialized to empty list

conclusions: Text summary of analysis findings and conclusions

Return Value

Instantiation returns a DataAnalysisSession object. The to_dict() method returns a dictionary with all attributes serialized to JSON-compatible types (datetime objects converted to ISO format strings, enums to values, nested objects to dictionaries). The add_message() method returns None but modifies the messages list in-place.

Class Interface

Methods

__post_init__(self) -> None

Purpose: Dataclass post-initialization hook that sets default values for created_at, updated_at, messages, and generated_plots if they are None

Returns: None, modifies instance attributes in-place

add_message(self, role: str, content: str, analysis_data: Dict = None) -> None

Purpose: Adds a new message to the session's conversation history with automatic ID generation, timestamp, and updates the session's updated_at timestamp

Parameters:

  • role: The role of the message sender (e.g., 'user', 'assistant', 'system')
  • content: The text content of the message
  • analysis_data: Optional dictionary containing additional analysis-related data or metadata for this message

Returns: None, appends message to self.messages list and updates self.updated_at

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

Purpose: Converts the DataAnalysisSession instance to a dictionary with proper serialization of complex types (datetime to ISO format, enums to values, nested objects to dicts)

Returns: Dictionary containing all session attributes in JSON-serializable format, suitable for database storage or API responses

Attributes

Name Type Description Scope
session_id str Unique identifier for this analysis session instance
section_id str Identifier of the document section this session is linked to instance
document_id str Identifier of the parent document instance
user_id str Identifier of the user who owns this session, defaults to 'default' instance
created_at datetime Timestamp when the session was created, auto-initialized to current time instance
updated_at datetime Timestamp of last modification, auto-updated by add_message() 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 being analyzed instance
analysis_config AnalysisConfiguration Configuration object with settings for the analysis instance
status AnalysisStatus Current status of the analysis (enum), defaults to PENDING instance
sql_query str SQL query string used to retrieve or filter data instance
messages List[Dict] List of message dictionaries representing conversation history, each with id, role, content, timestamp, and analysis_data instance
generated_plots List[str] List of plot identifiers or file paths for visualizations generated during analysis instance
conclusions str Text summary of analysis findings and conclusions instance

Dependencies

  • uuid
  • datetime
  • typing
  • dataclasses

Required Imports

import uuid
from datetime import datetime
from typing import List, Dict, Optional, Any
from dataclasses import dataclass

Usage Example

from datetime import datetime
from dataclasses import dataclass
import uuid

# Assuming DataSource, AnalysisConfiguration, and AnalysisStatus are defined
session = DataAnalysisSession(
    session_id=str(uuid.uuid4()),
    section_id='section_123',
    document_id='doc_456',
    user_id='user_789',
    title='Sales Analysis Q4',
    description='Quarterly sales performance analysis'
)

# Add messages to track conversation
session.add_message(
    role='user',
    content='Show me sales trends',
    analysis_data={'query_type': 'trend_analysis'}
)

session.add_message(
    role='assistant',
    content='Here are the sales trends...',
    analysis_data={'chart_id': 'chart_001'}
)

# Update session state
session.status = AnalysisStatus.COMPLETED
session.sql_query = 'SELECT * FROM sales WHERE quarter = 4'
session.generated_plots.append('plot_001.png')
session.conclusions = 'Sales increased by 15% in Q4'

# Serialize to dictionary for storage
session_dict = session.to_dict()

Best Practices

  • Always provide session_id, section_id, and document_id when instantiating to ensure proper tracking
  • Use add_message() method rather than directly appending to messages list to ensure proper timestamp and ID generation
  • The __post_init__ method automatically initializes created_at, updated_at, messages, and generated_plots if not provided
  • Call to_dict() when persisting the session to ensure proper serialization of datetime and enum objects
  • Update the status field as the analysis progresses through different stages
  • The updated_at timestamp is automatically updated when add_message() is called
  • Ensure DataSource and AnalysisConfiguration objects have to_dict() methods for proper serialization
  • Messages are stored as dictionaries with id, role, content, timestamp, and analysis_data fields
  • The class is designed to be serializable for database storage or API transmission

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class DataAnalysisSession 94.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 StatisticalSession 88.2% similar

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

    From: /tf/active/vicechatdev/vice_ai/smartstat_models.py
  • class StatisticalSession_v1 87.2% 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 DataSection 80.9% similar

    A dataclass representing a dedicated data analysis section that stores analysis results, plots, dataset information, and conclusions separately from text content.

    From: /tf/active/vicechatdev/vice_ai/models.py
  • class SmartStatSession 75.8% 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
← Back to Browse