Package: PeriodicGenerator
PeriodicGenerator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PeriodicGenerator(ApplicationContext, GeneratorScheduler, Map, Map) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
tick(Instant) |
|
|
|
|
|
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.kura.simulator.app.data;
14:
15: import org.eclipse.kapua.kura.simulator.app.ApplicationContext;
16: import org.eclipse.kapua.kura.simulator.app.Handler;
17: import org.eclipse.kapua.kura.simulator.app.Sender;
18: import org.eclipse.kapua.kura.simulator.generator.Generator;
19: import org.eclipse.kapua.kura.simulator.generator.GeneratorScheduler;
20: import org.eclipse.kapua.kura.simulator.generator.Payload;
21: import org.eclipse.kapua.kura.simulator.payload.Metrics;
22: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload;
23: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload.Builder;
24: import org.eclipse.kapua.kura.simulator.simulation.Configuration.MetricsMapping;
25:
26: import java.time.Instant;
27: import java.util.Collections;
28: import java.util.HashMap;
29: import java.util.Iterator;
30: import java.util.Map;
31: import java.util.Objects;
32:
33: public class PeriodicGenerator implements Handler {
34:
35: public static class Topic {
36:
37: private final String positionGenerator;
38:
39: private final String bodyGenerator;
40:
41: private final Map<String, MetricsMapping> metricMappings;
42:
43: public Topic(final String positionGenerator, final String bodyGenerator, final Map<String, MetricsMapping> metricMappings) {
44: this.positionGenerator = positionGenerator;
45: this.bodyGenerator = bodyGenerator;
46:
47: if (metricMappings == null) {
48: this.metricMappings = Collections.emptyMap();
49: } else {
50: this.metricMappings = Collections.unmodifiableMap(new HashMap<>(metricMappings));
51: }
52: }
53:
54: public String getBodyGenerator() {
55: return this.bodyGenerator;
56: }
57:
58: public String getPositionGenerator() {
59: return this.positionGenerator;
60: }
61:
62: public Map<String, MetricsMapping> getMetricMappings() {
63: return this.metricMappings;
64: }
65:
66: }
67:
68: private final ApplicationContext context;
69:
70: private final GeneratorScheduler.Handle handle;
71:
72: private final Map<String, Generator> generators;
73:
74: private final Map<String, Topic> topics;
75:
76: public PeriodicGenerator(final ApplicationContext context, final GeneratorScheduler scheduler, final Map<String, Generator> generators, final Map<String, Topic> topics) {
77: this.context = context;
78: this.handle = scheduler.add(this::tick);
79:
80: this.generators = Objects.requireNonNull(generators);
81: this.topics = Objects.requireNonNull(topics);
82: }
83:
84: @Override
85: public void close() throws Exception {
86: this.handle.remove();
87: }
88:
89: protected void tick(final Instant timestamp) {
90:
91: // process generators
92:
93: final Map<String, Payload> values = new HashMap<>();
94:• for (final Map.Entry<String, Generator> entry : this.generators.entrySet()) {
95: values.put(entry.getKey(), entry.getValue().update(timestamp));
96: }
97:
98: // process topics
99:
100:• for (final Map.Entry<String, Topic> topicEntry : this.topics.entrySet()) {
101:
102: final Sender sender = this.context.sender(org.eclipse.kapua.kura.simulator.topic.Topic.data(topicEntry.getKey()));
103:
104: final Builder payload = KuraPayload.newBuilder();
105: payload.setTimestamp(timestamp.toEpochMilli());
106:
107: final Topic topic = topicEntry.getValue();
108:
109: // set body
110:
111:• if (topic.getBodyGenerator() != null) {
112: final Payload generatorPayload = values.get(topic.getBodyGenerator());
113:• if (generatorPayload != null) {
114: Metrics.buildBody(payload, generatorPayload.getBody());
115: }
116: }
117:
118: // set position
119:
120:• if (topic.getPositionGenerator() != null) {
121: final Payload generatorPayload = values.get(topic.getPositionGenerator());
122:• if (generatorPayload != null) {
123: Metrics.buildPosition(payload, generatorPayload.getPosition());
124: }
125: }
126:
127: // set metrics
128:
129:• for (final Map.Entry<String, MetricsMapping> entry : topic.getMetricMappings().entrySet()) {
130:
131: // get ref
132:
133: String generator = entry.getValue().getGenerator();
134:• if (generator == null || generator.isEmpty()) {
135: generator = null;
136: }
137: String value = entry.getValue().getValue();
138:• if (value == null || value.isEmpty()) {
139: value = "value";
140: }
141:
142: // get generator values
143:
144: final Map<String, Object> generatorValues;
145:• if (generator == null) {
146: // pick first generator
147: final Iterator<Payload> i = values.values().iterator();
148:• if (i.hasNext()) {
149: generatorValues = i.next().getMetrics();
150: } else {
151: generatorValues = Collections.emptyMap();
152: }
153: } else {
154: final Payload generatorPayload = values.get(generator);
155:• if (generatorPayload != null) {
156: generatorValues = generatorPayload.getMetrics();
157: } else {
158: generatorValues = Collections.emptyMap();
159: }
160: }
161:
162: // map metrics
163:
164: Metrics.addMetric(payload, entry.getKey(), generatorValues.get(value));
165: }
166:
167: sender.send(payload);
168: }
169: }
170: }