Commit 597a396fc73f0d8fcddb6c564a603bb8f5839160
Merge branch 'fix/i18n-openapi' into 'master_dev'
perf: 调整openapi调用趋势查询 See merge request yunteng/thingskit!442
Showing
8 changed files
with
64 additions
and
38 deletions
... | ... | @@ -75,7 +75,7 @@ public class TkOpenApiRecordController extends BaseController { |
75 | 75 | @GetMapping("/getClassify") |
76 | 76 | @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{})") |
77 | 77 | @ApiImplicitParams(value = { |
78 | - @ApiImplicitParam(name = "type", value = "统计类型 hour按小时;week按7天;month按30天" ,required = true) | |
78 | + @ApiImplicitParam(name = "type", value = "统计类型 hour按小时;day按1天;week按7天;month按30天" ,required = true) | |
79 | 79 | }) |
80 | 80 | public ResponseEntity<List<OpenApiRecordClassifyAllDTO>> getClassify( |
81 | 81 | @RequestParam(value = "type" ) String type | ... | ... |
... | ... | @@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModelProperty; |
4 | 4 | import lombok.Data; |
5 | 5 | import org.thingsboard.server.common.data.yunteng.enums.ApplicationApiEnum; |
6 | 6 | |
7 | +import java.sql.Timestamp; | |
7 | 8 | import java.time.LocalDateTime; |
8 | 9 | |
9 | 10 | @Data |
... | ... | @@ -13,7 +14,7 @@ public class OpenApiRecordClassifyDTO { |
13 | 14 | private ApplicationApiEnum classify; |
14 | 15 | |
15 | 16 | @ApiModelProperty(value = "时间") |
16 | - private LocalDateTime time; | |
17 | + private String time; | |
17 | 18 | |
18 | 19 | @ApiModelProperty(value = "调用数量") |
19 | 20 | private Integer count; | ... | ... |
... | ... | @@ -17,7 +17,9 @@ import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
17 | 17 | import org.thingsboard.server.dao.exception.DataValidationException; |
18 | 18 | import org.thingsboard.server.dao.yunteng.entities.TkOpenApiEntity; |
19 | 19 | import org.thingsboard.server.dao.yunteng.mapper.OpenApiMapper; |
20 | +import org.thingsboard.server.dao.yunteng.mapper.OpenApiRecordMapper; | |
20 | 21 | import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; |
22 | +import org.thingsboard.server.dao.yunteng.service.OpenApiRecordService; | |
21 | 23 | import org.thingsboard.server.dao.yunteng.service.OpenApiService; |
22 | 24 | |
23 | 25 | import java.util.*; |
... | ... | @@ -29,6 +31,8 @@ import java.util.stream.Collectors; |
29 | 31 | public class OpenApiImpl extends AbstractBaseService<OpenApiMapper, TkOpenApiEntity> |
30 | 32 | implements OpenApiService { |
31 | 33 | |
34 | + OpenApiRecordMapper openApiRecordMapper; | |
35 | + | |
32 | 36 | |
33 | 37 | @Override |
34 | 38 | public OpenApiDTO getDto(String id) { |
... | ... | @@ -85,7 +89,9 @@ public class OpenApiImpl extends AbstractBaseService<OpenApiMapper, TkOpenApiEnt |
85 | 89 | NOT_HAVE_PERMISSION.getI18nCode()))); |
86 | 90 | } |
87 | 91 | }); |
88 | - return baseMapper.deleteBatchIds(apiIds) > 0; | |
92 | + baseMapper.deleteBatchIds(apiIds); | |
93 | + openApiRecordMapper.deleteByAppIds(apiIds); | |
94 | + return true; | |
89 | 95 | } |
90 | 96 | |
91 | 97 | @Override | ... | ... |
... | ... | @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; |
8 | 8 | import org.springframework.stereotype.Service; |
9 | 9 | import org.springframework.transaction.annotation.Transactional; |
10 | 10 | import org.thingsboard.server.common.data.yunteng.dto.*; |
11 | +import org.thingsboard.server.common.data.yunteng.utils.DateTimeUtils; | |
11 | 12 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
12 | 13 | import org.thingsboard.server.dao.yunteng.entities.TkApplicationEntity; |
13 | 14 | import org.thingsboard.server.dao.yunteng.entities.TkOpenApiRecordEntity; |
... | ... | @@ -16,6 +17,7 @@ import org.thingsboard.server.dao.yunteng.mapper.OpenApiRecordMapper; |
16 | 17 | import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; |
17 | 18 | import org.thingsboard.server.dao.yunteng.service.OpenApiRecordService; |
18 | 19 | |
20 | +import java.sql.Time; | |
19 | 21 | import java.util.ArrayList; |
20 | 22 | import java.util.List; |
21 | 23 | import java.util.Map; |
... | ... | @@ -28,6 +30,7 @@ public class OpenApiRecordImpl extends AbstractBaseService<OpenApiRecordMapper, |
28 | 30 | implements OpenApiRecordService { |
29 | 31 | |
30 | 32 | private final ApplicationMapper applicationMapper; |
33 | + private final long HOUR_TIMESTAMP=1000L*60L*60L; | |
31 | 34 | @Override |
32 | 35 | public OpenApiRecordDTO getDto(String id) { |
33 | 36 | TkOpenApiRecordEntity entity = baseMapper.selectById(id); |
... | ... | @@ -106,7 +109,7 @@ public class OpenApiRecordImpl extends AbstractBaseService<OpenApiRecordMapper, |
106 | 109 | |
107 | 110 | @Override |
108 | 111 | public TkPageData<OpenApiRecordTopDTO> getPage(String tenantId,Integer page,Integer pageSize) { |
109 | - List<OpenApiRecordTopDTO> tops = baseMapper.getTop(tenantId,page,pageSize); | |
112 | + List<OpenApiRecordTopDTO> tops = baseMapper.getTop(tenantId,page<1?1:page,pageSize); | |
110 | 113 | Long count = applicationMapper.selectCount(new LambdaQueryWrapper<TkApplicationEntity>() |
111 | 114 | .eq(TkApplicationEntity::getTenantId, tenantId)); |
112 | 115 | return new TkPageData<>(tops, count); |
... | ... | @@ -114,13 +117,24 @@ public class OpenApiRecordImpl extends AbstractBaseService<OpenApiRecordMapper, |
114 | 117 | |
115 | 118 | @Override |
116 | 119 | public List<OpenApiRecordClassifyAllDTO> getClassify(String tenantId,String type) { |
117 | - List<OpenApiRecordClassifyDTO> tops = baseMapper.getClassify(tenantId,type); | |
120 | + //value = "统计类型 hour按小时;day按1天;week按7天;month按30天" ,required = true) | |
121 | + java.util.Date endTime=new java.util.Date(); | |
122 | + java.util.Date startTime=null; | |
123 | + switch (type){ | |
124 | + case "hour":startTime=new java.util.Date(endTime.getTime()- HOUR_TIMESTAMP);break; | |
125 | + case "day":startTime =new java.util.Date(endTime.getTime()- HOUR_TIMESTAMP*24L);break; | |
126 | + case "week":startTime=new java.util.Date(endTime.getTime()- HOUR_TIMESTAMP*24L*7L);break; | |
127 | + case "month":startTime=new java.util.Date(endTime.getTime()- HOUR_TIMESTAMP*24L*30L);break; | |
128 | + } | |
129 | + | |
130 | + List<OpenApiRecordClassifyDTO> tops = baseMapper.getClassify(tenantId,type,startTime,endTime); | |
118 | 131 | List<OpenApiRecordClassifyAllDTO> allList =new ArrayList<>(); |
119 | 132 | if(!tops.isEmpty()){ |
120 | 133 | for (OpenApiRecordClassifyDTO entity: tops) { |
134 | + if(entity==null){continue;} | |
121 | 135 | // 查找 listDto 中是否已有相同 enum 的 entity |
122 | 136 | OpenApiRecordClassifyAllDTO existingDto = allList.stream() |
123 | - .filter(dto -> dto.getClassify().name().equals(entity.getClassify().name())) | |
137 | + .filter(dto -> entity.getClassify()!=null && dto.getClassify().name().equals(entity.getClassify().name())) | |
124 | 138 | .findFirst() |
125 | 139 | .orElse(null); |
126 | 140 | if (existingDto != null) { | ... | ... |
... | ... | @@ -15,8 +15,7 @@ public interface OpenApiRecordMapper extends BaseMapper<TkOpenApiRecordEntity> { |
15 | 15 | |
16 | 16 | List<OpenApiRecordTopDTO> getTop(@Param("tenantId") String tenantId,@Param("page")Integer page,@Param("pageSize")Integer pageSize); |
17 | 17 | |
18 | - List<OpenApiRecordClassifyDTO> getClassify(@Param("tenantId") String tenantId, | |
19 | - @Param("type") String type); | |
18 | + List<OpenApiRecordClassifyDTO> getClassify(@Param("tenantId") String tenantId,@Param("type") String type,@Param("startTime") java.util.Date startTime,@Param("endTime") java.util.Date endTime); | |
20 | 19 | |
21 | 20 | int deleteByAppIds(@Param("applicationIds") Collection<String> applicationId); |
22 | 21 | } | ... | ... |
... | ... | @@ -23,7 +23,7 @@ |
23 | 23 | AND tenant_id = '13814000-1dd2-11b2-8080-808080808080' AND status = 1 |
24 | 24 | OR tenant_id = #{tenantId} |
25 | 25 | </if> |
26 | - <if test="status !=null and status !=''"> | |
26 | + <if test="status !=null"> | |
27 | 27 | AND status = #{status} |
28 | 28 | </if> |
29 | 29 | <if test="dictItemId !=null and dictItemId !=''"> | ... | ... |
... | ... | @@ -14,7 +14,6 @@ |
14 | 14 | <result property="classify" column="classify"/> |
15 | 15 | <result property="time" column="time_period"/> |
16 | 16 | <result property="count" column="call_count"/> |
17 | - | |
18 | 17 | </resultMap> |
19 | 18 | |
20 | 19 | <select id="getTop" resultMap="DTOMap"> |
... | ... | @@ -35,34 +34,37 @@ |
35 | 34 | </select> |
36 | 35 | |
37 | 36 | <select id="getClassify" resultMap="map"> |
38 | - SELECT | |
39 | - classify, | |
40 | - <if test="type == 'hour'"> | |
41 | - DATE_TRUNC('hour', request_time) AS time_period, | |
42 | - </if> | |
43 | - <if test="type == 'week'"> | |
44 | - DATE_TRUNC('week', request_time) AS time_period, | |
45 | - </if> | |
46 | - <if test="type == 'month'"> | |
47 | - DATE_TRUNC('month', request_time) AS time_period, | |
48 | - </if> | |
49 | - COUNT(*) AS call_count | |
50 | - FROM | |
51 | - tk_open_api_record | |
52 | - WHERE tenant_id = #{tenantId} | |
53 | - GROUP BY | |
54 | - classify, | |
55 | - <if test="type == 'hour'"> | |
56 | - DATE_TRUNC('hour', request_time) | |
57 | - </if> | |
58 | - <if test="type == 'week'"> | |
59 | - DATE_TRUNC('week', request_time) | |
60 | - </if> | |
61 | - <if test="type == 'month'"> | |
62 | - DATE_TRUNC('month', request_time) | |
63 | - </if> | |
64 | - ORDER BY | |
65 | - time_period | |
37 | + SELECT foo.classify,foo.time_period,count(foo.time_period) as call_count from ( | |
38 | + | |
39 | + SELECT | |
40 | + classify, | |
41 | + | |
42 | + concat( | |
43 | + | |
44 | + EXTRACT(YEAR FROM request_time), '-', | |
45 | + EXTRACT(MONTH FROM request_time),'-', | |
46 | + EXTRACT(DAY FROM request_time) | |
47 | + <choose> | |
48 | + <when test="type == 'hour'"> | |
49 | + , ' ', | |
50 | + EXTRACT(HOUR FROM request_time), ':', | |
51 | + floor( EXTRACT(MINUTE FROM request_time) / 5 ) * 5 | |
52 | + </when> | |
53 | + <when test="type == 'day'"> | |
54 | + , ' ', | |
55 | + floor( EXTRACT(HOUR FROM request_time) / 2 ) * 2, ':', | |
56 | + '00' | |
57 | + </when> | |
58 | + | |
59 | + </choose> | |
60 | + ) | |
61 | + as time_period | |
62 | + FROM tk_open_api_record | |
63 | + WHERE tenant_id = #{tenantId} AND request_time <![CDATA[ >= ]]> #{startTime} AND request_time <![CDATA[ <= ]]> #{endTime} | |
64 | + | |
65 | + ) as foo | |
66 | + GROUP BY foo.classify,foo.time_period | |
67 | + ORDER BY foo.time_period; | |
66 | 68 | </select> |
67 | 69 | |
68 | 70 | <delete id="deleteByAppIds"> | ... | ... |