summaryrefslogtreecommitdiffstats
path: root/statistics/src/main/java/com/yahoo/statistics/ValueGroup.java
blob: 5d536db678e24da25940b20effe54669c77754f7 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.statistics;


import com.yahoo.log.event.Event;
import com.yahoo.statistics.Value.Parameters;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

/**
 * A set of related values which should be logged together.
 *
 * @author  <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class ValueGroup extends Group {
    // A map for names of subevents and Value instances
    private Map<String, Value> subEvents = new HashMap<>();

    /**
     * Create a ValueGroup.
     *
     * @param name
     *            The symbolic name of this group of values
     * @param manager
     *            the statistics manager acquired by injection
     */
    public ValueGroup(String name, Statistics manager) {
        this(name, manager, null);
    }

    /**
     * Create a ValueGroup.
     *
     * @param name
     *            The symbolic name of this group of values
     * @param manager
     *            the statistics manager acquired by injection
     * @param callback
     *            will be invoked each time data is written to the log
     */
    public ValueGroup(String name, Statistics manager, Callback callback) {
        super(name, manager, callback);
        manager.register(this);
    }

    /**
     * Put a value into the named value in the group.
     */
    public void put(String name, double x) {
        Value v = getValue(name);
        v.put(x);
    }

    /**
     * Get a value with a given name, creates a new value if no
     * value with the name given exists.
     *
     */
    synchronized Value getValue(String name) {
        Value v = subEvents.get(name);
        if (v == null) {
            v = getNewValue(name);
        }
        return v;
    }

    private Value getNewValue(String subName) {
        Value v = Value.initializeUnregisteredValue(subName, new Parameters().setLogRaw(true));
        subEvents.put(subName, v);
        return v;
    }

    /**
     * Dump state to log and reset.
     */
    @Override
    public void runHandle() {
        StringBuilder multi = new StringBuilder();
        ValueProxy[] proxies;
        int i = 0;

        synchronized (this) {
            proxies = new ValueProxy[subEvents.size()];
            i = 0;
            for (Iterator<Value> j = subEvents.values().iterator(); j.hasNext();) {
                Value v = j.next();
                proxies[i] = v.getProxyAndReset();
                i++;
            }
        }

        while (i > 0) {
            i--;
            if (multi.length() > 0) {
                multi.append(", ");
            }
            multi.append(proxies[i].getName());
            multi.append("=");
            multi.append(proxies[i].getRaw());
        }

        Event.valueGroup(getName(), multi.toString());
    }

    @Override
    public boolean equals(Object o) {
        if (o.getClass() != this.getClass()) {
            return false;
        }
        ValueGroup other = (ValueGroup) o;
        return getName().equals(other.getName());
    }

    @Override
    public int hashCode() {
        return getName().hashCode() + 31 * "ValueGroup".hashCode();
    }
}