package com.kendelong.util.monitoring.graphite;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.annotation.PostConstruct;

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

import com.timgroup.statsd.NoOpStatsDClient;
import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.StatsDClient;
import com.timgroup.statsd.StatsDClientException;

public class GraphiteClient
{
	private StatsDClient statsdClient;
	
	private String serverEnv;
	private String appName;
	
	private String statsdHost;
	private int statsdPort;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@PostConstruct
	public void init()
	{
		String prefix = serverEnv + "." + appName + "." + getHostname();
		try
		{
			statsdClient = new NonBlockingStatsDClient(prefix, statsdHost, statsdPort);
		}
		catch(StatsDClientException e)
		{
			logger.error("Unable to open connection to statsd at [" + statsdHost + ":" + statsdPort + "]; using NoOp client.");
			statsdClient = new NoOpStatsDClient();
		}
	}

	private String getHostname()
	{
		String hostname = null;
		try 
		{
		    InetAddress addr = InetAddress.getLocalHost();
		    hostname = addr.getHostName();
		} 
		catch (UnknownHostException e) 
		{
			hostname = "unknown";
		}
		return hostname;
	}

	public void count(String aspect, long delta)
	{
		statsdClient.count(aspect, delta);
	}

	public void count(String aspect, long delta, double sampleRate)
	{
		statsdClient.count(aspect, delta, sampleRate);
	}

	public void incrementCounter(String aspect)
	{
		statsdClient.incrementCounter(aspect);
	}

	public void increment(String aspect)
	{
		statsdClient.increment(aspect);
	}

	public void decrementCounter(String aspect)
	{
		statsdClient.decrementCounter(aspect);
	}

	public void decrement(String aspect)
	{
		statsdClient.decrement(aspect);
	}

	public void recordGaugeValue(String aspect, long value)
	{
		statsdClient.recordGaugeValue(aspect, value);
	}

	public void recordGaugeDelta(String aspect, long delta)
	{
		statsdClient.recordGaugeDelta(aspect, delta);
	}

	public void gauge(String aspect, long value)
	{
		statsdClient.gauge(aspect, value);
	}

	public void recordSetEvent(String aspect, String eventName)
	{
		statsdClient.recordSetEvent(aspect, eventName);
	}

	public void recordExecutionTime(String aspect, long timeInMs)
	{
		statsdClient.recordExecutionTime(aspect, timeInMs);
	}

	public void recordExecutionTime(String aspect, long timeInMs, double sampleRate)
	{
		statsdClient.recordExecutionTime(aspect, timeInMs, sampleRate);
	}

	public void recordExecutionTimeToNow(String aspect, long systemTimeMillisAtStart)
	{
		statsdClient.recordExecutionTimeToNow(aspect, systemTimeMillisAtStart);
	}

	public void time(String aspect, long value)
	{
		statsdClient.time(aspect, value);
	}

	public String getServerEnv()
	{
		return serverEnv;
	}

	public void setServerEnv(String env)
	{
		this.serverEnv = env;
	}

	public String getAppName()
	{
		return appName;
	}

	public void setAppName(String app)
	{
		this.appName = app;
	}

	public String getStatsdHost()
	{
		return statsdHost;
	}

	public void setStatsdHost(String statsdHost)
	{
		this.statsdHost = statsdHost;
	}

	public int getStatsdPort()
	{
		return statsdPort;
	}

	public void setStatsdPort(int statsdPort)
	{
		this.statsdPort = statsdPort;
	}
}

@@ -1212,16 +1212,66 @@
 Name();%0A
+%09%09    hostname = hostname.replaceAll(%22%5C%5C.%22, %22-%22);%0A
 %09%09%7D %0A%09%09c

change all dots in the hostname to dashes