]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6834 add WS api/ce/activity
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 08:58:04 +0000 (10:58 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 21:48:49 +0000 (23:48 +0200)
Limitations: no request parameters (paging/filters)

server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeActivityWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeQueueWsAction.java
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsTaskFormatter.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeActivityWsAction/example.json [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeQueueWsAction/example.json

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeActivityWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeActivityWsAction.java
new file mode 100644 (file)
index 0000000..700edb9
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.computation.ws;
+
+import java.util.List;
+import org.apache.ibatis.session.RowBounds;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.db.ce.CeActivityQuery;
+import org.sonar.server.user.UserSession;
+import org.sonar.server.ws.WsUtils;
+import org.sonarqube.ws.Common;
+import org.sonarqube.ws.WsCe;
+
+/**
+ * GET api/ce/activity
+ * <p>Get the past executed tasks</p>
+ */
+public class CeActivityWsAction implements CeWsAction {
+
+  private final UserSession userSession;
+  private final DbClient dbClient;
+  private final CeWsTaskFormatter formatter;
+
+  public CeActivityWsAction(UserSession userSession, DbClient dbClient, CeWsTaskFormatter formatter) {
+    this.userSession = userSession;
+    this.dbClient = dbClient;
+    this.formatter = formatter;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("activity")
+      .setInternal(true)
+      .setResponseExample(getClass().getResource("CeActivityWsAction/example.json"))
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request wsRequest, Response wsResponse) throws Exception {
+    userSession.checkGlobalPermission(UserRole.ADMIN);
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      CeActivityQuery query = new CeActivityQuery();
+      List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, new RowBounds(0, 100));
+
+      WsCe.ActivityResponse.Builder wsResponseBuilder = WsCe.ActivityResponse.newBuilder();
+      wsResponseBuilder.addAllTasks(formatter.formatActivity(dbSession, dtos));
+      Common.Paging paging = Common.Paging.newBuilder()
+        .setPageIndex(1)
+        .setPageSize(dtos.size())
+        .setTotal(dtos.size())
+        .build();
+      wsResponseBuilder.setPaging(paging);
+      WsUtils.writeProtobuf(wsResponseBuilder.build(), wsRequest, wsResponse);
+
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
+  }
+}
index dccf6f50946574675ebd1f6837fd4f3ae6d9da6a..6ffa6fc514eff2ecb9a3b5151c74496366268dbb 100644 (file)
@@ -78,7 +78,7 @@ public class CeQueueWsAction implements CeWsAction {
       }
 
       WsCe.QueueResponse.Builder wsResponseBuilder = WsCe.QueueResponse.newBuilder();
-      wsResponseBuilder.addAllTasks(formatter.format(dbSession, dtos));
+      wsResponseBuilder.addAllTasks(formatter.formatQueue(dbSession, dtos));
       WsUtils.writeProtobuf(wsResponseBuilder.build(), wsRequest, wsResponse);
 
     } finally {
index 6eba0f439bd57e73a1605086153da4d928d9b32b..eb5aabae5a5c20cd6f390ed763a1495f1578e9bd 100644 (file)
@@ -74,11 +74,11 @@ public class CeTaskWsAction implements CeWsAction {
       WsCe.TaskResponse.Builder wsTaskResponse = WsCe.TaskResponse.newBuilder();
       Optional<CeQueueDto> queueDto = dbClient.ceQueueDao().selectByUuid(dbSession, taskId);
       if (queueDto.isPresent()) {
-        wsTaskResponse.setTask(wsTaskFormatter.format(dbSession, queueDto.get()));
+        wsTaskResponse.setTask(wsTaskFormatter.formatQueue(dbSession, queueDto.get()));
       } else {
         Optional<CeActivityDto> activityDto = dbClient.ceActivityDao().selectByUuid(dbSession, taskId);
         if (activityDto.isPresent()) {
-          wsTaskResponse.setTask(wsTaskFormatter.format(dbSession, activityDto.get()));
+          wsTaskResponse.setTask(wsTaskFormatter.formatActivity(dbSession, activityDto.get()));
         } else {
           throw new NotFoundException();
         }
index 6205fb7c319305612eefdef95ccb2e78d4819462..89a03a064322d964e4d2907c56a4ea0fafc9e5ce 100644 (file)
@@ -43,20 +43,20 @@ public class CeWsTaskFormatter {
     this.dbClient = dbClient;
   }
 
-  public List<WsCe.Task> format(DbSession dbSession, List<CeQueueDto> dtos) {
+  public List<WsCe.Task> formatQueue(DbSession dbSession, List<CeQueueDto> dtos) {
     ComponentCache cache = new ComponentCache(dbSession);
     List<WsCe.Task> result = new ArrayList<>();
     for (CeQueueDto dto : dtos) {
-      result.add(format(dto, cache));
+      result.add(formatQueue(dto, cache));
     }
     return result;
   }
 
-  public WsCe.Task format(DbSession dbSession, CeQueueDto dto) {
-    return format(dto, new ComponentCache(dbSession));
+  public WsCe.Task formatQueue(DbSession dbSession, CeQueueDto dto) {
+    return formatQueue(dto, new ComponentCache(dbSession));
   }
 
-  private WsCe.Task format(CeQueueDto dto, ComponentCache componentCache) {
+  private WsCe.Task formatQueue(CeQueueDto dto, ComponentCache componentCache) {
     WsCe.Task.Builder builder = WsCe.Task.newBuilder();
     builder.setId(dto.getUuid());
     builder.setStatus(WsCe.TaskStatus.valueOf(dto.getStatus().name()));
@@ -75,11 +75,20 @@ public class CeWsTaskFormatter {
     return builder.build();
   }
 
-  public WsCe.Task format(DbSession dbSession, CeActivityDto dto) {
-    return format(dto, new ComponentCache(dbSession));
+  public WsCe.Task formatActivity(DbSession dbSession, CeActivityDto dto) {
+    return formatActivity(dto, new ComponentCache(dbSession));
   }
 
-  private WsCe.Task format(CeActivityDto dto, ComponentCache componentCache) {
+  public List<WsCe.Task> formatActivity(DbSession dbSession, List<CeActivityDto> dtos) {
+    ComponentCache cache = new ComponentCache(dbSession);
+    List<WsCe.Task> result = new ArrayList<>();
+    for (CeActivityDto dto : dtos) {
+      result.add(formatActivity(dto, cache));
+    }
+    return result;
+  }
+
+  private WsCe.Task formatActivity(CeActivityDto dto, ComponentCache componentCache) {
     WsCe.Task.Builder builder = WsCe.Task.newBuilder();
     builder.setId(dto.getUuid());
     builder.setStatus(WsCe.TaskStatus.valueOf(dto.getStatus().name()));
index 612948b2938032e46b570c07311110f67358fcce..0fa4ccab8ad036166a6846da6879b1e511908d90 100644 (file)
@@ -72,6 +72,7 @@ import org.sonar.server.computation.ReportFiles;
 import org.sonar.server.computation.ReportSubmitter;
 import org.sonar.server.computation.monitoring.CEQueueStatusImpl;
 import org.sonar.server.computation.monitoring.ComputeEngineQueueMonitor;
+import org.sonar.server.computation.ws.CeActivityWsAction;
 import org.sonar.server.computation.ws.CeQueueWsAction;
 import org.sonar.server.computation.ws.CeSubmitWsAction;
 import org.sonar.server.computation.ws.CeTaskWsAction;
@@ -725,6 +726,7 @@ public class PlatformLevel4 extends PlatformLevel {
       CeWsTaskFormatter.class,
       CeTaskWsAction.class,
       CeSubmitWsAction.class,
+      CeActivityWsAction.class,
       CeQueueWsAction.class,
       IsQueueEmptyWs.class,
       DefaultPeriodCleaner.class,
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeActivityWsAction/example.json b/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeActivityWsAction/example.json
new file mode 100644 (file)
index 0000000..733cd62
--- /dev/null
@@ -0,0 +1,29 @@
+{
+  "tasks": [
+    {
+      "id": "BU_dO1vsORa8_beWCwsP",
+      "type": "REPORT",
+      "componentId": "AU-Tpxb--iU5OvuD2FLy",
+      "componentKey": "project_1",
+      "componentName": "Project One",
+      "status": "SUCCESS",
+      "submittedAt": "2015-08-13T23:34:59+0200",
+      "submitterLogin": "john",
+      "startedAt": "2015-08-13T23:35:00+0200",
+      "finishedAt": "2015-08-13T23:35:10+0200",
+      "executionTimeMs": 10000
+    },
+    {
+      "id": "AU_dO1vsORa8_beWCwmP",
+      "taskType": "REPORT",
+      "componentId": "AU_dO1vlORa8_beWCwmO",
+      "componentKey": "project_2",
+      "componentName": "Project Two",
+      "status": "FAILED",
+      "submittedAt": "2015-09-17T23:34:59+0200",
+      "startedAt": "2015-09-17T23:35:00+0200",
+      "finishedAt": "2015-08-13T23:37:00+0200",
+      "executionTimeMs": 120000
+    }
+  ]
+}
index f16ec710004848b6d56e415489b3b2a390c4dcaa..12ff5cc5cc8776aa5498e0ee61c69295312b852d 100644 (file)
@@ -16,7 +16,7 @@
       "componentId": "AU_dO1vlORa8_beWCwmO",
       "componentKey": "project_2",
       "componentName": "Project Two",
-      "status": "IN_PROGRESS",
+      "status": "PENDING",
       "submittedAt": "2015-09-17T23:34:59+0200",
       "startedAt": "2015-09-17T23:35:00+0200"
     }