Search in sources :

Example 31 with LiveMeasureDto

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

the class ComponentTreeAction method searchMeasuresByComponentUuidAndMetric.

private Table<String, MetricDto, ComponentTreeData.Measure> searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, ComponentTreeQuery componentTreeQuery, List<ComponentDto> components, List<MetricDto> metrics) {
    Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
    MeasureTreeQuery measureQuery = MeasureTreeQuery.builder().setStrategy(MeasureTreeQuery.Strategy.valueOf(componentTreeQuery.getStrategy().name())).setNameOrKeyQuery(componentTreeQuery.getNameOrKeyQuery()).setQualifiers(componentTreeQuery.getQualifiers()).setMetricUuids(new ArrayList<>(metricsByUuid.keySet())).build();
    Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), metrics.size());
    dbClient.liveMeasureDao().selectTreeByQuery(dbSession, baseComponent, measureQuery, result -> {
        LiveMeasureDto measureDto = result.getResultObject();
        measuresByComponentUuidAndMetric.put(measureDto.getComponentUuid(), metricsByUuid.get(measureDto.getMetricUuid()), ComponentTreeData.Measure.createFromMeasureDto(measureDto));
    });
    addBestValuesToMeasures(measuresByComponentUuidAndMetric, components, metrics);
    return measuresByComponentUuidAndMetric;
}
Also used : MeasureTreeQuery(org.sonar.db.measure.MeasureTreeQuery) MetricDto(org.sonar.db.metric.MetricDto) ArrayList(java.util.ArrayList) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 32 with LiveMeasureDto

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

the class ListAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        List<BranchDto> pullRequests = dbClient.branchDao().selectByProject(dbSession, project).stream().filter(b -> b.getBranchType() == PULL_REQUEST).collect(toList());
        List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao().selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())).stream().collect(uniqueIndex(BranchDto::getUuid));
        Map<String, PrStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream().collect(uniqueIndex(PrStatistics::getBranchUuid, Function.identity()));
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
        pullRequests.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        writeProtobuf(protobufResponse.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) AbstractUserSession.insufficientPrivilegesException(org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException) ComponentFinder(org.sonar.server.component.ComponentFinder) StringUtils(org.apache.commons.lang.StringUtils) ProjectPullRequests(org.sonarqube.ws.ProjectPullRequests) GlobalPermission(org.sonar.db.permission.GlobalPermission) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) DbProjectBranches(org.sonar.db.protobuf.DbProjectBranches) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) IssueIndex(org.sonar.server.issue.index.IssueIndex) PullRequestsWs.addProjectParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) PrStatistics(org.sonar.server.issue.index.PrStatistics) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) PARAM_PROJECT(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) 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) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) PrStatistics(org.sonar.server.issue.index.PrStatistics) DbSession(org.sonar.db.DbSession) Objects(java.util.Objects)

Example 33 with LiveMeasureDto

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

the class MeasureToMeasureDto method toLiveMeasureDto.

public LiveMeasureDto toLiveMeasureDto(Measure measure, Metric metric, Component component) {
    LiveMeasureDto out = new LiveMeasureDto();
    out.setMetricUuid(metric.getUuid());
    out.setComponentUuid(component.getUuid());
    out.setProjectUuid(treeRootHolder.getRoot().getUuid());
    if (measure.hasVariation()) {
        out.setVariation(measure.getVariation());
    }
    out.setValue(valueAsDouble(measure));
    out.setData(data(measure));
    return out;
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 34 with LiveMeasureDto

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

the class LiveMeasureComputerImplTest method assertThatRatingMeasureHasValue.

private LiveMeasureDto assertThatRatingMeasureHasValue(ComponentDto component, Rating expectedRating) {
    LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), ratingMetric.getKey()).get();
    assertThat(measure.getComponentUuid()).isEqualTo(component.uuid());
    assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid());
    assertThat(measure.getMetricUuid()).isEqualTo(ratingMetric.getUuid());
    assertThat(measure.getValue()).isEqualTo(expectedRating.getIndex());
    assertThat(measure.getDataAsString()).isEqualTo(expectedRating.name());
    return measure;
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 35 with LiveMeasureDto

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

the class LiveMeasureComputerImplTest method variation_is_refreshed_when_int_value_is_changed.

@Test
public void variation_is_refreshed_when_int_value_is_changed() {
    markProjectAsAnalyzed(project);
    // value is:
    // 42 on last analysis
    // 42-12=30 on beginning of leak period
    db.measures().insertLiveMeasure(project, intMetric, m -> m.setValue(42.0).setVariation(12.0));
    // new value is 44, so variation on leak period is 44-30=14
    List<QGChangeEvent> result = run(file1, newIntConstantFormula(44.0));
    LiveMeasureDto measure = assertThatIntMeasureHasValue(project, 44.0);
    assertThat(measure.getVariation()).isEqualTo(14.0);
    assertThatProjectChanged(result, project);
}
Also used : LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) Test(org.junit.Test)

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