]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7553 DAO of CE_ACTIVITY count number of tasks still failing
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 19 Apr 2016 14:29:41 +0000 (16:29 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 20 Apr 2016 23:23:38 +0000 (01:23 +0200)
sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java
sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java
sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml
sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java

index 1055de867d5d2223a3ba91a70668f6d22530a6fa..91f155ee3ca3f4b20508bd6a0f269da479847d18 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.db.ce;
 import com.google.common.base.Optional;
 import java.util.Collections;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.apache.ibatis.session.RowBounds;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
@@ -72,6 +73,10 @@ public class CeActivityDao implements Dao {
     return mapper(dbSession).selectByQuery(query, offset, pageSize);
   }
 
+  public int countLastByStatusAndComponentUuid(DbSession dbSession, CeActivityDto.Status status, @Nullable String componentUuid) {
+    return mapper(dbSession).countLastByStatusAndComponentUuid(status, componentUuid);
+  }
+
   private static CeActivityMapper mapper(DbSession dbSession) {
     return dbSession.getMapper(CeActivityMapper.class);
   }
index ea4652d4185d108b259b50cab849310913dde709..8bdaa57b1af94ff77b4854a86250334707038b93 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.ce;
 
 import java.util.List;
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.session.RowBounds;
 
@@ -37,7 +38,7 @@ public interface CeActivityMapper {
 
   List<CeActivityDto> selectOlderThan(@Param("beforeDate") long beforeDate);
 
-  int countByQuery(@Param("query") CeTaskQuery query);
+  int countLastByStatusAndComponentUuid(@Param("status") CeActivityDto.Status status, @Nullable @Param("componentUuid") String componentUuid);
 
   void insert(CeActivityDto dto);
 
index 765c5f28a6b140e429735839dfdc2aafdaa4b75b..69e58b22989ad458c197875500242b01d3d1efdb 100644 (file)
     from ce_activity ca
     where ca.created_at &lt; #{beforeDate,jdbcType=BIGINT}
   </select>
+  
+  <select id="countLastByStatusAndComponentUuid" resultType="int">
+    select count(1)
+    from ce_activity
+    where status=#{status} 
+      and is_last=${_true}
+      <if test="componentUuid!=null">
+        and component_uuid=#{componentUuid}
+      </if>
+  </select>
 
   <insert id="insert" parameterType="org.sonar.db.ce.CeActivityDto" useGeneratedKeys="false">
     insert into ce_activity
index 14f3a2e4c7150e6819c687f0fc9654f07e887979..c233b42c15dfc8815b2d40c7c4444b18eddd483f 100644 (file)
@@ -28,10 +28,13 @@ import javax.annotation.Nonnull;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.ce.CeActivityDto.Status.FAILED;
+import static org.sonar.db.ce.CeActivityDto.Status.SUCCESS;
 import static org.sonar.db.ce.CeTaskTypes.REPORT;
 
 public class CeActivityDaoTest {
@@ -40,6 +43,7 @@ public class CeActivityDaoTest {
 
   @Rule
   public DbTester db = DbTester.create(system2);
+  DbSession dbSession = db.getSession();
 
   CeActivityDao underTest = new CeActivityDao(system2);
 
@@ -75,7 +79,7 @@ public class CeActivityDaoTest {
     assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").get().getIsLast()).isTrue();
 
     // two tasks on PROJECT_1, the most recent one is TASK_3
-    insert("TASK_3", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
+    insert("TASK_3", REPORT, "PROJECT_1", FAILED);
     assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").get().getIsLast()).isFalse();
     assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").get().getIsLast()).isTrue();
     assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").get().getIsLast()).isTrue();
@@ -91,7 +95,7 @@ public class CeActivityDaoTest {
   @Test
   public void test_selectByQuery() {
     insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
-    insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
+    insert("TASK_2", REPORT, "PROJECT_1", FAILED);
     insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
     insert("TASK_4", "views", null, CeActivityDto.Status.SUCCESS);
 
@@ -211,6 +215,22 @@ public class CeActivityDaoTest {
     assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isTrue();
   }
 
+  @Test
+  public void count_last_by_status_and_component_uuid() {
+    insert("TASK_1", CeTaskTypes.REPORT, "COMPONENT1", CeActivityDto.Status.SUCCESS);
+    // component 2
+    insert("TASK_2", CeTaskTypes.REPORT, "COMPONENT2", CeActivityDto.Status.SUCCESS);
+    // status failed
+    insert("TASK_3", CeTaskTypes.REPORT, "COMPONENT1", CeActivityDto.Status.FAILED);
+    // status canceled
+    insert("TASK_4", CeTaskTypes.REPORT, "COMPONENT1", CeActivityDto.Status.CANCELED);
+    insert("TASK_5", CeTaskTypes.REPORT, "COMPONENT1", CeActivityDto.Status.SUCCESS);
+    db.commit();
+
+    assertThat(underTest.countLastByStatusAndComponentUuid(dbSession, SUCCESS, "COMPONENT1")).isEqualTo(1);
+    assertThat(underTest.countLastByStatusAndComponentUuid(dbSession, SUCCESS, null)).isEqualTo(2);
+  }
+
   private void insert(String uuid, String type, String componentUuid, CeActivityDto.Status status) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setUuid(uuid);