// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/ClusterDiskSpaceManager.java
// public class ClusterDiskSpaceManager {
// 
// 	private final Set<String> servers;
// 	private final Map<String, DiskSpace> diskSpaceMap = new ConcurrentHashMap<>();
// 
// 	/**
// 	 * Construct the ClusterDiskSpaceManager.
// 	 * @param servers the set of valid servers for the cluster.
// 	 */
// 	public ClusterDiskSpaceManager(Set<String> servers) {
// 		this.servers = servers;
// 	}
// 
// 	/**
// 	 * Set the disk space for a server.
// 	 * @param server the server.
// 	 * @param space the DiskSpace object holding the disk space details.
// 	 * @throws DiskSpaceCheckerException if the server is not in the server list
// 	 * used to construct the manager.
// 	 */
// 	public void setDiskSpace(String server, DiskSpace space) throws DiskSpaceCheckerException {
// 		if (!servers.contains(server)) {
// 			throw new DiskSpaceCheckerException("Unrecognised server " + server);
// 		}
// 
// 		diskSpaceMap.put(server, space);
// 	}
// 
// 	/**
// 	 * Get the current map of server - disk space statuses.
// 	 * @return the map.
// 	 */
// 	public Map<String, DiskSpace> getDiskSpace() {
// 		return diskSpaceMap;
// 	}
// 
// 	/**
// 	 * Get the list of servers for which we should have disk space settings.
// 	 * @return the servers.
// 	 */
// 	public Set<String> getServers() {
// 		return servers;
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/data/DiskSpace.java
// public class DiskSpace {
// 
// 	private final long freeSpace;
// 	private final long maxSpace;
// 	private final Date creationDate = new Date();
// 
// 	public DiskSpace(long freeSpace, long maxSpace) {
// 		this.freeSpace = freeSpace;
// 		this.maxSpace = maxSpace;
// 	}
// 
// 	public long getFreeSpace() {
// 		return freeSpace;
// 	}
// 
// 	public long getMaxSpace() {
// 		return maxSpace;
// 	}
// 
// 	public Date getCreationDate() {
// 		return creationDate;
// 	}
// 
// }


import com.codahale.metrics.health.HealthCheck;
import uk.co.flax.harahachibu.services.ClusterDiskSpaceManager;
import uk.co.flax.harahachibu.services.data.DiskSpace;
import java.util.Date;
import java.util.Set;
/**
 * 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 ClusterDiskSpaceManager.
 *
 * Created by mlp on 19/04/16.
 */
public class ClusterDiskSpaceManagerHealthCheck extends HealthCheck {

	static final int EXPIRY_TIME_MINS = 5;

	private final ClusterDiskSpaceManager manager;

	public ClusterDiskSpaceManagerHealthCheck(ClusterDiskSpaceManager manager) {
		this.manager = manager;
	}

	@Override
	protected Result check() throws Exception {
		final Result result;

		StringBuilder message = new StringBuilder();
		boolean healthy = true;

		for (String server : manager.getServers()) {
			if (message.length() > 0) {
				message.append("; ");
			}
			if (manager.getDiskSpace().get(server) == null) {
				message.append(server).append(" : no disk space recorded");
				healthy = false;
			} else {
				DiskSpace disk = manager.getDiskSpace().get(server);
				Date checkDate = new Date(System.currentTimeMillis() - (EXPIRY_TIME_MINS * 60 * 1000));
				long updateInterval = (System.currentTimeMillis() - disk.getCreationDate().getTime()) / 1000;
				if (disk.getCreationDate().before(checkDate)) {
					healthy = false;
				}
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/ClusterDiskSpaceManager.java
// public class ClusterDiskSpaceManager {
// 
// 	private final Set<String> servers;
// 	private final Map<String, DiskSpace> diskSpaceMap = new ConcurrentHashMap<>();
// 
// 	/**
// 	 * Construct the ClusterDiskSpaceManager.
// 	 * @param servers the set of valid servers for the cluster.
// 	 */
// 	public ClusterDiskSpaceManager(Set<String> servers) {
// 		this.servers = servers;
// 	}
// 
// 	/**
// 	 * Set the disk space for a server.
// 	 * @param server the server.
// 	 * @param space the DiskSpace object holding the disk space details.
// 	 * @throws DiskSpaceCheckerException if the server is not in the server list
// 	 * used to construct the manager.
// 	 */
// 	public void setDiskSpace(String server, DiskSpace space) throws DiskSpaceCheckerException {
// 		if (!servers.contains(server)) {
// 			throw new DiskSpaceCheckerException("Unrecognised server " + server);
// 		}
// 
// 		diskSpaceMap.put(server, space);
// 	}
// 
// 	/**
// 	 * Get the current map of server - disk space statuses.
// 	 * @return the map.
// 	 */
// 	public Map<String, DiskSpace> getDiskSpace() {
// 		return diskSpaceMap;
// 	}
// 
// 	/**
// 	 * Get the list of servers for which we should have disk space settings.
// 	 * @return the servers.
// 	 */
// 	public Set<String> getServers() {
// 		return servers;
// 	}
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/data/DiskSpace.java
// public class DiskSpace {
// 
// 	private final long freeSpace;
// 	private final long maxSpace;
// 	private final Date creationDate = new Date();
// 
// 	public DiskSpace(long freeSpace, long maxSpace) {
// 		this.freeSpace = freeSpace;
// 		this.maxSpace = maxSpace;
// 	}
// 
// 	public long getFreeSpace() {
// 		return freeSpace;
// 	}
// 
// 	public long getMaxSpace() {
// 		return maxSpace;
// 	}
// 
// 	public Date getCreationDate() {
// 		return creationDate;
// 	}
// 
// }


// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/health/ClusterDiskSpaceManagerHealthCheck.java
import com.codahale.metrics.health.HealthCheck;
import uk.co.flax.harahachibu.services.ClusterDiskSpaceManager;
import uk.co.flax.harahachibu.services.data.DiskSpace;
import java.util.Date;
import java.util.Set;

/**
 * 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 ClusterDiskSpaceManager.
 *
 * Created by mlp on 19/04/16.
 */
public class ClusterDiskSpaceManagerHealthCheck extends HealthCheck {

	static final int EXPIRY_TIME_MINS = 5;

	private final ClusterDiskSpaceManager manager;

	public ClusterDiskSpaceManagerHealthCheck(ClusterDiskSpaceManager manager) {
		this.manager = manager;
	}

	@Override
	protected Result check() throws Exception {
		final Result result;

		StringBuilder message = new StringBuilder();
		boolean healthy = true;

		for (String server : manager.getServers()) {
			if (message.length() > 0) {
				message.append("; ");
			}
			if (manager.getDiskSpace().get(server) == null) {
				message.append(server).append(" : no disk space recorded");
				healthy = false;
			} else {
				DiskSpace disk = manager.getDiskSpace().get(server);
				Date checkDate = new Date(System.currentTimeMillis() - (EXPIRY_TIME_MINS * 60 * 1000));
				long updateInterval = (System.currentTimeMillis() - disk.getCreationDate().getTime()) / 1000;
				if (disk.getCreationDate().before(checkDate)) {
					healthy = false;
				}
				message.append(server).append(" : last updated ").append(updateInterval).append(" seconds ago");