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;
@Override
public void execute() {
+ IssueStatistics statistics = new IssueStatistics();
try (DbSession dbSession = dbClient.openSession(true);
CloseableIterator<DefaultIssue> issues = issueCache.traverse()) {
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;
}
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);
+ }
+ }
}
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;
@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();
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
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
assertThat(result.getSeverity()).isEqualTo(BLOCKER);
assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
+
+ assertStatisticsLog(1, 0, 0);
}
@Test
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
.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
.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);
+ }
}