aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOrlovAlexander <alexander.orlov@sonarsource.com>2024-12-04 15:44:45 +0100
committersonartech <sonartech@sonarsource.com>2024-12-04 20:03:23 +0000
commit14937df5136fce44296521061f11c571919fc854 (patch)
tree30aec037add8ca750f28dfd1d5005744d60c2c47
parent33ab48ae6fc78cd3e67029d402dc1861ea787251 (diff)
downloadsonarqube-14937df5136fce44296521061f11c571919fc854.tar.gz
sonarqube-14937df5136fce44296521061f11c571919fc854.zip
SONAR-23736 Add application report Telemetry
-rw-r--r--server/sonar-db-dao/src/it/java/org/sonar/db/report/ReportSubscriptionDaoIT.java26
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionDao.java4
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionMapper.java2
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/report/ReportSubscriptionMapper.xml10
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProvider.java46
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProviderTest.java51
-rw-r--r--server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java4
7 files changed, 143 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/report/ReportSubscriptionDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/report/ReportSubscriptionDaoIT.java
index dd55a3f2250..2fbee908d06 100644
--- a/server/sonar-db-dao/src/it/java/org/sonar/db/report/ReportSubscriptionDaoIT.java
+++ b/server/sonar-db-dao/src/it/java/org/sonar/db/report/ReportSubscriptionDaoIT.java
@@ -27,6 +27,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -152,6 +154,30 @@ class ReportSubscriptionDaoIT {
assertThat(reportSubscriptionDtos).isPresent().get().extracting(ReportSubscriptionDto::getUuid).isEqualTo("uuid3");
}
+ @Test
+ void countByQualifier_shouldReturnCorrectValue() {
+ ProjectData projectData1 = db.components().insertPrivateProject( p -> p.setQualifier("APP"));
+ ComponentDto mainBranch1 = projectData1.getMainBranchComponent();
+ ProjectData projectData2 = db.components().insertPrivateProject( p -> p.setQualifier("TRK"));
+ ComponentDto mainBranch2 = projectData2.getMainBranchComponent();
+
+ ComponentDto branch1 = db.components().insertProjectBranch(mainBranch1);
+ ComponentDto branch2 = db.components().insertProjectBranch(mainBranch2);
+
+ ReportSubscriptionDto subscriptionBranch1 = createSubscriptionDto("uuid2").setBranchUuid(branch1.branchUuid()).setUserUuid("userUuid2");
+ ReportSubscriptionDto subscriptionBranch2 = createSubscriptionDto("uuid3").setBranchUuid(branch2.branchUuid()).setUserUuid("userUuid3");
+ ReportSubscriptionDto subscriptionBranch3 = createSubscriptionDto("uuid4").setBranchUuid(branch2.branchUuid()).setUserUuid("userUuid4");
+ ReportSubscriptionDto subscriptionBranch4 = createSubscriptionDto("uuid").setPortfolioUuid("pf_uuid").setUserUuid("userUuid");
+
+ underTest.insert(db.getSession(), subscriptionBranch1);
+ underTest.insert(db.getSession(), subscriptionBranch2);
+ underTest.insert(db.getSession(), subscriptionBranch3);
+ underTest.insert(db.getSession(), subscriptionBranch4);
+
+ assertThat(underTest.countByQualifier(db.getSession(), "APP")).isEqualTo(1);
+ assertThat(underTest.countByQualifier(db.getSession(), "TRK")).isEqualTo(2);
+ }
+
@NotNull
private static ReportSubscriptionDto createSubscriptionDto(String uuid) {
return new ReportSubscriptionDto().setUuid(uuid);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionDao.java
index 69e961a36b0..e55b2c24f9f 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionDao.java
@@ -61,4 +61,8 @@ public class ReportSubscriptionDao implements Dao {
private static ReportSubscriptionMapper mapper(DbSession dbSession) {
return dbSession.getMapper(ReportSubscriptionMapper.class);
}
+
+ public int countByQualifier(DbSession dbSession, String qualifier) {
+ return mapper(dbSession).countByQualifier(qualifier);
+ }
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionMapper.java
index c9438fc7e27..15e79038d4f 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/report/ReportSubscriptionMapper.java
@@ -38,4 +38,6 @@ public interface ReportSubscriptionMapper {
void insert(ReportSubscriptionDto subscriptionDto);
void delete(ReportSubscriptionDto subscriptionDto);
+
+ int countByQualifier(String qualifier);
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/report/ReportSubscriptionMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/report/ReportSubscriptionMapper.xml
index ff19bcb53e7..6afd40d7a04 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/report/ReportSubscriptionMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/report/ReportSubscriptionMapper.xml
@@ -55,6 +55,16 @@
and branch_uuid = #{branchUuid,jdbcType=VARCHAR}
</select>
+ <select id="countByQualifier" parameterType="java.lang.String" resultType="int">
+ SELECT count(1)
+ FROM report_subscriptions rs
+ INNER JOIN project_branches pb
+ ON rs.branch_uuid = pb.uuid
+ INNER JOIN projects p
+ ON pb.project_uuid = p.uuid
+ WHERE p.qualifier = #{qualifier,jdbcType=VARCHAR}
+ </select>
+
<delete id="delete" parameterType="map">
delete from report_subscriptions
where <include refid="filterConditions"/>
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProvider.java
new file mode 100644
index 00000000000..87be470e976
--- /dev/null
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProvider.java
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.notification.email.telemetry;
+
+import java.util.Optional;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentQualifiers;
+import org.sonar.telemetry.core.AbstractTelemetryDataProvider;
+import org.sonar.telemetry.core.Dimension;
+import org.sonar.telemetry.core.Granularity;
+import org.sonar.telemetry.core.TelemetryDataType;
+
+public class TelemetryApplicationSubscriptionsProvider extends AbstractTelemetryDataProvider<Integer> {
+ private static final String METRIC_KEY = "application_report_pdf_subscriptions";
+ private final DbClient dbClient;
+
+ public TelemetryApplicationSubscriptionsProvider(DbClient dbClient) {
+ super(METRIC_KEY, Dimension.INSTALLATION, Granularity.DAILY, TelemetryDataType.INTEGER);
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public Optional<Integer> getValue() {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ return Optional.of(dbClient.reportSubscriptionDao().countByQualifier(dbSession, ComponentQualifiers.APP));
+ }
+ }
+}
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProviderTest.java
new file mode 100644
index 00000000000..2e3dc01bb5b
--- /dev/null
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationSubscriptionsProviderTest.java
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.notification.email.telemetry;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.db.DbClient;
+import org.sonar.db.report.ReportSubscriptionDao;
+import org.sonar.telemetry.core.Dimension;
+import org.sonar.telemetry.core.Granularity;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class TelemetryApplicationSubscriptionsProviderTest {
+
+ private final DbClient dbClient = mock(DbClient.class);
+ private final ReportSubscriptionDao reportSubscriptionDao = mock(ReportSubscriptionDao.class);
+
+ @Test
+ void testGetters() {
+ when(dbClient.reportSubscriptionDao()).thenReturn(reportSubscriptionDao);
+ TelemetryApplicationSubscriptionsProvider underTest = new TelemetryApplicationSubscriptionsProvider(dbClient);
+
+ assertThat(underTest.getMetricKey()).isEqualTo("application_report_pdf_subscriptions");
+ assertThat(underTest.getDimension()).isEqualTo(Dimension.INSTALLATION);
+ assertThat(underTest.getGranularity()).isEqualTo(Granularity.DAILY);
+ when(reportSubscriptionDao.countByQualifier(any(), any())).thenReturn(42);
+ assertThat(underTest.getValue()).contains(42);
+ when(reportSubscriptionDao.countByQualifier(any(), any())).thenReturn(0);
+ assertThat(underTest.getValue()).contains(0);
+ }
+}
diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
index c961ecbf756..78a1ce5e7f0 100644
--- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
+++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
@@ -181,6 +181,7 @@ import org.sonar.server.newcodeperiod.ws.NewCodePeriodsWsModule;
import org.sonar.server.notification.NotificationModule;
import org.sonar.server.notification.email.telemetry.EmailConfigAuthMethodTelemetryProvider;
import org.sonar.server.notification.email.telemetry.EmailConfigHostTelemetryProvider;
+import org.sonar.server.notification.email.telemetry.TelemetryApplicationSubscriptionsProvider;
import org.sonar.server.notification.ws.NotificationWsModule;
import org.sonar.server.permission.index.PermissionIndexer;
import org.sonar.server.permission.ws.PermissionsWsModule;
@@ -682,6 +683,9 @@ public class PlatformLevel4 extends PlatformLevel {
TelemetryFipsEnabledProvider.class,
TelemetryPortfolioConfidentialFlagProvider.class,
+ // Reports telemetry
+ TelemetryApplicationSubscriptionsProvider.class,
+
// telemetry
TelemetryMetricsLoader.class,
TelemetryDataLoaderImpl.class,