]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9721 Feed ncloc_language_distrution to project measures ES index
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 25 Aug 2017 12:26:09 +0000 (14:26 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Wed, 30 Aug 2017 14:24:53 +0000 (16:24 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java
server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java
server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresDoc.java
server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java

index 279dddd9276f5e13799932f7c866231117decde1..7246fc7243bbc4f125de06a35fca1e8597c856ac 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.db.measure;
 
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSortedSet;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -27,6 +27,7 @@ import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -203,7 +204,7 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator<ProjectMea
       return;
     }
     if (NCLOC_LANGUAGE_DISTRIBUTION_KEY.equals(metricKey)) {
-      readTextValue(rs, measures::setLanguages);
+      readTextValue(rs, measures::setNclocByLanguages);
       return;
     }
   }
@@ -283,10 +284,9 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator<ProjectMea
   }
 
   public static class Measures {
-
     private Map<String, Double> numericMeasures = new HashMap<>();
     private String qualityGateStatus;
-    private List<String> languages = new ArrayList<>();
+    private Map<String, Integer> nclocByLanguages = new LinkedHashMap<>();
 
     Measures addNumericMeasure(String metricKey, double value) {
       numericMeasures.put(metricKey, value);
@@ -307,13 +307,13 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator<ProjectMea
       return qualityGateStatus;
     }
 
-    Measures setLanguages(String languageDistributionValue) {
-      this.languages = ImmutableList.copyOf(parseStringInt(languageDistributionValue).keySet());
+    Measures setNclocByLanguages(String nclocByLangues) {
+      this.nclocByLanguages = ImmutableMap.copyOf(parseStringInt(nclocByLangues));
       return this;
     }
 
-    public List<String> getLanguages() {
-      return languages;
+    public Map<String, Integer> getNclocByLanguages() {
+      return nclocByLanguages;
     }
   }
 
index b11a447f0b262dbe0c54c4a44920a569d8f20404..be37829a7e06652eb2e7e45756f97b3503c6aeea 100644 (file)
@@ -129,7 +129,8 @@ public class ProjectMeasuresIndexerIteratorTest {
 
     Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById();
 
-    assertThat(docsById.get(project.uuid()).getMeasures().getLanguages()).containsOnly("<null>", "java", "xoo");
+    assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages())
+      .containsOnly(entry("<null>", 2), entry("java", 6), entry("xoo", 18));
   }
 
   @Test
index 1d4966d755b6209da255b21ee23af41ac4d0f65d..3058a2c7b9e9af4e64e4a449c1334d77d43f9eba 100644 (file)
@@ -34,12 +34,15 @@ import static org.sonar.api.measures.Metric.Level.ERROR;
 import static org.sonar.api.measures.Metric.Level.OK;
 import static org.sonar.api.measures.Metric.Level.WARN;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT;
+import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_DISTRIB_LANGUAGE;
+import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_DISTRIB_NCLOC;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_KEY;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_LANGUAGES;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES_KEY;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES_VALUE;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NAME;
+import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_LANGUAGE_DISTRIBUTION;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_ORGANIZATION_UUID;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE_STATUS;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_TAGS;
@@ -134,6 +137,25 @@ public class ProjectMeasuresDoc extends BaseDoc {
     return this;
   }
 
+  public Collection<Map<String, Object>> getNclocLanguageDistribution() {
+    return getField(FIELD_NCLOC_LANGUAGE_DISTRIBUTION);
+  }
+
+  public ProjectMeasuresDoc setNclocLanguageDistribution(Collection<Map<String, Object>> distribution) {
+    setField(FIELD_NCLOC_LANGUAGE_DISTRIBUTION, distribution);
+    return this;
+  }
+
+  public ProjectMeasuresDoc setNclocLanguageDistributionFromMap(Map<String, Integer> distribution) {
+    setNclocLanguageDistribution(
+      distribution.entrySet().stream()
+        .map(entry -> ImmutableMap.<String, Object>of(
+          FIELD_DISTRIB_LANGUAGE, entry.getKey(),
+          FIELD_DISTRIB_NCLOC, entry.getValue()))
+        .collect(MoreCollectors.toList()));
+    return this;
+  }
+
   public ProjectMeasuresDoc setQualityGateStatus(@Nullable String s) {
     setField(FIELD_QUALITY_GATE_STATUS, s != null ? QUALITY_GATE_STATUS.get(s) : null);
     return this;
index 5f0f7f73c84b8a332ad05c589d42de2c13a8794a..44108cc49a552c3cc46109fc8e09318223a2c1f0 100644 (file)
@@ -43,6 +43,9 @@ public class ProjectMeasuresIndexDefinition implements IndexDefinition {
   public static final String FIELD_MEASURES_KEY = "key";
   public static final String FIELD_MEASURES_VALUE = "value";
   public static final String FIELD_LANGUAGES = "languages";
+  public static final String FIELD_NCLOC_LANGUAGE_DISTRIBUTION = "nclocLanguageDistribution";
+  public static final String FIELD_DISTRIB_LANGUAGE = "language";
+  public static final String FIELD_DISTRIB_NCLOC = "ncloc";
 
   private final Configuration config;
 
@@ -73,6 +76,10 @@ public class ProjectMeasuresIndexDefinition implements IndexDefinition {
       .addKeywordField(FIELD_MEASURES_KEY)
       .addDoubleField(FIELD_MEASURES_VALUE)
       .build();
+    mapping.nestedFieldBuilder(FIELD_NCLOC_LANGUAGE_DISTRIBUTION)
+      .addKeywordField(FIELD_DISTRIB_LANGUAGE)
+      .addIntegerField(FIELD_DISTRIB_NCLOC)
+      .build();
     mapping.createDateTimeField(FIELD_ANALYSED_AT);
     mapping.setEnableSource(false);
   }
index 1b5378bc6aeb3e48107a03e02acdccea2a0f2ac9..bc0336a204f2d16f7f700399485e2b06ca8a9fe6 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.measure.index;
 
 import com.google.common.collect.ImmutableSet;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -46,7 +47,6 @@ import org.sonar.server.es.ProjectIndexer;
 import org.sonar.server.permission.index.AuthorizationScope;
 import org.sonar.server.permission.index.NeedAuthorizationIndexer;
 
-import static org.sonar.server.es.DefaultIndexSettings.REFRESH_IMMEDIATE;
 import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.INDEX_TYPE_PROJECT_MEASURES;
 
 public class ProjectMeasuresIndexer implements ProjectIndexer, NeedAuthorizationIndexer {
@@ -172,6 +172,7 @@ public class ProjectMeasuresIndexer implements ProjectIndexer, NeedAuthorization
       .setTags(project.getTags())
       .setAnalysedAt(analysisDate == null ? null : new Date(analysisDate))
       .setMeasuresFromMap(projectMeasures.getMeasures().getNumericMeasures())
-      .setLanguages(projectMeasures.getMeasures().getLanguages());
+      .setLanguages(new ArrayList<>(projectMeasures.getMeasures().getNclocByLanguages().keySet()))
+      .setNclocLanguageDistributionFromMap(projectMeasures.getMeasures().getNclocByLanguages());
   }
 }