From 95ccc0c85534c099ba0a71f50d3ea762e005417c Mon Sep 17 00:00:00 2001 From: WangJPLeo <103574007+WangJPLeo@users.noreply.github.com> Date: Fri, 15 Apr 2022 18:25:25 +0800 Subject: [PATCH] [Fix-9516] [Statistics] Statistics Manager shows data should belongs to current user (#9515) --- .../api/service/impl/DataAnalysisServiceImpl.java | 7 +++++-- .../api/service/DataAnalysisServiceTest.java | 6 +++--- .../dolphinscheduler/dao/mapper/ErrorCommandMapper.java | 2 ++ .../apache/dolphinscheduler/dao/mapper/CommandMapper.xml | 3 +++ .../dolphinscheduler/dao/mapper/ErrorCommandMapper.xml | 3 +++ .../dao/mapper/ErrorCommandMapperTest.java | 2 ++ 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java index b4cf3f8ae6..6617207a99 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java @@ -233,13 +233,16 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal Date end = null; Long[] projectCodeArray = getProjectCodesArrays(loginUser); + // admin can view all + int userId = loginUser.getUserType() == UserType.ADMIN_USER ? 0 : loginUser.getId(); + // count normal command state - Map normalCountCommandCounts = commandMapper.countCommandState(loginUser.getId(), start, end, projectCodeArray) + Map normalCountCommandCounts = commandMapper.countCommandState(userId, start, end, projectCodeArray) .stream() .collect(Collectors.toMap(CommandCount::getCommandType, CommandCount::getCount)); // count error command state - Map errorCommandCounts = errorCommandMapper.countCommandState(start, end, projectCodeArray) + Map errorCommandCounts = errorCommandMapper.countCommandState(userId, start, end, projectCodeArray) .stream() .collect(Collectors.toMap(CommandCount::getCommandType, CommandCount::getCount)); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java index 040221fb6c..bb6994f800 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java @@ -260,14 +260,14 @@ public class DataAnalysisServiceTest { commandCounts.add(commandCount); Mockito.when(commandMapper.countCommandState(0, null, null, new Long[]{1L})).thenReturn(commandCounts); - Mockito.when(errorCommandMapper.countCommandState(null, null, new Long[]{1L})).thenReturn(commandCounts); + Mockito.when(errorCommandMapper.countCommandState(0, null, null, new Long[]{1L})).thenReturn(commandCounts); Map result = dataAnalysisServiceImpl.countCommandState(user); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); // when no command found then return all count are 0 Mockito.when(commandMapper.countCommandState(anyInt(), any(), any(), any())).thenReturn(Collections.emptyList()); - Mockito.when(errorCommandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.emptyList()); + Mockito.when(errorCommandMapper.countCommandState(anyInt(), any(), any(), any())).thenReturn(Collections.emptyList()); Map result5 = dataAnalysisServiceImpl.countCommandState(user); assertThat(result5).containsEntry(Constants.STATUS, Status.SUCCESS); assertThat(result5.get(Constants.DATA_LIST)).asList().extracting("errorCount").allMatch(count -> count.equals(0)); @@ -281,7 +281,7 @@ public class DataAnalysisServiceTest { errorCommandCount.setCommandType(CommandType.START_PROCESS); errorCommandCount.setCount(5); Mockito.when(commandMapper.countCommandState(anyInt(), any(), any(), any())).thenReturn(Collections.singletonList(normalCommandCount)); - Mockito.when(errorCommandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.singletonList(errorCommandCount)); + Mockito.when(errorCommandMapper.countCommandState(anyInt(), any(), any(), any())).thenReturn(Collections.singletonList(errorCommandCount)); Map result6 = dataAnalysisServiceImpl.countCommandState(user); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.java index acd9ce79cb..f86ce27c67 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.java @@ -34,12 +34,14 @@ public interface ErrorCommandMapper extends BaseMapper { /** * count command state + * @param userId * @param startTime startTime * @param endTime endTime * @param projectCodeArray projectCodeArray * @return CommandCount list */ List countCommandState( + @Param("userId") int userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("projectCodeArray") Long[] projectCodeArray); diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/CommandMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/CommandMapper.xml index aa2bf13bdc..46d9f2ef5b 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/CommandMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/CommandMapper.xml @@ -22,6 +22,9 @@ select cmd.command_type as command_type, count(1) as count from t_ds_command cmd, t_ds_process_definition process where cmd.process_definition_code = process.code + + and process.user_id= #{userId} + and process.project_code in diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.xml index 8179ff44d1..cd096bb860 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapper.xml @@ -22,6 +22,9 @@ select cmd.command_type as command_type, count(1) as count from t_ds_error_command cmd, t_ds_process_definition process where cmd.process_definition_code = process.code + + and process.user_id= #{userId} + and process.project_code in diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java index 27c9467109..42d811ad7c 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ErrorCommandMapperTest.java @@ -77,6 +77,7 @@ public class ErrorCommandMapperTest extends BaseDaoTest { errorCommandMapper.updateById(errorCommand); List commandCounts = errorCommandMapper.countCommandState( + 0, null, null, new Long[0] @@ -86,6 +87,7 @@ public class ErrorCommandMapperTest extends BaseDaoTest { projectCodeArray[0] = processDefinition.getProjectCode(); projectCodeArray[1] = 200L; List commandCounts2 = errorCommandMapper.countCommandState( + 0, null, null, projectCodeArray