No description available
pip install fastpluggy-scheduled-query
pip install fastpluggy-scheduled-query==0.1.48
A powerful FastPluggy plugin for scheduling and executing SQL queries on a recurring basis with comprehensive monitoring, result history tracking, and Prometheus metrics integration.
The Scheduled Query plugin allows you to:
- Schedule SQL queries to run automatically using CRON syntax
- Monitor query execution with detailed history tracking
- Export query results as Prometheus metrics
- View results in multiple formats (table, counter, metric, raw)
- Set up query execution intervals with safety controls
- Track execution duration and status
pip install fastpluggy-scheduled-query
cd fastpluggy_plugin/scheduled_query
pip install -e .
The plugin will be automatically discovered by FastPluggy through the entry point system.
The plugin can be configured through environment variables or the FastPluggy configuration system:
class ScheduledQuerySettings(BaseDatabaseSettings):
# Enable notifications when queries run
notification_on_run: bool = False
# Comma-separated list of forbidden SQL keywords
forbidden_keywords: str = "drop,delete,truncate,alter"
# Minimum interval between query executions (seconds)
interval: int = 30
# Enable execution history tracking
enable_history: bool = True
# Maximum number of history records to keep (-1 = unlimited)
limit_history: int = -1
# Enable Prometheus metrics export
prometheus_enabled: bool = True
You can override these settings using environment variables with the prefix SCHEDULED_QUERY_:
export SCHEDULED_QUERY_FORBIDDEN_KEYWORDS="drop,delete,truncate,alter,update"
export SCHEDULED_QUERY_INTERVAL=60
export SCHEDULED_QUERY_ENABLE_HISTORY=true
export SCHEDULED_QUERY_PROMETHEUS_ENABLED=true
0 6 * * * for daily at 6 AM)from fastpluggy.core.database import session_scope
from fastpluggy_plugin.scheduled_query.src.models import ScheduledQuery
from datetime import datetime
with session_scope() as db:
query = ScheduledQuery(
name="Daily User Count",
query="SELECT COUNT(*) as total_users FROM users WHERE is_active = true",
cron_schedule="0 6 * * *", # Daily at 6 AM
enabled=True,
render_type="counter"
)
db.add(query)
db.commit()
Edit the query through the web interface and toggle the "Enabled" checkbox.
Click the Run Now button next to any query in the list view to execute it immediately, bypassing the schedule.
Navigate to Scheduled Query → Dashboard to see:
- Latest query results
- Execution history with pagination
- Performance metrics
- Error messages for failed executions
The dashboard (/scheduled_query/dashboard) provides:
- List of all scheduled queries
- Latest execution results
- Interactive history viewer with filters
- Real-time status updates
Visit /scheduled_query/results-widgets to see queries displayed as widgets with different rendering modes:
All API endpoints require authentication.
GET /api/run-now/{query_id}
Executes a scheduled query immediately and redirects to the queries list.
Parameters:
- query_id (int): ID of the query to execute
Response: Redirect to scheduled queries list (303)
GET /api/execution-history/{query_id}
Retrieves paginated execution history for a specific query.
Parameters:
- query_id (int): ID of the query
- page (int, optional): Page number (default: 1)
- limit (int, optional): Records per page (default: 50, max: 200)
- status_filter (str, optional): Filter by status: "all", "success", "failed", "timeout"
Response:
{
"data": [
{
"id": 1,
"executed_at": "2025-11-05 12:04:30",
"duration_ms": 245,
"result": "[(87, datetime.date(2025, 11, 05))]",
"result_key": ["count", "date"],
"status": "success",
"error_message": null,
"grafana_metrics_snapshot": null
}
],
"pagination": {
"page": 1,
"limit": 50,
"total_count": 150,
"total_pages": 3,
"has_next": true,
"has_prev": false
},
"query_info": {
"id": 1,
"name": "Daily User Activity"
}
}
GET /api/scheduled-queries/{query_id}
Returns detailed information about a specific scheduled query.
Response:
{
"id": 1,
"name": "Daily User Count",
"query": "SELECT COUNT(*) FROM users",
"cron_schedule": "0 6 * * *",
"last_executed": "2025-11-05 06:00:15",
"enabled": true,
"grafana_metric_config": null
}
| Route | Menu Label | Description |
|---|---|---|
/scheduled_query/ |
Scheduled Queries | List and manage all scheduled queries |
/scheduled_query/dashboard |
Dashboard | View query results and execution history |
/scheduled_query/results-widgets |
Results Widgets | Display query results as widgets |
/scheduled_query/edit/{query_id} |
- | Edit form for a specific query |
All routes require authentication.
Stores query definitions and configuration.
class ScheduledQuery(Base):
__tablename__ = 'fp_scheduled_queries'
id: int # Primary key
name: str # Query name
query: str # SQL query text
cron_schedule: str # CRON expression
last_executed: datetime # Last execution timestamp
grafana_metric_config: dict # Prometheus metric configuration
enabled: bool # Enable/disable flag
last_result: str # Latest query result
last_result_key: list # Column names from result
render_type: str # Display format: auto/table/counter/metric/raw
Tracks execution history for each query.
class ScheduledQueryResultHistory(Base):
__tablename__ = 'fp_scheduled_query_results'
id: int # Primary key
scheduled_query_id: int # Foreign key to ScheduledQuery
executed_at: datetime # Execution timestamp
duration_ms: int # Execution duration in milliseconds
result: str # Query result
result_key: list # Column names
status: str # 'success', 'failed', 'timeout'
error_message: str # Error details if failed
grafana_metrics_snapshot: dict # Metrics snapshot
When enabled, the plugin can export query results as Prometheus Gauge metrics.
Add Grafana metric configuration to a scheduled query:
query.grafana_metric_config = {
"metric_name": "user_count_total",
"labels": {
"environment": "production",
"service": "api"
}
}
# Query that counts active users
query = ScheduledQuery(
name="Active Users Metric",
query="SELECT COUNT(*) FROM users WHERE is_active = true",
cron_schedule="*/5 * * * *", # Every 5 minutes
enabled=True,
grafana_metric_config={
"metric_name": "active_users_total",
"labels": {"type": "active"}
}
)
This will create a Prometheus metric accessible at /metrics:
# HELP active_users_total Metric for query SELECT COUNT(*) FROM users WHERE is_active = true
# TYPE active_users_total gauge
active_users_total 594.0
The plugin includes built-in protection against dangerous SQL operations:
All endpoints and routes require authentication through FastPluggy's authentication system.
ScheduledQuery(
name="Daily Active Users",
query="SELECT COUNT(*) as active_users FROM users WHERE last_login >= CURDATE()",
cron_schedule="0 8 * * *", # Daily at 8 AM
enabled=True,
render_type="counter"
)
ScheduledQuery(
name="Order Status Summary",
query="""
SELECT status, COUNT(*) as count, SUM(total_amount) as revenue
FROM orders
WHERE created_at >= CURDATE() - INTERVAL 7 DAY
GROUP BY status
""",
cron_schedule="0 */4 * * *", # Every 4 hours
enabled=True,
render_type="table"
)
ScheduledQuery(
name="Database Connection Pool",
query="SHOW STATUS LIKE 'Threads_connected'",
cron_schedule="*/5 * * * *", # Every 5 minutes
enabled=True,
render_type="metric",
grafana_metric_config={
"metric_name": "mysql_threads_connected",
"labels": {"instance": "primary"}
}
)
Use the included example script to create test queries:
cd /path/to/fastpluggy
python fastpluggy_plugin/scheduled_query/examples/create_test_data.py
Options:
- --cleanup: Remove all test data
- --recreate: Clean up and recreate test data
The plugin depends on the following FastPluggy plugins and libraries:
collect_execute_scheduled_queryThe plugin supports multiple render types for displaying results:
enable_history is set to true in settingsprometheus_enabled is set to truegrafana_metric_config is properly configuredContributions are welcome! Please ensure:
- Code follows the existing style
- New features include tests
- Documentation is updated
- Changes are backward compatible
This plugin is part of the FastPluggy framework.
For issues and questions:
- Check the FastPluggy documentation
- Review the example scripts in the examples/ directory
- Check the logs in /var/log/fastpluggy/ or configured log directory
Version: 0.1.48
Last Updated: November 2025
Maintained by: FastPluggy Team
This plugin does not have a Changelog file or it could not be extracted from the package.
API documentation for this plugin is not available.
Last analysis performed: 2025-11-05 11:54:47
This plugin has a valid entry point:
scheduled_query
= fastpluggy_plugin.scheduled_query.plugin:ScheduledQueryPlugin
Issues were found during analysis, but details are not available.
fastpluggy_plugin.scheduled_query.plugin:ScheduledQueryPlugin