from rest_framework.response import Response
from rest_framework import status
from django.apps import apps
import os

SCHEDULER_BASE_URL = "http://localhost:5000"

# 动态生成上传文件夹 
BASE_FILE_PATH = os.path.join(apps.get_app_config('api').path, 'uploads')


OK = 0
FAILED = 1
FILE_ALREADY_EXIST = 101
FILE_FAILED_CREATE_DIR = 102
UNKNOWN_CONTENT = 998
UNKNOWN_ERROR = 999

def failed(message="访问失败", data=None, code=400):
    if code == 400:
        mStatus = status.HTTP_400_BAD_REQUEST
    elif code == 401:
        mStatus = status.HTTP_401_UNAUTHORIZED
    elif code == 403:
        mStatus = status.HTTP_403_FORBIDDEN
    else:
        raise ValueError("不支持的HTTP状态")
    return Response({
        'status': 'failed',
        'message': message,
        'data': data,    
    }, status=mStatus)

def success(message="访问成功", data=None, code=200):
    if code == 200:
        mStatus = status.HTTP_200_OK
    elif code == 201:
        mStatus = status.HTTP_201_CREATED
    elif code == 202:
        mStatus = status.HTTP_202_ACCEPTED
    else:
        raise ValueError("不支持的HTTP状态")
    return Response({
        'status': 'success',
        'message': message,
        'data': data,
    }, status=mStatus)

# 用来控制允许保存多少用户上传的文件和结果文件，默认5GB
MAX_STORAGE = 5

# 两个变量用来存储系统性能参数和进程性能参数
#: 存储系统性能信息
#: 可用参数包括: cpu, mem_total, mem_used, disk_used(单位是B)
SYSTEMPERFORMANCE = {
    'cpu': 0,
    'mem_total': 0,
    'mem_used': 0,
    'disk_used': 0,
    'disk_total': MAX_STORAGE * 1024**3,
}

# 存放正在运行的进程及参数信息
PROCESSPERFORMANCE = []

# 存放触发的告警信息
TRIGGEREDALERTS = []