or run

npx @tessl/cli init
Log in

Version

Files

docs

chart-components.mdchart-plugins.mddata-transformation.mdform-data.mdindex.mdquery-building.md
tile.json

task.mdevals/scenario-1/

Query Manager Service

A Python service for managing SQL query execution and history tracking in a data analytics platform. The service should support both synchronous and asynchronous query execution, maintain query history, and persist editor state.

Capabilities

Execute Synchronous Queries

Execute SQL queries synchronously with timeout protection and row limiting.

  • Executing "SELECT * FROM users LIMIT 5" with database connection and 30s timeout returns query results with execution metadata @test
  • Query execution captures start time, end time, and query status (success/failed/stopped) @test
  • Queries exceeding the timeout threshold are stopped and marked with status "stopped" @test

Execute Asynchronous Queries

Support long-running queries with async execution and progress tracking.

  • Submitting an async query returns a query ID immediately and executes in the background @test
  • Query progress can be tracked from 0 to 100 percent during execution @test
  • Long-running queries can be cancelled using their query ID @test

Manage Query History

Store and retrieve executed queries with full execution metadata.

  • Successfully executed queries are saved to query history with SQL text, status, and timestamps @test
  • Query history can be filtered by user ID to show only queries executed by that user @test
  • Query history entries include database name, schema, and executed SQL text @test

Persist Editor State

Save and restore editor tab state for session continuity.

  • Editor state with SQL text, database selection, and schema can be saved to a tab state @test
  • Tab state can be retrieved by tab ID to restore editor session @test
  • Multiple tabs can be persisted for the same user with unique tab IDs @test

Implementation

@generates

API

from typing import Dict, Any, List, Optional
from dataclasses import dataclass
from enum import Enum

class QueryStatus(Enum):
    """Query execution status"""
    SUCCESS = "success"
    FAILED = "failed"
    STOPPED = "stopped"
    RUNNING = "running"
    PENDING = "pending"

@dataclass
class QueryResult:
    """Query execution result with metadata"""
    query_id: int
    sql: str
    status: QueryStatus
    start_time: float
    end_time: Optional[float]
    rows: Optional[List[Dict[str, Any]]]
    error_message: Optional[str]
    database_name: str
    schema: Optional[str]

@dataclass
class TabState:
    """Editor tab state for persistence"""
    tab_id: str
    user_id: int
    sql: str
    database_id: int
    schema: Optional[str]

class QueryManager:
    """Manages SQL query execution and history"""

    def __init__(self, db_connection: Any):
        """
        Initialize query manager with database connection.

        Args:
            db_connection: Database connection object
        """
        pass

    def execute_sync_query(
        self,
        sql: str,
        database_name: str,
        schema: Optional[str] = None,
        timeout: int = 30,
        limit: int = 1000,
        user_id: Optional[int] = None
    ) -> QueryResult:
        """
        Execute a SQL query synchronously with timeout protection.

        Args:
            sql: SQL query text to execute
            database_name: Name of the database to query
            schema: Optional schema name
            timeout: Query timeout in seconds
            limit: Maximum number of rows to return
            user_id: Optional user ID for query history

        Returns:
            QueryResult with execution metadata and results
        """
        pass

    def execute_async_query(
        self,
        sql: str,
        database_name: str,
        schema: Optional[str] = None,
        user_id: Optional[int] = None
    ) -> int:
        """
        Submit a query for asynchronous execution.

        Args:
            sql: SQL query text to execute
            database_name: Name of the database to query
            schema: Optional schema name
            user_id: Optional user ID for query history

        Returns:
            Query ID for tracking execution
        """
        pass

    def get_query_progress(self, query_id: int) -> int:
        """
        Get execution progress for an async query.

        Args:
            query_id: Query identifier

        Returns:
            Progress percentage (0-100)
        """
        pass

    def cancel_query(self, query_id: int) -> bool:
        """
        Cancel a running query.

        Args:
            query_id: Query identifier

        Returns:
            True if query was cancelled successfully
        """
        pass

    def get_query_history(
        self,
        user_id: Optional[int] = None,
        limit: int = 100
    ) -> List[QueryResult]:
        """
        Retrieve query execution history.

        Args:
            user_id: Optional filter by user ID
            limit: Maximum number of history entries to return

        Returns:
            List of query results from history
        """
        pass

    def save_tab_state(self, tab_state: TabState) -> bool:
        """
        Save editor tab state for session persistence.

        Args:
            tab_state: Tab state to persist

        Returns:
            True if saved successfully
        """
        pass

    def get_tab_state(self, tab_id: str, user_id: int) -> Optional[TabState]:
        """
        Retrieve saved tab state.

        Args:
            tab_id: Tab identifier
            user_id: User identifier

        Returns:
            TabState if found, None otherwise
        """
        pass

Dependencies { .dependencies }

apache-superset { .dependency }

Provides SQL Lab query execution infrastructure, including query models, execution logic, and state management.

@satisfied-by