summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/metrics/MetricsAggregator.java
blob: 9c8712c6f1e2e3d48a8276a94894b15b037d2e2d (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.metrics;

import java.time.Instant;
import java.util.Optional;

/**
 * @author olaa
 */
public class MetricsAggregator {

    double feedLatencySum;
    double feedLatencyCount;
    double qrQueryLatencySum;
    double qrQueryLatencyCount;
    double containerQueryLatencySum;
    double containerQueryLatencyCount;
    double documentCount;
    Instant timestamp;

    public void addFeedLatencySum(double feedLatencySum) {
        this.feedLatencySum += feedLatencySum;
    }

    public void addFeedLatencyCount(double feedLatencyCount) {
        this.feedLatencyCount += feedLatencyCount;
    }

    public void addQrQueryLatencyCount(double qrQueryLatencyCount) {
        this.qrQueryLatencyCount += qrQueryLatencyCount;
    }

    public void addQrQueryLatencySum(double qrQueryLatencySum) {
        this.qrQueryLatencySum += qrQueryLatencySum;
    }

    public void addContainerQueryLatencyCount(double containerQueryLatencyCount) {
        this.containerQueryLatencyCount += containerQueryLatencyCount;
    }

    public void addContainerQueryLatencySum(double containerQueryLatencySum) {
        this.containerQueryLatencySum += containerQueryLatencySum;
    }

    public void addDocumentCount(double documentCount) {
        this.documentCount += documentCount;
    }

    public Optional<Double> aggregateFeedLatency() {
        if (isZero(feedLatencySum) || isZero(feedLatencyCount)) return Optional.empty();
        return Optional.of(feedLatencySum / feedLatencyCount);
    }

    public Optional<Double> aggregateFeedRate() {
        if (isZero(feedLatencyCount)) return Optional.empty();
        return Optional.of(feedLatencyCount / 60);
    }

    public Optional<Double> aggregateQueryLatency() {
        if (isZero(containerQueryLatencyCount, containerQueryLatencySum) && isZero(qrQueryLatencyCount, qrQueryLatencySum)) return Optional.empty();
        return Optional.of((containerQueryLatencySum + qrQueryLatencySum) / (containerQueryLatencyCount + qrQueryLatencyCount));
    }

    public Optional<Double> aggregateQueryRate() {
        if (isZero(containerQueryLatencyCount) && isZero(qrQueryLatencyCount)) return Optional.empty();
        return Optional.of((containerQueryLatencyCount + qrQueryLatencyCount) / 60);
    }

    public Optional<Double> aggregateDocumentCount() {
        if (isZero(documentCount)) return Optional.empty();
        return Optional.of(documentCount);
    }

    public void setTimestamp(Instant timestamp) {
        this.timestamp = timestamp;
    }

    public Instant getTimestamp() {
        return timestamp;
    }

    private boolean isZero(double... values) {
        boolean isZero = false;
        for (double value : values) {
            isZero |= Math.abs(value) < 0.001;
        }
        return isZero;
    }

}