use of org.sonar.api.batch.sensor.measure.Measure in project sonarqube by SonarSource.
the class DefaultSensorStorage method saveMeasure.
private void saveMeasure(InputComponent component, DefaultMeasure<?> measure) {
if (component.isFile()) {
DefaultInputFile defaultInputFile = (DefaultInputFile) component;
defaultInputFile.setPublished(true);
}
if (component instanceof InputDir || (component instanceof DefaultInputModule && ((DefaultInputModule) component).definition().getParent() != null)) {
logOnce(measure.metric().key(), "Storing measures on folders or modules is deprecated. Provided value of metric '{}' is ignored.", measure.metric().key());
return;
}
if (DEPRECATED_METRICS_KEYS.contains(measure.metric().key())) {
logOnce(measure.metric().key(), "Metric '{}' is deprecated. Provided value is ignored.", measure.metric().key());
return;
}
Metric metric = metricFinder.findByKey(measure.metric().key());
if (metric == null) {
throw new UnsupportedOperationException("Unknown metric: " + measure.metric().key());
}
if (!measure.isFromCore() && NEWLY_CORE_METRICS_KEYS.contains(measure.metric().key())) {
logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube/SonarCloud. Provided value is ignored.", measure.metric().key());
return;
}
if (!scannerMetrics.getMetrics().contains(metric)) {
throw new UnsupportedOperationException("Metric '" + metric.key() + "' should not be computed by a Sensor");
}
if (((DefaultInputComponent) component).hasMeasureFor(metric)) {
throw new UnsupportedOperationException("Can not add the same measure twice on " + component + ": " + measure);
}
((DefaultInputComponent) component).setHasMeasureFor(metric);
if (metric.key().equals(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)) {
if (component.isFile()) {
((DefaultInputFile) component).setExecutableLines(KeyValueFormat.parseIntInt((String) measure.value()).entrySet().stream().filter(e -> e.getValue() > 0).map(Map.Entry::getKey).collect(Collectors.toSet()));
} else {
throw new IllegalArgumentException("Executable lines can only be saved on files");
}
}
reportPublisher.getWriter().appendComponentMeasure(((DefaultInputComponent) component).scannerId(), toReportMeasure(measure));
}
use of org.sonar.api.batch.sensor.measure.Measure in project sonar-java by SonarSource.
the class StrutsTest method getMetrics.
private Map<String, Double> getMetrics() {
Map<String, Double> metrics = new HashMap<>();
for (InputFile inputFile : context.fileSystem().inputFiles()) {
for (Measure measure : context.measures(inputFile.key())) {
if (measure.value() != null) {
String key = measure.metric().key();
double value = 0;
try {
value = Double.parseDouble("" + measure.value());
} catch (NumberFormatException nfe) {
// do nothing
}
if (metrics.get(key) == null) {
metrics.put(key, value);
} else {
metrics.put(key, metrics.get(key) + value);
}
}
}
}
return metrics;
}
Aggregations