@Override public Measure addMeasure(Resource resource, Measure measure) { Bucket bucket = getBucket(resource); if (bucket != null) { org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(measure.getMetricKey()); if (metric == null) { throw new SonarException("Unknown metric: " + measure.getMetricKey()); } if (!isTechnicalProjectCopy(resource) && !measure.isFromCore() && INTERNAL_METRICS.contains(metric)) { LOG.debug( "Metric " + metric.key() + " is an internal metric computed by SonarQube. Provided value is ignored."); return measure; } if (measureCache.contains(resource, measure)) { throw new SonarException( "Can not add the same measure twice on " + resource + ": " + measure); } measureCache.put(resource, measure); } return measure; }
@CheckForNull @Override public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) { // Reload resource so that effective key is populated Resource indexedResource = getResource(resource); if (indexedResource == null) { return null; } Iterable<Measure> unfiltered; if (filter instanceof MeasuresFilters.MetricFilter) { // optimization unfiltered = measureCache.byMetric( indexedResource, ((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey()); } else { unfiltered = measureCache.byResource(indexedResource); } Collection<Measure> all = new ArrayList<Measure>(); if (unfiltered != null) { for (Measure measure : unfiltered) { all.add(measure); } } return filter.filter(all); }
@Override public void publish(BatchReportWriter writer) { for (final BatchResource resource : resourceCache.all()) { Iterable<Measure> batchMeasures = measureCache.byResource(resource.resource()); batchMeasures = Iterables.filter( batchMeasures, new Predicate<Measure>() { @Override public boolean apply(Measure input) { // Reload Metric to have all Hibernate fields populated input.setMetric(metricFinder.findByKey(input.getMetricKey())); return shouldPersistMeasure(resource.resource(), input); } }); Iterable<org.sonar.batch.protocol.output.BatchReport.Measure> reportMeasures = Iterables.transform( batchMeasures, new Function<Measure, BatchReport.Measure>() { private final BatchReport.Measure.Builder builder = BatchReport.Measure.newBuilder(); @Override public BatchReport.Measure apply(Measure input) { return toReportMeasure(builder, input); } }); writer.writeComponentMeasures(resource.batchId(), reportMeasures); } }