@Override public int compare(LiveMeasureDto o1, LiveMeasureDto o2) { int componentUuidComp = o1.getComponentUuid().compareTo(o2.getComponentUuid()); if (componentUuidComp != 0) { return componentUuidComp; } return Integer.compare(o1.getMetricId(), o2.getMetricId()); } }
MeasureMatrix(Collection<ComponentDto> components, Collection<MetricDto> metrics, List<LiveMeasureDto> dbMeasures) { for (MetricDto metric : metrics) { this.metricsByKeys.put(metric.getKey(), metric); this.metricsByIds.put(metric.getId(), metric); } this.table = ArrayTable.create(Collections2.transform(components, ComponentDto::uuid), metricsByKeys.keySet()); for (LiveMeasureDto dbMeasure : dbMeasures) { table.put(dbMeasure.getComponentUuid(), metricsByIds.get(dbMeasure.getMetricId()).getKey(), new MeasureCell(dbMeasure, false)); } }
@CheckForNull private static Metric.Level loadPreviousStatus(List<MetricDto> metrics, List<LiveMeasureDto> dbMeasures) { MetricDto alertStatusMetric = metrics.stream() .filter(m -> ALERT_STATUS_KEY.equals(m.getKey())) .findAny() .orElseThrow(() -> new IllegalStateException(String.format("Metric with key %s is not registered", ALERT_STATUS_KEY))); return dbMeasures.stream() .filter(m -> m.getMetricId() == alertStatusMetric.getId()) .map(LiveMeasureDto::getTextValue) .filter(Objects::nonNull) .map(m -> { try { return Metric.Level.valueOf(m); } catch (IllegalArgumentException e) { Loggers.get(LiveMeasureComputerImpl.class) .trace("Failed to parse value of metric '{}'", m, e); return null; } }) .filter(Objects::nonNull) .findAny() .orElse(null); }
private List<Measure> buildWsMeasures() { Map<String, ComponentDto> componentsByUuid = projects.stream().collect(toMap(ComponentDto::uuid, Function.identity())); Map<String, String> componentNamesByKey = projects.stream().collect(toMap(ComponentDto::getDbKey, ComponentDto::name)); Map<Integer, MetricDto> metricsById = metrics.stream().collect(toMap(MetricDto::getId, identity())); Function<LiveMeasureDto, MetricDto> dbMeasureToDbMetric = dbMeasure -> metricsById.get(dbMeasure.getMetricId()); Function<Measure, String> byMetricKey = Measure::getMetric; Function<Measure, String> byComponentName = wsMeasure -> componentNamesByKey.get(wsMeasure.getComponent()); Measure.Builder measureBuilder = Measure.newBuilder(); return measures.stream() .map(dbMeasure -> { updateMeasureBuilder(measureBuilder, dbMeasureToDbMetric.apply(dbMeasure), dbMeasure); measureBuilder.setComponent(componentsByUuid.get(dbMeasure.getComponentUuid()).getDbKey()); Measure measure = measureBuilder.build(); measureBuilder.clear(); return measure; }) .sorted(comparing(byMetricKey).thenComparing(byComponentName)) .collect(toList()); } }
private Map<String, LiveMeasureDto> loadMeasuresGroupedByMetricKey(ComponentDto component, DbSession dbSession) { List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, METRIC_KEYS); Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); List<LiveMeasureDto> measures = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricIds(dbSession, Collections.singletonList(component.uuid()), metricsById.keySet()); return Maps.uniqueIndex(measures, m -> metricsById.get(m.getMetricId()).getKey()); }
@Override public EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix) { QualityGateEvaluator.Measures measures = metricKey -> { Optional<LiveMeasureDto> liveMeasureDto = measureMatrix.getMeasure(project, metricKey); if (!liveMeasureDto.isPresent()) { return Optional.empty(); } MetricDto metric = measureMatrix.getMetric(liveMeasureDto.get().getMetricId()); return Optional.of(new LiveMeasure(liveMeasureDto.get(), metric)); }; EvaluatedQualityGate evaluatedGate = evaluator.evaluate(gate, measures); measureMatrix.setValue(project, CoreMetrics.ALERT_STATUS_KEY, evaluatedGate.getStatus().name()); measureMatrix.setValue(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY, QualityGateConverter.toJson(evaluatedGate)); return evaluatedGate; }
private static ComponentWsResponse buildResponse(ComponentRequest request, ComponentDto component, Optional<ComponentDto> refComponent, List<LiveMeasureDto> measures, List<MetricDto> metrics, List<Measures.Period> periods) { ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder(); Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); Map<MetricDto, LiveMeasureDto> measuresByMetric = new HashMap<>(); for (LiveMeasureDto measure : measures) { MetricDto metric = metricsById.get(measure.getMetricId()); measuresByMetric.put(metric, measure); } if (refComponent.isPresent()) { response.setComponent(componentDtoToWsComponent(component, measuresByMetric, singletonMap(refComponent.get().uuid(), refComponent.get()))); } else { response.setComponent(componentDtoToWsComponent(component, measuresByMetric, emptyMap())); } List<String> additionalFields = request.getAdditionalFields(); if (additionalFields != null) { if (additionalFields.contains(ADDITIONAL_METRICS)) { for (MetricDto metric : metrics) { response.getMetricsBuilder().addMetrics(metricDtoToWsMetric(metric)); } } if (additionalFields.contains(ADDITIONAL_PERIODS)) { response.getPeriodsBuilder().addAllPeriods(periods); } } return response.build(); }
private void verifyPersisted(LiveMeasureDto dto) { List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(dto.getComponentUuid()), singletonList(dto.getMetricId())); assertThat(selected).hasSize(1); assertThat(selected.get(0)).isEqualToComparingFieldByField(dto); } }
private Table<String, MetricDto, ComponentTreeData.Measure> searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, ComponentTreeQuery componentTreeQuery, List<ComponentDto> components, List<MetricDto> metrics) { Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); MeasureTreeQuery measureQuery = MeasureTreeQuery.builder() .setStrategy(MeasureTreeQuery.Strategy.valueOf(componentTreeQuery.getStrategy().name())) .setNameOrKeyQuery(componentTreeQuery.getNameOrKeyQuery()) .setQualifiers(componentTreeQuery.getQualifiers()) .setMetricIds(new ArrayList<>(metricsById.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(), metricsById.get(measureDto.getMetricId()), ComponentTreeData.Measure.createFromMeasureDto(measureDto)); }); addBestValuesToMeasures(measuresByComponentUuidAndMetric, components, metrics); return measuresByComponentUuidAndMetric; }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure(); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList("_missing_"), singletonList(measure.getMetricId())); assertThat(selected).isEmpty(); }
private LiveMeasureDto assertThatIntMeasureHasValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedValue); return measure; }
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.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedRating.getIndex()); assertThat(measure.getDataAsString()).isEqualTo(expectedRating.name()); return measure; }
private void assertThatRatingMeasureHasLeakValue(ComponentDto component, Rating expectedValue) { 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.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getVariation()).isEqualTo((double) expectedValue.getIndex()); }
private void assertThatIntMeasureHasLeakValue(ComponentDto component, double expectedValue) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), intMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(intMetric.getId()); assertThat(measure.getValue()).isNull(); assertThat(measure.getVariation()).isEqualTo(expectedValue); }
private void assertThatMeasureHasValue(LiveMeasureDto template, int expectedValue) { Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(), template.getComponentUuid(), metricRepository.getById(template.getMetricId()).getKey()); assertThat(persisted).isPresent(); assertThat(persisted.get().getValue()).isEqualTo((double) expectedValue); }
private void assertThatMeasureDoesNotExist(LiveMeasureDto template) { assertThat(dbClient.liveMeasureDao().selectMeasure(db.getSession(), template.getComponentUuid(), metricRepository.getById(template.getMetricId()).getKey())) .isEmpty(); }
@Test public void selectTreeByQuery() { List<LiveMeasureDto> results = new ArrayList<>(); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14)); underTest.selectTreeByQuery(db.getSession(), project, MeasureTreeQuery.builder() .setMetricIds(singleton(metric.getId())) .setStrategy(MeasureTreeQuery.Strategy.LEAVES).build(), context -> results.add(context.getResultObject())); assertThat(results).hasSize(1); LiveMeasureDto result = results.get(0); assertThat(result.getComponentUuid()).isEqualTo(file.uuid()); assertThat(result.getMetricId()).isEqualTo(metric.getId()); assertThat(result.getValue()).isEqualTo(3.14); }
@Test public void selectByComponentUuidsAndMetricIds() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure1); underTest.insert(db.getSession(), measure2); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getId())); assertThat(selected) .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), measure2.getValue(), measure2.getDataAsString())); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), emptyList(), singletonList(metric.getId()))).isEmpty(); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricKeys() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure1); underTest.insert(db.getSession(), measure2); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getKey())); assertThat(selected) .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), measure2.getValue(), measure2.getDataAsString())); assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), emptyList(), singletonList(metric.getKey()))).isEmpty(); assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty(); }
MeasureMatrix(Collection<ComponentDto> components, Collection<MetricDto> metrics, List<LiveMeasureDto> dbMeasures) { for (MetricDto metric : metrics) { this.metricsByKeys.put(metric.getKey(), metric); this.metricsByIds.put(metric.getId(), metric); } this.table = ArrayTable.create(Collections2.transform(components, ComponentDto::uuid), metricsByKeys.keySet()); for (LiveMeasureDto dbMeasure : dbMeasures) { table.put(dbMeasure.getComponentUuid(), metricsByIds.get(dbMeasure.getMetricId()).getKey(), new MeasureCell(dbMeasure, false)); } }