Scripts & Utilities
Anomstack includes a collection of utility scripts for common administrative tasks, database management, system maintenance, and development workflows. These scripts are located in the scripts/
directory.
๐ Directory Structureโ
scripts/
โโโ README.md # Overview of all scripts
โโโ kill_long_running_tasks.py # Terminate stuck Dagster jobs
โโโ reload_config.py # Hot-reload Dagster configuration
โโโ posthog_example.py # Test PostHog integration
โโโ sqlite/ # SQLite database utilities
โ โโโ create_index.py # Create performance indexes
โ โโโ list_tables.py # List all database tables
โ โโโ list_indexes.py # List all database indexes
โ โโโ qry.py # Execute custom SQL queries
โ โโโ qry.sql # Example/template SQL query
โโโ utils/ # System utilities
โโโ reset_docker.sh # Docker environment reset tool
โโโ cleanup_dagster_storage.sh # Clean up Dagster storage
โโโ README.md # Utilities documentation
๐ง Administrative Utilitiesโ
Configuration Hot-Reloadโ
Reload Dagster configuration without restarting containers.
Script: reload_config.py
cd scripts/
python reload_config.py
Use Cases:
- Applied configuration changes to YAML files
- Updated environment variables
- Added new metric batches
- Modified job schedules
Features:
- Works with running Docker containers
- Reloads code locations via GraphQL API
- Provides feedback on reload status
- Supports custom Dagster host/port configuration
Long-Running Task Managementโ
Terminate or mark as failed any Dagster runs that exceed configured timeouts.
Script: kill_long_running_tasks.py
cd scripts/
python kill_long_running_tasks.py
Use Cases:
- Clean up stuck or hanging jobs
- Recover from unresponsive user code servers
- Free up system resources
- Maintenance and cleanup
Features:
- Uses same timeout as the timeout sensor (
ANOMSTACK_KILL_RUN_AFTER_MINUTES
) - Graceful handling of unreachable code servers
- Detailed logging of termination actions
- Automatically marks stuck runs as failed when termination isn't possible
๐๏ธ Database Utilities (SQLite)โ
For users running Anomstack with SQLite as their database backend.
Performance Optimizationโ
Create indexes on common metric columns for better query performance.
Script: sqlite/create_index.py
cd scripts/sqlite/
python create_index.py
Creates indexes on:
metric_timestamp
- Time-based queriesmetric_batch
- Batch-based filteringmetric_type
- Type-based filtering (metric, score, alert)- Combined indexes for common query patterns
Database Inspectionโ
Explore your SQLite database structure and contents.
List all tables:
cd scripts/sqlite/
python list_tables.py
List all indexes:
cd scripts/sqlite/
python list_indexes.py
Custom SQL Queriesโ
Execute ad-hoc SQL queries against your SQLite database.
Script: sqlite/qry.py
cd scripts/sqlite/
# Edit qry.sql with your custom query first
python qry.py
Example queries:
-- Recent anomalies across all metric batches
SELECT
metric_timestamp,
metric_batch,
metric_name,
metric_value as anomaly_score
FROM metrics
WHERE metric_type = 'alert'
AND metric_value = 1
AND metric_timestamp >= datetime('now', '-7 days')
ORDER BY metric_timestamp DESC;
-- Top metrics by anomaly count
SELECT
metric_batch,
metric_name,
COUNT(*) as anomaly_count
FROM metrics
WHERE metric_type = 'alert'
AND metric_value = 1
GROUP BY metric_batch, metric_name
ORDER BY anomaly_count DESC
LIMIT 10;
๐ณ System Utilitiesโ
Docker Environment Resetโ
Comprehensive Docker reset utility with multiple cleanup levels.
Script: utils/reset_docker.sh
Interactive Mode (Recommended)โ
make reset-interactive
Provides a guided menu with options:
- ๐ Gentle - Rebuild containers (safest)
- ๐งน Medium - Remove containers, keep data
- โข๏ธ Nuclear - Remove all data and containers
- ๐ฅ Full Nuclear - Nuclear + full Docker cleanup
Direct Reset Levelsโ
Gentle Reset (Safest - keeps all data):
make reset-gentle
# or
./scripts/utils/reset_docker.sh gentle
Medium Reset (Removes containers, preserves data):
make reset-medium
# or
./scripts/utils/reset_docker.sh medium
Nuclear Reset (Removes all data):
make reset-nuclear
# or
./scripts/utils/reset_docker.sh nuclear
Full Nuclear Reset (Maximum cleanup):
make reset-full-nuclear
# or
./scripts/utils/reset_docker.sh full-nuclear
Featuresโ
- ๐ Shows current disk usage before cleanup
- โ ๏ธ Multiple safety confirmations for destructive operations
- ๐จ Colorful output with clear visual feedback
- ๐ก๏ธ Graceful fallback handling
- ๐พ Always preserves source code and configuration
Dagster Storage Cleanupโ
Clean up old Dagster storage files and logs.
Script: utils/cleanup_dagster_storage.sh
./scripts/utils/cleanup_dagster_storage.sh
Cleans up:
- Old compute logs
- Artifact storage files
- Event log storage
- Run storage artifacts
๐งช Development Utilitiesโ
API Integration Testingโ
Test external service integrations and credentials.
PostHog Integration Test:
cd scripts/
python posthog_example.py
Validates that your PostHog API credentials work correctly.
๐ Common Usage Patternsโ
Daily Maintenance Workflowโ
# 1. Clean up any stuck jobs
cd scripts/
python kill_long_running_tasks.py
# 2. Optimize database performance (if using SQLite)
cd sqlite/
python create_index.py
# 3. Check system status
cd ../
python list_tables.py # Verify data is being written
Configuration Update Workflowโ
# 1. Make changes to your YAML configs or .env file
vim metrics/my_metric/config.yaml
# 2. Hot-reload the configuration
cd scripts/
python reload_config.py
# 3. Verify changes took effect in Dagster UI
Database Analysis Workflowโ
# 1. Inspect database structure
cd scripts/sqlite/
python list_tables.py
python list_indexes.py
# 2. Run custom analysis queries
vim qry.sql # Write your analysis query
python qry.py
# 3. Create performance indexes if needed
python create_index.py
System Reset Workflowโ
# 1. Interactive guided reset (recommended)
make reset-interactive
# 2. Or direct reset for automation
make reset-gentle # Safe option
make reset-nuclear # Clean slate option