package com.lanyuanxiaoyao.service.web.entity;

import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.configuration.entity.info.CompactionMetrics;
import com.lanyuanxiaoyao.service.configuration.utils.DatetimeUtil;
import java.time.Instant;

/**
 * @author lanyuanxiaoyao
 * @date 2023-06-14
 */
public class CompactionMetricsVO {
    private final String flinkJobId;
    private final String alias;
    private final String applicationId;
    private final String cluster;
    private final String compactionPlanInstant;
    private final Boolean complete;
    private final Long startedTime;
    private final Long finishedTime;
    private final StatisticsVO before;
    private final StatisticsVO after;

    private String startedTimeFromNow;
    private String finishedTimeFromNow;

    public CompactionMetricsVO(CompactionMetrics metrics) {
        this.flinkJobId = metrics.getFlinkJobId().toString();
        this.alias = metrics.getAlias();
        this.applicationId = metrics.getApplicationId();
        this.cluster = metrics.getCluster();
        this.compactionPlanInstant = metrics.getCompactionPlanInstant();
        this.complete = metrics.getComplete();
        this.startedTime = metrics.getStartedTime();
        this.finishedTime = metrics.getFinishedTime();
        this.before = new StatisticsVO(metrics.getBefore());
        this.after = new StatisticsVO(metrics.getAfter());

        long now = Instant.now().toEpochMilli();
        if (ObjectUtil.isNotNull(metrics.getStartedTime()) && metrics.getStartedTime() != 0) {
            startedTimeFromNow = DatetimeUtil.fromNow(now, metrics.getStartedTime());
        }
        if (ObjectUtil.isNotNull(metrics.getFinishedTime()) && metrics.getFinishedTime() != 0) {
            finishedTimeFromNow = DatetimeUtil.fromNow(now, metrics.getFinishedTime());
        }
    }

    public String getFlinkJobId() {
        return flinkJobId;
    }

    public String getAlias() {
        return alias;
    }

    public String getApplicationId() {
        return applicationId;
    }

    public String getCluster() {
        return cluster;
    }

    public String getCompactionPlanInstant() {
        return compactionPlanInstant;
    }

    public Boolean getComplete() {
        return complete;
    }

    public Long getStartedTime() {
        return startedTime;
    }

    public Long getFinishedTime() {
        return finishedTime;
    }

    public StatisticsVO getBefore() {
        return before;
    }

    public StatisticsVO getAfter() {
        return after;
    }

    public String getStartedTimeFromNow() {
        return startedTimeFromNow;
    }

    public String getFinishedTimeFromNow() {
        return finishedTimeFromNow;
    }

    public static class StatisticsVO {
        private final String totalScanTime;
        private final String totalLogFilesCompacted;
        private final String totalLogFilesSize;
        private final String totalRecordsDeleted;
        private final String totalRecordsUpdated;
        private final String totalRecordsCompacted;

        public StatisticsVO(CompactionMetrics.Statistics statistics) {
            if (statistics == null) {
                this.totalScanTime = null;
                this.totalLogFilesCompacted = null;
                this.totalLogFilesSize = null;
                this.totalRecordsDeleted = null;
                this.totalRecordsUpdated = null;
                this.totalRecordsCompacted = null;
                return;
            }
            this.totalScanTime = statistics.getTotalScanTime() == null ? null : statistics.getTotalScanTime().toString();
            this.totalLogFilesCompacted = statistics.getTotalLogFilesCompacted() == null ? null : statistics.getTotalLogFilesCompacted().toString();
            this.totalLogFilesSize = statistics.getTotalLogFilesSize() == null ? null : statistics.getTotalLogFilesSize().toString();
            this.totalRecordsDeleted = statistics.getTotalRecordsDeleted() == null ? null : statistics.getTotalRecordsDeleted().toString();
            this.totalRecordsUpdated = statistics.getTotalRecordsUpdated() == null ? null : statistics.getTotalRecordsUpdated().toString();
            this.totalRecordsCompacted = statistics.getTotalRecordsCompacted() == null ? null : statistics.getTotalRecordsCompacted().toString();
        }

        public String getTotalScanTime() {
            return totalScanTime;
        }

        public String getTotalLogFilesCompacted() {
            return totalLogFilesCompacted;
        }

        public String getTotalLogFilesSize() {
            return totalLogFilesSize;
        }

        public String getTotalRecordsDeleted() {
            return totalRecordsDeleted;
        }

        public String getTotalRecordsUpdated() {
            return totalRecordsUpdated;
        }

        public String getTotalRecordsCompacted() {
            return totalRecordsCompacted;
        }
    }
}
