diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-08-03 11:01:27 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-08-05 23:17:30 +0200 |
commit | 0b3bbd0711a290d17a7376bc89059ff4750e32d9 (patch) | |
tree | 65b75d9c049fc7c67ac5100895a60b2d56b51ed5 | |
parent | bd154d53378bd78c6ef821e5466d91b09060ef48 (diff) | |
download | sonarqube-0b3bbd0711a290d17a7376bc89059ff4750e32d9.tar.gz sonarqube-0b3bbd0711a290d17a7376bc89059ff4750e32d9.zip |
SONAR-11077 add statistics to CE PersistIssuesStep
2 files changed, 39 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java index 27d5128b5aa..580afef038d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java @@ -20,6 +20,7 @@ package org.sonar.server.computation.task.projectanalysis.step; import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.Loggers; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.util.CloseableIterator; import org.sonar.db.DbClient; @@ -52,6 +53,7 @@ public class PersistIssuesStep implements ComputationStep { @Override public void execute() { + IssueStatistics statistics = new IssueStatistics(); try (DbSession dbSession = dbClient.openSession(true); CloseableIterator<DefaultIssue> issues = issueCache.traverse()) { @@ -59,26 +61,32 @@ public class PersistIssuesStep implements ComputationStep { IssueChangeMapper changeMapper = dbSession.getMapper(IssueChangeMapper.class); while (issues.hasNext()) { DefaultIssue issue = issues.next(); - boolean saved = persistIssueIfRequired(mapper, issue); + boolean saved = persistIssueIfRequired(mapper, issue, statistics); if (saved) { IssueStorage.insertChanges(changeMapper, issue); } } dbSession.flushStatements(); dbSession.commit(); + } finally { + statistics.log(); } } - private boolean persistIssueIfRequired(IssueMapper mapper, DefaultIssue issue) { + private boolean persistIssueIfRequired(IssueMapper mapper, DefaultIssue issue, IssueStatistics statistics) { if (issue.isNew() || issue.isCopied()) { persistNewIssue(mapper, issue); + statistics.inserts++; return true; } if (issue.isChanged()) { persistChangedIssue(mapper, issue); + statistics.updates++; return true; } + + statistics.untouched++; return false; } @@ -102,4 +110,14 @@ public class PersistIssuesStep implements ComputationStep { public String getDescription() { return "Persist issues"; } + + private static class IssueStatistics { + private int inserts = 0; + private int updates = 0; + private int untouched = 0; + + private void log() { + Loggers.get(PersistIssuesStep.class).debug("inserts={} | updates={} | untouched={}", inserts, updates, untouched); + } + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java index c970bb0409d..d08a3cda8fd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java @@ -30,6 +30,8 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.FieldDiffs; @@ -75,6 +77,8 @@ public class PersistIssuesStepTest extends BaseStepTest { @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() .setOrganizationUuid("org-1"); + @Rule + public LogTester logTester = new LogTester(); private DbSession session = db.getSession(); private DbClient dbClient = db.getDbClient(); @@ -152,6 +156,8 @@ public class PersistIssuesStepTest extends BaseStepTest { List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE")); assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE); + + assertStatisticsLog(1, 0, 0); } @Test @@ -202,6 +208,8 @@ public class PersistIssuesStepTest extends BaseStepTest { List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE")); assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE); + + assertStatisticsLog(1, 0, 0); } @Test @@ -236,6 +244,8 @@ public class PersistIssuesStepTest extends BaseStepTest { assertThat(result.getSeverity()).isEqualTo(BLOCKER); assertThat(result.getStatus()).isEqualTo(STATUS_OPEN); assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant()); + + assertStatisticsLog(1, 0, 0); } @Test @@ -263,6 +273,8 @@ public class PersistIssuesStepTest extends BaseStepTest { IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get(); assertThat(issueReloaded.getStatus()).isEqualTo(STATUS_CLOSED); assertThat(issueReloaded.getResolution()).isEqualTo(RESOLUTION_FIXED); + + assertStatisticsLog(0, 1, 0); } @Test @@ -299,6 +311,8 @@ public class PersistIssuesStepTest extends BaseStepTest { .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserLogin, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey, IssueChangeDto::getIssueChangeCreationDate) .containsOnly(IssueChangeDto.TYPE_COMMENT, "john", "Some text", issue.getKey(), NOW); + + assertStatisticsLog(0, 1, 0); } @Test @@ -333,6 +347,11 @@ public class PersistIssuesStepTest extends BaseStepTest { .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserLogin, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey, IssueChangeDto::getIssueChangeCreationDate) .containsOnly(IssueChangeDto.TYPE_FIELD_CHANGE, "john", "technicalDebt=1", issue.getKey(), NOW); + + assertStatisticsLog(0, 1, 0); } + private void assertStatisticsLog(int expectedInserts, int expectedUpdates, int expectedUntouched) { + assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("inserts=" + expectedInserts + " | updates=" + expectedUpdates + " | untouched=" + expectedUntouched); + } } |