package com.utic.center.utic.stat.server.controller;

import com.utic.center.common.utils.LogUtils;
import com.utic.center.common.utils.TimeUtils;
import com.utic.center.utic.stat.server.config.ApplicationConfig;
import com.utic.center.utic.stat.server.dto.BatchJobInfoDto;
import com.utic.center.utic.stat.server.repository.ApplicationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("")
public class UticStatServerRestController {

    private final ApplicationConfig config;

    @GetMapping(value = "/info", produces = {"application/json; charset=utf8"})
    public String stringInfo() {
        String sep = System.lineSeparator();
        String heading = "------------------------------------------------------------------------------------------------------------";

        StringBuilder sb = new StringBuilder();
        sb.append(heading).append(sep);
        sb.append("UTIC Center DWDB Statistics Processing Server").append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("Process Id: %s, %s, %s", this.config.getProcessId(), this.config.getBootingTime(), TimeUtils.now())).append(sep);
        sb.append(heading).append(sep);
        sb.append(String.format("%s %-20.20s %-20.20s    TOTAL  04_ETLP  05_FLTR  05_STAT  15_STAT  01_PTRN",
                " SEQ", "Start", "End")).append(sep);
        sb.append(heading).append(sep);
        int total = 0;

        int jobSize = ApplicationRepository.batchJobInfoList.size();
        for (int ii = jobSize-1; ii >= 0; ii--) {
            BatchJobInfoDto jobInfo = ApplicationRepository.batchJobInfoList.get(ii);
            total++;

            //   TOTAL  04_ETLP  05_FLTR  05_STAT  15_STAT  30_STAT  01_HOUR   01_DAY  01_PTRN    MONTH
            sb.append(String.format("%4d %-20.20s %-20.20s %s %s %s %s %s %s",
                    total,
                    jobInfo.getStartTime(), jobInfo.getEndTime(),
                    LogUtils.numberLog(jobInfo.getElapsedTime()),
                    LogUtils.numberLog(jobInfo.getEtlp04MinTime()),
                    LogUtils.numberLog(jobInfo.getFltr05MinTime()),
                    LogUtils.numberLog(jobInfo.getStat05MinTime()),
                    LogUtils.numberLog(jobInfo.getStat15MinTime()),
                    LogUtils.numberLog(jobInfo.getPtrnInitTime())
            )).append(sep);
        }
        sb.append(heading).append(sep);
        return sb.toString();
    }

}
