diff options
author | Claire Villard <claire.villard@sonarsource.com> | 2024-09-17 17:53:01 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-10-09 20:02:46 +0000 |
commit | c94654fc488cdd8d8f875631f0534d75d36630ab (patch) | |
tree | 64d72cfed073fb7077013b3955bf2f74bdc9c71c /server/sonar-ce-task-projectanalysis/src | |
parent | 79e63e6affafb432602bd559de2782a9a291a822 (diff) | |
download | sonarqube-c94654fc488cdd8d8f875631f0534d75d36630ab.tar.gz sonarqube-c94654fc488cdd8d8f875631f0534d75d36630ab.zip |
SONAR-22925 Import/export of JSON measures
Diffstat (limited to 'server/sonar-ce-task-projectanalysis/src')
3 files changed, 29 insertions, 85 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepIT.java index 5a0cf87a89c..05c29ee6a13 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepIT.java @@ -33,6 +33,7 @@ import org.sonar.db.DbTester; import org.sonar.db.component.BranchDto; import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; +import org.sonar.db.measure.MeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.project.ProjectDto; @@ -50,10 +51,9 @@ public class ExportLiveMeasuresStepIT { public DbTester dbTester = DbTester.create(System2.INSTANCE); private final ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl(); - private final MutableMetricRepository metricRepository = new MutableMetricRepositoryImpl(); private final ProjectHolder projectHolder = mock(ProjectHolder.class); private final FakeDumpWriter dumpWriter = new FakeDumpWriter(); - private final ExportLiveMeasuresStep underTest = new ExportLiveMeasuresStep(dbTester.getDbClient(), projectHolder, componentRepository, metricRepository, dumpWriter); + private final ExportLiveMeasuresStep underTest = new ExportLiveMeasuresStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter); @Before public void before() { @@ -69,7 +69,6 @@ public class ExportLiveMeasuresStepIT { assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES)).isEmpty(); assertThat(logTester.logs(Level.DEBUG)).contains("0 live measures exported"); - assertThat(metricRepository.getRefByUuid()).isEmpty(); } @Test @@ -77,22 +76,22 @@ public class ExportLiveMeasuresStepIT { ComponentDto project = createProject(true); componentRepository.register(1, project.uuid(), false); MetricDto metric = dbTester.measures().insertMetric(m -> m.setKey("metric1").setValueType(INT.name())); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d)); + MeasureDto measure = dbTester.measures().insertMeasure(project, m -> m.addValue(metric.getKey(), 4711.0d)); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(project)); when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto() .setProjectUuid(project.uuid()) .setUuid(project.uuid()) .setKey("master") .setBranchType(BranchType.BRANCH))); + underTest.execute(new TestComputationStepContext()); List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES); assertThat(exportedMeasures).hasSize(1); assertThat(exportedMeasures) - .extracting(ProjectDump.LiveMeasure::getMetricRef, m -> m.getDoubleValue().getValue(), ProjectDump.LiveMeasure::hasVariation) - .containsOnly(tuple(0, 4711.0d, false)); + .extracting(ProjectDump.LiveMeasure::getJsonValue) + .containsOnly(measure.getJsonValue()); assertThat(logTester.logs(Level.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test @@ -100,26 +99,7 @@ public class ExportLiveMeasuresStepIT { ComponentDto project = createProject(false); componentRepository.register(1, project.uuid(), false); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name())); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d)); - when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(project)); - when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto() - .setProjectUuid(project.uuid()) - .setUuid(project.uuid()) - .setKey("master") - .setBranchType(BranchType.BRANCH))); - - underTest.execute(new TestComputationStepContext()); - - List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES); - assertThat(exportedMeasures).isEmpty(); - } - - @Test - public void do_not_export_measures_on_disabled_metrics() { - ComponentDto project = createProject(true); - componentRepository.register(1, project.uuid(), false); - MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setEnabled(false)); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d)); + dbTester.measures().insertMeasure(project, m -> m.addValue(metric.getKey(), 4711.0d)); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(project)); when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto() .setProjectUuid(project.uuid()) @@ -138,7 +118,7 @@ public class ExportLiveMeasuresStepIT { ComponentDto project = createProject(true); componentRepository.register(1, project.uuid(), false); MetricDto metric = dbTester.measures().insertMetric(m -> m.setKey("new_metric").setValueType(INT.name())); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(7.0d).setData("test")); + MeasureDto measure = dbTester.measures().insertMeasure(project, m -> m.addValue(metric.getKey(), "test")); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(project)); when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto() .setProjectUuid(project.uuid()) @@ -153,18 +133,11 @@ public class ExportLiveMeasuresStepIT { assertThat(exportedMeasures) .extracting( ProjectDump.LiveMeasure::getComponentRef, - ProjectDump.LiveMeasure::getMetricRef, - m -> m.getDoubleValue().getValue(), - ProjectDump.LiveMeasure::getTextValue, - m -> m.getVariation().getValue()) + ProjectDump.LiveMeasure::getJsonValue) .containsOnly(tuple( 1L, - 0, - 0.0d, - "test", - 7.0d)); + measure.getJsonValue())); assertThat(logTester.logs(Level.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test @@ -172,7 +145,7 @@ public class ExportLiveMeasuresStepIT { ComponentDto project = createProject(true); componentRepository.register(1, project.uuid(), false); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name())); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(null).setData((String) null)); + dbTester.measures().insertMeasure(project, m -> m.addValue(metric.getKey(), null)); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(project)); when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto() .setProjectUuid(project.uuid()) @@ -186,15 +159,9 @@ public class ExportLiveMeasuresStepIT { assertThat(exportedMeasures).hasSize(1); assertThat(exportedMeasures) .extracting( - ProjectDump.LiveMeasure::hasDoubleValue, - ProjectDump.LiveMeasure::getTextValue, - ProjectDump.LiveMeasure::hasVariation) - .containsOnly(tuple( - false, - "", - false)); + ProjectDump.LiveMeasure::getJsonValue) + .containsOnly("{}"); assertThat(logTester.logs(Level.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStep.java index 316a939a605..955730f5375 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStep.java @@ -19,7 +19,7 @@ */ package org.sonar.ce.task.projectexport.steps; -import com.sonarsource.governance.projectdump.protobuf.ProjectDump; +import com.sonarsource.governance.projectdump.protobuf.ProjectDump.LiveMeasure; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -30,32 +30,25 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import static java.lang.String.format; -import static org.apache.commons.lang3.StringUtils.defaultString; -import static org.sonar.db.DatabaseUtils.getDouble; -import static org.sonar.db.DatabaseUtils.getString; public class ExportLiveMeasuresStep implements ComputationStep { - private static final String QUERY = "select pm.metric_uuid, pm.component_uuid, pm.text_value, pm.value, m.name" + - " from live_measures pm" + - " join metrics m on m.uuid=pm.metric_uuid" + - " join components p on p.uuid = pm.component_uuid" + - " join components pp on pp.uuid = pm.project_uuid" + + private static final String QUERY = "select m.component_uuid, m.json_value" + + " from measures m" + + " join components p on p.uuid = m.component_uuid" + + " join components pp on pp.uuid = m.branch_uuid" + " join project_branches pb on pb.uuid=pp.uuid" + - " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and p.enabled=? and m.enabled=?"; + " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and p.enabled=?"; private final DbClient dbClient; private final ProjectHolder projectHolder; private final ComponentRepository componentRepository; - private final MutableMetricRepository metricHolder; private final DumpWriter dumpWriter; - public ExportLiveMeasuresStep(DbClient dbClient, ProjectHolder projectHolder, ComponentRepository componentRepository, - MutableMetricRepository metricHolder, DumpWriter dumpWriter) { + public ExportLiveMeasuresStep(DbClient dbClient, ProjectHolder projectHolder, ComponentRepository componentRepository, DumpWriter dumpWriter) { this.dbClient = dbClient; this.projectHolder = projectHolder; this.componentRepository = componentRepository; - this.metricHolder = metricHolder; this.dumpWriter = dumpWriter; } @@ -63,18 +56,17 @@ public class ExportLiveMeasuresStep implements ComputationStep { public void execute(Context context) { long count = 0L; try ( - StreamWriter<ProjectDump.LiveMeasure> output = dumpWriter.newStreamWriter(DumpElement.LIVE_MEASURES); + StreamWriter<LiveMeasure> output = dumpWriter.newStreamWriter(DumpElement.LIVE_MEASURES); DbSession dbSession = dbClient.openSession(false); PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY)) { + stmt.setString(1, projectHolder.projectDto().getUuid()); stmt.setBoolean(2, true); stmt.setBoolean(3, true); - stmt.setBoolean(4, true); try (ResultSet rs = stmt.executeQuery()) { - ProjectDump.LiveMeasure.Builder liveMeasureBuilder = ProjectDump.LiveMeasure.newBuilder(); - ProjectDump.DoubleValue.Builder doubleBuilder = ProjectDump.DoubleValue.newBuilder(); + LiveMeasure.Builder liveMeasureBuilder = LiveMeasure.newBuilder(); while (rs.next()) { - ProjectDump.LiveMeasure measure = convertToLiveMeasure(rs, liveMeasureBuilder, doubleBuilder); + LiveMeasure measure = convertToLiveMeasure(rs, liveMeasureBuilder); output.write(measure); count++; } @@ -85,24 +77,11 @@ public class ExportLiveMeasuresStep implements ComputationStep { } } - private ProjectDump.LiveMeasure convertToLiveMeasure(ResultSet rs, ProjectDump.LiveMeasure.Builder builder, - ProjectDump.DoubleValue.Builder doubleBuilder) throws SQLException { - long componentRef = componentRepository.getRef(rs.getString(2)); - int metricRef = metricHolder.add(rs.getString(1)); + private LiveMeasure convertToLiveMeasure(ResultSet rs, LiveMeasure.Builder builder) throws SQLException { builder .clear() - .setMetricRef(metricRef) - .setComponentRef(componentRef) - .setTextValue(defaultString(getString(rs, 3))); - Double value = getDouble(rs, 4); - String metricKey = getString(rs, 5); - if (value != null && metricKey != null) { - if (metricKey.startsWith("new_")) { - builder.setVariation(doubleBuilder.setValue(value).build()); - } else { - builder.setDoubleValue(doubleBuilder.setValue(value).build()); - } - } + .setComponentRef(componentRepository.getRef(rs.getString(1))) + .setJsonValue(rs.getString(2)); return builder.build(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto index 114fdda60f4..e9847cea4c1 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto +++ b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto @@ -80,10 +80,7 @@ message Measure { message LiveMeasure { int64 component_ref = 1; - int32 metric_ref = 2; - DoubleValue double_value = 3; - string text_value = 4; - DoubleValue variation = 5; + string json_value = 2; } message DoubleValue { @@ -256,3 +253,4 @@ enum Severity { INFO = 3; BLOCKER = 4; } + |