Commit f9f6a7f45fa36eb549c66ec66095183627eae137
1 parent
0d8f9582
fix: 首页调整,获取租户、客户的统计,直接返回时间不返回时间戳了
Showing
8 changed files
with
99 additions
and
48 deletions
... | ... | @@ -10,11 +10,11 @@ import org.springframework.security.access.prepost.PreAuthorize; |
10 | 10 | import org.springframework.web.bind.annotation.*; |
11 | 11 | import org.springframework.web.context.request.async.DeferredResult; |
12 | 12 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
13 | -import org.thingsboard.server.common.data.query.TsValue; | |
14 | 13 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
15 | 14 | import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO; |
16 | 15 | import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; |
17 | 16 | import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO; |
17 | +import org.thingsboard.server.common.data.yunteng.dto.TkTsValue; | |
18 | 18 | import org.thingsboard.server.common.data.yunteng.dto.statistics.HomePageAppDTO; |
19 | 19 | import org.thingsboard.server.common.data.yunteng.enums.TrendType; |
20 | 20 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
... | ... | @@ -71,7 +71,7 @@ public class HomePageController extends BaseController { |
71 | 71 | @GetMapping("left/bottom") |
72 | 72 | @ApiOperation(value = "获取左侧底部信息") |
73 | 73 | @PreAuthorize("@check.checkPermissions({'SYS_ADMIN','PLATFORM_ADMIN','CUSTOMER_USER'},{})") |
74 | - public DeferredResult<List<TsValue>> getLeftBottomInfo( | |
74 | + public DeferredResult<List<TkTsValue>> getLeftBottomInfo( | |
75 | 75 | @RequestParam(value = "startTs") long startTs, |
76 | 76 | @RequestParam("endTs") long endTs, |
77 | 77 | @RequestParam("interval") long interval, |
... | ... | @@ -102,7 +102,7 @@ public class HomePageController extends BaseController { |
102 | 102 | @GetMapping("app") |
103 | 103 | @ApiOperation(value = "小程序首页统计信息") |
104 | 104 | public ResponseEntity<HomePageAppDTO> appStatistics(@ApiParam(value = "只取告警数据") @RequestParam("login") Boolean login) |
105 | - throws ThingsboardException, ExecutionException, InterruptedException { | |
105 | + throws ThingsboardException{ | |
106 | 106 | SecurityUser user = getCurrentUser(); |
107 | 107 | if(user == null){ |
108 | 108 | return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ... | ... |
... | ... | @@ -673,13 +673,13 @@ public class SysUserServiceImpl extends AbstractBaseService<UserMapper, SysUserE |
673 | 673 | |
674 | 674 | @Override |
675 | 675 | @Async |
676 | - public CompletableFuture<TsValue> findUsersAsyncByTs( | |
677 | - LocalDateTime startTs, LocalDateTime endTs, long ts) { | |
676 | + public CompletableFuture<TkTsValue> findUsersAsyncByTs( | |
677 | + LocalDateTime startTs, LocalDateTime endTs, String date) { | |
678 | 678 | DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
679 | 679 | String startTime = df.format(startTs); |
680 | 680 | String endTime = df.format(endTs); |
681 | 681 | List<UserDTO> list = baseMapper.findUsersAsyncByTs(startTime, endTime); |
682 | - return CompletableFuture.supplyAsync(() -> new TsValue(ts, String.valueOf(list.size()))); | |
682 | + return CompletableFuture.supplyAsync(() -> new TkTsValue(date, String.valueOf(list.size()))); | |
683 | 683 | } |
684 | 684 | |
685 | 685 | @Override | ... | ... |
... | ... | @@ -35,10 +35,9 @@ import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService; |
35 | 35 | import org.thingsboard.server.dao.yunteng.service.TkTenantService; |
36 | 36 | import org.thingsboard.server.dao.yunteng.service.TkUserService; |
37 | 37 | |
38 | -import java.time.LocalDate; | |
39 | -import java.time.LocalDateTime; | |
40 | -import java.time.LocalTime; | |
41 | -import java.time.ZoneOffset; | |
38 | +import java.time.*; | |
39 | +import java.time.format.DateTimeFormatter; | |
40 | +import java.time.temporal.TemporalAdjusters; | |
42 | 41 | import java.util.*; |
43 | 42 | import java.util.concurrent.CompletableFuture; |
44 | 43 | import java.util.concurrent.ExecutionException; |
... | ... | @@ -388,51 +387,66 @@ public class TkHomePageServiceImpl implements HomePageService { |
388 | 387 | } |
389 | 388 | |
390 | 389 | @Override |
391 | - public DeferredResult<List<TsValue>> getHomePageLeftBottomInfo( | |
390 | + public DeferredResult<List<TkTsValue>> getHomePageLeftBottomInfo( | |
392 | 391 | String customerId, |
393 | 392 | long startTs, |
394 | 393 | long endTs, |
395 | 394 | long interval, |
396 | 395 | TrendType trend, |
397 | 396 | boolean isCustomer) { |
398 | - List<CompletableFuture<TsValue>> futures = new ArrayList<>(); | |
399 | - interval = interval < 7200000 ? 7200000 : interval; | |
397 | + List<CompletableFuture<TkTsValue>> futures = new ArrayList<>(); | |
400 | 398 | long stepTs = startTs; |
399 | + boolean isYearQuery = false; | |
400 | + if((endTs - startTs)/86400000 >= 365){ | |
401 | + isYearQuery =true; | |
402 | + } | |
401 | 403 | while (stepTs < endTs) { |
402 | - long tempStartTs = stepTs; | |
403 | - long tempEndTs = stepTs + interval; | |
404 | - long ts = tempStartTs + (tempEndTs - tempStartTs) / 2; | |
405 | - LocalDateTime startTime = | |
406 | - LocalDateTime.ofEpochSecond(tempStartTs / 1000, 0, ZoneOffset.ofHours(8)); | |
407 | - LocalDateTime endTime = | |
408 | - LocalDateTime.ofEpochSecond(tempEndTs / 1000, 0, ZoneOffset.ofHours(8)); | |
409 | - CompletableFuture<TsValue> tsValueCompletableFuture = null; | |
404 | + Long tempStartTs = stepTs; | |
405 | + Long tempEndTs = stepTs + interval; | |
406 | + LocalDateTime startTime; | |
407 | + LocalDateTime endTime; | |
408 | + if(isYearQuery){ | |
409 | + //按月份过滤 | |
410 | + Instant instant = Instant.ofEpochMilli(tempStartTs); | |
411 | + LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); | |
412 | + startTime = getMonthStartTime(dateTime); | |
413 | + endTime = getNextMonthStartTime(dateTime); | |
414 | + }else{ | |
415 | + //按聚合条件过滤 | |
416 | + startTime = | |
417 | + LocalDateTime.ofEpochSecond(tempStartTs / 1000, 0, ZoneOffset.ofHours(8)); | |
418 | + endTime = | |
419 | + LocalDateTime.ofEpochSecond(tempEndTs / 1000, 0, ZoneOffset.ofHours(8)); | |
420 | + } | |
421 | + CompletableFuture<TkTsValue> tsValueCompletableFuture = null; | |
410 | 422 | // 客户查询的是告警统计 消息统计 |
411 | 423 | if (isCustomer) { |
424 | + String date = getDateByLocalTime(startTime,isYearQuery,true); | |
412 | 425 | if (trend == TrendType.CUSTOMER_ALARM_STATISTICAL) { |
413 | 426 | tsValueCompletableFuture = |
414 | 427 | findDeviceInfoByTs( |
415 | - customerId, tempStartTs, tempEndTs, ts, TrendType.CUSTOMER_ALARM_STATISTICAL); | |
428 | + customerId, tempStartTs, tempEndTs, date, TrendType.CUSTOMER_ALARM_STATISTICAL); | |
416 | 429 | } |
417 | 430 | if (trend == TrendType.CUSTOMER_MESSAGE_STATISTICAL) { |
418 | 431 | tsValueCompletableFuture = |
419 | 432 | findDeviceInfoByTs( |
420 | - customerId, tempStartTs, tempEndTs, ts, TrendType.CUSTOMER_MESSAGE_STATISTICAL); | |
433 | + customerId, tempStartTs, tempEndTs, date, TrendType.CUSTOMER_MESSAGE_STATISTICAL); | |
421 | 434 | } |
422 | 435 | } else { |
436 | + String date = getDateByLocalTime(startTime,isYearQuery,false); | |
423 | 437 | if (trend == TrendType.TENANT_TREND) { |
424 | - tsValueCompletableFuture = tenantService.findTenantsByTs(startTime, endTime, ts); | |
438 | + tsValueCompletableFuture = tenantService.findTenantsByTs(startTime, endTime, date); | |
425 | 439 | } |
426 | 440 | if (trend == TrendType.CUSTOMER_TREND) { |
427 | - tsValueCompletableFuture = tkUserService.findUsersAsyncByTs(startTime, endTime, ts); | |
441 | + tsValueCompletableFuture = tkUserService.findUsersAsyncByTs(startTime, endTime, date); | |
428 | 442 | } |
429 | 443 | } |
430 | 444 | futures.add(tsValueCompletableFuture); |
431 | 445 | stepTs = tempEndTs; |
432 | 446 | } |
433 | - final DeferredResult<List<TsValue>> deferredResult = new DeferredResult<>(); | |
447 | + final DeferredResult<List<TkTsValue>> deferredResult = new DeferredResult<>(); | |
434 | 448 | if (futures.size() > FastIotConstants.MagicNumber.ZERO) { |
435 | - ListenableFuture<List<TsValue>> listenableFuture = | |
449 | + ListenableFuture<List<TkTsValue>> listenableFuture = | |
436 | 450 | Futures.transform( |
437 | 451 | setFutures(futures), |
438 | 452 | tsValues -> { |
... | ... | @@ -765,9 +779,9 @@ public class TkHomePageServiceImpl implements HomePageService { |
765 | 779 | homePageLeftTopDTO.setCustomerInfo(baseHomePageTop); |
766 | 780 | } |
767 | 781 | |
768 | - private SettableFuture<List<TsValue>> setFutures(List<CompletableFuture<TsValue>> tsFutures) { | |
769 | - SettableFuture<List<TsValue>> listenableFuture = SettableFuture.create(); | |
770 | - CompletableFuture<List<TsValue>> entities = | |
782 | + private SettableFuture<List<TkTsValue>> setFutures(List<CompletableFuture<TkTsValue>> tsFutures) { | |
783 | + SettableFuture<List<TkTsValue>> listenableFuture = SettableFuture.create(); | |
784 | + CompletableFuture<List<TkTsValue>> entities = | |
771 | 785 | CompletableFuture.allOf(tsFutures.toArray(new CompletableFuture[tsFutures.size()])) |
772 | 786 | .thenApply( |
773 | 787 | v -> tsFutures.stream().map(CompletableFuture::join).collect(Collectors.toList())); |
... | ... | @@ -800,11 +814,11 @@ public class TkHomePageServiceImpl implements HomePageService { |
800 | 814 | return listenableFuture; |
801 | 815 | } |
802 | 816 | |
803 | - private FutureCallback<List<TsValue>> getTsValueCallback( | |
804 | - final DeferredResult<List<TsValue>> response) { | |
817 | + private FutureCallback<List<TkTsValue>> getTsValueCallback( | |
818 | + final DeferredResult<List<TkTsValue>> response) { | |
805 | 819 | return new FutureCallback<>() { |
806 | 820 | @Override |
807 | - public void onSuccess(@Nullable List<TsValue> tsValues) { | |
821 | + public void onSuccess(@Nullable List<TkTsValue> tsValues) { | |
808 | 822 | response.setResult(tsValues); |
809 | 823 | } |
810 | 824 | |
... | ... | @@ -852,14 +866,43 @@ public class TkHomePageServiceImpl implements HomePageService { |
852 | 866 | }; |
853 | 867 | } |
854 | 868 | |
855 | - private CompletableFuture<TsValue> findDeviceInfoByTs( | |
856 | - String customerId, Long startTs, Long endTs, long ts, TrendType trend) { | |
869 | + private CompletableFuture<TkTsValue> findDeviceInfoByTs( | |
870 | + String customerId, Long startTs, Long endTs, String date, TrendType trend) { | |
857 | 871 | Integer value; |
858 | 872 | if (trend == TrendType.CUSTOMER_MESSAGE_STATISTICAL) { |
859 | 873 | value = deviceMapper.findDeviceMessageInfoByTs(customerId, startTs, endTs); |
860 | 874 | } else { |
861 | 875 | value = deviceMapper.findDeviceAlarmInfoByCreatedTime(customerId, startTs, endTs); |
862 | 876 | } |
863 | - return CompletableFuture.supplyAsync(() -> new TsValue(ts, String.valueOf(value))); | |
877 | + return CompletableFuture.supplyAsync(() -> new TkTsValue(date, String.valueOf(value))); | |
878 | + } | |
879 | + | |
880 | + /** | |
881 | + * 获取本月的开始时间 | |
882 | + * @param dateTime 本月时间 | |
883 | + * @return 本月第一天时间 | |
884 | + */ | |
885 | + public LocalDateTime getMonthStartTime(LocalDateTime dateTime){ | |
886 | + // 获取本月的第一天0点的时间戳 | |
887 | + return dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0); | |
888 | + } | |
889 | + | |
890 | + /** | |
891 | + * 获取下个月的开始时间 | |
892 | + * @param dateTime 本月时间 | |
893 | + * @return 下个月第一天时间 | |
894 | + */ | |
895 | + public LocalDateTime getNextMonthStartTime(LocalDateTime dateTime){ | |
896 | + // 获取下个月的第一天 | |
897 | + LocalDateTime firstDayOfNextMonth = dateTime.with(TemporalAdjusters.firstDayOfNextMonth()).with(TemporalAdjusters.firstDayOfMonth()); | |
898 | + // 将时间设置为0点 | |
899 | + return firstDayOfNextMonth.withHour(0).withMinute(0).withSecond(0).withNano(0); | |
900 | + } | |
901 | + | |
902 | + public String getDateByLocalTime(LocalDateTime startTime,boolean isYearQuery,boolean isCustomer){ | |
903 | + String format = isYearQuery ?"yyyy-MM":"yyyy-MM-dd"; | |
904 | + format = isCustomer?"yyyy-MM-dd HH:mm:ss":format; | |
905 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); | |
906 | + return startTime.format(formatter); | |
864 | 907 | } |
865 | 908 | } | ... | ... |
... | ... | @@ -14,10 +14,10 @@ import org.springframework.stereotype.Service; |
14 | 14 | import org.springframework.transaction.annotation.Transactional; |
15 | 15 | import org.thingsboard.server.common.data.id.EntityId; |
16 | 16 | import org.thingsboard.server.common.data.id.UserId; |
17 | -import org.thingsboard.server.common.data.query.TsValue; | |
18 | 17 | import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException; |
19 | 18 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
20 | 19 | import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; |
20 | +import org.thingsboard.server.common.data.yunteng.dto.TkTsValue; | |
21 | 21 | import org.thingsboard.server.common.data.yunteng.dto.UserDTO; |
22 | 22 | import org.thingsboard.server.common.data.yunteng.dto.request.TenantReqDTO; |
23 | 23 | import org.thingsboard.server.common.data.yunteng.enums.TenantStatusEnum; |
... | ... | @@ -343,14 +343,14 @@ public class TkTenantServiceImpl extends AbstractBaseService<TenantMapper, SysTe |
343 | 343 | |
344 | 344 | @Override |
345 | 345 | @Async |
346 | - public CompletableFuture<TsValue> findTenantsByTs( | |
347 | - LocalDateTime startTs, LocalDateTime endTs, long ts) { | |
346 | + public CompletableFuture<TkTsValue> findTenantsByTs( | |
347 | + LocalDateTime startTs, LocalDateTime endTs, String date) { | |
348 | 348 | List<SysTenantEntity> tenants = |
349 | 349 | baseMapper.selectList( |
350 | 350 | new LambdaQueryWrapper<SysTenantEntity>() |
351 | 351 | .ge(SysTenantEntity::getCreateTime, startTs) |
352 | 352 | .lt(SysTenantEntity::getCreateTime, endTs)); |
353 | - return CompletableFuture.supplyAsync(() -> new TsValue(ts, String.valueOf(tenants.size()))); | |
353 | + return CompletableFuture.supplyAsync(() -> new TkTsValue(date, String.valueOf(tenants.size()))); | |
354 | 354 | } |
355 | 355 | |
356 | 356 | @Override | ... | ... |
... | ... | @@ -3,10 +3,10 @@ package org.thingsboard.server.dao.yunteng.service; |
3 | 3 | import org.springframework.web.context.request.async.DeferredResult; |
4 | 4 | import org.thingsboard.server.common.data.id.EntityId; |
5 | 5 | import org.thingsboard.server.common.data.id.TenantId; |
6 | -import org.thingsboard.server.common.data.query.TsValue; | |
7 | 6 | import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO; |
8 | 7 | import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; |
9 | 8 | import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO; |
9 | +import org.thingsboard.server.common.data.yunteng.dto.TkTsValue; | |
10 | 10 | import org.thingsboard.server.common.data.yunteng.dto.statistics.HomePageAppDTO; |
11 | 11 | import org.thingsboard.server.common.data.yunteng.enums.TrendType; |
12 | 12 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
... | ... | @@ -52,7 +52,7 @@ public interface HomePageService { |
52 | 52 | * @param trend 趋势类型 |
53 | 53 | * @return 左侧底部信息 |
54 | 54 | */ |
55 | - DeferredResult<List<TsValue>> getHomePageLeftBottomInfo( | |
55 | + DeferredResult<List<TkTsValue>> getHomePageLeftBottomInfo( | |
56 | 56 | String customerId, long startTs, long endTs, long interval, TrendType trend, boolean isCustomer); |
57 | 57 | |
58 | 58 | /** | ... | ... |
... | ... | @@ -3,8 +3,8 @@ package org.thingsboard.server.dao.yunteng.service; |
3 | 3 | |
4 | 4 | |
5 | 5 | import com.google.common.util.concurrent.ListenableFuture; |
6 | -import org.thingsboard.server.common.data.query.TsValue; | |
7 | 6 | import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; |
7 | +import org.thingsboard.server.common.data.yunteng.dto.TkTsValue; | |
8 | 8 | import org.thingsboard.server.common.data.yunteng.dto.request.TenantReqDTO; |
9 | 9 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
10 | 10 | |
... | ... | @@ -33,7 +33,7 @@ public interface TkTenantService { |
33 | 33 | |
34 | 34 | List<TenantDTO> getAllTenant(); |
35 | 35 | |
36 | - CompletableFuture<TsValue> findTenantsByTs(LocalDateTime startTs, LocalDateTime endTs,long ts); | |
36 | + CompletableFuture<TkTsValue> findTenantsByTs(LocalDateTime startTs, LocalDateTime endTs, String date); | |
37 | 37 | |
38 | 38 | List<TenantDTO> checkTenantProfileIdUsedByTenants(String tenantProfileId); |
39 | 39 | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | |
3 | 3 | import org.thingsboard.server.common.data.id.UserId; |
4 | -import org.thingsboard.server.common.data.query.TsValue; | |
4 | +import org.thingsboard.server.common.data.yunteng.dto.TkTsValue; | |
5 | 5 | import org.thingsboard.server.common.data.yunteng.dto.UserDTO; |
6 | 6 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO; |
7 | 7 | import org.thingsboard.server.common.data.yunteng.dto.UserInfoDTO; |
... | ... | @@ -133,11 +133,11 @@ public interface TkUserService { |
133 | 133 | * |
134 | 134 | * @param startTs 开始时间 |
135 | 135 | * @param endTs 结束时间 |
136 | - * @param ts 时间 | |
136 | + * @param date 时间 | |
137 | 137 | * @return 客户信息 |
138 | 138 | */ |
139 | - CompletableFuture<TsValue> findUsersAsyncByTs( | |
140 | - LocalDateTime startTs, LocalDateTime endTs, long ts); | |
139 | + CompletableFuture<TkTsValue> findUsersAsyncByTs( | |
140 | + LocalDateTime startTs, LocalDateTime endTs, String date); | |
141 | 141 | |
142 | 142 | /** |
143 | 143 | * 检查账号是否存在 | ... | ... |