// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.metrics;

import com.yahoo.metrics.simple.Counter;
import com.yahoo.metrics.simple.Gauge;
import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.metrics.simple.Point;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * This class reports metrics for feed operations by APIs that use documentapi.
 *
 * @author freva
 */
public class DocumentApiMetrics {

    private final Counter feeds;
    private final Gauge feedLatency;
    private final Counter feedRequests;
    private final Map<DocumentOperationStatus, Map<DocumentOperationType, Point>> points = new HashMap<>();
    private final Map<String, Point> versionPointCache = new ConcurrentHashMap<>();

    public DocumentApiMetrics(MetricReceiver metricReceiver, String apiName) {
        Map<String, String> dimensions = new HashMap<>();
        dimensions.put("api", apiName);
        for (DocumentOperationStatus status : DocumentOperationStatus.values()) {
            points.put(status, new HashMap<>());
            dimensions.put("status", status.name());
            for (DocumentOperationType operation : DocumentOperationType.values()) {
                dimensions.put("operation", operation.name());
                points.get(status).put(operation, new Point(dimensions));
            }
        }

        feeds = metricReceiver.declareCounter("feed.operations");
        feedLatency = metricReceiver.declareGauge("feed.latency");
        feedRequests = metricReceiver.declareCounter("feed.http-requests");
    }

    public void reportSuccessful(DocumentOperationType documentOperationType, double latencyInSeconds) {
        Point point = points.get(DocumentOperationStatus.OK).get(documentOperationType);

        feedLatency.sample(latencyInSeconds, point);
        feeds.add(point);
    }

    public void reportSuccessful(DocumentOperationType documentOperationType, Instant startTime) {
        double latency = Duration.between(startTime, Instant.now()).toMillis() / 1000.0d;
        reportSuccessful(documentOperationType, latency);
    }

    public void reportFailure(DocumentOperationType documentOperationType, DocumentOperationStatus documentOperationStatus) {
        Point point = points.get(documentOperationStatus).get(documentOperationType);
        feeds.add(point);
    }

    public void reportHttpRequest(String clientVersion) {
        if (clientVersion != null) {
            feedRequests.add(versionPointCache.computeIfAbsent(clientVersion, v -> new Point(Map.of("client-version", v))));
        } else {
            feedRequests.add();
        }
    }

}

@@ -152,16 +152,99 @@
 trics;%0A%0A
+import com.google.common.cache.Cache;%0Aimport com.google.common.cache.CacheBuilder;%0A
 import c
@@ -272,24 +272,24 @@
 le.Counter;%0A
-
 import com.y
@@ -532,33 +532,34 @@
 current.
-ConcurrentHashMap
+ExecutionException
 ;%0A%0A%0A/**%0A
@@ -933,19 +933,21 @@
 e final 
-Map
+Cache
 %3CString,
@@ -978,31 +978,56 @@
 e = 
-new ConcurrentHashMap%3C%3E
+CacheBuilder.newBuilder().maximumSize(256).build
 ();%0A
@@ -2738,33 +2738,52 @@
         
-feedRequests.add(
+try %7B%0A                Point point = 
 versionP
@@ -2796,22 +2796,10 @@
 che.
-computeIfAbsen
+ge
 t(cl
@@ -2811,17 +2811,18 @@
 ersion, 
-v
+()
  -%3E new 
@@ -2856,14 +2856,218 @@
 n%22, 
-v))));
+clientVersion)));%0A                feedRequests.add(point);%0A            %7D catch (ExecutionException e) %7B // When Point constructor throws an exception%0A                throw new RuntimeException(e);%0A            %7D
 %0A   

Use Guava cache with size-based eviction