From 3ecff763a99c1bad6e317663b5733f8d2e7c7775 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 5 Mar 2021 09:30:26 -0600 Subject: [PATCH] SONAR-14518 Telemetry - include number of sonarlint users --- .../src/main/java/org/sonar/db/user/UserDao.java | 3 ++- .../platform/web/SonarLintConnectionFilter.java | 12 +++++++----- .../web/SonarLintConnectionFilterTest.java | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java index 8fe12dcae2a..87e77341c71 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java @@ -38,12 +38,13 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.project.ProjectDto; import static java.util.Locale.ENGLISH; +import static java.util.concurrent.TimeUnit.DAYS; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; import static org.sonar.db.user.UserDto.SCM_ACCOUNTS_SEPARATOR; public class UserDao implements Dao { - private final static long WEEK_IN_MS = 7L * 24L * 3_600L * 1_000L; + private static final long WEEK_IN_MS = DAYS.toMillis(7L); private final System2 system2; private final UuidFactory uuidFactory; diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/SonarLintConnectionFilter.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/SonarLintConnectionFilter.java index 2b11c6a9328..7b79a47ca85 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/SonarLintConnectionFilter.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/SonarLintConnectionFilter.java @@ -30,18 +30,20 @@ import org.sonar.api.utils.System2; import org.sonar.api.web.ServletFilter; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.server.user.UserSession; +import org.sonar.server.user.ThreadLocalUserSession; import org.sonar.server.ws.ServletRequest; +import static java.util.concurrent.TimeUnit.HOURS; + public class SonarLintConnectionFilter extends ServletFilter { private static final UrlPattern URL_PATTERN = UrlPattern.builder() .includes("/api/*") .build(); private final DbClient dbClient; - private final UserSession userSession; + private final ThreadLocalUserSession userSession; private final System2 system2; - public SonarLintConnectionFilter(DbClient dbClient, UserSession userSession, System2 system2) { + public SonarLintConnectionFilter(DbClient dbClient, ThreadLocalUserSession userSession, System2 system2) { this.dbClient = dbClient; this.userSession = userSession; this.system2 = system2; @@ -84,11 +86,11 @@ public class SonarLintConnectionFilter extends ServletFilter { } private boolean shouldUpdate() { - if (!userSession.isLoggedIn()) { + if (!userSession.hasSession() || !userSession.isLoggedIn()) { return false; } long now = system2.now(); Long lastUpdate = userSession.getLastSonarlintConnectionDate(); - return (lastUpdate == null || lastUpdate < now - 3_600_000L); + return (lastUpdate == null || lastUpdate < now - HOURS.toMillis(1L)); } } diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/SonarLintConnectionFilterTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/SonarLintConnectionFilterTest.java index 53285dbba9c..07055d8e449 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/SonarLintConnectionFilterTest.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/web/SonarLintConnectionFilterTest.java @@ -32,6 +32,7 @@ import org.sonar.api.impl.utils.TestSystem2; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; import org.sonar.server.user.ServerUserSession; +import org.sonar.server.user.ThreadLocalUserSession; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -66,7 +67,7 @@ public class SonarLintConnectionFilterTest { @Test public void only_applies_to_api() { - SonarLintConnectionFilter underTest = new SonarLintConnectionFilter(dbTester.getDbClient(), mock(ServerUserSession.class), system2); + SonarLintConnectionFilter underTest = new SonarLintConnectionFilter(dbTester.getDbClient(), mock(ThreadLocalUserSession.class), system2); assertThat(underTest.doGetPattern().matches("/api/test")).isTrue(); assertThat(underTest.doGetPattern().matches("/test")).isFalse(); @@ -91,6 +92,16 @@ public class SonarLintConnectionFilterTest { assertThat(getLastUpdate("invalid")).isEqualTo(1_000_000L); } + @Test + public void dont_fail_if_no_user_set() throws IOException, ServletException { + SonarLintConnectionFilter underTest = new SonarLintConnectionFilter(dbTester.getDbClient(), new ThreadLocalUserSession(), system2); + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getHeader("User-Agent")).thenReturn("sonarlint"); + FilterChain chain = mock(FilterChain.class); + underTest.doFilter(request, mock(ServletResponse.class), chain); + verify(chain).doFilter(any(), any()); + } + @Test public void only_update_if_not_updated_within_1h() throws IOException, ServletException { system2.setNow(2_000_000L); @@ -111,7 +122,8 @@ public class SonarLintConnectionFilterTest { private void runFilter(String loggedInUser, @Nullable String agent) throws IOException, ServletException { UserDto user = dbTester.getDbClient().userDao().selectByLogin(dbTester.getSession(), loggedInUser); - ServerUserSession session = new ServerUserSession(dbTester.getDbClient(), user); + ThreadLocalUserSession session = new ThreadLocalUserSession(); + session.set(new ServerUserSession(dbTester.getDbClient(), user)); SonarLintConnectionFilter underTest = new SonarLintConnectionFilter(dbTester.getDbClient(), session, system2); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader("User-Agent")).thenReturn(agent); -- 2.39.5