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: [PATCH] SONAR-17752 Move Lines of Codes in System section --- ...temSection.java => StatisticsSupport.java} | 23 ++++--------------- .../platform/SystemInfoWriterModule.java | 4 +--- .../monitoring/StandaloneSystemSection.java | 7 +++++- .../cluster/GlobalSystemSection.java | 7 +++++- ...onTest.java => StatisticsSupportTest.java} | 22 ++++++------------ .../StandaloneSystemSectionTest.java | 11 ++++++++- .../cluster/GlobalSystemSectionTest.java | 12 +++++++++- 7 files changed, 45 insertions(+), 41 deletions(-) rename server/sonar-webserver-core/src/main/java/org/sonar/server/platform/{monitoring/StatisticsSystemSection.java => StatisticsSupport.java} (66%) rename server/sonar-webserver-core/src/test/java/org/sonar/server/platform/{monitoring/StatisticsSystemSectionTest.java => StatisticsSupportTest.java} (68%) 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/StatisticsSupport.java similarity index 66% rename from server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java rename to server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StatisticsSupport.java index 006dbe62743..729e664856f 100644 --- 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/StatisticsSupport.java @@ -17,35 +17,21 @@ * 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; +package org.sonar.server.platform; 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 { +public class StatisticsSupport { private final DbClient dbClient; - public StatisticsSystemSection(DbClient dbClient) { + public StatisticsSupport(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(){ + public long getLinesOfCode(){ try (DbSession dbSession = dbClient.openSession(false)) { SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) @@ -53,5 +39,4 @@ public class StatisticsSystemSection implements SystemInfoSection { 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/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/monitoring/StatisticsSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java similarity index 68% rename from server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java rename to server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java index 5ece3b8e079..c02cd7d9179 100644 --- 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/StatisticsSupportTest.java @@ -17,39 +17,31 @@ * 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; +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 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 StatisticsSupportTest { -public class StatisticsSystemSectionTest { - - private DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); - - private final StatisticsSystemSection statisticsSystemSection = new StatisticsSystemSection(dbClient); + private final DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); + private final StatisticsSupport statisticsSupport = new StatisticsSupport(dbClient); @Test - public void shouldWriteProtobuf() { - + public void should_return_metric_from_liveMeasureDao() { 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); + 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/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) { -- 2.39.5