diff options
author | OrlovAlexander <alexander.orlov@sonarsource.com> | 2024-12-10 11:02:07 +0100 |
---|---|---|
committer | Steve Marion <steve.marion@sonarsource.com> | 2024-12-18 11:13:21 +0100 |
commit | d0ace9eba4fcb4e79eb10a57f1f596e4237702f7 (patch) | |
tree | 3dbaafb122219407d233a2dbda5850222dae45e5 /server/sonar-server-common | |
parent | bd488052eb3e86a10cdc9c4c10f2ec51d6a97856 (diff) | |
download | sonarqube-d0ace9eba4fcb4e79eb10a57f1f596e4237702f7.tar.gz sonarqube-d0ace9eba4fcb4e79eb10a57f1f596e4237702f7.zip |
SONAR-23734 Total number of applications Telemetry
Diffstat (limited to 'server/sonar-server-common')
2 files changed, 95 insertions, 0 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProvider.java new file mode 100644 index 00000000000..3e18a22bbfe --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProvider.java @@ -0,0 +1,45 @@ +/* + * 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.telemetry.core.AbstractTelemetryDataProvider; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataType; + +public class TelemetryApplicationsCountProvider extends AbstractTelemetryDataProvider<Integer> { + private static final String METRIC_KEY = "applications_total_count"; + private final DbClient dbClient; + + protected TelemetryApplicationsCountProvider(DbClient dbClient) { + super(METRIC_KEY, Dimension.INSTALLATION, Granularity.ADHOC, TelemetryDataType.INTEGER); + this.dbClient = dbClient; + } + + @Override + public Optional<Integer> getValue() { + try (DbSession dbSession = dbClient.openSession(false)) { + return Optional.of(dbClient.projectDao().countApplications(dbSession)); + } + } +} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProviderTest.java new file mode 100644 index 00000000000..29bf7fae6e1 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/TelemetryApplicationsCountProviderTest.java @@ -0,0 +1,50 @@ +/* + * 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.project.ProjectDao; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataType; + +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 TelemetryApplicationsCountProviderTest { + private final DbClient dbClient = mock(DbClient.class); + + @Test + void testGetters() { + TelemetryApplicationsCountProvider underTest = new TelemetryApplicationsCountProvider(dbClient); + ProjectDao projectDao = mock(ProjectDao.class); + when(dbClient.projectDao()).thenReturn(projectDao); + when(projectDao.countApplications(any())).thenReturn(42); + assertThat(underTest.getMetricKey()).isEqualTo("applications_total_count"); + assertThat(underTest.getDimension()).isEqualTo(Dimension.INSTALLATION); + assertThat(underTest.getGranularity()).isEqualTo(Granularity.ADHOC); + assertThat(underTest.getType()).isEqualTo(TelemetryDataType.INTEGER); + assertThat(underTest.getValue()).contains(42); + } + +} |