public static String formatMeasureValue(LiveMeasureDto measure, MetricDto metric) { Double doubleValue = measure.getValue(); String stringValue = measure.getDataAsString(); return formatMeasureValue(doubleValue == null ? Double.NaN : doubleValue, stringValue, metric); }
@CheckForNull private static Double getDoubleValue(LiveMeasureDto measure, Metric metric) { Double value = measure.getValue(); if (BooleanUtils.isTrue(metric.isOptimizedBestValue()) && value == null) { value = metric.getBestValue(); } return value; } }
static void updateMeasureBuilder(Measure.Builder measureBuilder, MetricDto metricDto, LiveMeasureDto measureDto) { Double value = measureDto.getValue(); Double variation = measureDto.getVariation(); updateMeasureBuilder(measureBuilder, metricDto, value == null ? Double.NaN : value, measureDto.getDataAsString(), variation == null ? Double.NaN : variation); }
private Measure(LiveMeasureDto measureDto) { this.value = toPrimitive(measureDto.getValue()); this.data = measureDto.getDataAsString(); this.variation = toPrimitive(measureDto.getVariation()); }
private void verifyValue(MeasureMatrix underTest, ComponentDto component, MetricDto metric, @Nullable Double expectedValue) { Optional<LiveMeasureDto> measure = underTest.getMeasure(component, metric.getKey()); assertThat(measure).isPresent(); assertThat(measure.get().getValue()).isEqualTo(expectedValue); }
void setValue(ComponentDto component, String metricKey, double value) { changeCell(component, metricKey, m -> { MetricDto metric = getMetric(metricKey); double newValue = scale(metric, value); Double initialValue = m.getValue(); if (initialValue != null && Double.compare(initialValue, newValue) == 0) { return false; } m.setValue(newValue); Double initialVariation = m.getVariation(); if (initialValue != null && initialVariation != null) { double leakInitialValue = initialValue - initialVariation; m.setVariation(scale(metric, value - leakInitialValue)); } return true; }); }
private void assertThatMeasureHasValue(String componentUuid, Metric metric, int expectedValue) { Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(), componentUuid, metric.getKey()); assertThat(persisted).isPresent(); assertThat(persisted.get().getValue()).isEqualTo((double) expectedValue); }
void setValue(ComponentDto component, String metricKey, Rating value) { changeCell(component, metricKey, m -> { Double initialValue = m.getValue(); if (initialValue != null && Double.compare(initialValue, (double) value.getIndex()) == 0) { return false; } m.setData(value.name()); m.setValue((double) value.getIndex()); Double initialVariation = m.getVariation(); if (initialValue != null && initialVariation != null) { double leakInitialValue = initialValue - initialVariation; m.setVariation(value.getIndex() - leakInitialValue); } return true; }); }
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 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 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); }
@Test public void setValue_double_rounds_up_and_updates_value() { MetricDto metric = newMetricDto().setDecimalScale(2); LiveMeasureDto measure = newMeasure(metric, PROJECT).setValue(1.23); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT), asList(metric), asList(measure)); underTest.setValue(PROJECT, metric.getKey(), 3.14159); assertThat(underTest.getMeasure(PROJECT, metric.getKey()).get().getValue()).isEqualTo(3.14); assertThat(underTest.getChanged()).hasSize(1); underTest.setValue(PROJECT, metric.getKey(), 3.148); verifyValue(underTest, PROJECT, metric, 3.15); verifyVariation(underTest, PROJECT, metric, null); }
@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(); }
@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 measures_on_leak_period_are_persisted() { prepareProject(); measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().setVariation(42.0).createNoValue()); TestComputationStepContext context = new TestComputationStepContext(); step().execute(context); LiveMeasureDto persistedMeasure = selectMeasure("project-uuid", INT_METRIC).get(); assertThat(persistedMeasure.getValue()).isNull(); assertThat(persistedMeasure.getVariation()).isEqualTo(42.0); verifyStatistics(context, 1); }
@Test public void delete_measures_from_db_if_no_longer_computed() { prepareProject(); // measure to be updated LiveMeasureDto measureOnFileInProject = insertMeasure("file-uuid", "project-uuid", INT_METRIC); // measure to be deleted because not computed anymore LiveMeasureDto otherMeasureOnFileInProject = insertMeasure("file-uuid", "project-uuid", STRING_METRIC); // measure in another project, not touched LiveMeasureDto measureInOtherProject = insertMeasure("other-file-uuid", "other-project-uuid", INT_METRIC); db.commit(); measureRepository.addRawMeasure(REF_4, INT_METRIC.getKey(), newMeasureBuilder().create(42)); TestComputationStepContext context = new TestComputationStepContext(); step().execute(context); assertThatMeasureHasValue(measureOnFileInProject, 42); assertThatMeasureDoesNotExist(otherMeasureOnFileInProject); assertThatMeasureHasValue(measureInOtherProject, (int) measureInOtherProject.getValue().doubleValue()); verifyStatistics(context, 1); }
@Test public void insertOrUpdate() { // insert LiveMeasureDto dto = newLiveMeasure(); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); // update dto.setValue(dto.getValue() + 1); dto.setVariation(dto.getVariation() + 10); dto.setData(dto.getDataAsString() + "_new"); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); }
@Test public void pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST)); SnapshotDto analysis = db.components().insertSnapshot(branch); ComponentDto file = db.components().insertComponent(newFileDto(branch)); MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT")); LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d)); ComponentWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) .setParam(PARAM_PULL_REQUEST, "pr-123") .setParam(PARAM_METRIC_KEYS, complexity.getKey()) .executeProtobuf(ComponentWsResponse.class); assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest) .containsExactlyInAnyOrder(file.getKey(), "pr-123"); assertThat(response.getComponent().getMeasuresList()) .extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue())) .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue())); }