aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/logging/Coverage.java
blob: 397ff35b54c8c7ac0b5ef10e8f638d85df30bbcd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

/**
 * Carry information about how the query covered the document corpus.
 */
public class Coverage {
    private final long docs;
    private final long active;
    private final long targetActive;
    private final int degradedReason;
    private final static int DEGRADED_BY_MATCH_PHASE = 1;
    private final static int DEGRADED_BY_TIMEOUT = 2;
    private final static int DEGRADED_BY_ADAPTIVE_TIMEOUT = 4;
    public Coverage(long docs, long active, long targetActive, int degradedReason) {
        this.docs = docs;
        this.active = active;
        this.targetActive = targetActive;
        this.degradedReason = degradedReason;
    }

    public long getDocs() {
        return docs;
    }

    public long getActive() {
        return active;
    }

    public static int toDegradation(boolean degradeByMatchPhase, boolean degradedByTimeout, boolean degradedByAdaptiveTimeout) {
        int v = 0;
        if (degradeByMatchPhase) {
            v |= DEGRADED_BY_MATCH_PHASE;
        }
        if (degradedByTimeout) {
            v |= DEGRADED_BY_TIMEOUT;
        }
        if (degradedByAdaptiveTimeout) {
            v |= DEGRADED_BY_ADAPTIVE_TIMEOUT;
        }
        return v;
    }

    public long getTargetActive() { return targetActive; }

    public boolean isDegraded() { return (degradedReason != 0) || isDegradedByNonIdealState(); }
    public boolean isDegradedByMatchPhase() { return (degradedReason & DEGRADED_BY_MATCH_PHASE) != 0; }
    public boolean isDegradedByTimeout() { return (degradedReason & DEGRADED_BY_TIMEOUT) != 0; }
    public boolean isDegradedByAdapativeTimeout() { return (degradedReason & DEGRADED_BY_ADAPTIVE_TIMEOUT) != 0; }
    public boolean isDegradedByNonIdealState() { return (degradedReason == 0) && (getResultPercentage() != 100);}

    /**
     * An int between 0 (inclusive) and 100 (inclusive) representing how many
     * percent coverage the result sets this Coverage instance contains information
     * about had.
     */
    public int getResultPercentage() {
        long total = targetActive;
        if (docs < total) {
            return (int) Math.round(docs * 100.0d / total);
        }
        if ((total == 0) && isDegradedByTimeout()) {
            return 0;
        }
        return 100;
    }

}