From f790b6c6f8d7ddc0d8dd73d76ac416932e1d9fe2 Mon Sep 17 00:00:00 2001 From: Benjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com> Date: Tue, 13 Dec 2022 15:36:32 +0100 Subject: SONAR-17752 Move Lines of Codes in System section --- .../sonar/server/platform/StatisticsSupport.java | 42 ++++++++++++++++ .../server/platform/SystemInfoWriterModule.java | 4 +- .../monitoring/StandaloneSystemSection.java | 7 ++- .../monitoring/StatisticsSystemSection.java | 57 ---------------------- .../monitoring/cluster/GlobalSystemSection.java | 7 ++- .../server/platform/StatisticsSupportTest.java | 47 ++++++++++++++++++ .../monitoring/StandaloneSystemSectionTest.java | 11 ++++- .../monitoring/StatisticsSystemSectionTest.java | 55 --------------------- .../cluster/GlobalSystemSectionTest.java | 12 ++++- 9 files changed, 123 insertions(+), 119 deletions(-) create mode 100644 server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StatisticsSupport.java delete 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/StatisticsSupportTest.java delete 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/StatisticsSupport.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StatisticsSupport.java new file mode 100644 index 00000000000..729e664856f --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StatisticsSupport.java @@ -0,0 +1,42 @@ +/* + * 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; + +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.measure.SumNclocDbQuery; + +public class StatisticsSupport { + + private final DbClient dbClient; + + public StatisticsSupport(DbClient dbClient) { + this.dbClient = dbClient; + } + + public long getLinesOfCode(){ + 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/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java index 5ea367dadf5..fa73319f0bd 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,7 +32,6 @@ 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; @@ -66,8 +65,7 @@ public class SystemInfoWriterModule extends Module { AlmConfigurationSection.class, ServerPushSection.class, BundledSection.class, - StatisticsSystemSection.class - + StatisticsSupport.class ); if (standalone) { add( diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java index 643f39eaf48..7d027c32f83 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java @@ -36,9 +36,11 @@ import org.sonar.server.authentication.IdentityProviderRepository; import org.sonar.server.log.ServerLogging; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.OfficialDistribution; +import org.sonar.server.platform.StatisticsSupport; import org.sonar.server.user.SecurityRealmFactory; import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; +import static org.sonar.api.measures.CoreMetrics.NCLOC; import static org.sonar.process.ProcessProperties.Property.PATH_DATA; import static org.sonar.process.ProcessProperties.Property.PATH_HOME; import static org.sonar.process.ProcessProperties.Property.PATH_TEMP; @@ -55,12 +57,13 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS private final ServerLogging serverLogging; private final OfficialDistribution officialDistribution; private final DockerSupport dockerSupport; + private final StatisticsSupport statisticsSupport; private final SonarRuntime sonarRuntime; public StandaloneSystemSection(Configuration config, SecurityRealmFactory securityRealmFactory, IdentityProviderRepository identityProviderRepository, Server server, ServerLogging serverLogging, - OfficialDistribution officialDistribution, DockerSupport dockerSupport, SonarRuntime sonarRuntime) { + OfficialDistribution officialDistribution, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, SonarRuntime sonarRuntime) { this.config = config; this.securityRealmFactory = securityRealmFactory; this.identityProviderRepository = identityProviderRepository; @@ -68,6 +71,7 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS this.serverLogging = serverLogging; this.officialDistribution = officialDistribution; this.dockerSupport = dockerSupport; + this.statisticsSupport = statisticsSupport; this.sonarRuntime = sonarRuntime; } @@ -127,6 +131,7 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS setAttribute(protobuf, "Server ID", server.getId()); setAttribute(protobuf, "Version", getVersion()); setAttribute(protobuf, "Edition", sonarRuntime.getEdition().getLabel()); + setAttribute(protobuf, NCLOC.getName(), statisticsSupport.getLinesOfCode()); setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker()); setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); 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 deleted file mode 100644 index 006dbe62743..00000000000 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java index c9e3386d812..c69eda385e1 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java @@ -36,9 +36,11 @@ import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.authentication.IdentityProviderRepository; import org.sonar.server.platform.DockerSupport; +import org.sonar.server.platform.StatisticsSupport; import org.sonar.server.user.SecurityRealmFactory; import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE; +import static org.sonar.api.measures.CoreMetrics.NCLOC; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @ServerSide @@ -50,16 +52,18 @@ public class GlobalSystemSection implements SystemInfoSection, Global { private final SecurityRealmFactory securityRealmFactory; private final IdentityProviderRepository identityProviderRepository; private final DockerSupport dockerSupport; + private final StatisticsSupport statisticsSupport; private final SonarRuntime sonarRuntime; public GlobalSystemSection(Configuration config, Server server, SecurityRealmFactory securityRealmFactory, - IdentityProviderRepository identityProviderRepository, DockerSupport dockerSupport, SonarRuntime sonarRuntime) { + IdentityProviderRepository identityProviderRepository, DockerSupport dockerSupport, StatisticsSupport statisticsSupport, SonarRuntime sonarRuntime) { this.config = config; this.server = server; this.securityRealmFactory = securityRealmFactory; this.identityProviderRepository = identityProviderRepository; this.dockerSupport = dockerSupport; + this.statisticsSupport = statisticsSupport; this.sonarRuntime = sonarRuntime; } @@ -70,6 +74,7 @@ public class GlobalSystemSection implements SystemInfoSection, Global { setAttribute(protobuf, "Server ID", server.getId()); setAttribute(protobuf, "Edition", sonarRuntime.getEdition().getLabel()); + setAttribute(protobuf, NCLOC.getName() ,statisticsSupport.getLinesOfCode()); setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker()); setAttribute(protobuf, "High Availability", true); setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java new file mode 100644 index 00000000000..c02cd7d9179 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java @@ -0,0 +1,47 @@ +/* + * 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; + +import org.junit.Test; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.measure.SumNclocDbQuery; + +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; + +public class StatisticsSupportTest { + + private final DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); + private final StatisticsSupport statisticsSupport = new StatisticsSupport(dbClient); + + @Test + public void should_return_metric_from_liveMeasureDao() { + when(dbClient.liveMeasureDao().sumNclocOfBiggestBranch(any(DbSession.class), any(SumNclocDbQuery.class))).thenReturn(1800999L); + + long linesOfCode = statisticsSupport.getLinesOfCode(); + + assertThat(linesOfCode).isEqualTo(1800999L); + } + +} \ No newline at end of file diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java index 3b9f38757e1..bbda228b447 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java @@ -39,6 +39,7 @@ import org.sonar.server.authentication.TestIdentityProvider; import org.sonar.server.log.ServerLogging; import org.sonar.server.platform.DockerSupport; import org.sonar.server.platform.OfficialDistribution; +import org.sonar.server.platform.StatisticsSupport; import org.sonar.server.user.SecurityRealmFactory; import static org.assertj.core.api.Assertions.assertThat; @@ -63,11 +64,12 @@ public class StandaloneSystemSectionTest { private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class); private OfficialDistribution officialDistribution = mock(OfficialDistribution.class); private DockerSupport dockerSupport = mock(DockerSupport.class); + private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); private SonarRuntime sonarRuntime = mock(SonarRuntime.class); private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server, - serverLogging, officialDistribution, dockerSupport, sonarRuntime); + serverLogging, officialDistribution, dockerSupport, statisticsSupport, sonarRuntime); @Before public void setUp() { @@ -180,6 +182,13 @@ public class StandaloneSystemSectionTest { assertThatAttributeIs(protobuf, "Force authentication", false); } + @Test + public void return_Lines_of_Codes_from_StatisticsSupport(){ + when(statisticsSupport.getLinesOfCode()).thenReturn(17752L); + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + assertThatAttributeIs(protobuf,"Lines of Code", 17752L); + } + @Test @UseDataProvider("trueOrFalse") public void return_docker_flag_from_DockerSupport(boolean flag) { 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 deleted file mode 100644 index 5ece3b8e079..00000000000 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java index 258c7e0ad9f..0a69f7a297f 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java @@ -35,6 +35,7 @@ import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.authentication.IdentityProviderRepositoryRule; import org.sonar.server.authentication.TestIdentityProvider; import org.sonar.server.platform.DockerSupport; +import org.sonar.server.platform.StatisticsSupport; import org.sonar.server.user.SecurityRealmFactory; import static org.assertj.core.api.Assertions.assertThat; @@ -55,10 +56,12 @@ public class GlobalSystemSectionTest { private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class); private DockerSupport dockerSupport = mock(DockerSupport.class); + private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class); private SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(), - server, securityRealmFactory, identityProviderRepository, dockerSupport, sonarRuntime); + server, securityRealmFactory, identityProviderRepository, dockerSupport, statisticsSupport, sonarRuntime); @Before public void setUp() { @@ -142,6 +145,13 @@ public class GlobalSystemSectionTest { assertThatAttributeIs(protobuf, "Force authentication", false); } + @Test + public void return_Lines_of_Codes_from_StatisticsSupport(){ + when(statisticsSupport.getLinesOfCode()).thenReturn(17752L); + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + assertThatAttributeIs(protobuf,"Lines of Code", 17752L); + } + @Test @UseDataProvider("trueOrFalse") public void get_docker_flag(boolean flag) { -- cgit v1.2.3