package cn.reghao.bnt.web.devops.app.model.vo;

import cn.reghao.jutil.jdk.web.result.NotAvailable;
import cn.reghao.bnt.web.devops.app.model.constant.RunStatus;
import cn.reghao.bnt.web.devops.app.model.po.AppDeployingNode;
import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
import cn.reghao.bnt.web.devops.app.model.po.log.BuildLog;
import cn.reghao.bnt.web.devops.machine.model.po.MachineHost;
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import lombok.Data;

/**
 * @author reghao
 * @date 2021-05-31 13:36:46
 */
@Data
public class AppRunningNode {
    private String appId;
    private String appName;
    private String machineId;
    private String machineIpv4;
    private String bindPorts;
    private String healthCheck;
    private String packagePath;
    private String status;
    private String startTime;
    private int pid;
    private String lastCheck;

    public AppRunningNode(AppDeployingNode deployingNode, BuildLog buildLog) {
        AppConfig appConfig = deployingNode.getDeployConfig().getAppConfig();
        MachineHost machineHost = deployingNode.getDeployConfig().getMachineHost();
        this.appId = appConfig.getAppId();
        this.appName = appConfig.getAppName();
        this.machineId = machineHost.getMachineId();
        this.machineIpv4 = machineHost.getMachineIpv4();
        this.bindPorts = appConfig.getBindPorts();
        this.healthCheck = appConfig.getHealthCheck();

        if (buildLog != null) {
            this.packagePath = buildLog.getPackagePath();
            this.status = deployingNode.getRunning() ? RunStatus.running.getDesc() : RunStatus.notRun.getDesc();
            this.startTime = deployingNode.getStartTime() != null ?
                    DateTimeConverter.format(deployingNode.getStartTime()) : NotAvailable.na.getDesc();
            this.pid = deployingNode.getPid();
            this.lastCheck = deployingNode.getLastCheck() != null ?
                    DateTimeConverter.format(deployingNode.getLastCheck()) : NotAvailable.na.getDesc();
        } else {
            this.packagePath = NotAvailable.na.getDesc();
            this.status = RunStatus.notRun.getDesc();
            this.startTime = NotAvailable.na.getDesc();
            this.pid = -1;
            this.lastCheck = NotAvailable.na.getDesc();
        }
    }
}
