package com.hp.autonomy.frontend.find.core.metrics;

import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.boot.actuate.metrics.buffer.GaugeBuffers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.DoubleSummaryStatistics;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Custom implementation of {@link GaugeService} which provides maxTime/minTime/averageTime
 */
class FindGaugeService implements GaugeService {
    private static final String MAX_TIME_SUFFIX = ".max-time";
    private static final String MIN_TIME_SUFFIX = ".min-time";
    private static final String AVERAGE_TIME_SUFFIX = ".average-time";

    private final Map<String, Collection<Double>> metricMap = new ConcurrentHashMap<>();

    private final GaugeBuffers buffers;

    FindGaugeService(final GaugeBuffers buffers) {
        this.buffers = buffers;
    }

    @Override
    public void submit(final String metricName, final double value) {
        metricMap.putIfAbsent(metricName, new ArrayList<>());
        final Collection<Double> times = metricMap.get(metricName);
        times.add(value);
        final DoubleSummaryStatistics stats = new ArrayList<>(times).stream().collect(Collectors.summarizingDouble(Double::doubleValue));
        buffers.set(metricName + MAX_TIME_SUFFIX, stats.getMax());
        buffers.set(metricName + MIN_TIME_SUFFIX, stats.getMin());
        buffers.set(metricName + AVERAGE_TIME_SUFFIX, stats.getAverage());
    }
}

@@ -42,24 +42,47 @@
 e.metrics;%0A%0A
+import lombok.Builder;%0A
 import org.s
@@ -222,110 +222,38 @@
 til.
-ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.DoubleSummaryStatistics;%0Aimport java.util.Map
+Map;%0Aimport java.util.Optional
 ;%0Aim
@@ -300,44 +300,8 @@
 Map;
-%0Aimport java.util.stream.Collectors;
 %0A%0A/*
@@ -675,34 +675,26 @@
 String, 
-Collection%3CDouble%3E
+FindMetric
 %3E metric
@@ -933,20 +933,19 @@
  double 
-valu
+tim
 e) %7B%0A   
@@ -953,155 +953,270 @@
     
-metricMap.putIfAbsent(metricName, new ArrayList%3C%3E());%0A        final Collection%3CDouble%3E times = metricMap.get(metricName);%0A        times.add(value);
+final FindMetric stats = metricMap.compute(metricName, (key, maybeValue) -%3E Optional.ofNullable(maybeValue)%0A                .map(value -%3E FindMetric.builder()%0A                        .count(value.count + 1)%0A                        .max(Math.max(value.max, time))
 %0A   
@@ -1224,134 +1224,222 @@
     
-final DoubleSummaryStatistics stats = new ArrayList%3C%3E(times).stream().collect(Collectors.summarizingDouble(Double::doubleValue
+                .min(Math.min(value.min, time))%0A                        .max((value.average + time) / (value.count + 1))%0A                        .build())%0A                .orElse(new FindMetric(1, time, time, time)
 ));%0A
@@ -1498,16 +1498,11 @@
 ats.
-getMax()
+max
 );%0A 
@@ -1560,16 +1560,11 @@
 ats.
-getMin()
+min
 );%0A 
@@ -1626,21 +1626,213 @@
 ats.
-getA
+average);%0A    %7D%0A%0A    @Builder%0A    private static class FindMetric %7B%0A        private final int count;%0A        private final double max;%0A        private final double min;%0A        private final double a
 verage
-())
 ;%0A  

Rewrite gauge service to remove possibility of overflow (FIND-790)