From e385f53491d6bfdcf4f3a44656a93b17dc5e3096 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 14 Jan 2016 18:48:30 +0100 Subject: [PATCH] SONAR-7135 WS api/measures/component_tree baseComponent has measures --- .../measure/ws/ComponentTreeAction.java | 6 +++++- .../measure/ws/ComponentTreeDataLoader.java | 12 +++++++++--- .../measure/ws/component_tree-example.json | 19 +++++++++++++++++-- .../measure/ws/ComponentTreeActionTest.java | 15 +++++++++++---- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index e13cc234ffc..77d88aca131 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -173,7 +173,11 @@ public class ComponentTreeAction implements MeasuresWsAction { .setTotal(paging.total()) .build(); - response.setBaseComponent(componentDtoToWsComponent(data.getBaseComponent())); + response.setBaseComponent( + componentDtoToWsComponent( + data.getBaseComponent(), + data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()), + data.getReferenceComponentUuidsById())); for (ComponentDto componentDto : data.getComponents()) { response.addComponents(componentDtoToWsComponent( diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index 176db2a75b9..e05441ac0ab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -106,7 +106,8 @@ public class ComponentTreeDataLoader { int componentCount = componentDtosAndTotal.total; List metrics = searchMetrics(dbSession, wsRequest); List periods = periodsFromSnapshot(baseSnapshot); - Table measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, components, metrics, periods); + Table measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, baseComponent, baseSnapshot, components, metrics, + periods); components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric); components = paginateComponents(components, componentCount, wsRequest); @@ -174,9 +175,14 @@ public class ComponentTreeDataLoader { return metrics; } - private Table searchMeasuresByComponentUuidAndMetric(DbSession dbSession, List components, List metrics, + private Table searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, SnapshotDto baseSnapshot, + List components, List metrics, List periods) { - Map componentsBySnapshotId = Maps.uniqueIndex(components, ComponentDtoWithSnapshotIdToSnapshotIdFunction.INSTANCE); + Map componentsBySnapshotId = new HashMap<>(); + componentsBySnapshotId.put(baseSnapshot.getId(), baseComponent); + for (ComponentDtoWithSnapshotId component : components) { + componentsBySnapshotId.put(component.getSnapshotId(), component); + } Map metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); List measureDtos = dbClient.measureDao().selectBySnapshotIdsAndMetricIds(dbSession, diff --git a/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json b/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json index 1db85e0e61d..46d75e0884a 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json @@ -8,7 +8,23 @@ "id": "project-id", "key": "MY_PROJECT", "name": "My Project", - "qualifier": "TRK" + "qualifier": "TRK", + "measures": [ + { + "metric": "new_violations", + "variationValueP1": "255", + "variationValueP2": "0", + "variationValueP3": "255" + }, + { + "metric": "ncloc", + "value": "1984" + }, + { + "metric": "complexity", + "value": "42" + } + ] }, "components": [ { @@ -43,7 +59,6 @@ "measures": [ { "metric": "new_violations", - "value": "0", "variationValueP1": "0", "variationValueP2": "0", "variationValueP3": "0" diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index a89f3e8a78b..e0ebbe0740b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -405,7 +405,9 @@ public class ComponentTreeActionTest { .setValue(12.0d), newMeasureDto(complexity, directorySnapshot.getId()) .setValue(35.0d) - .setVariation(2, 0.0d)); + .setVariation(2, 0.0d), + newMeasureDto(complexity, projectSnapshot.getId()) + .setValue(42.0d)); MetricDto ncloc = insertNclocMetric(); dbClient.measureDao().insert(dbSession, @@ -413,7 +415,9 @@ public class ComponentTreeActionTest { .setValue(114.0d), newMeasureDto(ncloc, directorySnapshot.getId()) .setValue(217.0d) - .setVariation(2, 0.0d)); + .setVariation(2, 0.0d), + newMeasureDto(ncloc, projectSnapshot.getId()) + .setValue(1984.0d)); MetricDto newViolations = insertNewViolationsMetric(); dbClient.measureDao().insert(dbSession, @@ -422,14 +426,17 @@ public class ComponentTreeActionTest { .setVariation(2, 0.0d) .setVariation(3, 25.0d), newMeasureDto(newViolations, file2Snapshot.getId()) - .setValue(0.0d) .setVariation(1, 0.0d) .setVariation(2, 0.0d) .setVariation(3, 0.0d), newMeasureDto(newViolations, directorySnapshot.getId()) .setVariation(1, 25.0d) .setVariation(2, 0.0d) - .setVariation(3, 25.0d)); + .setVariation(3, 25.0d), + newMeasureDto(newViolations, projectSnapshot.getId()) + .setVariation(1, 255.0d) + .setVariation(2, 0.0d) + .setVariation(3, 255.0d)); db.commit(); } -- 2.39.5