-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStatisticalAnalyser.java
More file actions
160 lines (140 loc) · 4.72 KB
/
StatisticalAnalyser.java
File metadata and controls
160 lines (140 loc) · 4.72 KB
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
/*
*
* Copyright [ 2020 - 2024 ] Matthew Buckton
* Copyright [ 2024 - 2026 ] MapsMessaging B.V.
*
* Licensed under the Apache License, Version 2.0 with the Commons Clause
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://commonsclause.com/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mapsmessaging.analytics.impl;
import com.google.gson.JsonObject;
import io.mapsmessaging.analytics.Analyser;
import io.mapsmessaging.analytics.impl.stats.Statistics;
import io.mapsmessaging.analytics.impl.stats.StatisticsFactory;
import io.mapsmessaging.api.MessageBuilder;
import io.mapsmessaging.api.message.Message;
import io.mapsmessaging.dto.rest.analytics.StatisticsConfigDTO;
import io.mapsmessaging.engine.schema.SchemaManager;
import io.mapsmessaging.schemas.formatters.MessageFormatter;
import io.mapsmessaging.selector.IdentifierResolver;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class StatisticalAnalyser implements Analyser {
private final int eventCount;
private final List<String> ignoreList;
private final Map<String, Statistics> statistics;
private final String defaultAnalyser;
private final List<String> entries;
private int counter;
private MessageFormatter formatter;
public StatisticalAnalyser() {
formatter = null;
ignoreList = List.of();
eventCount = 0;
entries = List.of();
defaultAnalyser = null;
statistics = new LinkedHashMap<>();
}
public StatisticalAnalyser(String defaultName, int eventCount, List<String> ignoreList, List<String> entries) {
this.eventCount = eventCount;
this.ignoreList = ignoreList;
this.entries = entries;
this.defaultAnalyser = defaultName;
statistics = new LinkedHashMap<>();
}
@Override
public Analyser create(@NotNull StatisticsConfigDTO config) {
return new StatisticalAnalyser(config.getStatisticName(), config.getEventCount(), config.getIgnoreList(),config.getKeyList());
}
@Override
public Message ingest(@NotNull Message event) {
if(formatter == null) {
String schemaId = event.getSchemaId();
try {
formatter = SchemaManager.getInstance().getMessageFormatter(schemaId);
} catch (IOException e) {
// log
}
}
if(formatter != null) {
IdentifierResolver resolver = formatter.parse(event.getOpaqueData());
if(statistics.isEmpty()) {
loadEntries(resolver);
}
for(Map.Entry<String, Statistics> entry : statistics.entrySet()) {
Object val = resolver.get(entry.getKey());
if(val != null) {
entry.getValue().update(val);
}
else{
entry.getValue().incrementMismatch();
}
}
counter++;
if(counter >= eventCount) {
return buildEvent();
}
return null;
}
return event;
}
private Message buildEvent() {
counter = 0;
JsonObject o = new JsonObject();
for(Map.Entry<String, Statistics> entry : statistics.entrySet()) {
o.add(entry.getKey(), entry.getValue().toJson());
entry.getValue().reset();
}
String jsonString = o.toString();
MessageBuilder mb = new MessageBuilder();
mb.setOpaqueData(jsonString.getBytes());
mb.setContentType("application/json");
return mb.build();
}
private void loadEntries(IdentifierResolver resolver) {
resolver.getKeys().forEach(key -> {
boolean ignored = ignoreList != null && !ignoreList.isEmpty() &&
ignoreList.stream().anyMatch(s -> s.equals(key));
if (!ignored) {
boolean inEntries = entries.isEmpty() || entries.stream().anyMatch(s -> s.equals(key));
if (inEntries) {
Object value = resolver.get(key);
if (value instanceof String) {
statistics.put(key, StatisticsFactory.getInstance().getAnalyser("String"));
} else if (value instanceof Number) {
statistics.put(key, StatisticsFactory.getInstance().getAnalyser(defaultAnalyser));
}
}
}
});
}
@Override
public Message flush() {
return buildEvent();
}
@Override
public void close() {
//nothing to close here
}
@Override
public String getName() {
return "stats";
}
@Override
public String getDescription() {
return "Statistical Event Analyser";
}
}