Files
ServerKit/backend/app/api/speed_test.py
T
Juan DenisandClaude Fable 5 e7e2de486f feat(monitoring): on-demand server speed test card
Job-backed speed test (Ookla/speedtest-cli when present, pure-Python
fallback) with download/upload/latency surfaced on the Monitoring
overview; last result persisted in settings.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:23:29 -04:00

36 lines
1.3 KiB
Python

"""REST surface for the on-demand server speed test.
Mounted at /api/v1/speedtest (registered in app/__init__.py).
"""
from flask import Blueprint, jsonify
from ..middleware.rbac import admin_required, viewer_required
from ..services.speed_test_service import SPEEDTEST_JOB_KIND, SpeedTestService
speedtest_bp = Blueprint('speedtest', __name__)
@speedtest_bp.route('', methods=['GET'])
@speedtest_bp.route('/', methods=['GET'])
@viewer_required
def get_speed_test():
"""Last stored result + status of any in-flight speed test job."""
try:
return jsonify(SpeedTestService.get_status())
except Exception as exc: # noqa: BLE001 — surface as a clean JSON error
return jsonify({'error': str(exc)}), 500
@speedtest_bp.route('/run', methods=['POST'])
@admin_required
def run_speed_test():
"""Enqueue a one-off speed test job. Rejects if one is already in flight."""
try:
if SpeedTestService.is_running():
return jsonify({'error': 'A speed test is already in progress.'}), 409
from app.jobs.service import JobService
job = JobService.enqueue(SPEEDTEST_JOB_KIND, payload={}, max_attempts=1)
return jsonify({'job_id': job.id}), 202
except Exception as exc: # noqa: BLE001
return jsonify({'error': str(exc)}), 500