From c812db25005beba5c6c2d87e256fdc18403c2aa3 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 25 Aug 2017 14:26:09 +0200 Subject: [PATCH] SONAR-9721 Feed ncloc_language_distrution to project measures ES index --- .../ProjectMeasuresIndexerIterator.java | 16 +++++++------- .../ProjectMeasuresIndexerIteratorTest.java | 3 ++- .../measure/index/ProjectMeasuresDoc.java | 22 +++++++++++++++++++ .../index/ProjectMeasuresIndexDefinition.java | 7 ++++++ .../measure/index/ProjectMeasuresIndexer.java | 5 +++-- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java index 279dddd9276..7246fc7243b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java @@ -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 numericMeasures = new HashMap<>(); private String qualityGateStatus; - private List languages = new ArrayList<>(); + private Map nclocByLanguages = new LinkedHashMap<>(); Measures addNumericMeasure(String metricKey, double value) { numericMeasures.put(metricKey, value); @@ -307,13 +307,13 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator getLanguages() { - return languages; + public Map getNclocByLanguages() { + return nclocByLanguages; } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java index b11a447f0b2..be37829a7e0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java @@ -129,7 +129,8 @@ public class ProjectMeasuresIndexerIteratorTest { Map docsById = createResultSetAndReturnDocsById(); - assertThat(docsById.get(project.uuid()).getMeasures().getLanguages()).containsOnly("", "java", "xoo"); + assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages()) + .containsOnly(entry("", 2), entry("java", 6), entry("xoo", 18)); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresDoc.java b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresDoc.java index 1d4966d755b..3058a2c7b9e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresDoc.java @@ -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> getNclocLanguageDistribution() { + return getField(FIELD_NCLOC_LANGUAGE_DISTRIBUTION); + } + + public ProjectMeasuresDoc setNclocLanguageDistribution(Collection> distribution) { + setField(FIELD_NCLOC_LANGUAGE_DISTRIBUTION, distribution); + return this; + } + + public ProjectMeasuresDoc setNclocLanguageDistributionFromMap(Map distribution) { + setNclocLanguageDistribution( + distribution.entrySet().stream() + .map(entry -> ImmutableMap.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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java index 5f0f7f73c84..44108cc49a5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java @@ -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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java index 1b5378bc6ae..bc0336a204f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java @@ -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()); } } -- 2.39.5