]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11077 add statistics to CE PersistIssuesStep
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 3 Aug 2018 09:01:27 +0000 (11:01 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 5 Aug 2018 21:17:30 +0000 (23:17 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java

index 27d5128b5aa730ac92beb96a39b74737398a1414..580afef038dba5709c27c43b00ee89f309c02da4 100644 (file)
@@ -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);
+    }
+  }
 }
index c970bb0409d1ff7f9a7d214a167d5f0a9e36cdc5..d08a3cda8fd0d7d05b513355aa2d96587814b67e 100644 (file)
@@ -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);
+  }
 }