User Tools

Site Tools


manage_database

This is an old revision of the document!


Database Manager (SQL)

* More Developers Docs: The Database Manager (SQL) provides a robust, extensible interface for working with an SQLite database. With automated schema initialization and methods to save metrics, it is designed for efficient management of structured data.

Overview

The `DatabaseManagerSQL` class enables:

  • Seamless Database Initialization:

Automatically creates tables if they do not exist.

  • Flexible Metric Storage:

Inserts key-value metrics into the database for tracking and analysis.

  • Extensibility:

Supports modification by extending its methods for additional database operations.

  • Efficient Database Connection Management:

Manages initialization and connection closing securely, reducing resource leaks.

Key Features

  • Dynamic Schema Setup:

Automatically creates the required tables for metric storage.

  • Data Insertion:

Saves multiple metrics as key-value pairs with timestamps into the database.

  • Simplified API:

Provides clean and intuitive methods to interact with the database.

  • Error Handling:

Uses secure transactions to ensure data consistency and prevent partial updates.

System Scope

The system is designed for:

1. **Metric Storage**: Captures and stores system metrics with timestamps.
2. **PERF Tracking**: Collects and records performance evaluations for machine learning workflows.
3. **Advanced Extensibility**: Allows adding additional tables or queries for analytical purposes.
4. **Scalability**: Supports expanding to other database backends such as PostgreSQL or MySQL in the future.

Schema Design

The default schema managed by the Database Manager creates the following table:

Table Name Column Name Data Type Description
—————-————————————————————————————
`metrics` `id` INTEGER Unique ID for each metric (Auto-increment).
`metric_name` TEXT Name or key associated with the metric.
`metric_value` REAL The value of the recorded metric.
`timestamp` DATETIME The time when the metric was recorded.

Class Design

The DatabaseManagerSQL is a Python-based utility class for interacting with SQLite databases. Below is the design and implementation breakdown.

Initialization

The `init` method initializes the SQLite database connection and ensures the schema is pre-created.

```python
def __init__(self, db_path):
    """
    Initialize the database connection and set up the schema if it does not exist.

    :param db_path: Path to the SQLite database file.
    """
    self.connection = sqlite3.connect(db_path)
    self.cursor = self.connection.cursor()
    self._initialize_schema()
```

Schema Initialization

Automatically creates required tables via the `_initialize_schema` method.

```python
def _initialize_schema(self):
    """
    Create the required tables if they do not already exist.
    """
    self.cursor.execute('''
        CREATE TABLE IF NOT EXISTS metrics (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            metric_name TEXT NOT NULL,
            metric_value REAL NOT NULL,
            timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
        );
    ''')
    self.connection.commit()
```

Customizable Schema Changes: - Modify the `CREATE TABLE` SQL if additional tables or fields are required.

Metric Saving

The `save_metrics` method inserts metrics as `key-value` pairs into the `metrics` table. This allows flexible recording and easy retrieval of data points.

```python
def save_metrics(self, metrics):
    """
    Save metrics into the database.

    Args:
        metrics (dict): A dictionary of metric names and their corresponding values.
    """
    for metric_name, metric_value in metrics.items():
        self.cursor.execute('''
            INSERT INTO metrics (metric_name, metric_value)
            VALUES (?, ?);
        ''', (metric_name, metric_value))
    self.connection.commit()
```

Connection Closing

Ensures proper handling of resources by closing the database connection explicitly.

```python
def close(self):
    """
    Close the database connection.
    """
    if self.connection:
        self.connection.close()
```

Usage Examples

Below are advanced examples and scenarios for using the Database Manager.

Example 1: Initializing the Database Manager

Use the DatabaseManagerSQL to connect to a database and create required tables.

```python
from manage_database import DatabaseManagerSQL

# Initialize the database manager
db_manager = DatabaseManagerSQL(db_path="./my_database.db")
print("Database initialized successfully.")
```

Example 2: Saving Metrics into the Database

Insert key-value pairs of metrics into the `metrics` table programmatically.

```python
# Define metrics to save
metrics = {
    "accuracy": 0.95,
    "loss": 0.05,
    "precision": 0.92
}

# Save metrics into the database
db_manager.save_metrics(metrics)
print("Metrics saved successfully.")
```

Example 3: Closing the Connection

Always ensure the connection is closed after completing operations.

```python
# Close the database manager connection
db_manager.close()
print("Database connection closed.")
```

Example 4: Advanced Metric Recording Automation

Batch-save metrics in real time using the logging system.

```python
import time
import random

# Generate and insert random metrics over time
for i in range(5):
    metrics = {
        "metric_name": f"metric_{i}",
        "metric_value": random.uniform(0, 1)
    }
    db_manager.save_metrics(metrics)
    time.sleep(2)
```

Example 5: Extending Functionality

Add a query method to retrieve saved metrics:

```python
def fetch_all_metrics(self):
    """
    Retrieve all records from the metrics table.
    """
    self.cursor.execute('SELECT * FROM metrics;')
    return self.cursor.fetchall()
```

Fetch results after saving metrics:

```python
# Fetch and print all metrics
all_metrics = db_manager.fetch_all_metrics()
print(all_metrics)
```

Advanced Functionalities

1. Custom Metric Analysis:

 Extend the schema and add queries for computing averages, maxes, or trends.

2. Batch Framework Integration:

 Integrate as the backend for AI monitoring or feedback-driven systems.

3. Error Resilience:

 Extend the `save_metrics` method with exception handling for edge cases.

Best Practices

1. Ensure Connection Lifecycle:

 Always close the database connection using the `close()` method at the end of workflow execution.
 

2. Validate Metric Keys:

 Ensure all metric names follow a consistent naming scheme to avoid ambiguity.

3. Avoid Large Batches:

 Limit batch-inserts to avoid database locks or increased latency.
manage_database.1745624455.txt.gz · Last modified: 2025/04/25 23:40 by 127.0.0.1