summaryrefslogtreecommitdiffstats
path: root/tenant-cd/src/main/java/ai/vespa/hosted/cd/metric/Statistic.java
blob: fc52900bdac142e761c7b0f658c307e69377bdd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ai.vespa.hosted.cd.metric;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.StringJoiner;

import static java.util.Map.copyOf;

/**
 * Known statistic about a metric, at a certain point.
 *
 * @author jonmv
 */
public class Statistic {

    private final Map<Type, Double> data;

    /** Creates a new Statistic with a copy of the given data. */
    private Statistic(Map<Type, Double> data) {
        this.data = data;
    }

    public static Statistic of(Map<Type, Double> data) {
        return new Statistic(copyOf(data));
    }

    /** Returns the value of the given type, or throws a NoSuchElementException if this isn't known. */
    public double get(Type key) {
        if ( ! data.containsKey(key))
            throw new NoSuchElementException("No value with key '" + key + "' is known.");

        return data.get(key);
    }

    /** Returns the underlying, unmodifiable Map. */
    public Map<Type, Double> asMap() {
        return data;
    }

    Statistic mergedWith(Statistic other) {
        if (data.keySet().equals(other.data.keySet()))
            throw new IllegalArgumentException("Incompatible key sets '" + data.keySet() + "' and '" + other.data.keySet() + "'.");

        Map<Type, Double> merged = new HashMap<>();
        double n1 = get(Type.count), n2 = other.get(Type.count);
        for (Type type : data.keySet()) switch (type) {
            case count: merged.put(type, n1 + n2); break;
            case rate: merged.put(type, get(Type.rate) + other.get(Type.rate)); break;
            case max: merged.put(type, Math.max(get(Type.max), other.get(Type.max))); break;
            case min: merged.put(type, Math.min(get(Type.min), other.get(Type.min))); break;
            case average: merged.put(type, (n1 * get(Type.average) + n2 * other.get(Type.average)) / (n1 + n2)); break;
            case last:
            case percentile95:
            case percentile99: break;
            default: throw new IllegalArgumentException("Unexpected type '" + type + "'.");
        }
        return of(merged);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Statistic.class.getSimpleName() + "[", "]")
                .add("data=" + data)
                .toString();
    }

}