package com.microservices.mon.k8s.domain;

import com.alibaba.fastjson2.JSONArray;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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


@Data
@ApiModel("指标通用对象")
public class MetricCommonData {

    @ApiModelProperty(value = "时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date date;

    @ApiModelProperty(value = "指标值")
    private Float value;

    public static List<MetricCommonData> prometheusResultVoToMetric(JSONArray prometheusResultValues) {
        List<MetricCommonData> metricCommonDataList = new ArrayList<>();
        for (int i = 0; i < prometheusResultValues.size(); i++) {
            JSONArray value = prometheusResultValues.getJSONArray(i);
            MetricCommonData metricCommonData = new MetricCommonData();
            metricCommonData.setDate(new Date(value.getLong(0) * 1000));
            String valueStr = String.format("%.2f", value.getFloatValue(1));
            metricCommonData.setValue(Float.parseFloat(valueStr));
            metricCommonDataList.add(metricCommonData);
        }
        metricCommonDataList.sort(Comparator.comparing(MetricCommonData::getDate));
        return metricCommonDataList;
    }
}
