diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-09-18 10:58:04 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-09-18 23:48:49 +0200 |
commit | d3fd3a3175fac49d0c2874dc33e06497d4505de1 (patch) | |
tree | 92f816af776a14ebcb93204a92c470b114f3e44f | |
parent | b7e73ea7bd925ade9625a97c1a1419c1cee6ccd6 (diff) | |
download | sonarqube-d3fd3a3175fac49d0c2874dc33e06497d4505de1.tar.gz sonarqube-d3fd3a3175fac49d0c2874dc33e06497d4505de1.zip |
SONAR-6834 add WS api/ce/activity
Limitations: no request parameters (paging/filters)
7 files changed, 135 insertions, 12 deletions
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 index 00000000000..700edb98ae9 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeActivityWsAction.java @@ -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); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeQueueWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeQueueWsAction.java index dccf6f50946..6ffa6fc514e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeQueueWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeQueueWsAction.java @@ -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 { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java index 6eba0f439bd..eb5aabae5a5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java @@ -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(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsTaskFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsTaskFormatter.java index 6205fb7c319..89a03a06432 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsTaskFormatter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsTaskFormatter.java @@ -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())); diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 612948b2938..0fa4ccab8ad 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -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 index 00000000000..733cd6232f3 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeActivityWsAction/example.json @@ -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 + } + ] +} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeQueueWsAction/example.json b/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeQueueWsAction/example.json index f16ec710004..12ff5cc5cc8 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeQueueWsAction/example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/computation/ws/CeQueueWsAction/example.json @@ -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" } |