You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActivityStatusAction.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.ce.ws;
  21. import com.google.common.base.Optional;
  22. import org.sonar.api.server.ws.Change;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.core.util.Uuids;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.ce.CeActivityDto;
  30. import org.sonar.db.ce.CeQueueDto;
  31. import org.sonar.db.component.ComponentDto;
  32. import org.sonar.server.component.ComponentFinder;
  33. import org.sonar.server.user.UserSession;
  34. import org.sonar.server.ws.KeyExamples;
  35. import org.sonarqube.ws.Ce.ActivityStatusWsResponse;
  36. import static org.sonar.server.ce.ws.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY;
  37. import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID;
  38. import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY;
  39. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  40. public class ActivityStatusAction implements CeWsAction {
  41. private final UserSession userSession;
  42. private final DbClient dbClient;
  43. private final ComponentFinder componentFinder;
  44. public ActivityStatusAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder) {
  45. this.userSession = userSession;
  46. this.dbClient = dbClient;
  47. this.componentFinder = componentFinder;
  48. }
  49. @Override
  50. public void define(WebService.NewController controller) {
  51. WebService.NewAction action = controller
  52. .createAction("activity_status")
  53. .setDescription("Return CE activity related metrics.<br>" +
  54. "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
  55. .setSince("5.5")
  56. .setResponseExample(getClass().getResource("activity_status-example.json"))
  57. .setInternal(true)
  58. .setHandler(this);
  59. action.createParam(PARAM_COMPONENT_ID)
  60. .setDescription("Id of the component (project) to filter on")
  61. .setExampleValue(Uuids.UUID_EXAMPLE_03);
  62. action.createParam(DEPRECATED_PARAM_COMPONENT_KEY)
  63. .setDeprecatedSince("6.6")
  64. .setDescription("Key of the component (project) to filter on")
  65. .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
  66. action.setChangelog(new Change("6.6", "New field 'inProgress' in response"));
  67. }
  68. @Override
  69. public void handle(org.sonar.api.server.ws.Request request, Response response) throws Exception {
  70. ActivityStatusWsResponse activityStatusResponse = doHandle(toWsRequest(request));
  71. writeProtobuf(activityStatusResponse, request, response);
  72. }
  73. private ActivityStatusWsResponse doHandle(Request request) {
  74. try (DbSession dbSession = dbClient.openSession(false)) {
  75. Optional<ComponentDto> component = searchComponent(dbSession, request);
  76. String componentUuid = component.isPresent() ? component.get().uuid() : null;
  77. checkPermissions(component);
  78. int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid);
  79. int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid);
  80. int failingCount = dbClient.ceActivityDao().countLastByStatusAndMainComponentUuid(dbSession, CeActivityDto.Status.FAILED, componentUuid);
  81. return ActivityStatusWsResponse.newBuilder()
  82. .setPending(pendingCount)
  83. .setInProgress(inProgressCount)
  84. .setFailing(failingCount)
  85. .build();
  86. }
  87. }
  88. private Optional<ComponentDto> searchComponent(DbSession dbSession, Request request) {
  89. ComponentDto component = null;
  90. if (hasComponentInRequest(request)) {
  91. component = componentFinder.getByUuidOrKey(dbSession, request.getComponentId(), request.getComponentKey(), COMPONENT_ID_AND_KEY);
  92. }
  93. return Optional.fromNullable(component);
  94. }
  95. private void checkPermissions(Optional<ComponentDto> component) {
  96. if (component.isPresent()) {
  97. userSession.checkComponentPermission(UserRole.ADMIN, component.get());
  98. } else {
  99. userSession.checkIsSystemAdministrator();
  100. }
  101. }
  102. private static boolean hasComponentInRequest(Request request) {
  103. return request.getComponentId() != null || request.getComponentKey() != null;
  104. }
  105. private static Request toWsRequest(org.sonar.api.server.ws.Request request) {
  106. return new Request()
  107. .setComponentId(request.param(PARAM_COMPONENT_ID))
  108. .setComponentKey(request.param(DEPRECATED_PARAM_COMPONENT_KEY));
  109. }
  110. private static class Request {
  111. private String componentId;
  112. private String componentKey;
  113. public Request setComponentId(String componentId) {
  114. this.componentId = componentId;
  115. return this;
  116. }
  117. public String getComponentId() {
  118. return componentId;
  119. }
  120. public Request setComponentKey(String componentKey) {
  121. this.componentKey = componentKey;
  122. return this;
  123. }
  124. public String getComponentKey() {
  125. return componentKey;
  126. }
  127. }
  128. }