/**
 * Copyright (C) 2013 – 2015 SLUB Dresden & Avantgarde Labs GmbH (<code@dswarm.org>)
 *
 * 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
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.dswarm.controller.status;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.codahale.metrics.MetricRegistry;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import org.elasticsearch.metrics.ElasticsearchReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public final class MetricsReporter {

	private static final Logger		LOG	= LoggerFactory.getLogger(MetricsReporter.class);

	private final MetricRegistry	registry;
	private final boolean			isEnabled;
	private final String			esHost;
	private final long				reportIntervalInMillis;

	@Inject
	public MetricsReporter(
			final MetricRegistry registry,
			@Named("dswarm.reporting.enabled") final boolean isEnabled,
			@Named("dswarm.reporting.interval") final long reportIntervalInMillis,
			@Named("dswarm.reporting.elasticsearch.host") final String esHost) {
		this.registry = registry;
		this.isEnabled = isEnabled;
		this.esHost = esHost;
		this.reportIntervalInMillis = reportIntervalInMillis;
	}

	public void start() throws IOException {
		if (isEnabled) {
			MetricsReporter.LOG.trace("reporting metrics to Elasticsearch at {} every {} milliseconds", esHost, reportIntervalInMillis);
			final ElasticsearchReporter reporter = ElasticsearchReporter.forRegistry(registry).hosts(esHost).build();
			reporter.start(reportIntervalInMillis, TimeUnit.MILLISECONDS);
		}
	}
}

@@ -1260,16 +1260,47 @@
 esHost;%0A
+%09private final String%09%09%09index;%0A
 %09private
@@ -1338,16 +1338,16 @@
 Millis;%0A
-
 %0A%09@Injec
@@ -1544,16 +1544,86 @@
 Millis,%0A
+%09%09%09@Named(%22dswarm.reporting.elasticsearch.index%22) final String index,%0A
 %09%09%09@Name
@@ -1744,16 +1744,38 @@
 nabled;%0A
+%09%09this.index = index;%0A
 %09%09this.e
@@ -2039,16 +2039,16 @@
 illis);%0A
-
 %09%09%09final
@@ -2139,16 +2139,29 @@
 esHost).
+index(index).
 build();

allow for configuring elasticsearch index to report to