]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17578 Add LoC count in api/system/info
authorBenjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com>
Thu, 17 Nov 2022 15:16:24 +0000 (16:16 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 17 Nov 2022 20:03:07 +0000 (20:03 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/AbstractSystemInfoWriter.java
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/StatisticsSystemSection.java [new file with mode: 0644]
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StatisticsSystemSectionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java

index 4bdc6e5a23825d1723bf0d2e2600f70404730f4d..356368fbadbfe18887690f527ac84eb9b23603ac 100644 (file)
@@ -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",
index c0602d1315ec436a4b3ece2933254dcaeaed2c42..5ea367dadf55ecbf7951dc57630a7e7a8d435d3a 100644 (file)
@@ -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 (file)
index 0000000..006dbe6
--- /dev/null
@@ -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);
+    }
+  }
+
+}
index 21f300bd07be753919cea64f3ac33bfa163e2769..d8b1f404e93eb8ac38b77906972a8efdc2935d2b 100644 (file)
@@ -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 (file)
index 0000000..5ece3b8
--- /dev/null
@@ -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
index 81a0a19503c1f7e51d5401f5ab8012b27a861735..bd288f8e29af9022f2b4018179bcdde333575ca1 100644 (file)
@@ -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);
   }