Skip to content

Package: Metrics

Metrics

nameinstructionbranchcomplexitylinemethod
addMetric(KuraPayloadProto.KuraPayload.Builder, String, Object)
M: 125 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%
buildBody(KuraPayloadProto.KuraPayload.Builder, ByteBuffer)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
buildMetrics(KuraPayloadProto.KuraPayload.Builder, Map)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
extractMetrics(KuraPayloadProto.KuraPayload)
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
extractMetrics(List)
M: 84 C: 0
0%
M: 12 C: 0
0%
M: 10 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
getAsString(Map, String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getAsString(Map, String, String)
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.client.gateway.kura.internal;
14:
15: import com.google.protobuf.ByteString;
16: import org.eclipse.kapua.client.gateway.kura.proto.KuraPayloadProto.KuraPayload;
17: import org.eclipse.kapua.client.gateway.kura.proto.KuraPayloadProto.KuraPayload.KuraMetric;
18: import org.eclipse.kapua.client.gateway.kura.proto.KuraPayloadProto.KuraPayload.KuraMetric.ValueType;
19:
20: import java.nio.ByteBuffer;
21: import java.util.List;
22: import java.util.Map;
23: import java.util.Objects;
24: import java.util.TreeMap;
25:
26: public final class Metrics {
27:
28: private Metrics() {
29: }
30:
31: /**
32: * Convert plain key value map into a Kura metric structure <br>
33: * Only the supported Kura values types must be used (String, boolean, int,
34: * long, float, double, byte[])
35: *
36: * @param builder
37: * the builder to append the metrics to
38: * @param metrics
39: * the metrics map
40: * @throws IllegalArgumentException
41: * in case of an unsupported value type
42: */
43: public static void buildMetrics(final KuraPayload.Builder builder, final Map<String, ?> metrics) {
44: Objects.requireNonNull(metrics);
45:
46:• for (final Map.Entry<String, ?> metric : metrics.entrySet()) {
47: addMetric(builder, metric.getKey(), metric.getValue());
48: }
49: }
50:
51: public static void buildBody(final KuraPayload.Builder builder, final ByteBuffer body) {
52:• if (body == null) {
53: return;
54: }
55:
56: Objects.requireNonNull(builder);
57:
58: builder.setBody(ByteString.copyFrom(body));
59: }
60:
61: public static void addMetric(final KuraPayload.Builder builder, final String key, final Object value) {
62: final KuraMetric.Builder b = KuraMetric.newBuilder();
63: b.setName(key);
64:
65:• if (value == null) {
66: return;
67:• } else if (value instanceof Boolean) {
68: b.setType(ValueType.BOOL);
69: b.setBoolValue((boolean) value);
70:• } else if (value instanceof Integer) {
71: b.setType(ValueType.INT32);
72: b.setIntValue((int) value);
73:• } else if (value instanceof String) {
74: b.setType(ValueType.STRING);
75: b.setStringValue((String) value);
76:• } else if (value instanceof Long) {
77: b.setType(ValueType.INT64);
78: b.setLongValue((Long) value);
79:• } else if (value instanceof Double) {
80: b.setType(ValueType.DOUBLE);
81: b.setDoubleValue((Double) value);
82:• } else if (value instanceof Float) {
83: b.setType(ValueType.FLOAT);
84: b.setFloatValue((Float) value);
85:• } else if (value instanceof byte[]) {
86: b.setType(ValueType.BYTES);
87: b.setBytesValue(ByteString.copyFrom((byte[]) value));
88: } else {
89: throw new IllegalArgumentException(String.format("Illegal metric data type: %s", value.getClass()));
90: }
91:
92: builder.addMetric(b);
93: }
94:
95: public static Map<String, Object> extractMetrics(final KuraPayload payload) {
96:• if (payload == null) {
97: return null;
98: }
99: return extractMetrics(payload.getMetricList());
100: }
101:
102: public static Map<String, Object> extractMetrics(final List<KuraMetric> metricList) {
103:• if (metricList == null) {
104: return null;
105: }
106:
107: /*
108: * We are using a TreeMap in order to have a stable order of properties
109: */
110: final Map<String, Object> result = new TreeMap<>();
111:
112:• for (final KuraMetric metric : metricList) {
113: final String name = metric.getName();
114:• switch (metric.getType()) {
115: case BOOL:
116: result.put(name, metric.getBoolValue());
117: break;
118: case BYTES:
119: result.put(name, metric.getBytesValue().toByteArray());
120: break;
121: case DOUBLE:
122: result.put(name, metric.getDoubleValue());
123: break;
124: case FLOAT:
125: result.put(name, metric.getFloatValue());
126: break;
127: case INT32:
128: result.put(name, metric.getIntValue());
129: break;
130: case INT64:
131: result.put(name, metric.getLongValue());
132: break;
133: case STRING:
134: result.put(name, metric.getStringValue());
135: break;
136: }
137: }
138:
139: return result;
140: }
141:
142: public static String getAsString(final Map<String, Object> metrics, final String key) {
143: return getAsString(metrics, key, null);
144: }
145:
146: public static String getAsString(final Map<String, Object> metrics, final String key, final String defaultValue) {
147: final Object value = metrics.get(key);
148:• if (value == null) {
149: return defaultValue;
150: }
151:• if (value instanceof String) {
152: return (String) value;
153: }
154: return defaultValue;
155: }
156: }