From cd40a38f951a1355c086b99017401b93bd203558 Mon Sep 17 00:00:00 2001 From: lukasz-jarocki-sonarsource Date: Fri, 6 Sep 2024 08:47:33 +0200 Subject: [PATCH] SONAR-22891 added new telemetry metric --- .../step/MigrationStepsExecutorImpl.java | 8 ++- ...emetryDbMigrationStepDurationProvider.java | 66 +++++++++++++++++++ .../MigrationContainerPopulatorImplTest.java | 2 + .../engine/MigrationEngineImplTest.java | 5 +- .../step/MigrationStepsExecutorImplTest.java | 4 +- ...ryDbMigrationStepDurationProviderTest.java | 50 ++++++++++++++ .../telemetry/core/TelemetryDataProvider.java | 14 ++-- .../metrics/TelemetryMetricsLoaderIT.java | 2 +- .../metrics/TelemetryMetricsMapper.java | 23 +++++-- .../metrics/TelemetryMetricsMapperTest.java | 20 ++++++ .../telemetry/metrics/TestTelemetryBean.java | 2 +- ...ojectCppAutoconfigTelemetryProviderIT.java | 16 ++--- .../TelemetryUserEnabledProviderIT.java | 16 ++--- .../platformlevel/PlatformLevel2.java | 2 + ...ProjectCppAutoconfigTelemetryProvider.java | 2 +- .../telemetry/TelemetryNclocProvider.java | 2 +- .../TelemetryUserEnabledProvider.java | 2 +- .../platformlevel/PlatformLevel2Test.java | 6 +- .../TelemetryFipsEnabledProviderTest.java | 2 - .../telemetry/TelemetryNclocProviderTest.java | 12 ++-- .../TelemetryVersionProviderTest.java | 2 - 21 files changed, 208 insertions(+), 50 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java index bbccc564367..934b7202285 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java @@ -26,6 +26,7 @@ import org.sonar.core.platform.Container; import org.sonar.core.util.logs.Profiler; import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState; import org.sonar.server.platform.db.migration.history.MigrationHistory; +import org.sonar.server.telemetry.TelemetryDbMigrationStepDurationProvider; import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; @@ -45,16 +46,18 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor { private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider; private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider; private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider; + private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider; public MigrationStepsExecutorImpl(Container migrationContainer, MigrationHistory migrationHistory, MutableDatabaseMigrationState databaseMigrationState, TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider, TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider, - TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider) { + TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider, TelemetryDbMigrationStepDurationProvider stepDurationProvider) { this.migrationContainer = migrationContainer; this.migrationHistory = migrationHistory; this.databaseMigrationState = databaseMigrationState; this.telemetryDbMigrationTotalTimeProvider = telemetryDbMigrationTotalTimeProvider; this.telemetryDbMigrationStepsProvider = telemetryDbMigrationStepsProvider; this.telemetryDbMigrationSuccessProvider = telemetryDbMigrationSuccessProvider; + this.telemetryDbMigrationStepDurationProvider = stepDurationProvider; } @Override @@ -109,11 +112,12 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor { throw new MigrationStepExecutionException(step, e); } finally { if (done) { - stepProfiler.stopInfo(STEP_STOP_PATTERN, + long durationInMiliseconds = stepProfiler.stopInfo(STEP_STOP_PATTERN, databaseMigrationState.getCompletedMigrations() + 1, databaseMigrationState.getTotalMigrations(), step, "success"); + telemetryDbMigrationStepDurationProvider.addCompletedStep(step.getMigrationNumber(), durationInMiliseconds); } else { stepProfiler.stopError(STEP_STOP_PATTERN, databaseMigrationState.getCompletedMigrations() + 1, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java new file mode 100644 index 00000000000..06ac499de9c --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java @@ -0,0 +1,66 @@ +/* + * 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.telemetry; + +import java.util.HashMap; +import java.util.Map; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +public class TelemetryDbMigrationStepDurationProvider implements TelemetryDataProvider { + + private final Map dbMigrationStepDurations = new HashMap<>(); + + @Override + public String getMetricKey() { + return "db_migration_step_duration"; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + @Override + public Granularity getGranularity() { + return Granularity.ADHOC; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.INTEGER; + } + + @Override + public Map getValues() { + return dbMigrationStepDurations; + } + + @Override + public void after() { + dbMigrationStepDurations.clear(); + } + + public void addCompletedStep(Long migrationId, Long duration) { + dbMigrationStepDurations.put(migrationId.toString(), duration); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java index c1af7bacb2f..d74b459fa4b 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java @@ -27,6 +27,7 @@ import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; +import org.sonar.server.telemetry.TelemetryDbMigrationStepDurationProvider; import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; @@ -57,6 +58,7 @@ public class MigrationContainerPopulatorImplTest { migrationContainer.add(mock(TelemetryDbMigrationStepsProvider.class)); migrationContainer.add(mock(TelemetryDbMigrationTotalTimeProvider.class)); migrationContainer.add(mock(TelemetryDbMigrationSuccessProvider.class)); + migrationContainer.add(mock(TelemetryDbMigrationStepDurationProvider.class)); migrationContainer.startComponents(); underTest.populateContainer(migrationContainer); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java index db672f507fa..9462d6b7f49 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java @@ -31,6 +31,7 @@ import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.NoOpMigrationStatusListener; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; +import org.sonar.server.telemetry.TelemetryDbMigrationStepDurationProvider; import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; @@ -51,6 +52,7 @@ class MigrationEngineImplTest { private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider(); private final TelemetryDbMigrationStepsProvider telemetryUpgradeStepsProvider = new TelemetryDbMigrationStepsProvider(); private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider(); + private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider = new TelemetryDbMigrationStepDurationProvider(); private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps); @BeforeEach @@ -62,6 +64,7 @@ class MigrationEngineImplTest { serverContainer.add(migrationHistory); serverContainer.add(stepRegistry); serverContainer.add(databaseMigrationState); + serverContainer.add(telemetryDbMigrationStepDurationProvider); serverContainer.startComponents(); } @@ -97,7 +100,7 @@ class MigrationEngineImplTest { private record TestMigrationStep(StepRegistry registry) implements MigrationStep { @Override - public void execute() throws SQLException { + public void execute() { registry.stepRan = true; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java index 643bb332373..a7a40fb45ce 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java @@ -35,6 +35,7 @@ import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState; import org.sonar.server.platform.db.migration.engine.MigrationContainer; import org.sonar.server.platform.db.migration.engine.SimpleMigrationContainer; import org.sonar.server.platform.db.migration.history.MigrationHistory; +import org.sonar.server.telemetry.TelemetryDbMigrationStepDurationProvider; import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; @@ -60,8 +61,9 @@ class MigrationStepsExecutorImplTest { private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider(); private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider = new TelemetryDbMigrationStepsProvider(); private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider(); + private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider = new TelemetryDbMigrationStepDurationProvider(); private final MigrationStepsExecutorImpl underTest = new MigrationStepsExecutorImpl(migrationContainer, migrationHistory, databaseMigrationState, - telemetryDbMigrationTotalTimeProvider, telemetryDbMigrationStepsProvider, telemetryDbMigrationSuccessProvider); + telemetryDbMigrationTotalTimeProvider, telemetryDbMigrationStepsProvider, telemetryDbMigrationSuccessProvider, telemetryDbMigrationStepDurationProvider); private final NoOpMigrationStatusListener migrationStatusListener = mock(); @BeforeEach diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java new file mode 100644 index 00000000000..7ef33356421 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.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.telemetry; + +import java.util.Map; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.ADHOC; +import static org.sonar.telemetry.core.TelemetryDataType.INTEGER; + +class TelemetryDbMigrationStepDurationProviderTest { + + @Test + void testGetters() { + TelemetryDbMigrationStepDurationProvider underTest = new TelemetryDbMigrationStepDurationProvider(); + + underTest.addCompletedStep(1L, 10L); + underTest.addCompletedStep(9L, 20L); + + assertThat(underTest.getMetricKey()).isEqualTo("db_migration_step_duration"); + assertThat(underTest.getGranularity()).isEqualTo(ADHOC); + assertThat(underTest.getDimension()).isEqualTo(INSTALLATION); + assertThat(underTest.getType()).isEqualTo(INTEGER); + assertThat(underTest.getValue()).isEmpty(); + assertThat(underTest.getValues()).containsExactlyInAnyOrderEntriesOf(Map.of("1", 10L, "9", 20L)); + + underTest.after(); + + assertThat(underTest.getValues()).isEmpty(); + } +} diff --git a/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java b/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java index b91453f3aa3..8829701c4b1 100644 --- a/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java +++ b/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java @@ -58,23 +58,23 @@ public interface TelemetryDataProvider { /** * The implementation of this method might often need to make a call to a database. - * For each metric either this method or {@link TelemetryDataProvider#getUuidValues()} should be implemented and used. Not both at once. + * For each metric either this method or {@link TelemetryDataProvider#getValues()} should be used. Not both at once. * * @return the value of the data provided by this instance. */ default Optional getValue() { - throw new IllegalStateException("Not implemented"); + return Optional.empty(); } /** * The implementation of this method might often need to make a call to a database. - * Similiar as {@link TelemetryDataProvider#getValue()} this method returns values of the metric. Some of the metrics - * associate a UUID with a value. This method is used to return all the values associated with the UUIDs. + * Similar as {@link TelemetryDataProvider#getValue()} this method returns values of the metric. Some of the metrics + * associate a key with a value. This method is used to return all the values associated with the keys. * - * @return map of UUIDs and their values. + * @return map of keys and their values. */ - default Map getUuidValues() { - throw new IllegalStateException("Not implemented"); + default Map getValues() { + return Map.of(); } /** diff --git a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/metrics/TelemetryMetricsLoaderIT.java b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/metrics/TelemetryMetricsLoaderIT.java index 6dc2f649365..67e2ea872e8 100644 --- a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/metrics/TelemetryMetricsLoaderIT.java +++ b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/metrics/TelemetryMetricsLoaderIT.java @@ -270,7 +270,7 @@ class TelemetryMetricsLoaderIT { } @Override - public Map getUuidValues() { + public Map getValues() { return Stream.of(keys) .collect(Collectors.toMap( key -> key, diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsMapper.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsMapper.java index e3379e4772f..d282608d9c2 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsMapper.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsMapper.java @@ -52,7 +52,20 @@ public class TelemetryMetricsMapper { private static Set mapInstallationMetric(TelemetryDataProvider provider) { Optional optionalValue = provider.getValue(); - if (provider.getGranularity() == Granularity.ADHOC && optionalValue.isEmpty()) { + + Granularity granularity = provider.getGranularity(); + + if (granularity == Granularity.ADHOC && !provider.getValues().isEmpty()) { + return provider.getValues().entrySet().stream() + .map(entry -> new InstallationMetric( + provider.getMetricKey() + "." + entry.getKey(), + entry.getValue(), + provider.getType(), + granularity + )).collect(Collectors.toSet()); + } + + if (granularity == Granularity.ADHOC && optionalValue.isEmpty()) { return Collections.emptySet(); } @@ -60,12 +73,12 @@ public class TelemetryMetricsMapper { provider.getMetricKey(), optionalValue.orElse(null), provider.getType(), - provider.getGranularity() + granularity )); } private static Set mapUserMetric(TelemetryDataProvider provider) { - return provider.getUuidValues().entrySet().stream() + return provider.getValues().entrySet().stream() .map(entry -> new UserMetric( provider.getMetricKey(), entry.getValue(), @@ -76,7 +89,7 @@ public class TelemetryMetricsMapper { } private static Set mapProjectMetric(TelemetryDataProvider provider) { - return provider.getUuidValues().entrySet().stream() + return provider.getValues().entrySet().stream() .map(entry -> new ProjectMetric( provider.getMetricKey(), entry.getValue(), @@ -87,7 +100,7 @@ public class TelemetryMetricsMapper { } private static Set mapLanguageMetric(TelemetryDataProvider provider) { - return provider.getUuidValues().entrySet().stream() + return provider.getValues().entrySet().stream() .map(entry -> new LanguageMetric( provider.getMetricKey(), entry.getValue(), diff --git a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TelemetryMetricsMapperTest.java b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TelemetryMetricsMapperTest.java index d2fa9f9533c..d822623d167 100644 --- a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TelemetryMetricsMapperTest.java +++ b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TelemetryMetricsMapperTest.java @@ -53,6 +53,26 @@ class TelemetryMetricsMapperTest { ); } + @Test + void mapFromDataProvider_whenInstallationProviderWithMultiValue() { + TelemetryDataProvider provider = new TestTelemetryBean(Dimension.INSTALLATION) { + @Override + public Granularity getGranularity() { + return Granularity.ADHOC; + } + }; + + Set metrics = TelemetryMetricsMapper.mapFromDataProvider(provider); + List userMetrics = retrieveList(metrics); + + assertThat(userMetrics) + .extracting(InstallationMetric::getKey, InstallationMetric::getType, InstallationMetric::getValue, InstallationMetric::getGranularity) + .containsExactlyInAnyOrder( + tuple("telemetry-bean-a.key-1", TelemetryDataType.STRING, "value-1", Granularity.ADHOC), + tuple("telemetry-bean-a.key-2", TelemetryDataType.STRING, "value-2", Granularity.ADHOC) + ); + } + @Test void mapFromDataProvider_whenUserProvider() { TelemetryDataProvider provider = new TestTelemetryBean(Dimension.USER); diff --git a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TestTelemetryBean.java b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TestTelemetryBean.java index 7aa64e464dc..f4c4c844d85 100644 --- a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TestTelemetryBean.java +++ b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/metrics/TestTelemetryBean.java @@ -66,7 +66,7 @@ public class TestTelemetryBean implements TelemetryDataProvider { } @Override - public Map getUuidValues() { + public Map getValues() { return METRIC_UUID_VALUES; } diff --git a/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProviderIT.java b/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProviderIT.java index 666965be5cc..3ad5a8f3d6a 100644 --- a/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProviderIT.java +++ b/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProviderIT.java @@ -46,12 +46,12 @@ class ProjectCppAutoconfigTelemetryProviderIT { ProjectCppAutoconfigTelemetryProvider underTest = new ProjectCppAutoconfigTelemetryProvider(db.getDbClient()); @Test - void getUuidValues_whenNoProjects_returnEmptyList() { - assertThat(underTest.getUuidValues()).isEmpty(); + void getValues_whenNoProjects_returnEmptyList() { + assertThat(underTest.getValues()).isEmpty(); } @Test - void getUuidValues_whenNoCppAndCProjects_returnEmptyMap() { + void getValues_whenNoCppAndCProjects_returnEmptyMap() { Consumer configureMetric = metric -> metric .setValueType(STRING.name()) .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY); @@ -65,11 +65,11 @@ class ProjectCppAutoconfigTelemetryProviderIT { insertLiveMeasure("cobol", metric).accept(project2); - assertThat(underTest.getUuidValues()).isEmpty(); + assertThat(underTest.getValues()).isEmpty(); } @Test - void getUuidValues_when1CppAnd1CProject_returnMapWithSize2AndAutoconfigByDefault() { + void getValues_when1CppAnd1CProject_returnMapWithSize2AndAutoconfigByDefault() { Consumer configureMetric = metric -> metric .setValueType(STRING.name()) .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY); @@ -86,7 +86,7 @@ class ProjectCppAutoconfigTelemetryProviderIT { insertLiveMeasure("java", metric).accept(project3); insertLiveMeasure("cobol", metric).accept(project4); - Map actualResult = underTest.getUuidValues(); + Map actualResult = underTest.getValues(); assertThat(actualResult).hasSize(2) .containsExactlyInAnyOrderEntriesOf( @@ -95,7 +95,7 @@ class ProjectCppAutoconfigTelemetryProviderIT { } @Test - void getUuidValues_whenCAndCppProjectsWithDifferentConfig_returnMapWithSize2AndNotAutoconfig() { + void getValues_whenCAndCppProjectsWithDifferentConfig_returnMapWithSize2AndNotAutoconfig() { Consumer configureMetric = metric -> metric .setValueType(STRING.name()) .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY); @@ -115,7 +115,7 @@ class ProjectCppAutoconfigTelemetryProviderIT { db.properties().insertProperty("sonar.cfamily.build-wrapper-output", "anyvalue", project1.getProjectDto().getUuid()); db.properties().insertProperty("sonar.cfamily.compile-commands", "anyvalue", project2.getProjectDto().getUuid()); - Map actualResult = underTest.getUuidValues(); + Map actualResult = underTest.getValues(); assertThat(actualResult).hasSize(2) .containsExactlyInAnyOrderEntriesOf( diff --git a/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProviderIT.java b/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProviderIT.java index f0466dac7a8..fee743fd088 100644 --- a/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProviderIT.java +++ b/server/sonar-webserver/src/it/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProviderIT.java @@ -47,19 +47,19 @@ class TelemetryUserEnabledProviderIT { } @Test - void getUuidValues_whenNoUsersInDatabase_shouldReturnEmptyMap() { - Map uuidValues = underTest.getUuidValues(); + void getValues_whenNoUsersInDatabase_shouldReturnEmptyMap() { + Map uuidValues = underTest.getValues(); assertThat(uuidValues).isEmpty(); } @Test - void getUuidValues_whenSomeUsersActive_shouldReturnBothBooleanValues() { + void getValues_whenSomeUsersActive_shouldReturnBothBooleanValues() { db.users().insertUser(user -> user.setUuid("uuid1").setActive(true)); db.users().insertUser(user -> user.setUuid("uuid1").setActive(false)); db.getSession().commit(); - Map uuidValues = underTest.getUuidValues(); + Map uuidValues = underTest.getValues(); assertThat(uuidValues).hasSize(2); assertThat(uuidValues.values().stream().filter(Boolean::booleanValue)).hasSize(1); @@ -67,25 +67,25 @@ class TelemetryUserEnabledProviderIT { } @Test - void getUuidValues_when10ActiveUsers_shouldReturn10BooleanValues() { + void getValues_when10ActiveUsers_shouldReturn10BooleanValues() { for (int i = 0; i < 10; i++) { db.users().insertUser(user -> user.setActive(true)); } db.getSession().commit(); - Map uuidValues = underTest.getUuidValues(); + Map uuidValues = underTest.getValues(); assertThat(uuidValues).hasSize(10); assertThat(uuidValues.values().stream().filter(Boolean::booleanValue)).hasSize(10); } @Test - void getUuidValues_shouldAnonymizeUserUuids() { + void getValues_shouldAnonymizeUserUuids() { UserDto userDto1 = db.users().insertUser(); UserDto userDto2 = db.users().insertUser(); db.getSession().commit(); - Map uuidValues = underTest.getUuidValues(); + Map uuidValues = underTest.getValues(); String anonymizedUser1 = DigestUtil.sha3_224Hex(userDto1.getUuid()); String anonymizedUser2 = DigestUtil.sha3_224Hex(userDto2.getUuid()); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java index 18ad5986239..3bb99f3731f 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java @@ -36,6 +36,7 @@ import org.sonar.server.platform.db.migration.DatabaseMigrationStateImpl; import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; import org.sonar.server.platform.db.migration.version.DatabaseVersion; +import org.sonar.server.telemetry.TelemetryDbMigrationStepDurationProvider; import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; @@ -90,6 +91,7 @@ public class PlatformLevel2 extends PlatformLevel { TelemetryDbMigrationTotalTimeProvider.class, TelemetryDbMigrationStepsProvider.class, TelemetryDbMigrationSuccessProvider.class, + TelemetryDbMigrationStepDurationProvider.class, DatabaseMigrationStateImpl.class, DatabaseMigrationExecutorServiceImpl.class); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProvider.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProvider.java index 64f5130a359..51382bbebe9 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProvider.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/ProjectCppAutoconfigTelemetryProvider.java @@ -62,7 +62,7 @@ public class ProjectCppAutoconfigTelemetryProvider implements TelemetryDataProvi } @Override - public Map getUuidValues() { + public Map getValues() { Map cppConfigTypePerProjectUuid = new HashMap<>(); try (DbSession dbSession = dbClient.openSession(true)) { // In the future ideally languages should be defined in the codebase as enums, using strings is error-prone diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryNclocProvider.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryNclocProvider.java index 81f683cb96a..0d93c951d08 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryNclocProvider.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryNclocProvider.java @@ -68,7 +68,7 @@ public class TelemetryNclocProvider implements TelemetryDataProvider { } @Override - public Map getUuidValues() { + public Map getValues() { try (DbSession dbSession = dbClient.openSession(false)) { return getNclocDistribution(dbSession); } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProvider.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProvider.java index fa3aff0b38e..878fda62ba5 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProvider.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryUserEnabledProvider.java @@ -61,7 +61,7 @@ public class TelemetryUserEnabledProvider implements TelemetryDataProvider getUuidValues() { + public Map getValues() { Map result = new HashMap<>(); int pageSize = 1000; int page = 1; diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java index cf7fb74b314..d91e00f8d67 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java @@ -31,9 +31,9 @@ import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; import org.sonar.server.plugins.ServerPluginRepository; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.process.ProcessProperties.Property.PATH_DATA; @@ -73,7 +73,7 @@ public class PlatformLevel2Test { verify(container).add(ServerPluginRepository.class); verify(container).add(DatabaseCharsetChecker.class); - verify(container, times(24)).add(any()); + verify(container, atLeastOnce()).add(any()); } @Test @@ -94,7 +94,7 @@ public class PlatformLevel2Test { verify(container).add(ServerPluginRepository.class); verify(container, never()).add(DatabaseCharsetChecker.class); - verify(container, times(22)).add(any()); + verify(container, atLeastOnce()).add(any()); } diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryFipsEnabledProviderTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryFipsEnabledProviderTest.java index 40174aafa13..78144729ddb 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryFipsEnabledProviderTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryFipsEnabledProviderTest.java @@ -27,7 +27,6 @@ import org.sonar.telemetry.core.Granularity; import org.sonar.telemetry.core.TelemetryDataType; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -60,7 +59,6 @@ class TelemetryFipsEnabledProviderTest { assertEquals(Granularity.DAILY, telemetryVersionProvider.getGranularity()); assertEquals(TelemetryDataType.BOOLEAN, telemetryVersionProvider.getType()); assertEquals(Optional.of(expectedFipsEnabled), telemetryVersionProvider.getValue()); - assertThrows(IllegalStateException.class, telemetryVersionProvider::getUuidValues); } } diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryNclocProviderTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryNclocProviderTest.java index 6bfd81a6e91..5bab0a2cf3c 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryNclocProviderTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryNclocProviderTest.java @@ -44,7 +44,7 @@ class TelemetryNclocProviderTest { private final DbSession dbSession = mock(DbSession.class); @Test - void getUuidValues_returnsTheRightLanguageDistribution() { + void getValues_returnsTheRightLanguageDistribution() { TelemetryNclocProvider telemetryNclocProvider = new TelemetryNclocProvider(dbClient); when(dbClient.openSession(false)).thenReturn(dbSession); @@ -64,10 +64,10 @@ class TelemetryNclocProviderTest { assertEquals(Granularity.DAILY, telemetryNclocProvider.getGranularity()); assertEquals(Dimension.LANGUAGE, telemetryNclocProvider.getDimension()); - assertThat(telemetryNclocProvider.getUuidValues()).containsOnlyKeys("java", "xml", "csharp", "js"); - assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("java", 22000L); - assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("xml", 1000L); - assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("csharp", 2000L); - assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("js", 1500L); + assertThat(telemetryNclocProvider.getValues()).containsOnlyKeys("java", "xml", "csharp", "js"); + assertThat(telemetryNclocProvider.getValues()).containsEntry("java", 22000L); + assertThat(telemetryNclocProvider.getValues()).containsEntry("xml", 1000L); + assertThat(telemetryNclocProvider.getValues()).containsEntry("csharp", 2000L); + assertThat(telemetryNclocProvider.getValues()).containsEntry("js", 1500L); } } \ No newline at end of file diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryVersionProviderTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryVersionProviderTest.java index d13295312b8..b732036b235 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryVersionProviderTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryVersionProviderTest.java @@ -27,7 +27,6 @@ import org.sonar.telemetry.core.Granularity; import org.sonar.telemetry.core.TelemetryDataType; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -48,6 +47,5 @@ class TelemetryVersionProviderTest { assertEquals(Granularity.DAILY, telemetryVersionProvider.getGranularity()); assertEquals(TelemetryDataType.STRING, telemetryVersionProvider.getType()); assertEquals(Optional.of("10.6"), telemetryVersionProvider.getValue()); - assertThrows(IllegalStateException.class, telemetryVersionProvider::getUuidValues); } } -- 2.39.5