Search in sources :

Example 21 with LiveMeasureDto

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

the class LiveMeasureDtoToMeasureTest method toMeasure_creates_MeasureVariation_and_maps_the_right_one.

@Test
public void toMeasure_creates_MeasureVariation_and_maps_the_right_one() {
    LiveMeasureDto LiveMeasureDto = new LiveMeasureDto().setData("1").setVariation(2d);
    Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, SOME_STRING_METRIC);
    assertThat(measure.get().getVariation()).isEqualTo(2);
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Test(org.junit.Test)

Example 22 with LiveMeasureDto

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

the class MeasureToMeasureDtoTest method toLiveMeasureDto.

@Test
public void toLiveMeasureDto() {
    treeRootHolder.setRoot(SOME_COMPONENT);
    LiveMeasureDto liveMeasureDto = underTest.toLiveMeasureDto(Measure.newMeasureBuilder().create(Measure.Level.OK), SOME_LEVEL_METRIC, SOME_COMPONENT);
    assertThat(liveMeasureDto.getTextValue()).isEqualTo(Measure.Level.OK.name());
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Test(org.junit.Test)

Example 23 with LiveMeasureDto

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

the class LiveMeasureComputerImpl method refreshComponentsOnSameProject.

private Optional<QGChangeEvent> refreshComponentsOnSameProject(DbSession dbSession, List<ComponentDto> touchedComponents) {
    // load all the components to be refreshed, including their ancestors
    List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
    ComponentDto branchComponent = findBranchComponent(components);
    BranchDto branch = loadBranch(dbSession, branchComponent);
    ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
    Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
    if (!lastAnalysis.isPresent()) {
        return Optional.empty();
    }
    QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
    Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
    List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
    Map<String, MetricDto> metricsPerId = metrics.stream().collect(uniqueIndex(MetricDto::getUuid));
    List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
    List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerId.keySet());
    // previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
    Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);
    Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, branchComponent);
    DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
    MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
    FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
    long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
    components.forEach(c -> {
        IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeak));
        for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
            // use formulas when the leak period is defined, it's a PR, or the formula is not about the leak period
            if (shouldUseLeakFormulas(lastAnalysis.get(), branch) || !formula.isOnLeak()) {
                context.change(c, formula);
                try {
                    formula.compute(context, issueCounter);
                } catch (RuntimeException e) {
                    throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
                }
            }
        }
    });
    EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
    // persist the measures that have been created or updated
    matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE).forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m));
    projectIndexer.commitAndIndexComponents(dbSession, singleton(branchComponent), ProjectIndexer.Cause.MEASURE_CHANGE);
    return Optional.of(new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) Configuration(org.sonar.api.config.Configuration) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) MetricDto(org.sonar.db.metric.MetricDto) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Metric(org.sonar.api.measures.Metric) DebtRatingGrid(org.sonar.server.measure.DebtRatingGrid)

Example 24 with LiveMeasureDto

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

the class MeasureMatrix method changeCell.

private void changeCell(ComponentDto component, String metricKey, Predicate<LiveMeasureDto> changer) {
    MeasureCell cell = table.get(component.uuid(), metricKey);
    if (cell == null) {
        LiveMeasureDto measure = new LiveMeasureDto().setComponentUuid(component.uuid()).setProjectUuid(component.projectUuid()).setMetricUuid(metricsByKeys.get(metricKey).getUuid());
        cell = new MeasureCell(measure, true);
        table.put(component.uuid(), metricKey, cell);
        changer.test(cell.getMeasure());
    } else if (changer.test(cell.getMeasure())) {
        cell.setChanged(true);
    }
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 25 with LiveMeasureDto

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

the class ComponentAction method getMeasuresByMetric.

private static Map<MetricDto, LiveMeasureDto> getMeasuresByMetric(List<LiveMeasureDto> measures, Collection<MetricDto> metrics) {
    Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
    Map<MetricDto, LiveMeasureDto> measuresByMetric = new HashMap<>();
    for (LiveMeasureDto measure : measures) {
        MetricDto metric = metricsByUuid.get(measure.getMetricUuid());
        measuresByMetric.put(metric, measure);
    }
    return measuresByMetric;
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) HashMap(java.util.HashMap) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

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