]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17752 Move Lines of Codes in System section
authorBenjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com>
Tue, 13 Dec 2022 14:36:32 +0000 (15:36 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 13 Dec 2022 20:03:18 +0000 (20:03 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StatisticsSupport.java [new file with mode: 0644]
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StatisticsSystemSection.java [deleted file]
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StatisticsSupportTest.java [new file with mode: 0644]
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java [deleted file]
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.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 (file)
index 0000000..729e664
--- /dev/null
@@ -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);
+    }
+  }
+}
index 5ea367dadf55ecbf7951dc57630a7e7a8d435d3a..fa73319f0bde80b1d6bac9fff064434ecddffab5 100644 (file)
@@ -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(
index 643f39eaf487d8d85d76ff3be4b4b4bb7c7f34c5..7d027c32f837599f971053bff9d4ded60e4a3166 100644 (file)
@@ -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 (file)
index 006dbe6..0000000
+++ /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);
-    }
-  }
-
-}
index c9e3386d81299ff0829d0c9b43de2be6bc7c61df..c69eda385e13aa9a842959d9749586675b6473cd 100644 (file)
@@ -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 (file)
index 0000000..c02cd7d
--- /dev/null
@@ -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
index 3b9f38757e1734272f812b5b4174e76b65b30abf..bbda228b4478378022a0ce5d4398c4e9b63daae1 100644 (file)
@@ -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 (file)
index 5ece3b8..0000000
+++ /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
index 258c7e0ad9fed6a8c66c87e9cb71e67fa07c8c22..0a69f7a297f4eb8058debc568c1303521df0e14c 100644 (file)
@@ -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) {