Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Add metrics API endpoints and auto-start collection

Juan Denis189dead60379
diff --git a/backend/app/__init__.py b/backend/app/__init__.py
index 14868c71..12414109 100644
--- a/backend/app/__init__.py
+++ b/backend/app/__init__.py
@@ -155,6 +155,10 @@ def create_app(config_name=None):
from app.api.admin import admin_bp
app.register_blueprint(admin_bp, url_prefix='/api/v1/admin')
+ # Register blueprints - Historical Metrics
+ from app.api.metrics import metrics_bp
+ app.register_blueprint(metrics_bp, url_prefix='/api/v1/metrics')
+
# Create database tables
with app.app_context():
db.create_all()
@@ -164,6 +168,11 @@ def create_app(config_name=None):
SettingsService.initialize_defaults()
SettingsService.migrate_legacy_roles()
+ # Start metrics history collection in background
+ from app.services.metrics_history_service import MetricsHistoryService
+ if not MetricsHistoryService.is_running():
+ MetricsHistoryService.start_collection(app)
+
# Serve frontend for root path
@app.route('/')
def serve_index():
diff --git a/backend/app/api/metrics.py b/backend/app/api/metrics.py
new file mode 100644
index 00000000..9a703c24
--- /dev/null
+++ b/backend/app/api/metrics.py
@@ -0,0 +1,96 @@
+"""API endpoints for historical metrics data."""
+
+from flask import Blueprint, request, jsonify
+from flask_jwt_extended import jwt_required
+
+from app.services.metrics_history_service import MetricsHistoryService
+
+metrics_bp = Blueprint('metrics', __name__)
+
+
+@metrics_bp.route('/history', methods=['GET'])
+@jwt_required()
+def get_metrics_history():
+ """Get historical metrics data.
+
+ Query params:
+ period: 1h, 6h, 24h, 7d, 30d (default: 1h)
+
+ Returns:
+ JSON with time-series data points and summary statistics
+ """
+ period = request.args.get('period', '1h')
+
+ # Validate period
+ valid_periods = ['1h', '6h', '24h', '7d', '30d']
+ if period not in valid_periods:
+ return jsonify({
+ 'error': f"Invalid period. Must be one of: {', '.join(valid_periods)}"
+ }), 400
+
+ data = MetricsHistoryService.get_history(period)
+ return jsonify(data), 200
+
+
+@metrics_bp.route('/stats', methods=['GET'])
+@jwt_required()
+def get_metrics_stats():
+ """Get statistics about stored metrics data.
+
+ Returns:
+ JSON with record counts and date ranges per aggregation level
+ """
+ stats = MetricsHistoryService.get_stats()
+ return jsonify({
+ 'stats': stats,
+ 'collection_running': MetricsHistoryService.is_running()
+ }), 200
+
+
+@metrics_bp.route('/collection/start', methods=['POST'])
+@jwt_required()
+def start_collection():
+ """Start metrics collection (admin only).
+
+ Returns:
+ JSON with success status
+ """
+ from flask import current_app
+
+ if MetricsHistoryService.is_running():
+ return jsonify({'message': 'Collection already running'}), 200
+
+ MetricsHistoryService.start_collection(current_app._get_current_object())
+ return jsonify({'message': 'Collection started'}), 200
+
+
+@metrics_bp.route('/collection/stop', methods=['POST'])
+@jwt_required()
+def stop_collection():
+ """Stop metrics collection (admin only).
+
+ Returns:
+ JSON with success status
+ """
+ MetricsHistoryService.stop_collection()
+ return jsonify({'message': 'Collection stopped'}), 200
+
+
+@metrics_bp.route('/aggregate', methods=['POST'])
+@jwt_required()
+def trigger_aggregation():
+ """Manually trigger data aggregation (admin only).
+
+ Returns:
+ JSON with aggregation results
+ """
+ hourly = MetricsHistoryService.aggregate_hourly()
+ daily = MetricsHistoryService.aggregate_daily()
+ cleanup = MetricsHistoryService.cleanup_old_data()
+
+ return jsonify({
+ 'message': 'Aggregation complete',
+ 'hourly_created': hourly,
+ 'daily_created': daily,
+ 'cleaned_up': cleanup
+ }), 200