Browse Source

[Improvement][API] ProjectService queryAllProjectList API optimization (#4539)

* [Improvement][API] ProjectService queryAllProjectList API optimization

* ✏️ checkStyle typo
pull/3/MERGE
张旺 4 years ago committed by GitHub
parent
commit
046940b1a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java
  2. 11
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java
  3. 6
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapper.java
  4. 6
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapper.xml
  5. 8
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapperTest.java

21
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java

@ -24,6 +24,7 @@ import org.apache.dolphinscheduler.api.service.ProjectService;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.ProjectUser;
@ -442,21 +443,13 @@ public class ProjectServiceImpl extends BaseService implements ProjectService {
*/ */
public Map<String, Object> queryAllProjectList() { public Map<String, Object> queryAllProjectList() {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
List<Project> projects = projectMapper.selectList(null); List<Project> projects = new ArrayList<>();
List<ProcessDefinition> processDefinitions = processDefinitionMapper.selectList(null);
if (projects != null) { List<Integer> projectIds = processDefinitionMapper.listProjectIds();
Set<Integer> set = new HashSet<>(); if (CollectionUtils.isNotEmpty(projectIds)) {
for (ProcessDefinition processDefinition : processDefinitions) { projects = projectMapper.selectBatchIds(projectIds);
set.add(processDefinition.getProjectId());
}
List<Project> tempDeletelist = new ArrayList<>();
for (Project project : projects) {
if (!set.contains(project.getId())) {
tempDeletelist.add(project);
}
}
projects.removeAll(tempDeletelist);
} }
result.put(Constants.DATA_LIST, projects); result.put(Constants.DATA_LIST, projects);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;

11
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java

@ -32,6 +32,7 @@ import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -319,9 +320,8 @@ public class ProjectServiceTest {
@Test @Test
public void testQueryAllProjectList() { public void testQueryAllProjectList() {
Mockito.when(processDefinitionMapper.listProjectIds()).thenReturn(getProjectIds());
Mockito.when(projectMapper.selectList(null)).thenReturn(getList()); Mockito.when(projectMapper.selectBatchIds(getProjectIds())).thenReturn(getList());
Mockito.when(processDefinitionMapper.selectList(null)).thenReturn(getProcessDefinitions());
Map<String, Object> result = projectService.queryAllProjectList(); Map<String, Object> result = projectService.queryAllProjectList();
logger.info(result.toString()); logger.info(result.toString());
@ -387,6 +387,11 @@ public class ProjectServiceTest {
return list; return list;
} }
private List<Integer> getProjectIds() {
return Collections.singletonList(1);
}
private String getDesc() { private String getDesc() {
return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe" return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe"
+ ".deleteProjectRelation(projectId,userId)projectUserMappe" + ".deleteProjectRelation(projectId,userId)projectUserMappe"

6
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapper.java

@ -138,4 +138,10 @@ public interface ProcessDefinitionMapper extends BaseMapper<ProcessDefinition> {
* @param version version * @param version version
*/ */
void updateVersionByProcessDefinitionId(@Param("processDefinitionId") int processDefinitionId, @Param("version") long version); void updateVersionByProcessDefinitionId(@Param("processDefinitionId") int processDefinitionId, @Param("version") long version);
/**
* list all project ids
* @return project ids list
*/
List<Integer> listProjectIds();
} }

6
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapper.xml

@ -128,6 +128,12 @@
WHERE user_id = #{userId} and release_state = 1 and resource_ids is not null and resource_ids != '' WHERE user_id = #{userId} and release_state = 1 and resource_ids is not null and resource_ids != ''
</select> </select>
<select id="listProjectIds" resultType="java.lang.Integer">
SELECT DISTINCT(project_id) as project_id
FROM t_ds_process_definition
</select>
<update id="updateVersionByProcessDefinitionId"> <update id="updateVersionByProcessDefinitionId">
update t_ds_process_definition update t_ds_process_definition
set version = #{version} set version = #{version}

8
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ProcessDefinitionMapperTest.java

@ -366,4 +366,12 @@ public class ProcessDefinitionMapperTest {
ProcessDefinition processDefinition1 = processDefinitionMapper.selectById(processDefinition.getId()); ProcessDefinition processDefinition1 = processDefinitionMapper.selectById(processDefinition.getId());
Assert.assertEquals(expectedVersion, processDefinition1.getVersion()); Assert.assertEquals(expectedVersion, processDefinition1.getVersion());
} }
@Test
public void listProjectIds() {
ProcessDefinition processDefinition = insertOne();
List<Integer> projectIds = processDefinitionMapper.listProjectIds();
Assert.assertNotNull(projectIds);
}
} }

Loading…
Cancel
Save