/*
 * Copyright 2015 Timothy Brooks
 *
 * 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 net.uncontended.precipice.metrics.registry;

import net.uncontended.precipice.Service;
import net.uncontended.precipice.SuperStatusInterface;
import net.uncontended.precipice.metrics.*;

import java.util.concurrent.TimeUnit;

public class Summary<T extends Enum<T> & SuperStatusInterface> {
    private final long period;
    private final TimeUnit unit;
    private final ActionMetrics<T> actionMetrics;
    private final Service service;

    public long pendingCount = 0;
    public long remainingCapacity = 0;

    public final long[] totalMetricCounts;
    public final long[] metricCounts;
    
    public LatencySnapshot successLatency = LatencyMetrics.DEFAULT_SNAPSHOT;
    public LatencySnapshot errorLatency = LatencyMetrics.DEFAULT_SNAPSHOT;
    public LatencySnapshot timeoutLatency = LatencyMetrics.DEFAULT_SNAPSHOT;

    public LatencySnapshot totalSuccessLatency = LatencyMetrics.DEFAULT_SNAPSHOT;
    public LatencySnapshot totalErrorLatency = LatencyMetrics.DEFAULT_SNAPSHOT;
    public LatencySnapshot totalTimeoutLatency = LatencyMetrics.DEFAULT_SNAPSHOT;
    private final Class<T> metricType;

    Summary(long period, TimeUnit unit, ActionMetrics<T> metrics, Service service) {
        this.period = period;
        this.unit = unit;
        this.service = service;
        actionMetrics = metrics;
        remainingCapacity = service.remainingCapacity();
        metricType = metrics.getMetricType();
        totalMetricCounts = new long[metricType.getEnumConstants().length];
        metricCounts = new long[metricType.getEnumConstants().length];
    }

    public void refresh() {
        pendingCount = service.pendingCount();
        remainingCapacity = service.remainingCapacity();

        MetricCounter<T> totalMetricCounter = actionMetrics.totalCountMetricCounter();

        for (T t : metricType.getEnumConstants()) {
            int metricIndex = t.ordinal();
            metricCounts[metricIndex] = 0;
            totalMetricCounts[metricIndex] = totalMetricCounter.getMetricCount(t);
        }

        for (MetricCounter<T> m : actionMetrics.metricCounterIterable(period, unit)) {
            for (T t : metricType.getEnumConstants()) {
                metricCounts[t.ordinal()] += m.getMetricCount(t);
            }
        }

        LatencyMetrics latencyMetrics = service.getLatencyMetrics();
        if (latencyMetrics instanceof IntervalLatencyMetrics) {
            IntervalLatencyMetrics intervalMetrics = (IntervalLatencyMetrics) latencyMetrics;
//            successLatency = intervalMetrics.intervalSnapshot(Metric.SUCCESS);
//            errorLatency = intervalMetrics.intervalSnapshot(Metric.ERROR);
//            timeoutLatency = intervalMetrics.intervalSnapshot(Metric.TIMEOUT);
        }

//        totalSuccessLatency = latencyMetrics.latencySnapshot(Metric.SUCCESS);
//        totalErrorLatency = latencyMetrics.latencySnapshot(Metric.ERROR);
//        totalTimeoutLatency = latencyMetrics.latencySnapshot(Metric.TIMEOUT);
    }
}

@@ -679,24 +679,68 @@
 ce.Service;%0A
+import net.uncontended.precipice.SuperImpl;%0A
 import net.u
@@ -1236,20 +1236,16 @@
 Counts;%0A
-    
 %0A    pub
@@ -2966,24 +2966,74 @@
 yMetrics();%0A
+%0A        if (metricType == SuperImpl.class) %7B%0A    
         if (
@@ -3080,24 +3080,28 @@
 yMetrics) %7B%0A
+    
             
@@ -3182,18 +3182,20 @@
 etrics;%0A
-//
+    
         
@@ -3240,38 +3240,41 @@
 ntervalSnapshot(
-Metric
+SuperImpl
 .SUCCESS);%0A//   
@@ -3260,34 +3260,36 @@
 rImpl.SUCCESS);%0A
-//
+    
             erro
@@ -3324,38 +3324,41 @@
 ntervalSnapshot(
-Metric
+SuperImpl
 .ERROR);%0A//     
@@ -3342,34 +3342,36 @@
 perImpl.ERROR);%0A
-//
+    
             time
@@ -3408,38 +3408,41 @@
 ntervalSnapshot(
-Metric
+SuperImpl
 .TIMEOUT);%0A     
@@ -3448,13 +3448,19 @@
     
+  
+  
 %7D%0A%0A
-//
+    
     
@@ -3516,22 +3516,25 @@
 napshot(
-Metric
+SuperImpl
 .SUCCESS
@@ -3536,18 +3536,20 @@
 CCESS);%0A
-//
+    
         
@@ -3599,22 +3599,25 @@
 napshot(
-Metric
+SuperImpl
 .ERROR);
@@ -3617,18 +3617,20 @@
 ERROR);%0A
-//
+    
         
@@ -3682,22 +3682,25 @@
 napshot(
-Metric
+SuperImpl
 .TIMEOUT
@@ -3698,20 +3698,30 @@
 l.TIMEOUT);%0A
+        %7D%0A
     %7D%0A%7D%0A

Make summary latency work for SuperImpl type