All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.monitor.reload.model.ReloadHealthCheckResult Maven / Gradle / Ivy

package org.zodiac.monitor.reload.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

import org.zodiac.commons.api.ResultCode;
import org.zodiac.commons.model.Result;
import org.zodiac.core.application.AppInstanceStatus;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class ReloadHealthCheckResult extends Result {

    private static final long serialVersionUID = -1237745889978613070L;

    private static final String SERVER_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    private static final String FORMATTER = "{\"name\":\"%s\", \"appInstanceStatus\":\"%s\",\"data\": %s,\"serverTime\":\"%s\"}";

    private String name;
    private AppInstanceStatus appInstanceStatus;

    public static final int HTTP_STATUS_UP_CODE = AppInstanceStatus.UP.getCode();
    public static final int HTTP_STATUS_DOWN_CODE = AppInstanceStatus.DOWN.getCode();

    public ReloadHealthCheckResult() {
        this(null);
    }

    public ReloadHealthCheckResult(String name) {
        this(name, null);
    }

    public ReloadHealthCheckResult(String name, String data) {
        this(name, data, AppInstanceStatus.UP);
    }

    public ReloadHealthCheckResult(String name, String data, AppInstanceStatus appInstanceStatus) {
        super(AppInstanceStatus.UP == appInstanceStatus, appInstanceStatus.getCode(), data, appInstanceStatus.getText());
        this.name = name;
        this.appInstanceStatus = appInstanceStatus;
    }

    @JsonIgnore
    @Override
    public ResultCode getResultCode() {
        return super.getResultCode();
    }

    @JsonIgnore
    @Override
    public int getStatusCode() {
        return super.getStatusCode();
    }

    @JsonIgnore
    @Override
    public boolean isSuccess() {
        return super.isSuccess();
    }

    @JsonIgnore
    @Override
    public String getMsg() {
        return super.getMsg();
    }

    public String getServerTime() {
        return SERVER_TIME;
    }

//    @Override
//    public String toJSONString() {
//        return String.format(FORMATTER, getName(), getAppInstanceStatus(), getData(), SERVER_TIME);
//    }

    public AppInstanceStatus getAppInstanceStatus() {
        return appInstanceStatus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAppInstanceStatus(AppInstanceStatus appInstanceStatus) {
        this.appInstanceStatus = appInstanceStatus;
    }

    @JsonIgnore
    public AppInstanceStatus getHttpStatus() {
        return AppInstanceStatus.UP.equals(getAppInstanceStatus()) ? AppInstanceStatus.UP : AppInstanceStatus.DOWN;
    }

    @JsonIgnore
    public boolean isHttpStatusUp() {
        return getHttpStatus().equals(AppInstanceStatus.UP);
    }

    @JsonIgnore
    public boolean isHttpStatusDown() {
        return !isHttpStatusUp();
    }

    @Override
    public int hashCode() {
        return Objects.hash(appInstanceStatus, getData());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ReloadHealthCheckResult other = (ReloadHealthCheckResult)obj;
        return appInstanceStatus == other.appInstanceStatus && Objects.equals(getData(), other.getData());
    }

    @Override
    public String toString() {
        return toJSONString();
    }

    public static void main(String[] args) {
        ReloadHealthCheckResult result = new ReloadHealthCheckResult("demo", "sadegwEBwr", AppInstanceStatus.OUT_OF_SERVICE);
        System.out.println(result);
        System.out.println(String.format(FORMATTER, result.getName(), result.getAppInstanceStatus(), result.getData(), SERVER_TIME));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy