/*
 * Copyright 2013 Rackspace
 *
 *    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 com.rackspacecloud.blueflood.service;

import com.rackspacecloud.blueflood.exceptions.CacheException;
import com.rackspacecloud.blueflood.cache.MetadataCache;
import com.rackspacecloud.blueflood.exceptions.IncomingMetricException;
import com.rackspacecloud.blueflood.exceptions.IncomingTypeException;
import com.rackspacecloud.blueflood.exceptions.IncomingUnitException;
import com.rackspacecloud.blueflood.types.IMetric;
import com.rackspacecloud.blueflood.types.Metric;
import com.rackspacecloud.blueflood.types.MetricMetadata;
import com.rackspacecloud.blueflood.types.Locator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class IncomingMetricMetadataAnalyzer {
    private static final Logger log = LoggerFactory.getLogger(IncomingMetricMetadataAnalyzer.class);
    
    private final MetadataCache cache;
    
    public IncomingMetricMetadataAnalyzer(MetadataCache cache) {
        this.cache = cache;
    }
    
    public Collection<IncomingMetricException> scanMetrics(Collection<IMetric> metrics) {
        List<IncomingMetricException> problems = new ArrayList<IncomingMetricException>();
        for (IMetric metric : metrics) {
            try {
                if (metric instanceof Metric) {
                    Collection<IncomingMetricException> metricProblems = checkMetric((Metric)metric);
                    if (metricProblems != null) {
                        problems.addAll(metricProblems);
                    }
                }
            } catch (CacheException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }
        return problems;
    }

    private IncomingMetricException checkMeta(Locator locator, String key, String incoming) throws CacheException {
        String existing = cache.get(locator, key, String.class);

        // always update the cache. it is smart enough to avoid needless writes.
        cache.put(locator, key, incoming);

        boolean differs = existing != null && !incoming.equals(existing);
        if (differs) {
            if (key.equals(MetricMetadata.UNIT.name().toLowerCase())) {
                return new IncomingUnitException(locator, existing, incoming);
            } else {
                return new IncomingTypeException(locator, existing, incoming);
            }
        }

        return null;
    }

    private Collection<IncomingMetricException> checkMetric(Metric metric) throws CacheException {
        if (metric == null) {
            return null;
        }

        List<IncomingMetricException> problems = new ArrayList<IncomingMetricException>();

        IncomingMetricException typeProblem = checkMeta(metric.getLocator(), MetricMetadata.TYPE.name().toLowerCase(),
                metric.getDataType().toString());
        IncomingMetricException unitProblem = checkMeta(metric.getLocator(), MetricMetadata.UNIT.name().toLowerCase(),
                metric.getUnit());

        if (typeProblem != null) {
            problems.add(typeProblem);
        }
        if (unitProblem != null) {
            problems.add(unitProblem);
        }

        return problems;
    }
}

@@ -661,16 +661,51 @@
 rvice;%0A%0A
+import com.codahale.metrics.Timer;%0A
 import c
@@ -1238,16 +1238,67 @@
 ocator;%0A
+import com.rackspacecloud.blueflood.utils.Metrics;%0A
 import o
@@ -1575,24 +1575,251 @@
 class);%0A    
+private static Timer scanMetricsTimer = Metrics.timer(IncomingMetricMetadataAnalyzer.class, %22Scan meta for metrics%22);%0A    private static Timer checkMetaTimer = Metrics.timer(IncomingMetricMetadataAnalyzer.class, %22Check meta%22);%0A
 %0A    private
@@ -2135,16 +2135,70 @@
 tion%3E();
+%0A%0A        Timer.Context ctx = scanMetricsTimer.time();
 %0A       
@@ -2651,32 +2651,53 @@
     %7D%0A        %7D%0A
+        ctx.stop();%0A%0A
         return p
@@ -2820,32 +2820,101 @@
 acheException %7B%0A
+        Timer.Context ctx = checkMetaTimer.time();%0A        try %7B%0A    
         String e
@@ -2971,16 +2971,20 @@
         
+   
+ 
 // alway
@@ -3048,16 +3048,20 @@
 writes.%0A
+    
         
@@ -3104,16 +3104,20 @@
         
+    
 boolean 
@@ -3178,24 +3178,28 @@
 g);%0A        
+ 
+   
 if (differs)
@@ -3197,24 +3197,28 @@
 (differs) %7B%0A
+    
             
@@ -3273,24 +3273,28 @@
 rCase())) %7B%0A
+    
             
@@ -3372,16 +3372,20 @@
         
+    
 %7D else %7B
@@ -3385,16 +3385,20 @@
  else %7B%0A
+    
         
@@ -3472,33 +3472,95 @@
 g);%0A            
+  
-%7D
+  %7D%0A            %7D%0A        %7D finally %7B%0A            ctx.stop();
 %0A        %7D%0A%0A    

Add instrumentation around scanMetrics and checkMeta