// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.application.healthchecks;

import com.yahoo.bard.webservice.config.SystemConfig;
import com.yahoo.bard.webservice.config.SystemConfigException;
import com.yahoo.bard.webservice.config.SystemConfigProvider;

import com.codahale.metrics.health.HealthCheck;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Displays version in /status.html.
 */
public class VersionHealthCheck extends HealthCheck {

    private static final SystemConfig SYSTEM_CONFIG = SystemConfigProvider.getInstance();

    private static final Logger LOG = LoggerFactory.getLogger(VersionHealthCheck.class);
    public static final String VERSION_KEY = SYSTEM_CONFIG.getPackageVariableName("version");
    public static final String GIT_SHA_KEY = SYSTEM_CONFIG.getPackageVariableName("git_sha");

    private String version;
    private String gitSha;

    /**
     * Constructor using the default version key property name.
     */
    public VersionHealthCheck() {
        this(VERSION_KEY, GIT_SHA_KEY);
    }

    /**
     * Constructor.
     *
     * @param versionKey  The property name from which to get the version from the SystemConfig
     * @param gitShaKey  The property name from which to get the git sha from the SystemConfig
     */
    public VersionHealthCheck(String versionKey, String gitShaKey) {
        try {
            version = SYSTEM_CONFIG.getStringProperty(versionKey);
        } catch (SystemConfigException ignored) {
            LOG.error("{} not found in configuration", versionKey);
            version = null;
        }

        try {
            gitSha = SYSTEM_CONFIG.getStringProperty(gitShaKey);
        } catch (SystemConfigException ignored) {
            LOG.warn("{} not found in configuration", gitShaKey);
            gitSha = null;
        }
    }

    @Override
    protected Result check() throws Exception {
        return version == null ? Result.unhealthy(VERSION_KEY + " not set") : Result.healthy(version + ":" + gitSha);
    }
}

@@ -963,24 +963,30 @@
     private 
+final 
 String versi
@@ -1001,16 +1001,22 @@
 private 
+final 
 String g
@@ -1483,24 +1483,52 @@
 itShaKey) %7B%0A
+        String tempVersion;%0A
         try 
@@ -1533,33 +1533,37 @@
 y %7B%0A            
-v
+tempV
 ersion = SYSTEM_
@@ -1730,17 +1730,21 @@
         
-v
+tempV
 ersion =
@@ -1756,24 +1756,82 @@
 ;%0A        %7D%0A
+        version = tempVersion;%0A%0A        String tempGitSha;
 %0A        try
@@ -1837,33 +1837,37 @@
 y %7B%0A            
-g
+tempG
 itSha = SYSTEM_C
@@ -2030,17 +2030,21 @@
         
-g
+tempG
 itSha = 
@@ -2055,24 +2055,53 @@
 ;%0A        %7D%0A
+        gitSha = tempGitSha;%0A
     %7D%0A%0A    @

Clean up VersionHealthCheck finality and warnings