public Optional<Measure> toMeasure(@Nullable BatchReport.Measure batchMeasure, Metric metric) { Objects.requireNonNull(metric); if (batchMeasure == null) { return Optional.absent(); } Measure.NewMeasureBuilder builder = Measure.newMeasureBuilder(); String data = batchMeasure.hasStringValue() ? batchMeasure.getStringValue() : null; switch (metric.getType().getValueType()) { case INT: return toIntegerMeasure(builder, batchMeasure, data); case LONG: return toLongMeasure(builder, batchMeasure, data); case DOUBLE: return toDoubleMeasure(builder, batchMeasure, data); case BOOLEAN: return toBooleanMeasure(builder, batchMeasure, data); case STRING: return toStringMeasure(builder, batchMeasure); case LEVEL: return toLevelMeasure(builder, batchMeasure); case NO_VALUE: return toNoValueMeasure(builder, batchMeasure); default: throw new IllegalArgumentException( "Unsupported Measure.ValueType " + metric.getType().getValueType()); } }
@Test public void set_variation() { // Project SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient .measureDao() .insert( session, newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)); // Directory ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir"); dbClient.componentDao().insert(session, directoryDto); SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot); dbClient.snapshotDao().insert(session, period1DirectorySnapshot); dbClient .measureDao() .insert( session, newMeasureDto( ISSUES_METRIC.getId(), directoryDto.getId(), period1DirectorySnapshot.getId(), 10d)); session.commit(); periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot)); Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build(); Component project = ReportComponent.builder(Component.Type.PROJECT, 1) .setUuid(PROJECT_DTO.uuid()) .addChildren(directory) .build(); treeRootHolder.setRoot(project); addRawMeasure(project, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); addRawMeasure(directory, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null)); underTest.execute(); assertThat( measureRepository .getRawMeasure(project, ISSUES_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(20d); assertThat( measureRepository .getRawMeasure(directory, ISSUES_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(10d); }
@Test public void set_variations_on_all_periods() { SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); SnapshotDto period2ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); SnapshotDto period3ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); SnapshotDto period4ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); SnapshotDto period5ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); dbClient .snapshotDao() .insert( session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot); dbClient .measureDao() .insert( session, newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 0d), newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period2ProjectSnapshot.getId(), 20d), newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period3ProjectSnapshot.getId(), 40d), newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period4ProjectSnapshot.getId(), 80d), newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period5ProjectSnapshot.getId(), 100d)); session.commit(); periodsHolder.setPeriods( newPeriod(1, period1ProjectSnapshot), newPeriod(2, period2ProjectSnapshot), newPeriod(3, period3ProjectSnapshot), newPeriod(4, period4ProjectSnapshot), newPeriod(5, period5ProjectSnapshot)); treeRootHolder.setRoot(PROJECT); addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); underTest.execute(); assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1); Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get(); assertThat(measure.hasVariations()).isTrue(); assertThat(measure.getVariations().getVariation1()).isEqualTo(80d); assertThat(measure.getVariations().getVariation2()).isEqualTo(60d); assertThat(measure.getVariations().getVariation3()).isEqualTo(40d); assertThat(measure.getVariations().getVariation4()).isEqualTo(0d); assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d); }
private boolean isBestValueOptimized(Measure measure) { return measure.getDescription() == null && measure.getData() == null && !measure.hasQualityGateStatus() && hasNoVariation(measure) && (measure.getValueType() == NO_VALUE || isBestValue(measure, metric.getBestValue())); }
@Test public void do_nothing_when_no_raw_measure() { SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient .measureDao() .insert( session, newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)); session.commit(); periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot)); treeRootHolder.setRoot(PROJECT); underTest.execute(); assertThat(measureRepository.getRawMeasures(PROJECT).keys()).isEmpty(); }
private static boolean isBestValueOptimized(Metric metric) { return metric.isBestValueOptimized(); }
private static Condition createCondition(String metricKey) { Metric metric = mock(Metric.class); when(metric.getKey()).thenReturn(metricKey); return new Condition( metric, Condition.Operator.EQUALS.getDbValue(), "error threshold", "warn threshold", null); }
private void addRawMeasure(Component component, Metric metric, Measure measure) { measureRepository.addRawMeasure( component.getReportAttributes().getRef(), metric.getKey(), measure); }
@Test public void set_variation_on_all_numeric_metrics() { SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient .measureDao() .insert( session, newMeasureDto( ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d), newMeasureDto( DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d), newMeasureDto( FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 2d), newMeasureDto( BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 1d)); session.commit(); periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot)); treeRootHolder.setRoot(PROJECT); addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); addRawMeasure(PROJECT, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null)); addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, null)); addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null)); underTest.execute(); assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4); assertThat( measureRepository .getRawMeasure(PROJECT, ISSUES_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(20d); assertThat( measureRepository .getRawMeasure(PROJECT, DEBT_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(-5d); assertThat( measureRepository .getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(1d); assertThat( measureRepository .getRawMeasure(PROJECT, BUILD_BREAKER_METRIC) .get() .getVariations() .getVariation1()) .isEqualTo(-1d); }