Search in sources :

Example 26 with LiveMeasureDto

use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.

the class ComponentAction method doHandle.

private ComponentWsResponse doHandle(ComponentRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        String branch = request.getBranch();
        String pullRequest = request.getPullRequest();
        ComponentDto component = loadComponent(dbSession, request, branch, pullRequest);
        checkPermissions(component);
        SnapshotDto analysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, component.projectUuid()).orElse(null);
        boolean isPR = isPR(pullRequest);
        Set<String> metricKeysToRequest = new HashSet<>(request.metricKeys);
        if (isPR) {
            PrMeasureFix.addReplacementMetricKeys(metricKeysToRequest);
        }
        List<MetricDto> metrics = searchMetrics(dbSession, metricKeysToRequest);
        List<LiveMeasureDto> measures = searchMeasures(dbSession, component, metrics);
        Map<MetricDto, LiveMeasureDto> measuresByMetric = getMeasuresByMetric(measures, metrics);
        if (isPR) {
            Set<String> originalMetricKeys = new HashSet<>(request.metricKeys);
            PrMeasureFix.createReplacementMeasures(metrics, measuresByMetric, originalMetricKeys);
            PrMeasureFix.removeMetricsNotRequested(metrics, originalMetricKeys);
        }
        Optional<Measures.Period> period = snapshotToWsPeriods(analysis);
        Optional<ComponentDto> refComponent = getReferenceComponent(dbSession, component);
        return buildResponse(request, component, refComponent, measuresByMetric, metrics, period);
    }
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) HashSet(java.util.HashSet)

Example 27 with LiveMeasureDto

use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.

the class PrMeasureFix method createReplacementMeasures.

static void createReplacementMeasures(List<MetricDto> metrics, Map<MetricDto, LiveMeasureDto> measuresByMetric, Set<String> requestedMetricKeys) {
    Map<String, MetricDto> metricByKey = Maps.uniqueIndex(metrics, MetricDto::getKey);
    Map<MetricDto, LiveMeasureDto> newEntries = new HashMap<>();
    for (Map.Entry<MetricDto, LiveMeasureDto> e : measuresByMetric.entrySet()) {
        String originalKey = METRICS.inverse().get(e.getKey().getKey());
        if (originalKey != null && requestedMetricKeys.contains(originalKey)) {
            MetricDto metricDto = metricByKey.get(originalKey);
            newEntries.put(metricDto, copyMeasureToVariation(e.getValue(), metricDto.getUuid()));
        }
    }
    measuresByMetric.entrySet().removeIf(e -> !requestedMetricKeys.contains(e.getKey().getKey()));
    measuresByMetric.putAll(newEntries);
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) HashMap(java.util.HashMap) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap)

Example 28 with LiveMeasureDto

use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.

the class PrMeasureFix method copyMeasureToVariation.

private static LiveMeasureDto copyMeasureToVariation(LiveMeasureDto dto, String metricUuid) {
    LiveMeasureDto copy = new LiveMeasureDto();
    copy.setVariation(dto.getValue());
    copy.setProjectUuid(dto.getProjectUuid());
    copy.setComponentUuid(dto.getComponentUuid());
    copy.setMetricUuid(metricUuid);
    return copy;
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 29 with LiveMeasureDto

use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.

the class LiveQualityGateComputerImpl method refreshGateStatus.

@Override
public EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix, Configuration configuration) {
    QualityGateEvaluator.Measures measures = metricKey -> {
        Optional<LiveMeasureDto> liveMeasureDto = measureMatrix.getMeasure(project, metricKey);
        if (!liveMeasureDto.isPresent()) {
            return Optional.empty();
        }
        MetricDto metric = measureMatrix.getMetricByUuid(liveMeasureDto.get().getMetricUuid());
        return Optional.of(new LiveMeasure(liveMeasureDto.get(), metric));
    };
    EvaluatedQualityGate evaluatedGate = evaluator.evaluate(gate, measures, configuration);
    measureMatrix.setValue(project, CoreMetrics.ALERT_STATUS_KEY, evaluatedGate.getStatus().name());
    measureMatrix.setValue(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY, QualityGateConverter.toJson(evaluatedGate));
    return evaluatedGate;
}
Also used : BranchDto(org.sonar.db.component.BranchDto) QualityGateConverter(org.sonar.server.qualitygate.QualityGateConverter) QualityGateEvaluator(org.sonar.server.qualitygate.QualityGateEvaluator) OptionalDouble(java.util.OptionalDouble) DbSession(org.sonar.db.DbSession) QualityGateFinder(org.sonar.server.qualitygate.QualityGateFinder) Condition(org.sonar.server.qualitygate.Condition) HashSet(java.util.HashSet) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) QualityGate(org.sonar.server.qualitygate.QualityGate) BranchType(org.sonar.db.component.BranchType) Collection(java.util.Collection) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Set(java.util.Set) QualityGateDto(org.sonar.db.qualitygate.QualityGateDto) MoreCollectors.toHashSet(org.sonar.core.util.stream.MoreCollectors.toHashSet) CoreMetrics(org.sonar.api.measures.CoreMetrics) DbClient(org.sonar.db.DbClient) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Metric(org.sonar.api.measures.Metric) MetricDto(org.sonar.db.metric.MetricDto) Optional(java.util.Optional) QualityGateEvaluator(org.sonar.server.qualitygate.QualityGateEvaluator) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Example 30 with LiveMeasureDto

use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.

the class ComponentDtoToWsComponent method componentDtoToWsComponent.

static Component.Builder componentDtoToWsComponent(ComponentDto component, Map<MetricDto, LiveMeasureDto> measuresByMetric, Map<String, ComponentDto> referenceComponentsByUuid) {
    Component.Builder wsComponent = componentDtoToWsComponent(component);
    ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid());
    if (referenceComponent != null) {
        wsComponent.setRefKey(referenceComponent.getDbKey());
    }
    Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
    for (Map.Entry<MetricDto, LiveMeasureDto> entry : measuresByMetric.entrySet()) {
        MeasureDtoToWsMeasure.updateMeasureBuilder(measureBuilder, entry.getKey(), entry.getValue());
        wsComponent.addMeasures(measureBuilder);
        measureBuilder.clear();
    }
    return wsComponent;
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentDto(org.sonar.db.component.ComponentDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Component(org.sonarqube.ws.Measures.Component) Map(java.util.Map)

Aggregations

LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)48 Test (org.junit.Test)29 MetricDto (org.sonar.db.metric.MetricDto)26 ComponentDto (org.sonar.db.component.ComponentDto)15 SnapshotDto (org.sonar.db.component.SnapshotDto)10 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)9 UserRole (org.sonar.api.web.UserRole)8 DbSession (org.sonar.db.DbSession)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Assertions.tuple (org.assertj.core.api.Assertions.tuple)7 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)7 NotFoundException (org.sonar.server.exceptions.NotFoundException)7 Common (org.sonarqube.ws.Common)7 Double.parseDouble (java.lang.Double.parseDouble)6 String.format (java.lang.String.format)6 Map (java.util.Map)6 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)6 Rule (org.junit.Rule)6 Param (org.sonar.api.server.ws.WebService.Param)6 DbClient (org.sonar.db.DbClient)6