fix(系统设置): 操作日志加载时间过长

--bug=1013896 --user=李玉号
【系统设置】66环境系统-操作日志点击查询数据加载时间过长
https://www.tapd.cn/55049933/s/1180309
This commit is contained in:
shiziyuan9527 2022-06-13 18:50:55 +08:00 committed by f2c-ci-robot[bot]
parent 543a0a41ef
commit 95f6c458fe
10 changed files with 58 additions and 21 deletions

View File

@ -12,15 +12,11 @@
upper (t.oper_module) oper_module , upper (t.oper_module) oper_module ,
t.oper_title, t.oper_title,
t.oper_time, t.oper_time,
t1.NAME userName,
t2.`name` projectName , t2.`name` projectName ,
t2.workspace_id workspace_id , t2.workspace_id workspace_id
w.name workspaceName
FROM FROM
operating_log t operating_log t
LEFT JOIN `user` t1 ON t.oper_user = t1.id
LEFT JOIN project t2 ON t.project_id = t2.id LEFT JOIN project t2 ON t.project_id = t2.id
LEFT JOIN workspace w on t2.workspace_id = w.id
LEFT JOIN operating_log_resource t3 ON t.id = t3.operating_log_id LEFT JOIN operating_log_resource t3 ON t.id = t3.operating_log_id
<where> <where>
<if test="request.projectIds != null and request.projectIds.size > 0 "> <if test="request.projectIds != null and request.projectIds.size > 0 ">
@ -29,19 +25,8 @@
#{projectId} #{projectId}
</foreach> </foreach>
</if> </if>
<if test="request.workspaceIds != null and request.workspaceIds.size > 0 ">
t2.workspace_id in
<foreach collection="request.workspaceIds" item="workspaceId" separator="," open="(" close=")">
#{workspaceId}
</foreach>
</if>
<if test="request.workspaceId == ''">
or t.project_id is null
or t.project_id = ""
</if>
<if test="request.operUser != null and request.operUser != ''"> <if test="request.operUser != null and request.operUser != ''">
and (t.oper_user like #{request.operUser, jdbcType=VARCHAR} or t1.name like and t.oper_user like #{request.operUser, jdbcType=VARCHAR}
#{request.operUser, jdbcType=VARCHAR})
</if> </if>
<if test="request.projectId != null and request.projectId !=''"> <if test="request.projectId != null and request.projectId !=''">
and t.project_id = #{request.projectId} and t.project_id = #{request.projectId}

View File

@ -1,6 +1,7 @@
package io.metersphere.base.mapper.ext; package io.metersphere.base.mapper.ext;
import io.metersphere.base.domain.Project; import io.metersphere.base.domain.Project;
import io.metersphere.base.domain.Workspace;
import io.metersphere.controller.request.ProjectRequest; import io.metersphere.controller.request.ProjectRequest;
import io.metersphere.dto.ProjectDTO; import io.metersphere.dto.ProjectDTO;
import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.MapKey;
@ -30,6 +31,9 @@ public interface ExtProjectMapper {
@MapKey("id") @MapKey("id")
Map<String, Project> queryNameByIds(@Param("ids") List<String> ids); Map<String, Project> queryNameByIds(@Param("ids") List<String> ids);
@MapKey("id")
Map<String, Workspace> queryWorkNameByProjectIds(@Param("ids") List<String> ids);
Project selectProjectByResourceId(@Param("resourceId") String resourceId); Project selectProjectByResourceId(@Param("resourceId") String resourceId);
long getProjectMemberSize(@Param("projectId") String projectId); long getProjectMemberSize(@Param("projectId") String projectId);

View File

@ -396,4 +396,13 @@
<select id="getProjectForCustomField" resultType="io.metersphere.base.domain.Project"> <select id="getProjectForCustomField" resultType="io.metersphere.base.domain.Project">
select id, platform, third_part_template from project where workspace_id = #{workspaceId} select id, platform, third_part_template from project where workspace_id = #{workspaceId}
</select> </select>
<select id="queryWorkNameByProjectIds" resultType="io.metersphere.base.domain.Workspace">
SELECT project.id, workspace.name
FROM project inner join workspace on project.workspace_id = workspace.id
WHERE project.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</select>
</mapper> </mapper>

View File

@ -3,6 +3,7 @@ package io.metersphere.commons.utils;
import io.metersphere.base.domain.Project; import io.metersphere.base.domain.Project;
import io.metersphere.base.domain.ProjectVersion; import io.metersphere.base.domain.ProjectVersion;
import io.metersphere.base.domain.User; import io.metersphere.base.domain.User;
import io.metersphere.base.domain.Workspace;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.controller.request.BaseQueryRequest; import io.metersphere.controller.request.BaseQueryRequest;
import io.metersphere.controller.request.OrderRequest; import io.metersphere.controller.request.OrderRequest;
@ -135,6 +136,21 @@ public class ServiceUtils {
return nameMap; return nameMap;
} }
public static Map<String, String> getWorkspaceNameByProjectIds(List<String> projectIds) {
ProjectService projectService = CommonBeanFactory.getBean(ProjectService.class);
HashMap<String, String> nameMap = new HashMap<>();
if (!CollectionUtils.isEmpty(projectIds)) {
Map<String, Workspace> workspaceMap = projectService.getWorkspaceNameByProjectIds(projectIds);
workspaceMap.forEach((k, v) -> {
nameMap.put(k, v.getName());
});
return nameMap;
}
return nameMap;
}
public static Map<String, Project> getProjectMap(List<String> ids) { public static Map<String, Project> getProjectMap(List<String> ids) {
ProjectService projectService = CommonBeanFactory.getBean(ProjectService.class); ProjectService projectService = CommonBeanFactory.getBean(ProjectService.class);
if (!CollectionUtils.isEmpty(ids)) { if (!CollectionUtils.isEmpty(ids)) {

View File

@ -37,6 +37,7 @@ public class WorkspaceController {
PermissionConstants.SYSTEM_GROUP_READ_EDIT, PermissionConstants.SYSTEM_GROUP_READ_EDIT,
PermissionConstants.SYSTEM_USER_READ_CREATE, PermissionConstants.SYSTEM_USER_READ_CREATE,
PermissionConstants.SYSTEM_USER_READ_EDIT, PermissionConstants.SYSTEM_USER_READ_EDIT,
PermissionConstants.SYSTEM_WORKSPACE_READ
}, logical = Logical.OR) }, logical = Logical.OR)
public List<Workspace> getWorkspaceList() { public List<Workspace> getWorkspaceList() {
return workspaceService.getWorkspaceList(new WorkspaceRequest()); return workspaceService.getWorkspaceList(new WorkspaceRequest());

View File

@ -7,6 +7,7 @@ import io.metersphere.base.mapper.OperatingLogMapper;
import io.metersphere.base.mapper.OperatingLogResourceMapper; import io.metersphere.base.mapper.OperatingLogResourceMapper;
import io.metersphere.base.mapper.ext.ExtOperatingLogMapper; import io.metersphere.base.mapper.ext.ExtOperatingLogMapper;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.ServiceUtils;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.log.vo.OperatingLogDTO; import io.metersphere.log.vo.OperatingLogDTO;
import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.OperatingLogDetails;
@ -23,7 +24,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -68,7 +71,17 @@ public class OperatingLogService {
request.setStartTime(request.getTimes().get(0)); request.setStartTime(request.getTimes().get(0));
request.setEndTime(request.getTimes().get(1)); request.setEndTime(request.getTimes().get(1));
} }
return extOperatingLogMapper.list(request); List<OperatingLogDTO> list = extOperatingLogMapper.list(request);
List<String> userIds = list.stream().map(OperatingLogDTO::getOperUser).collect(Collectors.toList());
List<String> projectIds = list.stream().map(OperatingLogDTO::getProjectId).collect(Collectors.toList());
Map<String, String> userNameMap = ServiceUtils.getUserNameMap(userIds);
Map<String, String> workspaceNameMap = ServiceUtils.getWorkspaceNameByProjectIds(projectIds);
for (OperatingLogDTO dto : list) {
dto.setUserName(userNameMap.getOrDefault(dto.getOperUser(), ""));
dto.setWorkspaceName(workspaceNameMap.getOrDefault(dto.getProjectId(), ""));
}
return list;
} }
public OperatingLogDTO get(String id) { public OperatingLogDTO get(String id) {

View File

@ -33,4 +33,6 @@ public class OperatingLogDTO implements Serializable {
private String operContent; private String operContent;
private OperatingLogDetails details; private OperatingLogDetails details;
private String projectId;
} }

View File

@ -825,6 +825,13 @@ public class ProjectService {
return extProjectMapper.queryNameByIds(ids); return extProjectMapper.queryNameByIds(ids);
} }
public Map<String, Workspace> getWorkspaceNameByProjectIds(List<String> projectIds) {
if (projectIds.isEmpty()) {
return new HashMap<>(0);
}
return extProjectMapper.queryWorkNameByProjectIds(projectIds);
}
public void openMockTcp(Project project) { public void openMockTcp(Project project) {
if (project == null) { if (project == null) {
MSException.throwException("Project not found!"); MSException.throwException("Project not found!");

View File

@ -257,7 +257,7 @@ export default {
}, },
reset() { reset() {
let projectIds = this.condition.projectIds; let projectIds = this.condition.projectIds;
this.condition = {projectIds: projectIds, projectId: getCurrentProjectID()}; this.condition = {projectIds: projectIds, projectId: getCurrentProjectID(), times: [new Date().getTime() - 3600 * 1000 * 24 * 7, new Date().getTime()]};
this.initTableData(); this.initTableData();
}, },
initProject(url) { initProject(url) {

View File

@ -43,7 +43,7 @@
<el-col :span="8" v-if="isSystem"> <el-col :span="8" v-if="isSystem">
<el-form-item :label="$t('commons.workspace')" prop="workspace"> <el-form-item :label="$t('commons.workspace')" prop="workspace">
<el-select size="small" v-model="condition.workspaceId" @change="initTableData" clearable <el-select size="small" v-model="condition.workspaceId" @change="initTableData" clearable filterable
style="width: 100%"> style="width: 100%">
<el-option v-for="o in workspaceList" :key="o.id" :label="$t(o.label)" :value="o.id"/> <el-option v-for="o in workspaceList" :key="o.id" :label="$t(o.label)" :value="o.id"/>
</el-select> </el-select>
@ -301,7 +301,7 @@ export default {
}, },
reset() { reset() {
let projectIds = this.condition.projectIds; let projectIds = this.condition.projectIds;
this.condition = {projectIds: projectIds}; this.condition = {projectIds: projectIds, times: [new Date().getTime() - 3600 * 1000 * 24 * 7, new Date().getTime()]};
this.initTableData(); this.initTableData();
}, },
getWorkSpaceList() { getWorkSpaceList() {