package com.microservices.mon.k8s.domain;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.mon.k8s.domain.vo.PrometheusResVo;
import com.microservices.mon.k8s.domain.vo.PrometheusResultVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;


@Data
@ApiModel("集群状态")
public class K8sClusterStatus {
    @ApiModelProperty(value = "集群节点数量")
    private Integer nodeCount;

    @ApiModelProperty(value = "不可用节点数量")
    private Integer unavailableNodeCount;

    @ApiModelProperty(value = "容器使用率")
    private Float podUsage;

    @ApiModelProperty(value = "CPU使用率")
    private Float cpuUsage;

    @ApiModelProperty(value = "内存使用率")
    private Float memoryUsage;

    @ApiModelProperty(value = "最近24小时请求总数")
    private Long totalRequestCountToday;

    @ApiModelProperty(value = "最近24小时请求平均处理时间(单位：ms)")
    private Float averageRequestProcessingTimeToday;

    @ApiModelProperty(value = "最近24小时请求成功率")
    private Float requestSuccessRateToday;

    @ApiModelProperty(value = "微服务应用数量")
    private Integer microserviceApplicationCount;

    @ApiModelProperty(value = "最近24小时微服务重启次数")
    private Integer microserviceRestartCountToday;

    @ApiModelProperty(value = "微服务Pod运行状态（Key代表状态，Value代表数量）")
    private HashMap<String, Integer> podStatus;

    public void setNodeCount(PrometheusResVo prometheusResVo) {
        this.nodeCount = Integer.parseInt(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setUnavailableNodeCount(PrometheusResVo prometheusResVo) {
        this.unavailableNodeCount = Integer.parseInt(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setCpuUsage(PrometheusResVo prometheusResVo) {
        this.cpuUsage = Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setPodUsage(PrometheusResVo prometheusResVo) {
        this.podUsage = Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setMemoryUsage(PrometheusResVo prometheusResVo) {
        this.memoryUsage = Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setTotalRequestCountToday(PrometheusResVo prometheusResVo) {
        this.totalRequestCountToday = Long.parseLong(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setAverageRequestProcessingTimeToday(PrometheusResVo prometheusResVo) {
        this.averageRequestProcessingTimeToday = Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setRequestSuccessRateToday(PrometheusResVo prometheusResVo) {
        this.requestSuccessRateToday = Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setMicroserviceApplicationCount(List<K8sNamespace> k8sNamespaceList) {
        this.microserviceApplicationCount = k8sNamespaceList.stream().map(x -> x.getApplicationList().size()).reduce(0, Integer::sum);
    }

    public void setMicroserviceRestartCountToday(PrometheusResVo prometheusResVo) {
        this.microserviceRestartCountToday = (int) Float.parseFloat(PrometheusResVo.getPromStringValue(prometheusResVo));
    }

    public void setPodStatus(PrometheusResVo prometheusResVo) {
        HashMap<String, Integer> podStatus = new HashMap<>();
        List<PrometheusResultVo> resultList = PrometheusResVo.getPrometheusResultVoList(prometheusResVo);
        for (PrometheusResultVo result : resultList) {
            Integer value = result.getValue().getIntValue(1);
            switch (result.getMetric().getString("phase")) {
                case "Running":
                    podStatus.put("运行中", value);
                    break;
                case "Pending":
                    podStatus.put("等待中", value);
                    break;
                case "Succeeded":
                    podStatus.put("运行已完成", value);
                    break;
                case "Unknown":
                    podStatus.put("未知", value);
                    break;
                case "Failed":
                    podStatus.put("运行失败", value);
            }
        }
        this.podStatus = podStatus;
    }

}
