Package: SimplePeriodicGenerator
SimplePeriodicGenerator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SimplePeriodicGenerator(ApplicationContext, GeneratorScheduler, String, Generator) |
|
|
|
|
|
||||||||||||||||||||
SimplePeriodicGenerator(ApplicationContext, GeneratorScheduler, String, Map) |
|
|
|
|
|
||||||||||||||||||||
generateMetrics(Instant, Map) |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0(Map, Instant) |
|
|
|
|
|
||||||||||||||||||||
update(Instant, Sender) |
|
|
|
|
|
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.Sender;
17: import org.eclipse.kapua.kura.simulator.generator.Generator;
18: import org.eclipse.kapua.kura.simulator.generator.GeneratorScheduler;
19: import org.eclipse.kapua.kura.simulator.generator.Payload;
20: import org.eclipse.kapua.kura.simulator.payload.Metrics;
21: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto;
22: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload.Builder;
23:
24: import java.time.Instant;
25: import java.util.HashMap;
26: import java.util.Map;
27: import java.util.function.Function;
28:
29: public class SimplePeriodicGenerator extends AbstractSingleTopicPeriodicGenerator {
30:
31: private final Generator generator;
32:
33: public SimplePeriodicGenerator(final ApplicationContext context, final GeneratorScheduler scheduler, final String dataTopic, final Generator generator) {
34: super(context, scheduler, dataTopic);
35: this.generator = generator;
36: }
37:
38: public SimplePeriodicGenerator(final ApplicationContext context, final GeneratorScheduler scheduler, final String dataTopic, final Map<String, Function<Instant, ?>> generators) {
39: super(context, scheduler, dataTopic);
40: this.generator = timestamp -> new Payload(generateMetrics(timestamp, generators));
41: }
42:
43: @Override
44: protected void update(final Instant timestamp, final Sender sender) {
45: final Payload payload = this.generator.update(timestamp);
46:
47:• if (payload == null) {
48: return;
49: }
50:
51: final Builder builder = KuraPayloadProto.KuraPayload.newBuilder();
52: Metrics.buildPayload(builder, payload);
53: builder.setTimestamp(timestamp.toEpochMilli());
54: sender.send(builder);
55: }
56:
57: protected static Map<String, Object> generateMetrics(final Instant timestamp, final Map<String, Function<Instant, ?>> generators) {
58: final Map<String, Object> result = new HashMap<>(generators.size());
59:
60:• for (final Map.Entry<String, Function<Instant, ?>> entry : generators.entrySet()) {
61: result.put(entry.getKey(), entry.getValue().apply(timestamp));
62: }
63:
64: return result;
65: }
66: }