package com.lanyuanxiaoyao.service.web.entity;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lanyuanxiaoyao.service.common.utils.NameHelper;
import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication;
import com.lanyuanxiaoyao.service.configuration.utils.DatetimeUtil;
import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * YarnApplication 展示类
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-21
 */
public class YarnApplicationVO {
    private static final Pattern TASK_REGEX = Pattern.compile("^Service_Task(.*)#(.{8})$");

    @JsonIgnore
    private final YarnApplication yarnApplication;

    private final Boolean isHudiApplication;
    private final Boolean isSyncApplication;
    private final Boolean isCompactionApplication;
    private final Boolean isTaskApplication;
    private String startTimeFromNow;
    private String finishTimeFromNow;

    private String flinkJobId;
    private String flinkJobName;
    private String alias;

    private String taskName;
    private String taskId;

    private Double compactionCompletionRatio = 0.0;

    public YarnApplicationVO(YarnApplication yarnApplication) {
        this.yarnApplication = yarnApplication;

        isSyncApplication = NameHelper.isSyncJob(yarnApplication.getName());
        isCompactionApplication = NameHelper.isCompactionJob(yarnApplication.getName());
        isHudiApplication = isSyncApplication || isCompactionApplication;

        Matcher matcher = TASK_REGEX.matcher(yarnApplication.getName());
        isTaskApplication = matcher.matches();
        if (isTaskApplication && matcher.groupCount() == 2) {
            taskName = StrUtil.trim(matcher.group(1));
            taskId = StrUtil.format(matcher.group(2));
        }

        long now = Instant.now().toEpochMilli();
        if (ObjectUtil.isNotNull(yarnApplication.getStartedTime()) && yarnApplication.getStartedTime() != 0) {
            startTimeFromNow = DatetimeUtil.fromNow(now, yarnApplication.getStartedTime());
        }
        if (ObjectUtil.isNotNull(yarnApplication.getFinishedTime()) && yarnApplication.getFinishedTime() != 0) {
            finishTimeFromNow = DatetimeUtil.fromNow(now, yarnApplication.getFinishedTime());
        }

        if (isSyncApplication) {
            flinkJobId = ReUtil.get(NameHelper.SYNC_JOB_NAME_REGEX, yarnApplication.getName(), 1);
            flinkJobName = ReUtil.get(NameHelper.SYNC_JOB_NAME_REGEX, yarnApplication.getName(), 2);
        }
        if (isCompactionApplication) {
            flinkJobId = ReUtil.get(NameHelper.COMPACTION_JOB_NAME_REGEX, yarnApplication.getName(), 1);
            alias = ReUtil.get(NameHelper.COMPACTION_JOB_NAME_REGEX, yarnApplication.getName(), 2);
        }
    }

    public String getCluster() {
        return yarnApplication.getCluster();
    }

    public String getId() {
        return yarnApplication.getId();
    }

    public String getUser() {
        return yarnApplication.getUser();
    }

    public String getQueue() {
        return yarnApplication.getQueue();
    }

    public String getName() {
        return yarnApplication.getName();
    }

    public String getState() {
        return yarnApplication.getState();
    }

    public String getFinalStatus() {
        return yarnApplication.getFinalStatus();
    }

    public Float getProgress() {
        return yarnApplication.getProgress();
    }

    public String getDiagnostics() {
        return yarnApplication.getDiagnostics();
    }

    public String getApplicationType() {
        return yarnApplication.getApplicationType();
    }

    public Long getStartedTime() {
        return yarnApplication.getStartedTime();
    }

    public Long getLaunchTime() {
        return yarnApplication.getLaunchTime();
    }

    public Long getFinishedTime() {
        return yarnApplication.getFinishedTime();
    }

    public Long getElapsedTime() {
        return yarnApplication.getElapsedTime();
    }

    public String getTrackingUrl() {
        return yarnApplication.getTrackingUrl();
    }

    public Long getAllocatedMB() {
        return yarnApplication.getAllocatedMB();
    }

    public Long getAllocatedVCores() {
        return yarnApplication.getAllocatedVCores();
    }

    public Integer getRunningContainers() {
        return yarnApplication.getRunningContainers();
    }

    public Float getQueueUsagePercentage() {
        return yarnApplication.getQueueUsagePercentage();
    }

    public Float getClusterUsagePercentage() {
        return yarnApplication.getClusterUsagePercentage();
    }

    public Boolean getSyncApplication() {
        return isSyncApplication;
    }

    public Boolean getCompactionApplication() {
        return isCompactionApplication;
    }

    public Boolean getHudiApplication() {
        return isHudiApplication;
    }

    public Boolean getTaskApplication() {
        return isTaskApplication;
    }

    public String getStartTimeFromNow() {
        return startTimeFromNow;
    }

    public String getFinishTimeFromNow() {
        return finishTimeFromNow;
    }

    public String getFlinkJobId() {
        return flinkJobId;
    }

    public String getFlinkJobName() {
        return flinkJobName;
    }

    public String getAlias() {
        return alias;
    }

    public String getTaskName() {
        return taskName;
    }

    public String getTaskId() {
        return taskId;
    }

    public Double getCompactionCompletionRatio() {
        return compactionCompletionRatio;
    }

    public void setCompactionCompletionRatio(Double compactionCompletionRatio) {
        this.compactionCompletionRatio = compactionCompletionRatio;
    }
}
