-
Notifications
You must be signed in to change notification settings - Fork 215
/
MongoMetrics.java
164 lines (143 loc) · 6 KB
/
MongoMetrics.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.persistence.mongo;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
/**
* Holds metrics for current MongoDB maximum roundtrip timers, in which resolution they were reported and from which
* reporter.
*/
public final class MongoMetrics implements Jsonifiable<JsonObject> {
private final String reporter;
private final Duration resolution;
private final List<Long> maxTimerNanos;
private MongoMetrics(final String reporter, final Duration resolution, final List<Long> maxTimerNanos) {
this.reporter = reporter;
this.resolution = resolution;
this.maxTimerNanos = Collections.unmodifiableList(new ArrayList<>(maxTimerNanos));
}
/**
* Returns a new {@code MongoMetrics} instance.
*
* @param reporter the reporter who reports this metrics.
* @param resolution the resolution in which the metrics were reported.
* @param maxTimerNanos a list of maximum nanoseconds encountered on MongoDB insert times.
* @return the new MongoMetrics instance.
*/
public static MongoMetrics of(final String reporter, final Duration resolution,
final Iterable<Long> maxTimerNanos) {
final List<Long> target = new ArrayList<>();
maxTimerNanos.forEach(target::add);
return new MongoMetrics(reporter, resolution, target);
}
/**
* Creates a new {@code MongoMetrics} from a JSON string.
*
* @param jsonString the JSON string of which a new MongoMetrics is to be created.
* @return the MongoMetrics which was created from the given JSON string.
* @throws NullPointerException if {@code jsonString} is {@code null}.
* @throws IllegalArgumentException if {@code jsonString} is empty.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonString} does not contain a JSON
* object or if it is not valid JSON.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the passed in {@code jsonString} was not in the
* expected 'MongoMetrics' format.
*/
public static MongoMetrics fromJson(final String jsonString) {
return fromJson(JsonFactory.newObject(jsonString));
}
/**
* Creates a new {@code MongoMetrics} from a JSON object.
*
* @param jsonObject the JSON object of which a new MongoMetrics is to be created.
* @return the MongoMetrics which was created from the given JSON object.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws IllegalArgumentException if {@code jsonObject} is empty.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} is not valid JSON.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the passed in {@code jsonObject} was not in the
* expected 'MongoMetrics' format.
*/
public static MongoMetrics fromJson(final JsonObject jsonObject) {
final String extractedReporter = jsonObject.getValueOrThrow(JsonFields.REPORTER);
final Duration extractedResolution = Duration.parse(jsonObject.getValueOrThrow(JsonFields.RESOLUTION));
final List<Long> extractedMaxTimerNanos = jsonObject.getValue(JsonFields.MAX_TIMER_NANOS)
.orElse(JsonArray.empty())
.stream()
.filter(JsonValue::isLong)
.map(JsonValue::asLong)
.toList();
return of(extractedReporter, extractedResolution, extractedMaxTimerNanos);
}
/**
* @return the reporter who reported this metrics.
*/
public String getReporter() {
return reporter;
}
/**
* @return the resolution in which the metrics were reported.
*/
public Duration getResolution() {
return resolution;
}
/**
* @return the list of maximum nanoseconds encountered on MongoDB insert times.
*/
public List<Long> getMaxTimerNanos() {
return maxTimerNanos;
}
@Override
public JsonObject toJson() {
return JsonFactory.newObjectBuilder()
.set(JsonFields.REPORTER, reporter)
.set(JsonFields.RESOLUTION, resolution.toString())
.set(JsonFields.MAX_TIMER_NANOS, maxTimerNanos.stream()
.map(JsonFactory::newValue)
.collect(JsonCollectors.valuesToArray())
)
.build();
}
/**
* An enumeration of the known {@link JsonFieldDefinition}s of a MongoMetrics.
*/
@Immutable
public static final class JsonFields {
/**
* JSON field of the reporter value.
*/
static final JsonFieldDefinition<String> REPORTER =
JsonFactory.newStringFieldDefinition("reporter");
/**
* JSON field of the resolution value.
*/
static final JsonFieldDefinition<String> RESOLUTION =
JsonFactory.newStringFieldDefinition("resolution");
/**
* JSON field of the max-timer-nanos array.
*/
static final JsonFieldDefinition<JsonArray> MAX_TIMER_NANOS =
JsonFactory.newJsonArrayFieldDefinition("maxTimerNanos");
private JsonFields() {
throw new AssertionError();
}
}
}