public LiveMeasureDto toLiveMeasureDto(Measure measure, Metric metric, Component component) { LiveMeasureDto out = new LiveMeasureDto(); out.setMetricId(metric.getId()); 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; }
@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()); } }
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); }
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); }
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; }); }
@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(); }
MetricDtoWithBestValue(MetricDto metric) { this.metric = metric; LiveMeasureDto measure = new LiveMeasureDto().setMetricId(metric.getId()); boolean isNewTypeMetric = metric.getKey().toLowerCase(Locale.ENGLISH).startsWith(LOWER_CASE_NEW_METRIC_PREFIX); if (isNewTypeMetric) { measure.setVariation(metric.getBestValue()); } else { measure.setValue(metric.getBestValue()); } this.bestValue = measure; }
private void changeCell(ComponentDto component, String metricKey, Function<LiveMeasureDto, Boolean> changer) { MeasureCell cell = table.get(component.uuid(), metricKey); if (cell == null) { LiveMeasureDto measure = new LiveMeasureDto() .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setMetricId(metricsByKeys.get(metricKey).getId()); cell = new MeasureCell(measure, true); table.put(component.uuid(), metricKey, cell); changer.apply(cell.getMeasure()); } else if (changer.apply(cell.getMeasure())) { cell.setChanged(true); } }
private LiveMeasureDto newMeasure(MetricDto metric, ComponentDto component) { return new LiveMeasureDto().setMetricId(metric.getId()).setData("foo").setComponentUuid(component.uuid()); } }
@Test public void toMeasure_returns_no_value_if_dta_has_data_in_wrong_case_for_Level_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("waRn"), SOME_LEVEL_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_metric_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure.getComponentUuid()), singletonList("_other_")); assertThat(selected).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); }
@DataProvider public static Object[][] all_types_LiveMeasureDtos() { return new Object[][] { {new LiveMeasureDto().setValue(1d), SOME_BOOLEAN_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_INT_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_LONG_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_DOUBLE_METRIC}, {new LiveMeasureDto().setData("1"), SOME_STRING_METRIC}, {new LiveMeasureDto().setData(Level.OK.name()), SOME_LEVEL_METRIC} }; }
@Test public void toMeasure_returns_long_part_of_value_in_dto_for_Long_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.5d), SOME_LONG_METRIC); assertThat(measure.isPresent()).isTrue(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG); assertThat(measure.get().getLongValue()).isEqualTo(1); }
@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); }
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); }
void setValue(ComponentDto component, String metricKey, @Nullable String data) { changeCell(component, metricKey, m -> { if (Objects.equals(m.getDataAsString(), data)) { return false; } m.setData(data); return true; }); }
@Test public void selectMeasure_map_fields() { 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).setVariation(0.1).setData("text_value")); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(result).as("Fail to map fields of %s", result.toString()).extracting( LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getVariation, LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue) .contains(project.uuid(), file.uuid(), metric.getId(), 3.14, 0.1, "text_value", "text_value"); }
@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); }
private LiveMeasureDto insertMeasure(String componentUuid, String projectUuid, Metric metric) { LiveMeasureDto measure = newLiveMeasure() .setComponentUuid(componentUuid) .setProjectUuid(projectUuid) .setMetricId(metricRepository.getByKey(metric.getKey()).getId()); dbClient.liveMeasureDao().insertOrUpdate(db.getSession(), measure); return measure; }