|
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
4
|
import com.google.common.util.concurrent.*;
|
4
|
import com.google.common.util.concurrent.*;
|
5
|
import lombok.RequiredArgsConstructor;
|
5
|
import lombok.RequiredArgsConstructor;
|
6
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
6
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
|
|
7
|
+import org.springframework.scheduling.annotation.Async;
|
7
|
import org.springframework.stereotype.Service;
|
8
|
import org.springframework.stereotype.Service;
|
8
|
import org.springframework.web.context.request.async.DeferredResult;
|
9
|
import org.springframework.web.context.request.async.DeferredResult;
|
9
|
import org.thingsboard.server.common.data.ApiUsageState;
|
10
|
import org.thingsboard.server.common.data.ApiUsageState;
|
|
@@ -106,18 +107,15 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -106,18 +107,15 @@ public class HomePageServiceImpl implements HomePageService { |
106
|
}
|
107
|
}
|
107
|
|
108
|
|
108
|
@Override
|
109
|
@Override
|
109
|
- public HomePageRightDTO getHomePageRightInfo(Map<String, Object> queryMap) {
|
|
|
110
|
- HomePageRightDTO right = new HomePageRightDTO();
|
|
|
111
|
- YtPageData<TenantDTO> expireTenants = tenantService.getCurrentMonthExpireTenantPage(queryMap);
|
|
|
112
|
- right.setTop10(null);
|
|
|
113
|
- right.setExpireTenant(expireTenants);
|
|
|
114
|
- return right;
|
110
|
+ public YtPageData<TenantDTO> getHomePageRightInfo(Map<String, Object> queryMap) {
|
|
|
111
|
+ return tenantService.getCurrentMonthExpireTenantPage(queryMap);
|
115
|
}
|
112
|
}
|
116
|
|
113
|
|
117
|
@Override
|
114
|
@Override
|
118
|
public DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(
|
115
|
public DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(
|
119
|
long startTs, long endTs, long interval, TrendType trend) {
|
116
|
long startTs, long endTs, long interval, TrendType trend) {
|
120
|
List<CompletableFuture<TsValue>> futures = new ArrayList<>();
|
117
|
List<CompletableFuture<TsValue>> futures = new ArrayList<>();
|
|
|
118
|
+ interval = interval < 7200000 ? 7200000 : interval;
|
121
|
long stepTs = startTs;
|
119
|
long stepTs = startTs;
|
122
|
while (stepTs < endTs) {
|
120
|
while (stepTs < endTs) {
|
123
|
long tempStartTs = stepTs;
|
121
|
long tempStartTs = stepTs;
|
|
@@ -155,6 +153,70 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -155,6 +153,70 @@ public class HomePageServiceImpl implements HomePageService { |
155
|
return deferredResult;
|
153
|
return deferredResult;
|
156
|
}
|
154
|
}
|
157
|
|
155
|
|
|
|
156
|
+ @Override
|
|
|
157
|
+ public DeferredResult<List<TenantTransportMessageDTO>> getTop10() {
|
|
|
158
|
+ List<TenantDTO> tenants = tenantService.getAllTenant();
|
|
|
159
|
+ List<CompletableFuture<TenantTransportMessageDTO>> futures = new ArrayList<>();
|
|
|
160
|
+ tenants.forEach(
|
|
|
161
|
+ tenant -> {
|
|
|
162
|
+ TenantId tenantId = new TenantId(UUID.fromString(tenant.getTenantId()));
|
|
|
163
|
+ futures.add(getTransportMessageByTenantId(tenantId, tenant.getName()));
|
|
|
164
|
+ });
|
|
|
165
|
+ final DeferredResult<List<TenantTransportMessageDTO>> deferredResult = new DeferredResult<>();
|
|
|
166
|
+ if (futures.size() > FastIotConstants.MagicNumber.ZERO) {
|
|
|
167
|
+ ListenableFuture<List<TenantTransportMessageDTO>> listenableFuture =
|
|
|
168
|
+ Futures.transform(
|
|
|
169
|
+ settableFuture(futures),
|
|
|
170
|
+ transportMessage -> {
|
|
|
171
|
+ if (transportMessage != null && !transportMessage.isEmpty()) {
|
|
|
172
|
+ return transportMessage;
|
|
|
173
|
+ } else {
|
|
|
174
|
+ return null;
|
|
|
175
|
+ }
|
|
|
176
|
+ },
|
|
|
177
|
+ MoreExecutors.directExecutor());
|
|
|
178
|
+ Futures.addCallback(
|
|
|
179
|
+ listenableFuture,
|
|
|
180
|
+ getTransportMessageCallback(deferredResult),
|
|
|
181
|
+ MoreExecutors.directExecutor());
|
|
|
182
|
+ }
|
|
|
183
|
+ return deferredResult;
|
|
|
184
|
+ }
|
|
|
185
|
+
|
|
|
186
|
+ @Override
|
|
|
187
|
+ @Async
|
|
|
188
|
+ public CompletableFuture<TenantTransportMessageDTO> getTransportMessageByTenantId(
|
|
|
189
|
+ TenantId tenantId, String tenantName) {
|
|
|
190
|
+ List<EntityKey> latestValues = new ArrayList<>();
|
|
|
191
|
+ latestValues.add(new EntityKey(EntityKeyType.TIME_SERIES, "transportMsgCount"));
|
|
|
192
|
+ PageData<EntityData> pageData = queryEntityData(tenantId, null, latestValues);
|
|
|
193
|
+ Map<EntityKeyType, Map<String, TsValue>> latest = pageData.getData().get(0).getLatest();
|
|
|
194
|
+ TenantTransportMessageDTO transportMessage = new TenantTransportMessageDTO();
|
|
|
195
|
+ latest
|
|
|
196
|
+ .keySet()
|
|
|
197
|
+ .forEach(
|
|
|
198
|
+ item -> {
|
|
|
199
|
+ if (item.equals(EntityKeyType.TIME_SERIES)) {
|
|
|
200
|
+ Map<String, TsValue> tsValueMap = latest.get(item);
|
|
|
201
|
+ tsValueMap
|
|
|
202
|
+ .keySet()
|
|
|
203
|
+ .forEach(
|
|
|
204
|
+ mapKey -> {
|
|
|
205
|
+ Long value =
|
|
|
206
|
+ Long.valueOf(
|
|
|
207
|
+ tsValueMap.get(mapKey).getValue().isEmpty()
|
|
|
208
|
+ ? FastIotConstants.MagicNumber.ZERO + ""
|
|
|
209
|
+ : tsValueMap.get(mapKey).getValue());
|
|
|
210
|
+ if (mapKey.equals("transportMsgCount")) {
|
|
|
211
|
+ transportMessage.setName(tenantName);
|
|
|
212
|
+ transportMessage.setCount(value);
|
|
|
213
|
+ }
|
|
|
214
|
+ });
|
|
|
215
|
+ }
|
|
|
216
|
+ });
|
|
|
217
|
+ return CompletableFuture.supplyAsync(() -> transportMessage);
|
|
|
218
|
+ }
|
|
|
219
|
+
|
158
|
/**
|
220
|
/**
|
159
|
* 设置设备相关的数据
|
221
|
* 设置设备相关的数据
|
160
|
*
|
222
|
*
|
|
@@ -221,15 +283,6 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -221,15 +283,6 @@ public class HomePageServiceImpl implements HomePageService { |
221
|
private void setAlarmAndMessageInfo(
|
283
|
private void setAlarmAndMessageInfo(
|
222
|
String tenantId, HomePageTopMessage messageInfo, BaseHomePageTop alarm)
|
284
|
String tenantId, HomePageTopMessage messageInfo, BaseHomePageTop alarm)
|
223
|
throws ExecutionException, InterruptedException {
|
285
|
throws ExecutionException, InterruptedException {
|
224
|
- TenantId currentTenantId = new TenantId(UUID.fromString(tenantId));
|
|
|
225
|
- CustomerId customerId = new CustomerId(EntityId.NULL_UUID);
|
|
|
226
|
- EntityKey key = new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime");
|
|
|
227
|
- EntityDataSortOrder sortOrder =
|
|
|
228
|
- new EntityDataSortOrder(key, EntityDataSortOrder.Direction.DESC);
|
|
|
229
|
- EntityDataPageLink pageLink =
|
|
|
230
|
- new EntityDataPageLink(
|
|
|
231
|
- FastIotConstants.MagicNumber.ONE, FastIotConstants.MagicNumber.ZERO, null, sortOrder);
|
|
|
232
|
-
|
|
|
233
|
List<EntityKey> entityFields = new ArrayList<>();
|
286
|
List<EntityKey> entityFields = new ArrayList<>();
|
234
|
entityFields.add(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
|
287
|
entityFields.add(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
|
235
|
entityFields.add(new EntityKey(EntityKeyType.ENTITY_FIELD, "label"));
|
288
|
entityFields.add(new EntityKey(EntityKeyType.ENTITY_FIELD, "label"));
|
|
@@ -239,14 +292,9 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -239,14 +292,9 @@ public class HomePageServiceImpl implements HomePageService { |
239
|
latestValues.add(new EntityKey(EntityKeyType.TIME_SERIES, "transportDataPointsCount"));
|
292
|
latestValues.add(new EntityKey(EntityKeyType.TIME_SERIES, "transportDataPointsCount"));
|
240
|
latestValues.add(new EntityKey(EntityKeyType.TIME_SERIES, "createdAlarmsCount"));
|
293
|
latestValues.add(new EntityKey(EntityKeyType.TIME_SERIES, "createdAlarmsCount"));
|
241
|
|
294
|
|
242
|
- SingleEntityFilter singleEntityFilter = new SingleEntityFilter();
|
295
|
+ TenantId currentTenantId = new TenantId(UUID.fromString(tenantId));
|
243
|
ApiUsageState apiUsageState = apiUsageStateService.findTenantApiUsageState(currentTenantId);
|
296
|
ApiUsageState apiUsageState = apiUsageStateService.findTenantApiUsageState(currentTenantId);
|
244
|
- singleEntityFilter.setSingleEntity(apiUsageState.getId());
|
|
|
245
|
-
|
|
|
246
|
- EntityDataQuery query =
|
|
|
247
|
- new EntityDataQuery(singleEntityFilter, pageLink, entityFields, latestValues, null);
|
|
|
248
|
- PageData<EntityData> pageData =
|
|
|
249
|
- entityService.findEntityDataByQuery(currentTenantId, customerId, query);
|
297
|
+ PageData<EntityData> pageData = queryEntityData(currentTenantId, entityFields, latestValues);
|
250
|
Map<EntityKeyType, Map<String, TsValue>> latest = pageData.getData().get(0).getLatest();
|
298
|
Map<EntityKeyType, Map<String, TsValue>> latest = pageData.getData().get(0).getLatest();
|
251
|
latest
|
299
|
latest
|
252
|
.keySet()
|
300
|
.keySet()
|
|
@@ -335,6 +383,24 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -335,6 +383,24 @@ public class HomePageServiceImpl implements HomePageService { |
335
|
}
|
383
|
}
|
336
|
});
|
384
|
});
|
337
|
}
|
385
|
}
|
|
|
386
|
+
|
|
|
387
|
+ private PageData<EntityData> queryEntityData(
|
|
|
388
|
+ TenantId currentTenantId, List<EntityKey> entityFields, List<EntityKey> latestValues) {
|
|
|
389
|
+ CustomerId customerId = new CustomerId(EntityId.NULL_UUID);
|
|
|
390
|
+ EntityKey key = new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime");
|
|
|
391
|
+ EntityDataSortOrder sortOrder =
|
|
|
392
|
+ new EntityDataSortOrder(key, EntityDataSortOrder.Direction.DESC);
|
|
|
393
|
+ EntityDataPageLink pageLink =
|
|
|
394
|
+ new EntityDataPageLink(
|
|
|
395
|
+ FastIotConstants.MagicNumber.ONE, FastIotConstants.MagicNumber.ZERO, null, sortOrder);
|
|
|
396
|
+ SingleEntityFilter singleEntityFilter = new SingleEntityFilter();
|
|
|
397
|
+ ApiUsageState apiUsageState = apiUsageStateService.findTenantApiUsageState(currentTenantId);
|
|
|
398
|
+ singleEntityFilter.setSingleEntity(apiUsageState.getId());
|
|
|
399
|
+
|
|
|
400
|
+ EntityDataQuery query =
|
|
|
401
|
+ new EntityDataQuery(singleEntityFilter, pageLink, entityFields, latestValues, null);
|
|
|
402
|
+ return entityService.findEntityDataByQuery(currentTenantId, customerId, query);
|
|
|
403
|
+ }
|
338
|
/**
|
404
|
/**
|
339
|
* 设置客户返回信息
|
405
|
* 设置客户返回信息
|
340
|
*
|
406
|
*
|
|
@@ -378,6 +444,24 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -378,6 +444,24 @@ public class HomePageServiceImpl implements HomePageService { |
378
|
return listenableFuture;
|
444
|
return listenableFuture;
|
379
|
}
|
445
|
}
|
380
|
|
446
|
|
|
|
447
|
+ private SettableFuture<List<TenantTransportMessageDTO>> settableFuture(
|
|
|
448
|
+ List<CompletableFuture<TenantTransportMessageDTO>> tsFutures) {
|
|
|
449
|
+ SettableFuture<List<TenantTransportMessageDTO>> listenableFuture = SettableFuture.create();
|
|
|
450
|
+ CompletableFuture<List<TenantTransportMessageDTO>> entities =
|
|
|
451
|
+ CompletableFuture.allOf(tsFutures.toArray(new CompletableFuture[tsFutures.size()]))
|
|
|
452
|
+ .thenApply(
|
|
|
453
|
+ v -> tsFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
|
|
|
454
|
+ entities.whenComplete(
|
|
|
455
|
+ (tsValues, throwable) -> {
|
|
|
456
|
+ if (throwable != null) {
|
|
|
457
|
+ listenableFuture.setException(throwable);
|
|
|
458
|
+ } else {
|
|
|
459
|
+ listenableFuture.set(tsValues);
|
|
|
460
|
+ }
|
|
|
461
|
+ });
|
|
|
462
|
+ return listenableFuture;
|
|
|
463
|
+ }
|
|
|
464
|
+
|
381
|
private FutureCallback<List<TsValue>> getTsValueCallback(
|
465
|
private FutureCallback<List<TsValue>> getTsValueCallback(
|
382
|
final DeferredResult<List<TsValue>> response) {
|
466
|
final DeferredResult<List<TsValue>> response) {
|
383
|
return new FutureCallback<>() {
|
467
|
return new FutureCallback<>() {
|
|
@@ -392,4 +476,41 @@ public class HomePageServiceImpl implements HomePageService { |
|
@@ -392,4 +476,41 @@ public class HomePageServiceImpl implements HomePageService { |
392
|
}
|
476
|
}
|
393
|
};
|
477
|
};
|
394
|
}
|
478
|
}
|
|
|
479
|
+
|
|
|
480
|
+ private FutureCallback<List<TenantTransportMessageDTO>> getTransportMessageCallback(
|
|
|
481
|
+ final DeferredResult<List<TenantTransportMessageDTO>> response) {
|
|
|
482
|
+ return new FutureCallback<>() {
|
|
|
483
|
+ @Override
|
|
|
484
|
+ public void onSuccess(@Nullable List<TenantTransportMessageDTO> values) {
|
|
|
485
|
+ // sort
|
|
|
486
|
+ if (null != values && values.size() > FastIotConstants.MagicNumber.ZERO) {
|
|
|
487
|
+ int length = values.size() - FastIotConstants.MagicNumber.ONE;
|
|
|
488
|
+ for (int i = FastIotConstants.MagicNumber.ZERO; i < length; i++) {
|
|
|
489
|
+ for (int j = FastIotConstants.MagicNumber.ZERO; j < length - i; j++) {
|
|
|
490
|
+ if (values.get(j).getCount()
|
|
|
491
|
+ < values.get(j + FastIotConstants.MagicNumber.ONE).getCount()) {
|
|
|
492
|
+ String name = values.get(j).getName();
|
|
|
493
|
+ Long count = values.get(j).getCount();
|
|
|
494
|
+ values.get(j).setName(values.get(j + FastIotConstants.MagicNumber.ONE).getName());
|
|
|
495
|
+ values.get(j).setCount(values.get(j + FastIotConstants.MagicNumber.ONE).getCount());
|
|
|
496
|
+ values.get(j + FastIotConstants.MagicNumber.ONE).setName(name);
|
|
|
497
|
+ values.get(j + FastIotConstants.MagicNumber.ONE).setCount(count);
|
|
|
498
|
+ }
|
|
|
499
|
+ }
|
|
|
500
|
+ }
|
|
|
501
|
+ values =
|
|
|
502
|
+ values.size() > FastIotConstants.MagicNumber.TEN
|
|
|
503
|
+ ? values.subList(
|
|
|
504
|
+ FastIotConstants.MagicNumber.ZERO, FastIotConstants.MagicNumber.TEN)
|
|
|
505
|
+ : values;
|
|
|
506
|
+ }
|
|
|
507
|
+ response.setResult(values);
|
|
|
508
|
+ }
|
|
|
509
|
+
|
|
|
510
|
+ @Override
|
|
|
511
|
+ public void onFailure(Throwable throwable) {
|
|
|
512
|
+ response.setResult(null);
|
|
|
513
|
+ }
|
|
|
514
|
+ };
|
|
|
515
|
+ }
|
395
|
} |
516
|
} |