3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.issue.index;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Sets;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.server.es.EsIndexSyncInProgressException;
31 import static org.sonar.api.resources.Qualifiers.APP;
32 import static org.sonar.api.resources.Qualifiers.SUBVIEW;
33 import static org.sonar.api.resources.Qualifiers.VIEW;
35 public class IssueIndexSyncProgressChecker {
36 private static final ImmutableSet<String> APP_VIEW_OR_SUBVIEW = ImmutableSet.<String>builder().add(VIEW, SUBVIEW, APP).build();
37 private final DbClient dbClient;
39 public IssueIndexSyncProgressChecker(DbClient dbClient) {
40 this.dbClient = dbClient;
43 public IssueSyncProgress getIssueSyncProgress(DbSession dbSession) {
44 int completed = dbClient.branchDao().countByNeedIssueSync(dbSession, false);
45 boolean hasFailures = dbClient.ceActivityDao().hasAnyFailedIssueSyncTask(dbSession);
46 boolean isCompleted = !dbClient.ceQueueDao().hasAnyIssueSyncTaskPendingOrInProgress(dbSession);
47 int total = dbClient.branchDao().countAll(dbSession);
48 return new IssueSyncProgress(isCompleted, completed, total, hasFailures);
51 public void checkIfAnyComponentsNeedIssueSync(DbSession dbSession, List<String> componentKeys) {
52 boolean isAppOrViewOrSubview = dbClient.componentDao().existAnyOfComponentsWithQualifiers(dbSession, componentKeys, APP_VIEW_OR_SUBVIEW);
53 boolean needIssueSync;
54 if (isAppOrViewOrSubview) {
55 needIssueSync = dbClient.branchDao().hasAnyBranchWhereNeedIssueSync(dbSession, true);
57 needIssueSync = dbClient.branchDao().doAnyOfComponentsNeedIssueSync(dbSession, componentKeys);
60 throw new EsIndexSyncInProgressException(IssueIndexDefinition.TYPE_ISSUE.getMainType(),
61 "Results are temporarily unavailable. Indexing of issues is in progress.");
65 public void checkIfComponentNeedIssueSync(DbSession dbSession, String componentKey) {
66 checkIfAnyComponentsNeedIssueSync(dbSession, Collections.singletonList(componentKey));
70 * Checks if issue index sync is in progress, if it is, method throws exception org.sonar.server.es.EsIndexSyncInProgressException
72 public void checkIfIssueSyncInProgress(DbSession dbSession) {
73 if (isIssueSyncInProgress(dbSession)) {
74 throw new EsIndexSyncInProgressException(IssueIndexDefinition.TYPE_ISSUE.getMainType(),
75 "Results are temporarily unavailable. Indexing of issues is in progress.");
79 public boolean isIssueSyncInProgress(DbSession dbSession) {
80 return dbClient.branchDao().hasAnyBranchWhereNeedIssueSync(dbSession, true);
83 public boolean doProjectNeedIssueSync(DbSession dbSession, String projectUuid) {
84 return !findProjectUuidsWithIssuesSyncNeed(dbSession, Sets.newHashSet(projectUuid)).isEmpty();
87 public List<String> findProjectUuidsWithIssuesSyncNeed(DbSession dbSession, Collection<String> projectUuids) {
88 return dbClient.branchDao().selectProjectUuidsWithIssuesNeedSync(dbSession, projectUuids);