|
@@ -37,10 +37,11 @@ import java.util.ArrayList; |
|
@@ -37,10 +37,11 @@ import java.util.ArrayList; |
37
|
import java.util.Arrays;
|
37
|
import java.util.Arrays;
|
38
|
import java.util.Collection;
|
38
|
import java.util.Collection;
|
39
|
import java.util.Collections;
|
39
|
import java.util.Collections;
|
|
|
40
|
+import java.util.Comparator;
|
40
|
import java.util.HashMap;
|
41
|
import java.util.HashMap;
|
41
|
import java.util.List;
|
42
|
import java.util.List;
|
42
|
import java.util.Map;
|
43
|
import java.util.Map;
|
43
|
-import java.util.function.BiConsumer;
|
44
|
+import java.util.Optional;
|
44
|
|
45
|
|
45
|
@Slf4j
|
46
|
@Slf4j
|
46
|
@Data
|
47
|
@Data
|
|
@@ -145,12 +146,7 @@ public class TbEntityDataSubCtx { |
|
@@ -145,12 +146,7 @@ public class TbEntityDataSubCtx { |
145
|
.subscriptionId(subIdx)
|
146
|
.subscriptionId(subIdx)
|
146
|
.tenantId(sessionRef.getSecurityCtx().getTenantId())
|
147
|
.tenantId(sessionRef.getSecurityCtx().getTenantId())
|
147
|
.entityId(entityData.getEntityId())
|
148
|
.entityId(entityData.getEntityId())
|
148
|
- .updateConsumer(new BiConsumer<String, SubscriptionUpdate>() {
|
|
|
149
|
- @Override
|
|
|
150
|
- public void accept(String sessionId, SubscriptionUpdate subscriptionUpdate) {
|
|
|
151
|
- sendWsMsg(sessionId, subscriptionUpdate, EntityKeyType.TIME_SERIES, resultToLatestValues);
|
|
|
152
|
- }
|
|
|
153
|
- })
|
149
|
+ .updateConsumer((sessionId, subscriptionUpdate) -> sendWsMsg(sessionId, subscriptionUpdate, EntityKeyType.TIME_SERIES, resultToLatestValues))
|
154
|
.allKeys(false)
|
150
|
.allKeys(false)
|
155
|
.keyStates(keyStates)
|
151
|
.keyStates(keyStates)
|
156
|
.build();
|
152
|
.build();
|
|
@@ -179,47 +175,90 @@ public class TbEntityDataSubCtx { |
|
@@ -179,47 +175,90 @@ public class TbEntityDataSubCtx { |
179
|
EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
|
175
|
EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
|
180
|
if (entityId != null) {
|
176
|
if (entityId != null) {
|
181
|
log.trace("[{}][{}][{}][{}] Received subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
|
177
|
log.trace("[{}][{}][{}][{}] Received subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
|
182
|
- Map<String, TsValue> latestUpdate = new HashMap<>();
|
|
|
183
|
- subscriptionUpdate.getData().forEach((k, v) -> {
|
|
|
184
|
- Object[] data = (Object[]) v.get(0);
|
|
|
185
|
- latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
|
|
|
186
|
- });
|
|
|
187
|
- EntityData entityData = getDataForEntity(entityId);
|
|
|
188
|
- if (entityData != null && entityData.getLatest() != null) {
|
|
|
189
|
- Map<String, TsValue> latestCtxValues = entityData.getLatest().get(keyType);
|
|
|
190
|
- log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
|
|
191
|
- if (latestCtxValues != null) {
|
|
|
192
|
- latestCtxValues.forEach((k, v) -> {
|
|
|
193
|
- TsValue update = latestUpdate.get(k);
|
|
|
194
|
- if (update != null) {
|
178
|
+ if (resultToLatestValues) {
|
|
|
179
|
+ sendLatestWsMsg(entityId, sessionId, subscriptionUpdate, keyType);
|
|
|
180
|
+ } else {
|
|
|
181
|
+ sendTsWsMsg(entityId, sessionId, subscriptionUpdate, keyType);
|
|
|
182
|
+ }
|
|
|
183
|
+ } else {
|
|
|
184
|
+ log.trace("[{}][{}][{}][{}] Received stale subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
|
|
|
185
|
+ }
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ private void sendLatestWsMsg(EntityId entityId, String sessionId, SubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
|
|
189
|
+ Map<String, TsValue> latestUpdate = new HashMap<>();
|
|
|
190
|
+ subscriptionUpdate.getData().forEach((k, v) -> {
|
|
|
191
|
+ Object[] data = (Object[]) v.get(0);
|
|
|
192
|
+ latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
|
|
|
193
|
+ });
|
|
|
194
|
+ EntityData entityData = getDataForEntity(entityId);
|
|
|
195
|
+ if (entityData != null && entityData.getLatest() != null) {
|
|
|
196
|
+ Map<String, TsValue> latestCtxValues = entityData.getLatest().get(keyType);
|
|
|
197
|
+ log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
|
|
198
|
+ if (latestCtxValues != null) {
|
|
|
199
|
+ latestCtxValues.forEach((k, v) -> {
|
|
|
200
|
+ TsValue update = latestUpdate.get(k);
|
|
|
201
|
+ if (update != null) {
|
|
|
202
|
+ if (update.getTs() < v.getTs()) {
|
|
|
203
|
+ log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
|
|
204
|
+ latestUpdate.remove(k);
|
|
|
205
|
+ } else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) {
|
|
|
206
|
+ log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
|
|
207
|
+ latestUpdate.remove(k);
|
|
|
208
|
+ }
|
|
|
209
|
+ }
|
|
|
210
|
+ });
|
|
|
211
|
+ //Setting new values
|
|
|
212
|
+ latestUpdate.forEach(latestCtxValues::put);
|
|
|
213
|
+ }
|
|
|
214
|
+ }
|
|
|
215
|
+ if (!latestUpdate.isEmpty()) {
|
|
|
216
|
+ Map<EntityKeyType, Map<String, TsValue>> latestMap = Collections.singletonMap(keyType, latestUpdate);
|
|
|
217
|
+ entityData = new EntityData(entityId, latestMap, null);
|
|
|
218
|
+ wsService.sendWsMsg(sessionId, new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData)));
|
|
|
219
|
+ }
|
|
|
220
|
+ }
|
|
|
221
|
+
|
|
|
222
|
+ private void sendTsWsMsg(EntityId entityId, String sessionId, SubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
|
|
223
|
+ Map<String, List<TsValue>> tsUpdate = new HashMap<>();
|
|
|
224
|
+ subscriptionUpdate.getData().forEach((k, v) -> {
|
|
|
225
|
+ Object[] data = (Object[]) v.get(0);
|
|
|
226
|
+ tsUpdate.computeIfAbsent(k, key -> new ArrayList<>()).add(new TsValue((Long) data[0], (String) data[1]));
|
|
|
227
|
+ });
|
|
|
228
|
+ EntityData entityData = getDataForEntity(entityId);
|
|
|
229
|
+ if (entityData != null && entityData.getLatest() != null) {
|
|
|
230
|
+ Map<String, TsValue> latestCtxValues = entityData.getLatest().get(keyType);
|
|
|
231
|
+ log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
|
|
232
|
+ if (latestCtxValues != null) {
|
|
|
233
|
+ latestCtxValues.forEach((k, v) -> {
|
|
|
234
|
+ List<TsValue> updateList = tsUpdate.get(k);
|
|
|
235
|
+ if (updateList != null) {
|
|
|
236
|
+ for (TsValue update : new ArrayList<>(updateList)) {
|
195
|
if (update.getTs() < v.getTs()) {
|
237
|
if (update.getTs() < v.getTs()) {
|
196
|
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
238
|
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
197
|
- latestUpdate.remove(k);
|
239
|
+ updateList.remove(update);
|
198
|
} else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) {
|
240
|
} else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) {
|
199
|
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
241
|
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
200
|
- latestUpdate.remove(k);
|
242
|
+ updateList.remove(update);
|
|
|
243
|
+ }
|
|
|
244
|
+ if (updateList.isEmpty()) {
|
|
|
245
|
+ tsUpdate.remove(k);
|
201
|
}
|
246
|
}
|
202
|
}
|
247
|
}
|
203
|
- });
|
|
|
204
|
- //Setting new values
|
|
|
205
|
- latestUpdate.forEach(latestCtxValues::put);
|
|
|
206
|
- }
|
|
|
207
|
- }
|
|
|
208
|
- if (!latestUpdate.isEmpty()) {
|
|
|
209
|
- if (resultToLatestValues) {
|
|
|
210
|
- Map<EntityKeyType, Map<String, TsValue>> latestMap = Collections.singletonMap(keyType, latestUpdate);
|
|
|
211
|
- entityData = new EntityData(entityId, latestMap, null);
|
|
|
212
|
- } else {
|
|
|
213
|
- Map<String, TsValue[]> tsMap = new HashMap<>();
|
|
|
214
|
- latestUpdate.forEach((key, tsValue) -> {
|
|
|
215
|
- tsMap.put(key, new TsValue[]{tsValue});
|
|
|
216
|
- });
|
|
|
217
|
- entityData = new EntityData(entityId, null, tsMap);
|
|
|
218
|
- }
|
|
|
219
|
- wsService.sendWsMsg(sessionId, new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData)));
|
248
|
+ }
|
|
|
249
|
+ });
|
|
|
250
|
+ //Setting new values
|
|
|
251
|
+ tsUpdate.forEach((k, v) -> {
|
|
|
252
|
+ Optional<TsValue> maxValue = v.stream().max(Comparator.comparingLong(TsValue::getTs));
|
|
|
253
|
+ maxValue.ifPresent(max -> latestCtxValues.put(k, max));
|
|
|
254
|
+ });
|
220
|
}
|
255
|
}
|
221
|
- } else {
|
|
|
222
|
- log.trace("[{}][{}][{}][{}] Received stale subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
|
256
|
+ }
|
|
|
257
|
+ if (!tsUpdate.isEmpty()) {
|
|
|
258
|
+ Map<String, TsValue[]> tsMap = new HashMap<>();
|
|
|
259
|
+ tsUpdate.forEach((key, tsValue) -> tsMap.put(key, tsValue.toArray(new TsValue[tsValue.size()])));
|
|
|
260
|
+ entityData = new EntityData(entityId, null, tsMap);
|
|
|
261
|
+ wsService.sendWsMsg(sessionId, new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData)));
|
223
|
}
|
262
|
}
|
224
|
}
|
263
|
}
|
225
|
|
264
|
|