Search in sources :

Example 1 with Metric

use of org.sonar.api.batch.measure.Metric 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));
}
Also used : Metric(org.sonar.api.batch.measure.Metric) ScannerMetrics(org.sonar.core.metric.ScannerMetrics) Block(org.sonar.duplications.block.Block) Measure(org.sonar.api.batch.sensor.measure.Measure) COMMENT_LINES_DATA_KEY(org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DATA_KEY) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) NewSignificantCode(org.sonar.api.batch.sensor.code.NewSignificantCode) Loggers(org.sonar.api.utils.log.Loggers) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) MetricFinder(org.sonar.api.batch.measure.MetricFinder) InputComponent(org.sonar.api.batch.fs.InputComponent) CloseableIterator(org.sonar.core.util.CloseableIterator) Set(java.util.Set) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) List(java.util.List) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) ReportPublisher(org.sonar.scanner.report.ReportPublisher) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) PUBLIC_DOCUMENTED_API_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY) Math.max(java.lang.Math.max) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ContextPropertiesCache(org.sonar.scanner.repository.ContextPropertiesCache) SortedMap(java.util.SortedMap) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TEST_SUCCESS_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY) InputFile(org.sonar.api.batch.fs.InputFile) IssuePublisher(org.sonar.scanner.issue.IssuePublisher) BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Constants(org.sonar.scanner.protocol.Constants) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) HashSet(java.util.HashSet) ScannerReportUtils(org.sonar.scanner.report.ScannerReportUtils) AdHocRule(org.sonar.api.batch.sensor.rule.AdHocRule) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) TextRange(org.sonar.api.batch.fs.TextRange) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) LINES_KEY(org.sonar.api.measures.CoreMetrics.LINES_KEY) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Issue(org.sonar.api.batch.sensor.issue.Issue) FileStructure(org.sonar.scanner.protocol.output.FileStructure) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultCoverage(org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) InputDir(org.sonar.api.batch.fs.InputDir) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Metric(org.sonar.api.batch.measure.Metric) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputDir(org.sonar.api.batch.fs.InputDir)

Aggregations

Serializable (java.io.Serializable)1 Math.max (java.lang.Math.max)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Nullable (javax.annotation.Nullable)1 InputComponent (org.sonar.api.batch.fs.InputComponent)1 InputDir (org.sonar.api.batch.fs.InputDir)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TextRange (org.sonar.api.batch.fs.TextRange)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)1 Metric (org.sonar.api.batch.measure.Metric)1 MetricFinder (org.sonar.api.batch.measure.MetricFinder)1