import random from django.shortcuts import render from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods from app.models import Alert ALERT_TYPES = [Alert.TYPE_INFO, Alert.TYPE_WARNING, Alert.TYPE_CRITICAL] DESCRIPTIONS = [ "CPU usage above 90%", "Memory leak detected in worker", "Disk space below 10%", "Network timeout on upstream", "Service unreachable: auth", "Database slow query detected", "HTTP 5xx rate spike", "SSL certificate expiring soon", "Replica lag too high", "Queue backlog growing", ] def index(request): alerts = Alert.objects.all() return render(request, "index.html", {"alerts": alerts, "count": alerts.count()}) @csrf_exempt @require_http_methods(["GET", "POST"]) def bench_clear(request): Alert.objects.all().delete() return JsonResponse({"status": "ok", "deleted": True}) @csrf_exempt @require_http_methods(["GET", "POST"]) def bench_populate(request): count = int(request.GET.get("count", 10)) for _ in range(count): Alert.objects.create( alert_type=random.choice(ALERT_TYPES), description=random.choice(DESCRIPTIONS), ) return JsonResponse({"status": "ok", "created": count})