refactor(任务中心): 当天执行任务查询优化

--bug=1021075 --user=赵勇 【任务中心】/api/task/center/list/1/10 接口只有10条数据时查询也需要10-20s https://www.tapd.cn/55049933/s/1317861
This commit is contained in:
fit2-zhao 2022-12-21 10:29:12 +08:00 committed by fit2-zhao
parent 1c73442c54
commit 4ddbfe130a
5 changed files with 58 additions and 19 deletions

View File

@ -2,12 +2,23 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.metersphere.base.mapper.ext.BaseTaskMapper">
<select id="getApiTasks" resultType="io.metersphere.task.dto.TaskCenterDTO" parameterType="java.lang.String">
SELECT t.id,t.name,'API' AS executionModule, t.report_type, ifnull(t2.name,'LOCAL') AS actuator, t1.`name` AS
executor,t.create_time AS executionTime, ifnull(t.trigger_mode,'MANUAL') AS triggerMode
,ifnull(t.status,'Saved') AS executionStatus
FROM api_definition_exec_result t INNER JOIN `user` t1 ON t.user_id = t1.id LEFT JOIN test_resource_pool t2 ON
t.actuator = t2.id
WHERE to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) AND t.project_id IN
SELECT
t.id,
t.NAME,
'API' AS executionModule,
t.report_type,
ifnull( t2.NAME, 'LOCAL' ) AS actuator,
t1.`name` AS executor,
t.create_time AS executionTime,
ifnull( t.trigger_mode, 'MANUAL' ) AS triggerMode,
ifnull( t.STATUS, 'Saved' ) AS executionStatus
FROM
api_definition_exec_result t
INNER JOIN `user` t1 ON t.user_id = t1.id
LEFT JOIN test_resource_pool t2 ON t.actuator = t2.id
WHERE
t.create_time BETWEEN #{request.startTime} and #{request.endTime}
AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
@ -38,7 +49,7 @@
executionTime, t.trigger_mode AS triggerMode ,t.status AS executionStatus
FROM api_scenario_report t INNER JOIN `user` t1 ON t.user_id = t1.id LEFT JOIN test_resource_pool t2 ON
t.actuator = t2.id
WHERE to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) AND t.execute_type !='Debug' AND t.execute_type
WHERE t.create_time BETWEEN #{request.startTime} and #{request.endTime} AND t.execute_type !='Debug' AND t.execute_type
!='Marge' AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
@ -68,7 +79,7 @@
executionStatus
FROM load_test_report t INNER JOIN `user` t1 ON t.user_id = t1.id LEFT JOIN test_resource_pool t2 ON
t.test_resource_pool_id = t2.id
WHERE to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) AND t.project_id IN
WHERE t.create_time BETWEEN #{request.startTime} and #{request.endTime} AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
@ -97,7 +108,7 @@
executionTime, t.trigger_mode AS triggerMode ,t.status AS executionStatus
FROM ui_scenario_report t INNER JOIN `user` t1 ON t.user_id = t1.id LEFT JOIN test_resource_pool t2 ON
t.actuator = t2.id
WHERE to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) AND t.execute_type !='Debug' AND t.execute_type
WHERE t.create_time BETWEEN #{request.startTime} and #{request.endTime} AND t.execute_type !='Debug' AND t.execute_type
!='Marge' AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
@ -140,7 +151,7 @@
LEFT JOIN `user` t1 ON t.user_id = t1.id
LEFT JOIN test_resource_pool t2 ON t.actuator = t2.id
WHERE
to_days(FROM_UNIXTIME( t.create_time / 1000 ))= to_days(now())
t.create_time BETWEEN #{request.startTime} and #{request.endTime}
AND t.execute_type != 'Debug'
AND t.execute_type != 'Marge'
AND t.project_id IN
@ -173,7 +184,7 @@
LEFT JOIN `user` t1 ON t.user_id = t1.id
LEFT JOIN test_resource_pool t2 ON t.actuator = t2.id
WHERE
to_days( FROM_UNIXTIME( t.create_time / 1000 ))= to_days(now())
t.create_time BETWEEN #{request.startTime} and #{request.endTime}
AND t.execute_type != 'Debug'
AND t.execute_type != 'Marge'
AND t.project_id IN
@ -207,7 +218,7 @@
LEFT JOIN `user` t1 ON t.user_id = t1.id
LEFT JOIN test_resource_pool t2 ON t.actuator = t2.id
WHERE
to_days( FROM_UNIXTIME( t.create_time / 1000 ))= to_days(now())
t.create_time BETWEEN #{request.startTime} and #{request.endTime}
AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
@ -239,7 +250,7 @@
LEFT JOIN `user` t1 ON t.user_id = t1.id
LEFT JOIN test_resource_pool t2 ON t.test_resource_pool_id = t2.id
WHERE
to_days(FROM_UNIXTIME( t.create_time / 1000 ))= to_days(now())
t.create_time BETWEEN #{request.startTime} and #{request.endTime}
AND t.project_id IN
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}

View File

@ -126,4 +126,24 @@ public class DateUtils {
public static Date getDayStartTime(Date time) throws Exception {
return getDate(getDateString(time));
}
public static long getDailyStartTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static long getDailyEndTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
}
}

View File

@ -3,6 +3,7 @@ package io.metersphere.task.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.commons.utils.CronUtils;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.dto.TaskInfoResult;
@ -27,6 +28,8 @@ public class TaskController {
@PostMapping("/list/{goPage}/{pageSize}")
public Pager<List<TaskCenterDTO>> getTasks(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody TaskCenterRequest request) {
request.setProjects(taskService.getOwnerProjectIds(request.getUserId()));
request.setStartTime(DateUtils.getDailyStartTime());
request.setEndTime(DateUtils.getDailyEndTime());
request.setGoPage(goPage);
request.setPageSize(pageSize);
return taskService.getTasks(request);
@ -48,8 +51,8 @@ public class TaskController {
}
@PostMapping("/runningTask/{projectID}/{goPage}/{pageSize}")
public Pager<List<TaskInfoResult> >runningTask(@PathVariable String projectID, @PathVariable int goPage, @PathVariable int pageSize,
@RequestBody BaseQueryRequest request) {
public Pager<List<TaskInfoResult>> runningTask(@PathVariable String projectID, @PathVariable int goPage, @PathVariable int pageSize,
@RequestBody BaseQueryRequest request) {
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
List<TaskInfoResult> resultList = taskService.findRunningTaskInfoByProjectID(projectID, request);
int dataIndex = 1;

View File

@ -33,4 +33,9 @@ public class TaskCenterRequest {
private boolean hasLicense;
List<String> projects;
private long startTime;
private long endTime;
}

View File

@ -7,6 +7,7 @@ import io.metersphere.base.mapper.ext.BaseScheduleMapper;
import io.metersphere.base.mapper.ext.BaseTaskMapper;
import io.metersphere.commons.constants.MicroServiceName;
import io.metersphere.commons.constants.TaskCenterType;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
@ -24,10 +25,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -106,6 +104,8 @@ public class TaskService {
public TaskStatisticsDTO getRunningTasks(TaskCenterRequest request) {
request.setProjects(this.getOwnerProjectIds(request.getUserId()));
request.setStartTime(DateUtils.getDailyStartTime());
request.setEndTime(DateUtils.getDailyEndTime());
if (CollectionUtils.isEmpty(request.getProjects())) {
return new TaskStatisticsDTO();
}