Enable preloading dashboard queries on root url

When authenticated user visits root url like
https://gerrit-review.googlesource.com/ frontend will redirect to
dashboard/self without informing backend.

We need to preload for both root url and dashboard/self. From data
it looks root path is 10x more visited than dashboard/self, although
many visits to root path could be made by guests.

+ preload dashboard queries only when the user is logged in.

Change-Id: I73158d98fe8d6d77dcbbfbb70ebc96609d1854b8
This commit is contained in:
Milutin Kristofic
2020-08-26 21:57:37 +02:00
parent 9e965438a7
commit d291cbf362
3 changed files with 39 additions and 20 deletions

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.httpd.raw;
import static com.google.gerrit.httpd.raw.IndexPreloadingUtil.RequestedPage;
import static com.google.template.soy.data.ordainers.GsonOrdainer.serializeObject;
import static java.util.stream.Collectors.toSet;
@@ -74,7 +75,7 @@ public class IndexHtmlUtil {
/** Returns dynamic parameters of {@code index.html}. */
public static ImmutableMap<String, Object> dynamicTemplateData(
GerritApi gerritApi, String requestedURL) throws RestApiException {
GerritApi gerritApi, String requestedURL) throws RestApiException, URISyntaxException {
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
Map<String, SanitizedContent> initialData = new HashMap<>();
Server serverApi = gerritApi.config().server();
@@ -82,25 +83,24 @@ public class IndexHtmlUtil {
initialData.put("\"/config/server/version\"", serializeObject(GSON, serverApi.getVersion()));
initialData.put("\"/config/server/top-menus\"", serializeObject(GSON, serverApi.topMenus()));
IndexPreloadingUtil.RequestedPage page = IndexPreloadingUtil.parseRequestedPage(requestedURL);
String requestedPath = IndexPreloadingUtil.getPath(requestedURL);
IndexPreloadingUtil.RequestedPage page = IndexPreloadingUtil.parseRequestedPage(requestedPath);
switch (page) {
case CHANGE:
data.put(
"defaultChangeDetailHex", IndexPreloadingUtil.getDefaultChangeDetailOptionsAsHex());
data.put(
"changeRequestsPath",
IndexPreloadingUtil.computeChangeRequestsPath(requestedURL, page).get());
IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
break;
case DIFF:
data.put("defaultDiffDetailHex", IndexPreloadingUtil.getDefaultDiffDetailOptionsAsHex());
data.put(
"changeRequestsPath",
IndexPreloadingUtil.computeChangeRequestsPath(requestedURL, page).get());
IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
break;
case DASHBOARD:
data.put("defaultDashboardHex", IndexPreloadingUtil.getDefaultDashboardHex(serverApi));
data.put("dashboardQuery", IndexPreloadingUtil.computeDashboardQueryList(serverApi));
break;
// Dashboard is preloaded queries are added later when we check user is authenticated.
case PAGE_WITHOUT_PRELOADING:
break;
}
@@ -117,6 +117,10 @@ public class IndexHtmlUtil {
"\"/accounts/self/preferences.edit\"",
serializeObject(GSON, accountApi.getEditPreferences()));
data.put("userIsAuthenticated", true);
if (page == RequestedPage.DASHBOARD) {
data.put("defaultDashboardHex", IndexPreloadingUtil.getDefaultDashboardHex(serverApi));
data.put("dashboardQuery", IndexPreloadingUtil.computeDashboardQueryList(serverApi));
}
} catch (AuthException e) {
logger.atFine().log("Can't inline account-related data because user is unauthenticated");
// Don't render data

View File

@@ -27,6 +27,8 @@ import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.extensions.client.ListOption;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.Url;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@@ -46,13 +48,14 @@ public class IndexPreloadingUtil {
PAGE_WITHOUT_PRELOADING,
}
public static final String CHANGE_CANONICAL_URL = ".*/c/(?<project>.+)/\\+/(?<changeNum>\\d+)";
public static final String BASE_PATCH_NUM_URL_PART = "(/(-?\\d+|edit)(\\.\\.(\\d+|edit))?)";
public static final String CHANGE_CANONICAL_PATH = "/c/(?<project>.+)/\\+/(?<changeNum>\\d+)";
public static final String BASE_PATCH_NUM_PATH_PART = "(/(-?\\d+|edit)(\\.\\.(\\d+|edit))?)";
public static final Pattern CHANGE_URL_PATTERN =
Pattern.compile(CHANGE_CANONICAL_URL + BASE_PATCH_NUM_URL_PART + "?" + "/?$");
Pattern.compile(CHANGE_CANONICAL_PATH + BASE_PATCH_NUM_PATH_PART + "?" + "/?$");
public static final Pattern DIFF_URL_PATTERN =
Pattern.compile(CHANGE_CANONICAL_URL + BASE_PATCH_NUM_URL_PART + "(/(.+))" + "/?$");
public static final Pattern DASHBOARD_PATTERN = Pattern.compile(".*/dashboard/self$");
Pattern.compile(CHANGE_CANONICAL_PATH + BASE_PATCH_NUM_PATH_PART + "(/(.+))" + "/?$");
public static final Pattern DASHBOARD_PATTERN = Pattern.compile("/dashboard/self$");
public static final String ROOT_PATH = "/";
// These queries should be kept in sync with PolyGerrit:
// polygerrit-ui/app/elements/core/gr-navigation/gr-navigation.ts
@@ -128,27 +131,39 @@ public class IndexPreloadingUtil {
return ListOption.toHex(options);
}
public static RequestedPage parseRequestedPage(@Nullable String requestedURL) {
public static String getPath(@Nullable String requestedURL) throws URISyntaxException {
if (requestedURL == null) {
return null;
}
URI uri = new URI(requestedURL);
return uri.getPath();
}
public static RequestedPage parseRequestedPage(@Nullable String requestedPath) {
if (requestedPath == null) {
return RequestedPage.PAGE_WITHOUT_PRELOADING;
}
Optional<String> changeRequestsPath =
computeChangeRequestsPath(requestedURL, RequestedPage.CHANGE);
computeChangeRequestsPath(requestedPath, RequestedPage.CHANGE);
if (changeRequestsPath.isPresent()) {
return RequestedPage.CHANGE;
}
changeRequestsPath = computeChangeRequestsPath(requestedURL, RequestedPage.DIFF);
changeRequestsPath = computeChangeRequestsPath(requestedPath, RequestedPage.DIFF);
if (changeRequestsPath.isPresent()) {
return RequestedPage.DIFF;
}
Matcher dashboardMatcher = IndexPreloadingUtil.DASHBOARD_PATTERN.matcher(requestedURL);
Matcher dashboardMatcher = IndexPreloadingUtil.DASHBOARD_PATTERN.matcher(requestedPath);
if (dashboardMatcher.matches()) {
return RequestedPage.DASHBOARD;
}
if (ROOT_PATH.equals(requestedPath)) {
return RequestedPage.DASHBOARD;
}
return RequestedPage.PAGE_WITHOUT_PRELOADING;
}

View File

@@ -51,11 +51,11 @@ public class IndexPreloadingUtilTest {
@Test
public void preloadOnlyForSelfDashboard() throws Exception {
assertThat(parseRequestedPage("https://gerrit-review.googlesource.com/dashboard/self"))
.isEqualTo(RequestedPage.DASHBOARD);
assertThat(parseRequestedPage("https://gerrit-review.googlesource.com/dashboard/1085901"))
assertThat(parseRequestedPage("/dashboard/self")).isEqualTo(RequestedPage.DASHBOARD);
assertThat(parseRequestedPage("/dashboard/1085901"))
.isEqualTo(RequestedPage.PAGE_WITHOUT_PRELOADING);
assertThat(parseRequestedPage("https://gerrit-review.googlesource.com/dashboard/gerrit"))
assertThat(parseRequestedPage("/dashboard/gerrit"))
.isEqualTo(RequestedPage.PAGE_WITHOUT_PRELOADING);
assertThat(parseRequestedPage("/")).isEqualTo(RequestedPage.DASHBOARD);
}
}