package com.microservices.mon.k8s.domain;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@ApiModel("K8S事件")
public class K8SEvent {
    @ApiModelProperty(value = "事件id")
    private String docId;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "发生时间")
    private Date occurredTime;
    @ApiModelProperty(value = "操作类型/异常原因")
    private String reason;
    @ApiModelProperty(value = "事件发生对象")
    private String object;
    @ApiModelProperty(value = "事件发生子对象")
    private String childObject;
    @ApiModelProperty(value = "所处命名空间")
    private String namespace;
    @ApiModelProperty(value = "运行结果/详细信息")
    private String message;

    public static GenericsTableDataInfo<K8SEvent> toK8SEventTable(JSONObject esQueryRes) {
        List<K8SEvent> k8SEventList = new ArrayList<>();
        Long total = getK8SEventTotal(esQueryRes, k8SEventList);
        return new GenericsTableDataInfo<>(k8SEventList, total);
    }

    public static List<K8SEvent> toK8SEventList(JSONObject esQueryRes) {
        List<K8SEvent> k8SEventList = new ArrayList<>();
        getK8SEventTotal(esQueryRes, k8SEventList);
        return k8SEventList;
    }

    private static Long getK8SEventTotal(JSONObject esQueryRes, List<K8SEvent> k8SEventList) {
        Long total = 0L;
        if (esQueryRes.getJSONObject("hits") != null && esQueryRes.getJSONObject("hits").getJSONArray("hits") != null) {
            JSONObject hits = esQueryRes.getJSONObject("hits");
            JSONArray dataHits = hits.getJSONArray("hits");

            for (int i = 0; i < dataHits.size(); i++) {
                JSONObject dataHit = dataHits.getJSONObject(i);
                K8SEvent k8SEvent = new K8SEvent();
                k8SEvent.setDocId(dataHit.getString("_id"));
                JSONObject source = dataHit.getJSONObject("_source");
                k8SEvent.setOccurredTime(source.getDate("lastTimestamp"));
                k8SEvent.setReason(source.getString("reason"));
                if (source.getJSONObject("involvedObject") != null) {
                    JSONObject involvedObject = source.getJSONObject("involvedObject");
                    k8SEvent.setObject(String.format("%s:%s", involvedObject.getString("kind"), involvedObject.getString("name")));
                    k8SEvent.setChildObject(involvedObject.getString("fieldPath"));
                    k8SEvent.setNamespace(involvedObject.getString("namespace"));
                }

                k8SEvent.setMessage(source.getString("message"));
                k8SEventList.add(k8SEvent);
            }
            total = hits.getJSONObject("total").getLong("value");
        }
        return total;

    }

}
