You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/03/18 12:33:58 UTC

[incubator-linkis] branch dev-1.1.1 updated: Dev 1.1.1 creator search bug (#1755)

This is an automated email from the ASF dual-hosted git repository.

peacewong pushed a commit to branch dev-1.1.1
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git


The following commit(s) were added to refs/heads/dev-1.1.1 by this push:
     new 8c7d469  Dev 1.1.1 creator search bug (#1755)
8c7d469 is described below

commit 8c7d46970c85e50d3a54cd856324765a2bd99e13
Author: Shang <no...@live.com>
AuthorDate: Fri Mar 18 20:33:49 2022 +0800

    Dev 1.1.1 creator search bug (#1755)
    
    * fixes #1753 [jobhistory] use fuzzy search sql to filter jobs by creator
    
    * fixed format
    
    * [jobhistory] fixed a bug
---
 .../linkis/jobhistory/dao/JobHistoryMapper.java    | 20 ++++++++++++
 .../jobhistory/dao/impl/JobHistoryMapper.xml       | 30 +++++++++++++++++
 .../service/impl/JobHistoryQueryServiceImpl.scala  | 38 +++++++++-------------
 .../service/JobHistoryDetailQueryServiceTest.java  | 20 ++++++++++--
 4 files changed, 84 insertions(+), 24 deletions(-)

diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/JobHistoryMapper.java b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/JobHistoryMapper.java
index 6538d35..30d128d 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/JobHistoryMapper.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/JobHistoryMapper.java
@@ -40,5 +40,25 @@ public interface JobHistoryMapper {
             @Param("endDate") Date endDate,
             @Param("engineType") String engineType);
 
+    List<JobHistory> searchWithUserCreator(
+            @Param("id") Long id,
+            @Param("umUser") String username,
+            @Param("userCreatorKey") String userCreatorKey,
+            @Param("userCreatorValue") String userCreator,
+            @Param("status") List<String> status,
+            @Param("startDate") Date startDate,
+            @Param("endDate") Date endDate,
+            @Param("engineType") String engineType);
+
+    List<JobHistory> searchWithCreatorOnly(
+            @Param("id") Long id,
+            @Param("umUser") String username,
+            @Param("userCreatorKey") String userCreatorKey,
+            @Param("creator") String userCreator,
+            @Param("status") List<String> status,
+            @Param("startDate") Date startDate,
+            @Param("endDate") Date endDate,
+            @Param("engineType") String engineType);
+
     String selectJobHistoryStatusForUpdate(Long jobId);
 }
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/impl/JobHistoryMapper.xml b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/impl/JobHistoryMapper.xml
index 9e98f9a..a65b867 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/impl/JobHistoryMapper.xml
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/java/org/apache/linkis/jobhistory/dao/impl/JobHistoryMapper.xml
@@ -92,6 +92,36 @@
         ORDER BY linkis_ps_job_history_group_history.created_time DESC
     </select>
 
+    <select id="searchWithUserCreator" useCache="true" resultMap="jobHistoryMap" >
+        /*slave*/ SELECT * FROM linkis_ps_job_history_group_history
+        <where>
+            <if test="id != null">id = #{id}</if>
+            <if test="umUser != null">and submit_user = #{umUser}</if>
+            <if test="engineType != null">and engine_type = #{engineType}</if>
+            <if test="startDate != null">and created_time >= #{startDate} AND created_time <![CDATA[<=]]> #{endDate}</if>
+            <if test="status != null">and <foreach collection="status" item="element" close=")" separator="," open="status in (">#{element}</foreach></if>
+            <if test="userCreatorKey != null and userCreatorValue != null">
+                and  LOCATE('"${userCreatorKey}":"${userCreatorValue}"', labels) > 0
+            </if>
+        </where>
+        ORDER BY linkis_ps_job_history_group_history.created_time DESC
+    </select>
+
+    <select id="searchWithCreatorOnly" useCache="true" resultMap="jobHistoryMap" >
+        /*slave*/ SELECT * FROM linkis_ps_job_history_group_history
+        <where>
+            <if test="id != null">id = #{id}</if>
+            <if test="umUser != null">and submit_user = #{umUser}</if>
+            <if test="engineType != null">and engine_type = #{engineType}</if>
+            <if test="startDate != null">and created_time >= #{startDate} AND created_time <![CDATA[<=]]> #{endDate}</if>
+            <if test="status != null">and <foreach collection="status" item="element" close=")" separator="," open="status in (">#{element}</foreach></if>
+            <if test="userCreatorKey != null and creator != null">
+                and labels like '%${userCreatorKey}":"%-${creator}%'
+            </if>
+        </where>
+        ORDER BY linkis_ps_job_history_group_history.created_time DESC
+    </select>
+
     <!-- // todo check -->
     <update id="updateJobHistory" flushCache="true" parameterType="org.apache.linkis.jobhistory.entity.JobHistory">
         UPDATE linkis_ps_job_history_group_history
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/scala/org/apache/linkis/jobhistory/service/impl/JobHistoryQueryServiceImpl.scala b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/scala/org/apache/linkis/jobhistory/service/impl/JobHistoryQueryServiceImpl.scala
index 59e6898..08faaf3 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/scala/org/apache/linkis/jobhistory/service/impl/JobHistoryQueryServiceImpl.scala
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/main/scala/org/apache/linkis/jobhistory/service/impl/JobHistoryQueryServiceImpl.scala
@@ -39,6 +39,7 @@ import java.util.Date
 import org.apache.linkis.jobhistory.entity.QueryJobHistory
 import org.apache.linkis.jobhistory.service.JobHistoryQueryService
 import org.apache.linkis.jobhistory.transitional.TaskStatus
+import org.apache.linkis.manager.label.entity.engine.UserCreatorLabel
 import org.apache.linkis.manager.label.utils.LabelUtil
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Service
@@ -216,30 +217,23 @@ class JobHistoryQueryServiceImpl extends JobHistoryQueryService with Logging {
   override def search(jobId: java.lang.Long, username: String, status: String, creator: String, sDate: Date, eDate: Date, engineType: String): util.List[JobHistory] = {
     import scala.collection.JavaConversions._
     val split: util.List[String] = if (status != null) status.split(",").toList else null
-    val result = jobHistoryMapper.search(jobId, username, split, sDate, eDate, engineType)
-    if (creator == null || result == null | result.size() == 0) {
-      result
+    val result = if (StringUtils.isBlank(creator)) {
+      jobHistoryMapper.search(jobId, username, split, sDate, eDate, engineType)
+    } else if(StringUtils.isBlank(username)) {
+      val fakeLabel = new UserCreatorLabel
+      jobHistoryMapper.searchWithCreatorOnly(jobId, username, fakeLabel.getLabelKey, creator, split, sDate, eDate, engineType)
     } else {
-      /* filter result given creator, creator is stored as a label in db, so we cannot filter directly by SQL */
-      filterJobHistoryByCreator(result, creator)
-    }
-  }
-
-  private def filterJobHistoryByCreator(jobHistoryList: util.List[JobHistory], creator: String): util.List[JobHistory] = {
-    jobHistoryList.filter(jobHistory => {
-      val labels = TaskConversions.getLabelListFromJson(jobHistory.getLabels)
-      if (labels == null || labels.size() == 0) {
-        warn("label is null or size is 0 for jobHistory record: " + jobHistory.getId)
-        false
-      } else {
-        val userCreatorLabel = LabelUtil.getUserCreatorLabel(labels)
-        if (userCreatorLabel == null) {
-          false
-        } else {
-          StringUtils.equals(userCreatorLabel.getCreator, creator)
-        }
+      val fakeLabel = new UserCreatorLabel
+      fakeLabel.setUser(username)
+      fakeLabel.setCreator(creator)
+      val userCreator = fakeLabel.getStringValue
+      Utils.tryCatch(fakeLabel.valueCheck(userCreator)) {
+        t => info("input user or creator is not correct", t)
+          throw t
       }
-    })
+      jobHistoryMapper.searchWithUserCreator(jobId, username, fakeLabel.getLabelKey, userCreator, split, sDate, eDate, engineType)
+    }
+    result
   }
 
   override def getQueryVOList(list: java.util.List[JobHistory]): java.util.List[JobRequest] = {
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/test/java/org/apache/linkis/jobhistory/service/JobHistoryDetailQueryServiceTest.java b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/test/java/org/apache/linkis/jobhistory/service/JobHistoryDetailQueryServiceTest.java
index d894042..3a35c9c 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/test/java/org/apache/linkis/jobhistory/service/JobHistoryDetailQueryServiceTest.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-jobhistory/src/test/java/org/apache/linkis/jobhistory/service/JobHistoryDetailQueryServiceTest.java
@@ -23,6 +23,7 @@ import org.apache.linkis.governance.common.entity.job.SubJobInfo;
 import org.apache.linkis.governance.common.protocol.job.*;
 import org.apache.linkis.jobhistory.dao.JobDetailMapper;
 import org.apache.linkis.jobhistory.service.impl.JobHistoryDetailQueryServiceImpl;
+import org.apache.linkis.manager.label.entity.engine.UserCreatorLabel;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -36,8 +37,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.function.Predicate;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
 
 @ExtendWith(MockitoExtension.class)
 class JobHistoryDetailQueryServiceTest {
@@ -141,4 +141,20 @@ class JobHistoryDetailQueryServiceTest {
         JobRespProtocol jobRespProtocol = service.query(jobDetailReqQuery);
         assertEquals(jobRespProtocol.getStatus(), 0);
     }
+
+    @Test
+    void testUserCreatorLabel() {
+        UserCreatorLabel fakeLabel = new UserCreatorLabel();
+        fakeLabel.setUser("user");
+        fakeLabel.setCreator("creator");
+        String userCreator = fakeLabel.getStringValue();
+        assertEquals(userCreator, "user-creator");
+        assertEquals(fakeLabel.getLabelKey(), "userCreator");
+        try {
+            assertDoesNotThrow(() -> fakeLabel.valueCheck(fakeLabel.getStringValue()));
+            assertThrows(Exception.class, () -> fakeLabel.valueCheck("fake-label-error"));
+        } catch (Exception e) {
+
+        }
+    }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org