3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.component.ws;
22 import com.google.common.collect.Maps;
23 import java.util.Collections;
24 import java.util.List;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import org.apache.commons.lang.BooleanUtils;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.api.utils.text.JsonWriter;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.measure.LiveMeasureDto;
35 import org.sonar.db.metric.MetricDto;
36 import org.sonar.db.property.PropertyDto;
37 import org.sonar.db.property.PropertyQuery;
38 import org.sonar.server.user.UserSession;
40 import static org.sonar.api.measures.CoreMetrics.COVERAGE;
41 import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
42 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY;
43 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
44 import static org.sonar.api.measures.CoreMetrics.LINES;
45 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
46 import static org.sonar.api.measures.CoreMetrics.TESTS;
47 import static org.sonar.api.measures.CoreMetrics.TESTS_KEY;
48 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS;
49 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
51 public class ComponentViewerJsonWriter {
52 private static final List<String> METRIC_KEYS = List.of(
56 DUPLICATED_LINES_DENSITY_KEY,
59 private final DbClient dbClient;
61 public ComponentViewerJsonWriter(DbClient dbClient) {
62 this.dbClient = dbClient;
65 public void writeComponentWithoutFav(JsonWriter json, ComponentDto component, DbSession session, @Nullable String branch, @Nullable String pullRequest) {
66 json.prop("key", component.getKey());
67 json.prop("uuid", component.uuid());
68 json.prop("path", component.path());
69 json.prop("name", component.name());
70 json.prop("longName", component.longName());
71 json.prop("q", component.qualifier());
73 ComponentDto project = dbClient.componentDao().selectOrFailByUuid(session, component.branchUuid());
75 json.prop("project", project.getKey());
76 json.prop("projectName", project.longName());
78 json.prop("branch", branch);
80 if (pullRequest != null) {
81 json.prop("pullRequest", pullRequest);
85 public void writeComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session, @Nullable String branch,
86 @Nullable String pullRequest) {
87 writeComponentWithoutFav(json, component, session, branch, pullRequest);
89 List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
91 .setEntityUuid(component.uuid())
92 .setUserUuid(userSession.getUuid())
95 boolean isFavourite = propertyDtos.size() == 1;
96 json.prop("fav", isFavourite);
99 public void writeMeasures(JsonWriter json, ComponentDto component, DbSession session) {
100 Map<String, LiveMeasureDto> measuresByMetricKey = loadMeasuresGroupedByMetricKey(component, session);
102 json.name("measures").beginObject();
103 json.prop("lines", formatMeasure(measuresByMetricKey, LINES));
104 json.prop("coverage", formatMeasure(measuresByMetricKey, COVERAGE));
105 json.prop("duplicationDensity", formatMeasure(measuresByMetricKey, DUPLICATED_LINES_DENSITY));
106 json.prop("issues", formatMeasure(measuresByMetricKey, VIOLATIONS));
107 json.prop("tests", formatMeasure(measuresByMetricKey, TESTS));
111 private Map<String, LiveMeasureDto> loadMeasuresGroupedByMetricKey(ComponentDto component, DbSession dbSession) {
112 List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, METRIC_KEYS);
113 Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
114 List<LiveMeasureDto> measures = dbClient.liveMeasureDao()
115 .selectByComponentUuidsAndMetricUuids(dbSession, Collections.singletonList(component.uuid()), metricsByUuid.keySet());
116 return Maps.uniqueIndex(measures, m -> metricsByUuid.get(m.getMetricUuid()).getKey());
120 private static String formatMeasure(Map<String, LiveMeasureDto> measuresByMetricKey, Metric metric) {
121 LiveMeasureDto measure = measuresByMetricKey.get(metric.getKey());
122 return formatMeasure(measure, metric);
125 private static String formatMeasure(@Nullable LiveMeasureDto measure, Metric metric) {
126 if (measure == null) {
129 Double value = getDoubleValue(measure, metric);
131 return Double.toString(value);
137 private static Double getDoubleValue(LiveMeasureDto measure, Metric metric) {
138 Double value = measure.getValue();
139 if (BooleanUtils.isTrue(metric.isOptimizedBestValue()) && value == null) {
140 value = metric.getBestValue();