Commit 5f65ac838eca92a554a39bf31e46c251a21f1e31

Authored by 黄 x
1 parent e2a5cb43

fix: 修改系统管理员情况下Top10的排名查询及过期租户分页查询返回

@@ -3,16 +3,15 @@ package org.thingsboard.server.controller.yunteng; @@ -3,16 +3,15 @@ package org.thingsboard.server.controller.yunteng;
3 import io.swagger.annotations.Api; 3 import io.swagger.annotations.Api;
4 import io.swagger.annotations.ApiOperation; 4 import io.swagger.annotations.ApiOperation;
5 import lombok.RequiredArgsConstructor; 5 import lombok.RequiredArgsConstructor;
  6 +import org.springframework.http.ResponseEntity;
  7 +import org.springframework.security.access.prepost.PreAuthorize;
6 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.GetMapping;
7 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.bind.annotation.RestController;
10 import org.springframework.web.context.request.async.DeferredResult; 12 import org.springframework.web.context.request.async.DeferredResult;
11 import org.thingsboard.server.common.data.query.TsValue; 13 import org.thingsboard.server.common.data.query.TsValue;
12 -import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftBottomDTO;  
13 -import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;  
14 -import org.thingsboard.server.common.data.yunteng.dto.HomePageRightDTO;  
15 -import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; 14 +import org.thingsboard.server.common.data.yunteng.dto.*;
16 import org.thingsboard.server.common.data.yunteng.enums.TrendType; 15 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
17 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; 16 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
18 import org.thingsboard.server.controller.BaseController; 17 import org.thingsboard.server.controller.BaseController;
@@ -51,23 +50,32 @@ public class HomePageController extends BaseController { @@ -51,23 +50,32 @@ public class HomePageController extends BaseController {
51 return null; 50 return null;
52 } 51 }
53 52
54 - @GetMapping("right")  
55 - @ApiOperation(value = "获取右侧信息")  
56 - public HomePageRightDTO getRightTopInfo( 53 + @GetMapping("right/overdue")
  54 + @ApiOperation(value = "获取右侧过期租户信息")
  55 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')")
  56 + public ResponseEntity<YtPageData<TenantDTO>> getRightTopInfo(
57 @RequestParam(PAGE) int page, @RequestParam(PAGE_SIZE) int pageSize) { 57 @RequestParam(PAGE) int page, @RequestParam(PAGE_SIZE) int pageSize) {
58 HashMap<String, Object> queryMap = new HashMap<>(); 58 HashMap<String, Object> queryMap = new HashMap<>();
59 queryMap.put(PAGE_SIZE, pageSize); 59 queryMap.put(PAGE_SIZE, pageSize);
60 queryMap.put(PAGE, page); 60 queryMap.put(PAGE, page);
61 - return homePageService.getHomePageRightInfo(queryMap); 61 + return ResponseEntity.ok(homePageService.getHomePageRightInfo(queryMap));
  62 + }
  63 +
  64 + @GetMapping("right/top10")
  65 + @ApiOperation(value = "获取右侧Top10")
  66 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')")
  67 + public DeferredResult<List<TenantTransportMessageDTO>> getTop10() {
  68 + return homePageService.getTop10();
62 } 69 }
63 70
64 @GetMapping("left/bottom") 71 @GetMapping("left/bottom")
65 @ApiOperation(value = "获取左侧底部信息") 72 @ApiOperation(value = "获取左侧底部信息")
  73 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')")
66 public DeferredResult<List<TsValue>> getLeftBottomInfo( 74 public DeferredResult<List<TsValue>> getLeftBottomInfo(
67 @RequestParam(value = "startTs") long startTs, 75 @RequestParam(value = "startTs") long startTs,
68 @RequestParam("endTs") long endTs, 76 @RequestParam("endTs") long endTs,
69 @RequestParam("interval") long interval, 77 @RequestParam("interval") long interval,
70 @RequestParam("trend") TrendType trend) { 78 @RequestParam("trend") TrendType trend) {
71 - return homePageService.getHomePageLeftBottomInfo(startTs, endTs, interval,trend); 79 + return homePageService.getHomePageLeftBottomInfo(startTs, endTs, interval, trend);
72 } 80 }
73 } 81 }
@@ -13,6 +13,7 @@ public interface FastIotConstants { @@ -13,6 +13,7 @@ public interface FastIotConstants {
13 class MagicNumber{ 13 class MagicNumber{
14 public static final int ZERO = 0; 14 public static final int ZERO = 0;
15 public static final int ONE = 1; 15 public static final int ONE = 1;
  16 + public static final int TEN = 10;
16 } 17 }
17 18
18 String MOBILE = 19 String MOBILE =
@@ -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 }
1 package org.thingsboard.server.dao.yunteng.service; 1 package org.thingsboard.server.dao.yunteng.service;
2 2
3 import org.springframework.web.context.request.async.DeferredResult; 3 import org.springframework.web.context.request.async.DeferredResult;
  4 +import org.thingsboard.server.common.data.id.TenantId;
  5 +import org.thingsboard.server.common.data.query.EntityKey;
4 import org.thingsboard.server.common.data.query.TsValue; 6 import org.thingsboard.server.common.data.query.TsValue;
5 -import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftBottomDTO;  
6 import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO; 7 import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
7 import org.thingsboard.server.common.data.yunteng.dto.HomePageRightDTO; 8 import org.thingsboard.server.common.data.yunteng.dto.HomePageRightDTO;
  9 +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO;
  10 +import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO;
8 import org.thingsboard.server.common.data.yunteng.enums.TrendType; 11 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
  12 +import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
9 13
10 import java.util.List; 14 import java.util.List;
11 import java.util.Map; 15 import java.util.Map;
  16 +import java.util.concurrent.CompletableFuture;
12 import java.util.concurrent.ExecutionException; 17 import java.util.concurrent.ExecutionException;
13 18
14 public interface HomePageService { 19 public interface HomePageService {
@@ -36,7 +41,7 @@ public interface HomePageService { @@ -36,7 +41,7 @@ public interface HomePageService {
36 * @param queryMap 查询条件 41 * @param queryMap 查询条件
37 * @return 右侧信息 42 * @return 右侧信息
38 */ 43 */
39 - HomePageRightDTO getHomePageRightInfo(Map<String, Object> queryMap); 44 + YtPageData<TenantDTO> getHomePageRightInfo(Map<String, Object> queryMap);
40 45
41 /** 46 /**
42 * 获取首页左侧底部信息 47 * 获取首页左侧底部信息
@@ -48,4 +53,18 @@ public interface HomePageService { @@ -48,4 +53,18 @@ public interface HomePageService {
48 * @return 左侧底部信息 53 * @return 左侧底部信息
49 */ 54 */
50 DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(long startTs, long endTs, long interval, TrendType trend); 55 DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(long startTs, long endTs, long interval, TrendType trend);
  56 +
  57 + /**
  58 + * 获取首页TOP10
  59 + * @return top10
  60 + */
  61 + DeferredResult<List<TenantTransportMessageDTO>> getTop10();
  62 +
  63 + /**
  64 + * 获取租户的传输信息
  65 + * @param tenantId 租户ID
  66 + * @param tenantName 租户姓名
  67 + * @return 传输信息
  68 + */
  69 + CompletableFuture<TenantTransportMessageDTO> getTransportMessageByTenantId(TenantId tenantId,String tenantName);
51 } 70 }