package com.hpcloud.mon.app;

import javax.annotation.Nullable;
import javax.inject.Inject;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.hpcloud.mon.MonApiConfiguration;
import com.hpcloud.mon.common.model.metric.Metric;
import com.hpcloud.mon.common.model.metric.MetricEnvelope;
import com.hpcloud.mon.common.model.metric.MetricEnvelopes;

/**
 * Metric service implementation.
 * 
 * @author Todd Walk
 */
public class MetricService {
  // private Map<HashCode, String> metricTypeCache = new ConcurrentHashMap<HashCode, String>();
  // private AtomicInteger cacheMonth = new AtomicInteger(-1); // Must empty out the metricTypeCache
  // // each month
  private final MonApiConfiguration config;
  private final Producer<String, String> producer;
  // private final MetricRepository repo;
  private final Meter metricMeter;

  @Inject
  public MetricService(MonApiConfiguration config, Producer<String, String> producer,
      MetricRegistry metricRegistry) {
    this.config = config;
    this.producer = producer;
    metricMeter = metricRegistry.meter(MetricRegistry.name(MetricService.class, "metrics.published"));
    // cacheMonth = new AtomicInteger(Calendar.getInstance().get(Calendar.MONTH));
  }

  public void create(Metric metric, String tenantId, @Nullable String crossTenantId,
      String authToken) {
    Builder<String, Object> meta = new ImmutableMap.Builder<String, Object>().put("tenantId",
        tenantId).put("authToken", authToken);
    if (crossTenantId != null)
      meta.put("crossTenantId", crossTenantId);
    MetricEnvelope envelope = new MetricEnvelope(metric, meta.build());
    KeyedMessage<String, String> keyedMessage = new KeyedMessage<>(config.metricsTopic, tenantId,
        MetricEnvelopes.toJson(envelope));
    producer.send(keyedMessage);

    // // Add metric stats
    // int month = Calendar.getInstance().get(Calendar.MONTH);
    // int prevMonth = month > 0 ? month - 1 : 11;
    // if (cacheMonth.compareAndSet(prevMonth, month))
    // metricTypeCache.clear();
    // HashCode hash = metric.definition().toHashCode();
    // if (!metricTypeCache.containsKey(hash)) {
    // metricTypeCache.put(hash, tenantId);
    // repo.persist(tenantId, hash);
    // }

    metricMeter.mark();
  }
}

@@ -659,224 +659,8 @@
 e %7B%0A
-  // private Map%3CHashCode, String%3E metricTypeCache = new ConcurrentHashMap%3CHashCode, String%3E();%0A  // private AtomicInteger cacheMonth = new AtomicInteger(-1); // Must empty out the metricTypeCache%0A  // // each month%0A
   pr
@@ -699,16 +699,16 @@
 config;%0A
+
   privat
@@ -754,50 +754,8 @@
 er;%0A
-  // private final MetricRepository repo;%0A
   pr
@@ -1084,91 +1084,8 @@
 ));%0A
-    // cacheMonth = new AtomicInteger(Calendar.getInstance().get(Calendar.MONTH));%0A
   %7D%0A
@@ -1625,24 +1625,24 @@
 envelope));%0A
+
     producer
@@ -1666,434 +1666,8 @@
 e);%0A
-%0A    // // Add metric stats%0A    // int month = Calendar.getInstance().get(Calendar.MONTH);%0A    // int prevMonth = month %3E 0 ? month - 1 : 11;%0A    // if (cacheMonth.compareAndSet(prevMonth, month))%0A    // metricTypeCache.clear();%0A    // HashCode hash = metric.definition().toHashCode();%0A    // if (!metricTypeCache.containsKey(hash)) %7B%0A    // metricTypeCache.put(hash, tenantId);%0A    // repo.persist(tenantId, hash);%0A    // %7D%0A%0A
     

Remove unused stuff from MetricService