]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7187 WS api/ce/activity stop using count(1) on DB table CE_QUEUE
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 19 Apr 2016 14:27:57 +0000 (16:27 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 20 Apr 2016 23:23:38 +0000 (01:23 +0200)
server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java
sonar-db/src/test/java/org/sonar/db/DbTester.java

index 1c3a635565598751f4400307c6be8aeb4ebe6753..1a15334bd80ddbc7ce41f053478d7d2c61041a26 100644 (file)
@@ -61,7 +61,6 @@ import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.StringUtils.defaultString;
 import static org.sonar.api.utils.DateUtils.parseDateQuietly;
 import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly;
-import static org.sonar.api.utils.Paging.offset;
 import static org.sonar.server.ws.WsUtils.checkRequest;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID;
@@ -73,7 +72,7 @@ import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE;
 
 public class ActivityAction implements CeWsAction {
-  private static final int PAGE_INDEX = 1;
+  private static final int OFFSET = 0;
   private static final int MAX_PAGE_SIZE = 1000;
 
   private final UserSession userSession;
@@ -98,7 +97,7 @@ public class ActivityAction implements CeWsAction {
       .setDescription(format("Search for tasks.<br> " +
         "Requires the system administration permission, " +
         "or project administration permission if %s is set.<br/>" +
-        "Since 5.5, it's no more posible to specify the page parameter", PARAM_COMPONENT_ID))
+        "Since 5.5, it's no more possible to specify the page parameter", PARAM_COMPONENT_ID))
       .setResponseExample(getClass().getResource("activity-example.json"))
       .setHandler(this)
       .setSince("5.2");
@@ -174,12 +173,12 @@ public class ActivityAction implements CeWsAction {
 
       CeTaskQuery query = buildQuery(dbSession, request);
       checkPermissions(query);
-      TaskResult queuedTasks = loadQueuedTasks(dbSession, request, query);
-      TaskResult pastTasks = loadPastTasks(dbSession, request, query, queuedTasks.total);
+      Iterable<WsCe.Task> queuedTasks = loadQueuedTasks(dbSession, request, query);
+      Iterable<WsCe.Task> pastTasks = loadPastTasks(dbSession, request, query);
 
       return buildResponse(
-        queuedTasks.tasks,
-        pastTasks.tasks,
+        queuedTasks,
+        pastTasks,
         request.getPageSize());
     } finally {
       dbClient.closeSession(dbSession);
@@ -235,20 +234,14 @@ public class ActivityAction implements CeWsAction {
     }
   }
 
-  private TaskResult loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
-    int total = dbClient.ceQueueDao().countByQuery(dbSession, query);
+  private Iterable<WsCe.Task> loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
     List<CeQueueDto> dtos = dbClient.ceQueueDao().selectByQueryInDescOrder(dbSession, query, request.getPageSize());
-    Iterable<WsCe.Task> tasks = formatter.formatQueue(dbSession, dtos);
-    return new TaskResult(tasks, total);
+    return formatter.formatQueue(dbSession, dtos);
   }
 
-  private TaskResult loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query, int totalQueuedTasks) {
-    // we have to take into account the total number of queue tasks found
-    int offset = Math.max(0, offset(PAGE_INDEX, request.getPageSize()) - totalQueuedTasks);
-    List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, offset, request.getPageSize());
-    Iterable<WsCe.Task> ceTasks = formatter.formatActivity(dbSession, dtos);
-
-    return new TaskResult(ceTasks, MAX_PAGE_SIZE);
+  private Iterable<WsCe.Task> loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
+    List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, OFFSET, request.getPageSize());
+    return formatter.formatActivity(dbSession, dtos);
   }
 
   private void checkPermissions(CeTaskQuery query) {
@@ -319,14 +312,4 @@ public class ActivityAction implements CeWsAction {
 
     return activityWsRequest;
   }
-
-  private static class TaskResult {
-    private final Iterable<WsCe.Task> tasks;
-    private final int total;
-
-    private TaskResult(Iterable<WsCe.Task> tasks, int total) {
-      this.tasks = tasks;
-      this.total = total;
-    }
-  }
 }
index 64c1015061966fbbd97bc9c097415887750861c3..d58f519899ff7df3e8ca68ee1b0f08a55e841cd3 100644 (file)
@@ -155,7 +155,7 @@ public class DbTester extends ExternalResource {
 
   /**
    * Returns the number of rows in the table. Example:
-   * <pre>int issues = countTable("issues")</pre>
+   * <pre>int issues = countRowsOfTable("issues")</pre>
    */
   public int countRowsOfTable(String tableName) {
     Preconditions.checkArgument(StringUtils.containsNone(tableName, " "), "Parameter must be the name of a table. Got " + tableName);