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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 java.util.Optional;
  22. import javax.annotation.CheckForNull;
  23. import javax.annotation.Nullable;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.web.UserRole;
  29. import org.sonar.core.util.Uuids;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.ce.CeActivityDto;
  33. import org.sonar.db.ce.CeQueueDto;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.server.component.ComponentFinder;
  36. import org.sonar.server.user.UserSession;
  37. import org.sonar.server.ws.KeyExamples;
  38. import org.sonarqube.ws.Ce.ActivityStatusWsResponse;
  39. import static org.sonar.server.ce.ws.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY;
  40. import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID;
  41. import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY;
  42. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  43. public class ActivityStatusAction implements CeWsAction {
  44. private final UserSession userSession;
  45. private final DbClient dbClient;
  46. private final ComponentFinder componentFinder;
  47. private final System2 system2;
  48. public ActivityStatusAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder, System2 system2) {
  49. this.userSession = userSession;
  50. this.dbClient = dbClient;
  51. this.componentFinder = componentFinder;
  52. this.system2 = system2;
  53. }
  54. @Override
  55. public void define(WebService.NewController controller) {
  56. WebService.NewAction action = controller
  57. .createAction("activity_status")
  58. .setDescription("Returns CE activity related metrics.<br>" +
  59. "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
  60. .setSince("5.5")
  61. .setResponseExample(getClass().getResource("activity_status-example.json"))
  62. .setHandler(this);
  63. action.createParam(PARAM_COMPONENT_ID)
  64. .setDescription("Id of the component (project) to filter on")
  65. .setExampleValue(Uuids.UUID_EXAMPLE_03);
  66. action.createParam(DEPRECATED_PARAM_COMPONENT_KEY)
  67. .setDeprecatedSince("6.6")
  68. .setDescription("Key of the component (project) to filter on")
  69. .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
  70. action.setChangelog(new Change("6.6", "New field 'inProgress' in response"));
  71. action.setChangelog(new Change("7.8", "New field 'pendingTime' in response, only included when there are pending tasks"));
  72. }
  73. @Override
  74. public void handle(org.sonar.api.server.ws.Request request, Response response) throws Exception {
  75. ActivityStatusWsResponse activityStatusResponse = doHandle(toWsRequest(request));
  76. writeProtobuf(activityStatusResponse, request, response);
  77. }
  78. private ActivityStatusWsResponse doHandle(Request request) {
  79. try (DbSession dbSession = dbClient.openSession(false)) {
  80. Optional<ComponentDto> component = searchComponent(dbSession, request);
  81. String componentUuid = component.map(ComponentDto::uuid).orElse(null);
  82. checkPermissions(component);
  83. int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid);
  84. int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid);
  85. int failingCount = dbClient.ceActivityDao().countLastByStatusAndMainComponentUuid(dbSession, CeActivityDto.Status.FAILED, componentUuid);
  86. Optional<Long> creationDate = dbClient.ceQueueDao().selectCreationDateOfOldestPendingByMainComponentUuid(dbSession, componentUuid);
  87. ActivityStatusWsResponse.Builder builder = ActivityStatusWsResponse.newBuilder()
  88. .setPending(pendingCount)
  89. .setInProgress(inProgressCount)
  90. .setFailing(failingCount);
  91. creationDate.ifPresent(d -> {
  92. long ageOfOldestPendingTime = system2.now() - d;
  93. builder.setPendingTime(ageOfOldestPendingTime);
  94. });
  95. return builder.build();
  96. }
  97. }
  98. private Optional<ComponentDto> searchComponent(DbSession dbSession, Request request) {
  99. ComponentDto component = null;
  100. if (hasComponentInRequest(request)) {
  101. component = componentFinder.getByUuidOrKey(dbSession, request.getComponentId(), request.getComponentKey(), COMPONENT_ID_AND_KEY);
  102. }
  103. return Optional.ofNullable(component);
  104. }
  105. private void checkPermissions(Optional<ComponentDto> component) {
  106. if (component.isPresent()) {
  107. userSession.checkComponentPermission(UserRole.ADMIN, component.get());
  108. } else {
  109. userSession.checkIsSystemAdministrator();
  110. }
  111. }
  112. private static boolean hasComponentInRequest(Request request) {
  113. return request.getComponentId() != null || request.getComponentKey() != null;
  114. }
  115. private static Request toWsRequest(org.sonar.api.server.ws.Request request) {
  116. return new Request(request.param(PARAM_COMPONENT_ID), request.param(DEPRECATED_PARAM_COMPONENT_KEY));
  117. }
  118. private static class Request {
  119. private String componentId;
  120. private String componentKey;
  121. Request(@Nullable String componentId, @Nullable String componentKey) {
  122. this.componentId = componentId;
  123. this.componentKey = componentKey;
  124. }
  125. @CheckForNull
  126. public String getComponentId() {
  127. return componentId;
  128. }
  129. @CheckForNull
  130. public String getComponentKey() {
  131. return componentKey;
  132. }
  133. }
  134. }