CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-superset

A modern, enterprise-ready business intelligence web application

Pending
Overview
Eval results
Files

data-models.mddocs/

Data Models

Core data models representing databases, tables, charts, dashboards, and user activity. Built on SQLAlchemy ORM with comprehensive relationship mapping and business logic methods for managing Superset's data layer.

Capabilities

Core Models

Primary entities that form the foundation of the Superset data architecture.

class Database:
    """
    Database connection configuration and management.
    
    Key Fields:
    - database_name: str, connection display name
    - sqlalchemy_uri: str, database connection string
    - password: str, encrypted database password
    - cache_timeout: int, default query cache duration in seconds
    - expose_in_sqllab: bool, availability in SQL Lab interface
    - allow_run_async: bool, asynchronous query execution support
    - allow_csv_upload: bool, CSV file upload capability
    - allow_ctas: bool, CREATE TABLE AS SELECT support
    - allow_dml: bool, data modification language support
    - force_ctas_schema: str, default schema for CTAS operations
    """
    
    def get_sqla_engine(self):
        """
        Get SQLAlchemy engine instance.
        Creates configured database engine with connection pooling.
        
        Returns:
        SQLAlchemy Engine object with proper configuration
        """
    
    def get_reserved_words(self):
        """
        Get database-specific reserved words.
        
        Returns:
        Set of reserved keywords for SQL generation
        """
    
    def get_quoter(self):
        """
        Get identifier quoter function.
        
        Returns:
        Function for quoting database identifiers
        """
    
    def db_engine_spec(self):
        """
        Get database engine specification.
        
        Returns:
        Engine-specific configuration and capabilities class
        """
    
    def grains(self):
        """
        Get available time grains for grouping.
        
        Returns:
        List of supported time grain definitions
        """
    
    def fetch_metadata(self):
        """
        Fetch and update database metadata.
        Discovers tables, columns, and relationships.
        """
    
    def all_table_names(self):
        """
        List all table names in database.
        
        Returns:
        List of table names across all schemas
        """
    
    def all_view_names(self):
        """
        List all view names in database.
        
        Returns:
        List of view names across all schemas
        """

class Slice:
    """
    Chart/visualization definition and configuration.
    
    Key Fields:
    - slice_name: str, display name for chart
    - datasource_type: str, type of underlying datasource
    - datasource_id: int, identifier of datasource
    - viz_type: str, visualization type identifier
    - params: str, JSON-encoded chart configuration parameters
    - cache_timeout: int, chart-specific cache duration
    
    Relationships:
    - owners: User[], chart owners with edit permissions (many-to-many)
    - dashboards: Dashboard[], associated dashboards (many-to-many)
    """
    
    def datasource(self):
        """
        Get associated datasource object.
        
        Returns:
        Datasource instance (SqlaTable or DruidDatasource)
        """
    
    def viz(self):
        """
        Create visualization object from configuration.
        
        Returns:
        Configured visualization instance for rendering
        """
    
    def get_viz(self):
        """
        Get cached visualization instance.
        
        Returns:
        Cached or newly created visualization object
        """

class Dashboard:
    """
    Dashboard composition, layout, and metadata management.
    
    Key Fields:
    - dashboard_title: str, dashboard display name
    - position_json: str, JSON-encoded layout configuration
    - description: str, dashboard description text
    - css: str, custom CSS styling rules
    - json_metadata: str, additional metadata and configuration
    - published: bool, publication status for sharing
    
    Relationships:
    - slices: Slice[], included charts and visualizations (many-to-many)
    - owners: User[], dashboard owners with edit permissions (many-to-many)
    """
    
    def clone(self):
        """
        Create dashboard copy with new identifier.
        
        Returns:
        New Dashboard instance with copied configuration
        """
    
    def data(self):
        """
        Get complete dashboard data for rendering.
        
        Returns:
        Dictionary with dashboard metadata, slices, and layout
        """
    
    def url(self):
        """
        Get dashboard URL path.
        
        Returns:
        URL string for dashboard access
        """

Supporting Models

Additional models for templates, logging, and system functionality.

class CssTemplate:
    """
    Reusable CSS template definitions.
    
    Key Fields:
    - template_name: str, template identifier name
    - css: str, CSS content and styling rules
    """

class Log:
    """
    Audit logging for user actions and system events.
    
    Key Fields:
    - action: str, type of action performed
    - user_id: int, identifier of user performing action
    - json: str, JSON-encoded action details and context
    - dttm: datetime, timestamp of action occurrence
    - dashboard_id: int, associated dashboard identifier
    - slice_id: int, associated chart identifier  
    - referrer: str, HTTP referrer header value
    - duration_ms: int, action duration in milliseconds
    """

class Url:
    """
    Short URL storage for link sharing.
    
    Key Fields:
    - url: str, original long URL to be shortened
    """

class KeyValue:
    """
    Generic key-value storage for application data.
    
    Key Fields:
    - value: str, stored value content
    """

class FavStar:
    """
    User favorites tracking across object types.
    
    Key Fields:
    - user_id: int, user identifier
    - class_name: str, favorited object type
    - obj_id: int, favorited object identifier
    - dttm: datetime, favorite creation timestamp
    """

class DatasourceAccessRequest:
    """
    Access request workflow for restricted datasources.
    
    Key Fields:
    - datasource_type: str, requested datasource type
    - datasource_id: int, requested datasource identifier
    - changed_by_fk: int, user requesting access
    - created_by_fk: int, user who created request
    """

SQL Lab Models

Specialized models for SQL Lab query execution and management.

class Query:
    """
    SQL Lab query execution tracking and results management.
    
    Key Fields:
    - client_id: str, client session identifier
    - database_id: int, database connection identifier
    - tmp_table_name: str, temporary table name for results
    - user_id: int, user executing query
    - status: str, current execution status
    - tab_name: str, SQL Lab tab display name
    - sql_editor_id: str, editor instance identifier
    - schema: str, target schema for query execution
    - sql: str, original SQL query text
    - select_sql: str, generated SELECT statement
    - executed_sql: str, actually executed SQL with modifications
    - limit: int, requested result row limit
    - limit_used: bool, whether limit was applied
    - select_as_cta: bool, CREATE TABLE AS SELECT flag
    - select_as_cta_used: bool, whether CTAS was used
    - progress: int, execution progress percentage
    - rows: int, number of result rows returned
    - error_message: str, error details if execution failed
    - results_key: str, storage key for large result sets
    - start_time: datetime, query execution start time
    - start_running_time: datetime, actual execution start
    - end_time: datetime, query completion time
    - end_result_backend_time: datetime, backend storage completion
    """

class SavedQuery:
    """
    Saved SQL Lab queries for reuse and sharing.
    
    Key Fields:
    - user_id: int, query owner user identifier
    - db_id: int, associated database identifier
    - label: str, query display name
    - schema: str, target schema context
    - sql: str, saved SQL query text
    - description: str, query description and notes
    """

class TabState:
    """
    SQL Lab tab persistence and state management.
    
    Key Fields:
    - user_id: int, tab owner user identifier
    - label: str, tab display label
    - active: bool, active tab indicator
    - database_id: int, selected database connection
    - schema: str, selected schema context
    - sql: str, current SQL content
    - query_limit: int, default result limit
    - latest_query_id: str, most recent query identifier
    - autorun: bool, automatic execution flag
    - template_params: str, Jinja template parameters
    """

class TableSchema:
    """
    Table metadata cache for performance optimization.
    
    Key Fields:
    - database_id: int, database connection identifier
    - schema: str, schema name
    - table_name: str, table name
    - perm: str, permission identifier string
    - description: str, table description text
    """

Model Relationships

Many-to-Many Associations

# Dashboard-Slice Association
dashboard_slices = Table(
    'dashboard_slices',
    Column('dashboard_id', Integer, ForeignKey('dashboards.id')),
    Column('slice_id', Integer, ForeignKey('slices.id'))
)

# Dashboard-User Ownership
dashboard_user = Table(
    'dashboard_user',
    Column('dashboard_id', Integer, ForeignKey('dashboards.id')),
    Column('user_id', Integer, ForeignKey('ab_user.id'))
)

# Slice-User Ownership  
slice_user = Table(
    'slice_user',
    Column('slice_id', Integer, ForeignKey('slices.id')),
    Column('user_id', Integer, ForeignKey('ab_user.id'))
)

Foreign Key Relationships

# Slice to Datasource (Polymorphic)
slice.datasource_id -> {sqla_table.id, druid_datasource.id}
slice.datasource_type -> {'table', 'druid'}

# Query to Database
query.database_id -> database.id

# Log Entry Associations
log.dashboard_id -> dashboard.id (optional)
log.slice_id -> slice.id (optional)
log.user_id -> user.id

# Access Request Associations
datasource_access_request.datasource_id -> {sqla_table.id, druid_datasource.id}
datasource_access_request.changed_by_fk -> user.id

Data Access Patterns

Query Construction

# Get user's accessible databases
databases = (
    session.query(Database)
    .filter(Database.expose_in_sqllab == True)
    .filter(security_manager.database_access(Database))
    .all()
)

# Get dashboard with slices
dashboard = (
    session.query(Dashboard)
    .options(joinedload(Dashboard.slices))
    .filter(Dashboard.id == dashboard_id)
    .one_or_none()
)

# Get recent user activity
recent_logs = (
    session.query(Log)
    .filter(Log.user_id == user_id)
    .order_by(Log.dttm.desc())
    .limit(50)
    .all()
)

Permissions Integration

# Check slice access
if security_manager.datasource_access(slice.datasource):
    # User can access this slice
    return slice.get_viz()

# Filter accessible datasources
accessible_tables = [
    table for table in tables
    if security_manager.datasource_access(table)
]

Model Extensions

Mixins and Base Classes

class AuditMixin:
    """
    Audit trail mixin for tracking changes.
    Adds created_on, changed_on, created_by, changed_by fields.
    """

class ImportMixin:
    """
    Import/export functionality mixin.
    Provides YAML serialization for configuration management.
    """

class BaseDatasource:
    """
    Base class for all datasource types.
    Defines common interface for tables and Druid datasources.
    """

The data model layer provides a robust foundation for Superset's functionality, with proper abstractions for different datasource types, comprehensive audit trails, and flexible permission integration.

Install with Tessl CLI

npx tessl i tessl/pypi-superset

docs

cli-interface.md

configuration.md

data-models.md

database-connectors.md

index.md

security.md

sql-lab.md

utilities.md

visualization.md

web-application.md

tile.json