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

import cn.reghao.devops.web.mgr.app.model.po.AppDeployingNode;
import cn.reghao.devops.web.mgr.app.model.po.config.AppConfig;
import cn.reghao.devops.web.mgr.app.model.po.log.BuildLog;
import cn.reghao.devops.web.mgr.machine.model.po.MachineHost;
import cn.reghao.devops.common.util.NotAvailable;
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import lombok.Data;

/**
 * @author reghao
 * @date 2021-05-31 13:36:46
 */
@Data
public class AppRunning {
    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 AppRunning(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.getStatus();
            this.startTime = DateTimeConverter.format(deployingNode.getStartTime());
            this.pid = deployingNode.getPid();
            this.lastCheck = DateTimeConverter.format(deployingNode.getLastCheck());
        } else {
            this.packagePath = NotAvailable.na.getDesc();
            this.status = NotAvailable.na.getDesc();
            this.startTime = NotAvailable.na.getDesc();
            this.pid = -1;
            this.lastCheck = NotAvailable.na.getDesc();
        }
    }
}
