*/
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;
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;
return;
}
if (NCLOC_LANGUAGE_DISTRIBUTION_KEY.equals(metricKey)) {
- readTextValue(rs, measures::setLanguages);
+ readTextValue(rs, measures::setNclocByLanguages);
return;
}
}
}
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);
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;
}
}
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
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;
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;
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;
.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);
}
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;
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 {
.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());
}
}