From a7a84e21235fd677162d11795df0ddc7a66af806 Mon Sep 17 00:00:00 2001 From: Benjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com> Date: Thu, 17 Nov 2022 16:16:24 +0100 Subject: [PATCH] SONAR-17578 Add LoC count in api/system/info --- .../platform/AbstractSystemInfoWriter.java | 2 +- .../platform/SystemInfoWriterModule.java | 4 +- .../monitoring/StatisticsSystemSection.java | 57 +++++++++++++++++++ .../platform/SystemInfoWriterModuleTest.java | 4 +- .../StatisticsSystemSectionTest.java | 55 ++++++++++++++++++ .../sonar/server/component/ws/AppAction.java | 3 +- 6 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java create mode 100644 server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java index 4bdc6e5a238..356368fbadb 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java @@ -28,7 +28,7 @@ import org.sonar.server.health.Health; public abstract class AbstractSystemInfoWriter implements SystemInfoWriter { private static final String[] ORDERED_SECTION_NAMES = { // standalone - "System", "Database", "Bundled", "Plugins", + "System", "Statistics", "Database", "Bundled", "Plugins", // cluster "Web JVM State", "Web Database Connection", "Web Logging", "Web JVM Properties", diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java index c0602d1315e..5ea367dadf5 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java @@ -32,6 +32,7 @@ import org.sonar.server.platform.monitoring.LoggingSection; import org.sonar.server.platform.monitoring.PluginsSection; import org.sonar.server.platform.monitoring.SettingsSection; import org.sonar.server.platform.monitoring.StandaloneSystemSection; +import org.sonar.server.platform.monitoring.StatisticsSystemSection; import org.sonar.server.platform.monitoring.cluster.AppNodesInfoLoaderImpl; import org.sonar.server.platform.monitoring.cluster.CeQueueGlobalSection; import org.sonar.server.platform.monitoring.cluster.EsClusterStateSection; @@ -64,7 +65,8 @@ public class SystemInfoWriterModule extends Module { SettingsSection.class, AlmConfigurationSection.class, ServerPushSection.class, - BundledSection.class + BundledSection.class, + StatisticsSystemSection.class ); if (standalone) { diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java new file mode 100644 index 00000000000..006dbe62743 --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java @@ -0,0 +1,57 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.measure.SumNclocDbQuery; +import org.sonar.process.systeminfo.SystemInfoSection; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; + +import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; + +public class StatisticsSystemSection implements SystemInfoSection { + + private final DbClient dbClient; + + public StatisticsSystemSection(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public ProtobufSystemInfo.Section toProtobuf() { + ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); + + protobuf.setName("Statistics"); + setAttribute(protobuf, "loc", getLoc()); + + return protobuf.build(); + } + + private long getLoc(){ + try (DbSession dbSession = dbClient.openSession(false)) { + SumNclocDbQuery query = SumNclocDbQuery.builder() + .setOnlyPrivateProjects(false) + .build(); + return dbClient.liveMeasureDao().sumNclocOfBiggestBranch(dbSession, query); + } + } + +} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java index 21f300bd07b..d8b1f404e93 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java @@ -35,7 +35,7 @@ public class SystemInfoWriterModuleTest { when(webServer.isStandalone()).thenReturn(false); ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getAddedObjects()).hasSize(20); + assertThat(container.getAddedObjects()).hasSize(21); } @Test @@ -44,6 +44,6 @@ public class SystemInfoWriterModuleTest { ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.getAddedObjects()).hasSize(14); + assertThat(container.getAddedObjects()).hasSize(15); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java new file mode 100644 index 00000000000..5ece3b8e079 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import org.junit.Test; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.measure.SumNclocDbQuery; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; + + +public class StatisticsSystemSectionTest { + + private DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); + + private final StatisticsSystemSection statisticsSystemSection = new StatisticsSystemSection(dbClient); + + @Test + public void shouldWriteProtobuf() { + + when(dbClient.liveMeasureDao().sumNclocOfBiggestBranch(any(DbSession.class), any(SumNclocDbQuery.class))).thenReturn(1800999L); + + ProtobufSystemInfo.Section protobuf = statisticsSystemSection.toProtobuf(); + long value = attribute(protobuf, "loc").getLongValue(); + + assertThat(value).isEqualTo(1800999L); + + } + + +} \ No newline at end of file diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java index 81a0a19503c..bd288f8e29a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java @@ -85,6 +85,7 @@ public class AppAction implements ComponentsWsAction { @Override public void handle(Request request, Response response) { + int a = 100; try (DbSession session = dbClient.openSession(false)) { ComponentDto component = loadComponent(session, request); userSession.checkComponentPermission(UserRole.USER, component); @@ -96,7 +97,7 @@ public class AppAction implements ComponentsWsAction { String branch = request.param(PARAM_BRANCH); String pullRequest = request.param(PARAM_PULL_REQUEST); String componentKey = request.mandatoryParam(PARAM_COMPONENT); - +int a = 9; return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest); } -- 2.39.5