/*
 * 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.codahale.grpcproxy.stats;

import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.HdrHistogram.Histogram;

public class Recorder {

  private static final Logger LOGGER = Logger.getLogger(Recorder.class.getCanonicalName());

  private final IntervalAdder count;
  private final IntervalAdder responseTime;
  private final org.HdrHistogram.Recorder latency;
  private volatile Histogram histogram;

  public Recorder(long minLatency, long maxLatency, TimeUnit latencyUnit) {
    this.count = new IntervalAdder();
    this.responseTime = new IntervalAdder();
    this.latency = new org.HdrHistogram.Recorder(latencyUnit.toMicros(minLatency),
        latencyUnit.toMicros(maxLatency), 1);
    this.histogram = latency.getIntervalHistogram(); // preload reporting histogram
  }

  public void record(long startNanoTime) {
    final long duration = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanoTime);
    count.add(1);
    responseTime.add(duration);
    try {
      latency.recordValue(duration);
    } catch (ArrayIndexOutOfBoundsException ignored) {
      LOGGER.warning("Very slow value: " + duration + "us");
    }
  }

  public Snapshot interval() {
    final IntervalCount requestCount = count.interval();
    final IntervalCount responseTimeCount = responseTime.interval();
    final long c = requestCount.count();
    final double x = requestCount.rate();
    final double r, n;
    if (c == 0) {
      r = n = 0;
    } else {
      r = responseTimeCount.rate() / c * 1e-6;
      n = x * r;
    }
    this.histogram = latency.getIntervalHistogram(histogram);
    return new AutoValue_Snapshot(c, x, n, r,
        histogram.getValueAtPercentile(50) * 1e-6,
        histogram.getValueAtPercentile(90) * 1e-6,
        histogram.getValueAtPercentile(99) * 1e-6,
        histogram.getValueAtPercentile(99.9) * 1e-6);
  }
}

@@ -645,67 +645,91 @@
 ort 
-java.util.logging.Logger;%0Aimport org.HdrHistogram.Histogram
+org.HdrHistogram.Histogram;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ap
@@ -797,16 +797,23 @@
 = Logger
+Factory
 .getLogg
@@ -1706,19 +1706,16 @@
 GER.warn
-ing
 (%22Very s
@@ -1729,11 +1729,14 @@
 ue: 
-%22 +
+%7B%7Dus%22,
  dur
@@ -1744,15 +1744,8 @@
 tion
- + %22us%22
 );%0A 

Remove final use of j.u.l.