package org.kairosdb.plugin.carbon;

import com.google.inject.Inject;
import com.google.inject.name.Named;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 Created with IntelliJ IDEA.
 User: bhawkins
 Date: 10/1/13
 Time: 3:31 PM
 To change this template use File | Settings | File Templates.
 */
public class HostTagParser implements TagParser
{
	public static final String HOST_PATTERN_PROP = "kairosdb.carbon.hosttagparser.host_pattern";
	public static final String HOST_REPLACEMENT_PROP = "kairosdb.carbon.hosttagparser.host_replacement";
	public static final String METRIC_PATTERN_PROP = "kairosdb.carbon.hosttagparser.metric_pattern";
	public static final String METRIC_REPLACEMENT_PROP = "kairosdb.carbon.hosttagparser.metric_replacement";

	private Pattern m_hostPattern;
	private String m_hostReplacement;

	private Pattern m_metricPattern;
	private String m_metricReplacement;

	@Inject
	public HostTagParser(
			@Named(HOST_PATTERN_PROP)String hostPattern,
			@Named(HOST_REPLACEMENT_PROP)String hostReplacement,
			@Named(METRIC_PATTERN_PROP)String metricPattern,
			@Named(METRIC_REPLACEMENT_PROP)String metricReplacement)
	{
		m_hostPattern = Pattern.compile(hostPattern);
		m_hostReplacement = hostReplacement;
		m_metricPattern = Pattern.compile(metricPattern);
		m_metricReplacement = metricReplacement;
	}

	@Override
	public CarbonMetric parseMetricName(String metricName)
	{

		Matcher metricMatcher = m_metricPattern.matcher(metricName);
		if (!metricMatcher.matches())
			return (null);

		CarbonMetric ret = new CarbonMetric(metricMatcher.replaceAll(m_metricReplacement));

		Matcher hostMatcher = m_hostPattern.matcher(metricName);
		if (!hostMatcher.matches())
			return (null);

		ret.addTag("host", hostMatcher.replaceAll(m_hostReplacement));
		ret.addTag("type", "graphite");

		return (ret);
	}
}

@@ -937,17 +937,16 @@
 rser(%0A%09%09
-%09
 @Named(H
@@ -974,33 +974,32 @@
 ng hostPattern,%0A
-%09
 %09%09@Named(HOST_RE
@@ -1037,17 +1037,16 @@
 cement,%0A
-%09
 %09%09@Named
@@ -1090,17 +1090,16 @@
 tern,%0A%09%09
-%09
 @Named(M
@@ -1145,16 +1145,21 @@
 lacement
+//,%0A%09
 )%0A%09%7B%0A%09%09m
@@ -1407,19 +1407,143 @@
 ricName)
-%0A%09%7B
+ %7B%0A%0A%09%09String metric;%0A%09%09String%5B%5D splited = metricName.split(%22%5C%5C.%22);%0A%09%09int prefixLength = 5;%0A%09%09int metricLength = splited.length;
 %0A%0A%09%09Matc
@@ -1599,23 +1599,23 @@
 cName);%0A
+%0A
 %09%09if (
-!
 metricMa
@@ -1630,37 +1630,124 @@
 tches())%0A%09%09%09
-return (null)
+metric = metricMatcher.replaceAll(m_metricReplacement);%0A%09%09else%0A%09%09%09metric = splited%5BmetricLength - 1%5D
 ;%0A%0A%09%09CarbonM
@@ -1785,47 +1785,43 @@
 tric
-Matcher.replaceAll(m_metricReplacement)
+);%0A%0A%09%09ret.addTag(%22type%22, %22graphite%22
 );%0A%0A
@@ -1883,15 +1883,15 @@
 e);%0A
+%0A
 %09%09if (
-!
 host
@@ -1914,26 +1914,8 @@
 ))%0A%09
-%09%09return (null);%0A%0A
 %09%09re
@@ -1979,41 +1979,147 @@
 ));%0A
-%09%09ret.addTag(%22type%22, %22graphite%22);
+%0A%09%09for ( int i = 0; i %3C metricLength - 1; i++ ) %7B%0A%09%09%09if ( splited%5Bi%5D.equals(%22hosts%22) )%0A%09%09%09%09break;%0A%09%09%09ret.addTag(%22tag%22 + i, splited%5Bi%5D);%0A%09%09%7D
 %0A%0A%09%09
@@ -2129,14 +2129,13 @@
 urn 
-(
 ret
-)
 ;
+%0A
 %0A%09%7D%0A

Split metric to tags