from django.contrib import auth
from rest_framework.views import APIView
from django.core.exceptions import ValidationError

from api.utils import TRIGGEREDALERTS, PROCESSPERFORMANCE, SYSTEMPERFORMANCE, success, failed
from api.models import Alert, Mission
import requests

import json

class SystemPerformanceAPI(APIView):
    # 获取系统性能信息
    def get(self, request):
        if not request.user.identity == 'admin':
            return failed(message="仅允许管理员访问")
        processes = []
        for process in PROCESSPERFORMANCE:
            processes.append({
                **process,
                'user': Mission.objects.get(id=process['missionId']).user.username,
            })
        # 注意将触发的告警一并传递
        triggeredAlerts = []
        for alert in TRIGGEREDALERTS:
            triggeredAlerts.append({
                'name': alert.name,
                'level': alert.level,
                'metric': alert.metric,
                'threshold': alert.threshold,
                'handle': alert.handle,
            })
            
        return success(data={**SYSTEMPERFORMANCE, 'processes': processes, 'triggeredAlerts': triggeredAlerts})
