// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceCheckerException.java
// public class DiskSpaceCheckerException extends Exception {
// 
// 	public DiskSpaceCheckerException() {
// 		super();
// 	}
// 
// 	public DiskSpaceCheckerException(String message) {
// 		super(message);
// 	}
// 
// 	public DiskSpaceCheckerException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public DiskSpaceCheckerException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/data/ElasticsearchClusterStats.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class ElasticsearchClusterStats {
// 
// 	@JsonProperty("timestamp")
// 	private final long timestamp;
// 
// 	@JsonProperty("cluster_name")
// 	private final String clusterName;
// 
// 	@JsonProperty("status")
// 	private final String status;
// 
// 	@JsonProperty(value = "nodes")
// 	private final ElasticsearchClusterNodes nodes;
// 
// 	public ElasticsearchClusterStats(@JsonProperty("timestamp") long timestamp,
// 									 @JsonProperty("cluster_name") String clusterName,
// 									 @JsonProperty("status") String status,
// 									 @JsonProperty("nodes") ElasticsearchClusterNodes nodes) {
// 		this.timestamp = timestamp;
// 		this.clusterName = clusterName;
// 		this.status = status;
// 		this.nodes = nodes;
// 	}
// 
// 	public long getTimestamp() {
// 		return timestamp;
// 	}
// 
// 	public String getClusterName() {
// 		return clusterName;
// 	}
// 
// 	public String getStatus() {
// 		return status;
// 	}
// 
// 	public ElasticsearchClusterNodes getNodes() {
// 		return nodes;
// 	}
// 
// 	public long getFilesystemFreeBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getFreeBytes();
// 		}
// 		return 0;
// 	}
// 
// 	public long getFilesystemAvailableBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getAvailableBytes();
// 		}
// 		return 0;
// 	}
// 
// 	public long getFilesystemTotalBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getTotalBytes();
// 		}
// 		return 0;
// 	}
// 
// 	private ElasticsearchClusterFilesystem getFilesystem() {
// 		if (nodes != null) {
// 			return nodes.getFileSystem();
// 		}
// 		return null;
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/impl/ElasticsearchClient.java
// public class ElasticsearchClient {
// 
// 	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchClient.class);
// 
// 	static final String CLUSTER_STATS_ENDPOINT = "/_cluster/stats";
// 
// 	private final Client client;
// 	private final String baseUrl;
// 	private final long cacheMs;
// 
// 	private ElasticsearchClusterStats latestStats;
// 	private long lastUpdate;
// 
// 	private final Object statsLock = new Object();
// 
// 	public ElasticsearchClient(Client client, String baseUrl, long cacheMs) {
// 		this.client = client;
// 		this.baseUrl = baseUrl;
// 		this.cacheMs = cacheMs;
// 	}
// 
// 	public ElasticsearchClusterStats getClusterStats() throws DiskSpaceCheckerException {
// 		final ElasticsearchClusterStats stats;
// 
// 		if (cacheUpToDate()) {
// 			stats = latestStats;
// 		} else {
// 			stats = getRemoteClusterStats();
// 			synchronized (statsLock) {
// 				this.latestStats = stats;
// 				this.lastUpdate = System.currentTimeMillis();
// 			}
// 		}
// 
// 		return stats;
// 	}
// 
// 	private boolean cacheUpToDate() {
// 		return latestStats != null && lastUpdate > (System.currentTimeMillis() - cacheMs);
// 	}
// 
// 	private ElasticsearchClusterStats getRemoteClusterStats() throws DiskSpaceCheckerException {
// 		try {
// 			LOGGER.debug("Retrieving stats from cluster");
// 			return client.target(baseUrl + CLUSTER_STATS_ENDPOINT)
// 					.request(MediaType.APPLICATION_JSON)
// 					.buildGet()
// 					.invoke(ElasticsearchClusterStats.class);
// 		} catch (Exception e) {
// 			LOGGER.error("Exception thrown getting cluster stats from {}: {}", baseUrl, e.getMessage());
// 			throw new DiskSpaceCheckerException(e);
// 		}
// 	}
// 
// }


import com.codahale.metrics.health.HealthCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.flax.harahachibu.services.DiskSpaceCheckerException;
import uk.co.flax.harahachibu.services.data.ElasticsearchClusterStats;
import uk.co.flax.harahachibu.services.impl.ElasticsearchClient;
/**
 * Copyright (c) 2016 Lemur Consulting Ltd.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.flax.harahachibu.health;


/**
 * Healthcheck for the Elasticsearch Client class.
 *
 * Created by mlp on 19/04/16.
 */
public class ElasticsearchClientHealthCheck extends HealthCheck {

	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchClientHealthCheck.class);

	private static final String CLUSTER_HEALTH_MSG = "Cluster status: %s";

// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceCheckerException.java
// public class DiskSpaceCheckerException extends Exception {
// 
// 	public DiskSpaceCheckerException() {
// 		super();
// 	}
// 
// 	public DiskSpaceCheckerException(String message) {
// 		super(message);
// 	}
// 
// 	public DiskSpaceCheckerException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public DiskSpaceCheckerException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/data/ElasticsearchClusterStats.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class ElasticsearchClusterStats {
// 
// 	@JsonProperty("timestamp")
// 	private final long timestamp;
// 
// 	@JsonProperty("cluster_name")
// 	private final String clusterName;
// 
// 	@JsonProperty("status")
// 	private final String status;
// 
// 	@JsonProperty(value = "nodes")
// 	private final ElasticsearchClusterNodes nodes;
// 
// 	public ElasticsearchClusterStats(@JsonProperty("timestamp") long timestamp,
// 									 @JsonProperty("cluster_name") String clusterName,
// 									 @JsonProperty("status") String status,
// 									 @JsonProperty("nodes") ElasticsearchClusterNodes nodes) {
// 		this.timestamp = timestamp;
// 		this.clusterName = clusterName;
// 		this.status = status;
// 		this.nodes = nodes;
// 	}
// 
// 	public long getTimestamp() {
// 		return timestamp;
// 	}
// 
// 	public String getClusterName() {
// 		return clusterName;
// 	}
// 
// 	public String getStatus() {
// 		return status;
// 	}
// 
// 	public ElasticsearchClusterNodes getNodes() {
// 		return nodes;
// 	}
// 
// 	public long getFilesystemFreeBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getFreeBytes();
// 		}
// 		return 0;
// 	}
// 
// 	public long getFilesystemAvailableBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getAvailableBytes();
// 		}
// 		return 0;
// 	}
// 
// 	public long getFilesystemTotalBytes() {
// 		ElasticsearchClusterFilesystem fs = getFilesystem();
// 		if (fs != null) {
// 			return fs.getTotalBytes();
// 		}
// 		return 0;
// 	}
// 
// 	private ElasticsearchClusterFilesystem getFilesystem() {
// 		if (nodes != null) {
// 			return nodes.getFileSystem();
// 		}
// 		return null;
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/impl/ElasticsearchClient.java
// public class ElasticsearchClient {
// 
// 	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchClient.class);
// 
// 	static final String CLUSTER_STATS_ENDPOINT = "/_cluster/stats";
// 
// 	private final Client client;
// 	private final String baseUrl;
// 	private final long cacheMs;
// 
// 	private ElasticsearchClusterStats latestStats;
// 	private long lastUpdate;
// 
// 	private final Object statsLock = new Object();
// 
// 	public ElasticsearchClient(Client client, String baseUrl, long cacheMs) {
// 		this.client = client;
// 		this.baseUrl = baseUrl;
// 		this.cacheMs = cacheMs;
// 	}
// 
// 	public ElasticsearchClusterStats getClusterStats() throws DiskSpaceCheckerException {
// 		final ElasticsearchClusterStats stats;
// 
// 		if (cacheUpToDate()) {
// 			stats = latestStats;
// 		} else {
// 			stats = getRemoteClusterStats();
// 			synchronized (statsLock) {
// 				this.latestStats = stats;
// 				this.lastUpdate = System.currentTimeMillis();
// 			}
// 		}
// 
// 		return stats;
// 	}
// 
// 	private boolean cacheUpToDate() {
// 		return latestStats != null && lastUpdate > (System.currentTimeMillis() - cacheMs);
// 	}
// 
// 	private ElasticsearchClusterStats getRemoteClusterStats() throws DiskSpaceCheckerException {
// 		try {
// 			LOGGER.debug("Retrieving stats from cluster");
// 			return client.target(baseUrl + CLUSTER_STATS_ENDPOINT)
// 					.request(MediaType.APPLICATION_JSON)
// 					.buildGet()
// 					.invoke(ElasticsearchClusterStats.class);
// 		} catch (Exception e) {
// 			LOGGER.error("Exception thrown getting cluster stats from {}: {}", baseUrl, e.getMessage());
// 			throw new DiskSpaceCheckerException(e);
// 		}
// 	}
// 
// }


// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/health/ElasticsearchClientHealthCheck.java
import com.codahale.metrics.health.HealthCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.flax.harahachibu.services.DiskSpaceCheckerException;
import uk.co.flax.harahachibu.services.data.ElasticsearchClusterStats;
import uk.co.flax.harahachibu.services.impl.ElasticsearchClient;

/**
 * Copyright (c) 2016 Lemur Consulting Ltd.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.flax.harahachibu.health;


/**
 * Healthcheck for the Elasticsearch Client class.
 *
 * Created by mlp on 19/04/16.
 */
public class ElasticsearchClientHealthCheck extends HealthCheck {

	private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchClientHealthCheck.class);

	private static final String CLUSTER_HEALTH_MSG = "Cluster status: %s";

	private final ElasticsearchClient client;