aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/main/java/ai/vespa/metrics/set/Metric.java
blob: c60ee0b560278dbab77f9001ea30ebd70dd5af32 (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
91
92
93
94
95
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metrics.set;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Helper class to model a metric.
 *
 * @author trygve
 * @author gjoranv
 */
public class Metric {

    public final String name;
    public final String outputName;
    public final String description;
    public final Map<String, String> dimensions;

    public Metric(String name, String outputName, String description, Map<String, String> dimensions) {
        this.name = name;
        this.outputName = outputName;
        this.description = description;
        this.dimensions = Map.copyOf(dimensions);
    }

    public Metric(String name, String outputName, String description) {
        this(name, outputName, description, new LinkedHashMap<>());
    }

    /**
     * Creates a metric with empty description
     */
    public Metric(String name, String outputName) {
        this(name, outputName, "");
    }

    /**
     * Creates a metric with same outputname as metricname
     *
     * @param name The name of the metric, same name used for output name
     */
    public Metric(String name) {
        this(name, name);
    }

    /**
     * Returns a new Metric that is a combination of this and the given metric.
     * New dimensions from the given metric are added, but already existing
     * dimensions will be kept unchanged.
     *
     * @param other The metric to add dimensions from.
     * @return A new metric with dimensions from this and the other.
     */
    public Metric addDimensionsFrom(Metric other) {
        Map<String, String> combined = new LinkedHashMap<>(dimensions);
        other.dimensions.forEach(
                (k, v) -> {
                    if (!combined.containsKey(k)) combined.put(k, v);
                });

        return new Metric(name, outputName, description, combined);
    }


    @Override
    public String toString() {
        return "Metric{" +
                "name='" + name + '\'' +
                ", outputName='" + outputName + '\'' +
                ", dimensions=" + dimensions +
                '}';
    }


    /**
     * Two metrics are considered equal if they have the same name.
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Metric metric = (Metric) o;

        return name.equals(metric.name);

    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

}