Search in sources :

Example 6 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class MetricRepositoryImpl method start.

@Override
public void start() {
    DbSession dbSession = dbClient.openSession(false);
    try {
        List<MetricDto> metricList = dbClient.metricDao().selectEnabled(dbSession);
        this.metricsByKey = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToKey.INSTANCE);
        this.metricsById = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToId.INSTANCE);
    } finally {
        dbSession.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto)

Example 7 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class ComponentTreeAction method buildResponse.

private static ComponentTreeWsResponse buildResponse(ComponentTreeWsRequest request, ComponentTreeData data, Paging paging) {
    ComponentTreeWsResponse.Builder response = ComponentTreeWsResponse.newBuilder();
    response.getPagingBuilder().setPageIndex(paging.pageIndex()).setPageSize(paging.pageSize()).setTotal(paging.total()).build();
    response.setBaseComponent(componentDtoToWsComponent(data.getBaseComponent(), data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()), data.getReferenceComponentsByUuid()));
    for (ComponentDto componentDto : data.getComponents()) {
        response.addComponents(componentDtoToWsComponent(componentDto, data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()), data.getReferenceComponentsByUuid()));
    }
    if (areMetricsInResponse(request)) {
        WsMeasures.Metrics.Builder metricsBuilder = response.getMetricsBuilder();
        for (MetricDto metricDto : data.getMetrics()) {
            metricsBuilder.addMetrics(metricDtoToWsMetric(metricDto));
        }
    }
    if (arePeriodsInResponse(request)) {
        response.getPeriodsBuilder().addAllPeriods(data.getPeriods());
    }
    return response.build();
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentDto(org.sonar.db.component.ComponentDto) ComponentTreeWsResponse(org.sonarqube.ws.WsMeasures.ComponentTreeWsResponse)

Example 8 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class ComponentTreeDataLoader method load.

ComponentTreeData load(ComponentTreeWsRequest wsRequest) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto baseComponent = componentFinder.getByUuidOrKey(dbSession, wsRequest.getBaseComponentId(), wsRequest.getBaseComponentKey(), BASE_COMPONENT_ID_AND_KEY);
        checkPermissions(baseComponent);
        Optional<SnapshotDto> baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid());
        if (!baseSnapshot.isPresent()) {
            return ComponentTreeData.builder().setBaseComponent(baseComponent).build();
        }
        Long developerId = searchDeveloperId(dbSession, wsRequest);
        ComponentTreeQuery componentTreeQuery = toComponentTreeQuery(wsRequest, baseComponent);
        List<ComponentDto> components = searchComponents(dbSession, componentTreeQuery);
        List<MetricDto> metrics = searchMetrics(dbSession, wsRequest);
        Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, baseComponent, componentTreeQuery, components, metrics, developerId);
        components = filterComponents(components, measuresByComponentUuidAndMetric, metrics, wsRequest);
        components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric);
        int componentCount = components.size();
        components = paginateComponents(components, wsRequest);
        return ComponentTreeData.builder().setBaseComponent(baseComponent).setComponentsFromDb(components).setComponentCount(componentCount).setMeasuresByComponentUuidAndMetric(measuresByComponentUuidAndMetric).setMetrics(metrics).setPeriods(snapshotToWsPeriods(baseSnapshot.get())).setReferenceComponentsByUuid(searchReferenceComponentsById(dbSession, components)).build();
    }
}
Also used : SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) MeasureDto(org.sonar.db.measure.MeasureDto) DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) ComponentTreeQuery(org.sonar.db.component.ComponentTreeQuery)

Example 9 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class ComponentTreeSort method metricValueOrdering.

private static Ordering<ComponentDto> metricValueOrdering(ComponentTreeWsRequest wsRequest, List<MetricDto> metrics, Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric) {
    if (wsRequest.getMetricSort() == null) {
        return componentNameOrdering(wsRequest.getAsc());
    }
    Map<String, MetricDto> metricsByKey = Maps.uniqueIndex(metrics, MetricDto::getKey);
    MetricDto metric = metricsByKey.get(wsRequest.getMetricSort());
    boolean isAscending = wsRequest.getAsc();
    ValueType metricValueType = ValueType.valueOf(metric.getValueType());
    if (NUMERIC_VALUE_TYPES.contains(metricValueType)) {
        return numericalMetricOrdering(isAscending, metric, measuresByComponentUuidAndMetric);
    } else if (TEXTUAL_VALUE_TYPES.contains(metricValueType)) {
        return stringOrdering(isAscending, new ComponentDtoToTextualMeasureValue(metric, measuresByComponentUuidAndMetric));
    } else if (ValueType.LEVEL.equals(ValueType.valueOf(metric.getValueType()))) {
        return levelMetricOrdering(isAscending, metric, measuresByComponentUuidAndMetric);
    }
    throw new IllegalStateException("Unrecognized metric value type: " + metric.getValueType());
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ValueType(org.sonar.api.measures.Metric.ValueType)

Example 10 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class ComponentTreeSort method metricPeriodOrdering.

private static Ordering<ComponentDto> metricPeriodOrdering(ComponentTreeWsRequest wsRequest, List<MetricDto> metrics, Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric) {
    if (wsRequest.getMetricSort() == null || wsRequest.getMetricPeriodSort() == null) {
        return componentNameOrdering(wsRequest.getAsc());
    }
    Map<String, MetricDto> metricsByKey = Maps.uniqueIndex(metrics, MetricDto::getKey);
    MetricDto metric = metricsByKey.get(wsRequest.getMetricSort());
    ValueType metricValueType = ValueType.valueOf(metric.getValueType());
    if (NUMERIC_VALUE_TYPES.contains(metricValueType)) {
        return numericalMetricPeriodOrdering(wsRequest, metric, measuresByComponentUuidAndMetric);
    }
    throw BadRequestException.create(format("Impossible to sort metric '%s' by measure period.", metric.getKey()));
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ValueType(org.sonar.api.measures.Metric.ValueType)

Aggregations

MetricDto (org.sonar.db.metric.MetricDto)137 Test (org.junit.Test)83 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)61 ComponentDto (org.sonar.db.component.ComponentDto)49 SnapshotDto (org.sonar.db.component.SnapshotDto)33 CustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureDto)26 DbSession (org.sonar.db.DbSession)11 ComponentTreeWsResponse (org.sonarqube.ws.WsMeasures.ComponentTreeWsResponse)11 CustomMeasureTesting.newCustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto)10 MeasureDto (org.sonar.db.measure.MeasureDto)9 OrganizationDto (org.sonar.db.organization.OrganizationDto)8 ProjectMeasures (org.sonar.db.measure.ProjectMeasuresIndexerIterator.ProjectMeasures)7 QualityGateConditionDto (org.sonar.db.qualitygate.QualityGateConditionDto)6 JsonWriter (org.sonar.api.utils.text.JsonWriter)5 WsTester (org.sonar.server.ws.WsTester)5 SearchWsResponse (org.sonarqube.ws.WsMeasures.SearchWsResponse)5 MeasureQuery (org.sonar.db.measure.MeasureQuery)3 UserDto (org.sonar.db.user.UserDto)3 WsMeasures (org.sonarqube.ws.WsMeasures)3 ComponentWsResponse (org.sonarqube.ws.WsMeasures.ComponentWsResponse)3