Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IssueIndexSyncProgressChecker.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.issue.index;
  21. import com.google.common.collect.ImmutableSet;
  22. import com.google.common.collect.Sets;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.server.es.EsIndexSyncInProgressException;
  29. import static org.sonar.api.resources.Qualifiers.APP;
  30. import static org.sonar.api.resources.Qualifiers.SUBVIEW;
  31. import static org.sonar.api.resources.Qualifiers.VIEW;
  32. public class IssueIndexSyncProgressChecker {
  33. private static final ImmutableSet<String> APP_VIEW_OR_SUBVIEW = ImmutableSet.<String>builder().add(VIEW, SUBVIEW, APP).build();
  34. private final DbClient dbClient;
  35. public IssueIndexSyncProgressChecker(DbClient dbClient) {
  36. this.dbClient = dbClient;
  37. }
  38. public IssueSyncProgress getIssueSyncProgress(DbSession dbSession) {
  39. int completedCount = dbClient.projectDao().countIndexedProjects(dbSession);
  40. int total = dbClient.projectDao().countProjects(dbSession);
  41. boolean hasFailures = dbClient.ceActivityDao().hasAnyFailedOrCancelledIssueSyncTask(dbSession);
  42. boolean isCompleted = !dbClient.ceQueueDao().hasAnyIssueSyncTaskPendingOrInProgress(dbSession);
  43. return new IssueSyncProgress(isCompleted, completedCount, total, hasFailures);
  44. }
  45. public void checkIfAnyComponentsNeedIssueSync(DbSession dbSession, List<String> componentKeys) {
  46. boolean isAppOrViewOrSubview = dbClient.componentDao().existAnyOfComponentsWithQualifiers(dbSession, componentKeys, APP_VIEW_OR_SUBVIEW);
  47. boolean needIssueSync;
  48. if (isAppOrViewOrSubview) {
  49. needIssueSync = dbClient.branchDao().hasAnyBranchWhereNeedIssueSync(dbSession, true);
  50. } else {
  51. needIssueSync = dbClient.branchDao().doAnyOfComponentsNeedIssueSync(dbSession, componentKeys);
  52. }
  53. if (needIssueSync) {
  54. throw new EsIndexSyncInProgressException(IssueIndexDefinition.TYPE_ISSUE.getMainType(),
  55. "Results are temporarily unavailable. Indexing of issues is in progress.");
  56. }
  57. }
  58. public void checkIfComponentNeedIssueSync(DbSession dbSession, String componentKey) {
  59. checkIfAnyComponentsNeedIssueSync(dbSession, Collections.singletonList(componentKey));
  60. }
  61. /**
  62. * Checks if issue index sync is in progress, if it is, method throws exception org.sonar.server.es.EsIndexSyncInProgressException
  63. */
  64. public void checkIfIssueSyncInProgress(DbSession dbSession) {
  65. if (isIssueSyncInProgress(dbSession)) {
  66. throw new EsIndexSyncInProgressException(IssueIndexDefinition.TYPE_ISSUE.getMainType(),
  67. "Results are temporarily unavailable. Indexing of issues is in progress.");
  68. }
  69. }
  70. public boolean isIssueSyncInProgress(DbSession dbSession) {
  71. return dbClient.branchDao().hasAnyBranchWhereNeedIssueSync(dbSession, true);
  72. }
  73. public boolean doProjectNeedIssueSync(DbSession dbSession, String projectUuid) {
  74. return !findProjectUuidsWithIssuesSyncNeed(dbSession, Sets.newHashSet(projectUuid)).isEmpty();
  75. }
  76. public List<String> findProjectUuidsWithIssuesSyncNeed(DbSession dbSession, Collection<String> projectUuids) {
  77. return dbClient.branchDao().selectProjectUuidsWithIssuesNeedSync(dbSession, projectUuids);
  78. }
  79. }