From 6a8abc3302f7d064fc7d1b546a4bc14e775e2a6b Mon Sep 17 00:00:00 2001 From: "Michael H." Date: Fri, 13 Nov 2026 21:22:54 +0700 Subject: [PATCH 1/2] perf: speed up playtime and download count analytics queries Add a by_parent projection on playtime so modpack playtime lookups no longer scan the whole time window. Split the dashboard's 9-dimension download count request into per-field requests, which also stops small cross-product buckets from collapsing countries into XX. --- .../analytics/analytics-data-utils.ts | 23 +++++---- .../src/providers/analytics/analytics.ts | 51 +++++++++++++------ apps/labrinth/src/clickhouse/mod.rs | 15 ++++++ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/apps/frontend/src/providers/analytics/analytics-data-utils.ts b/apps/frontend/src/providers/analytics/analytics-data-utils.ts index d415eb8a57e..8e1d4fe298f 100644 --- a/apps/frontend/src/providers/analytics/analytics-data-utils.ts +++ b/apps/frontend/src/providers/analytics/analytics-data-utils.ts @@ -326,6 +326,17 @@ export function buildDailyAnalyticsFetchRequest( export function buildAnalyticsFacetsRequest( projectIds: string[], timeRange: Labrinth.Analytics.v3.TimeRange, + bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[] = [ + 'project_id', + 'domain', + 'user_agent', + 'version_id', + 'monetized', + 'country', + 'reason', + 'game_version', + 'loader', + ], ): Labrinth.Analytics.v3.FetchRequest { return { time_range: { @@ -338,17 +349,7 @@ export function buildAnalyticsFacetsRequest( project_ids: projectIds, return_metrics: { project_downloads: { - bucket_by: [ - 'project_id', - 'domain', - 'user_agent', - 'version_id', - 'monetized', - 'country', - 'reason', - 'game_version', - 'loader', - ], + bucket_by: bucketBy, }, }, } diff --git a/apps/frontend/src/providers/analytics/analytics.ts b/apps/frontend/src/providers/analytics/analytics.ts index a623a3913e8..fe71feecf66 100644 --- a/apps/frontend/src/providers/analytics/analytics.ts +++ b/apps/frontend/src/providers/analytics/analytics.ts @@ -1184,6 +1184,7 @@ export function createAnalyticsDashboardContext( return buildAnalyticsFacetsRequest( sortedAvailableProjectIds.value, nextFetchRequest.time_range, + ['project_id'], ) }, ) @@ -1275,7 +1276,7 @@ export function createAnalyticsDashboardContext( refetchOnWindowFocus: false, }) - const { data: analyticsDownloadCountTimeSlices } = useQuery({ + const { data: analyticsDownloadCounts } = useQuery({ queryKey: computed(() => [ 'analytics', 'dashboard', @@ -1285,22 +1286,33 @@ export function createAnalyticsDashboardContext( analyticsFacetsRequest.value, queryRefreshTimestamp.value, ]), - queryFn: () => { + queryFn: async () => { const nextRequest = analyticsFacetsRequest.value if (!isAnalyticsFetchRequestReady(nextRequest)) { - return [] + return null } - return fetchAnalyticsTimeSlices(nextRequest, (request) => - client.labrinth.analytics_v3.fetch(request), - ) + // One request per breakdown: a combined bucket_by multiplies the group count + // and pushes most country buckets under the privacy floor. + const fetchDownloadCounts = (bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[]) => + fetchAnalyticsTimeSlices( + { ...nextRequest, return_metrics: { project_downloads: { bucket_by: bucketBy } } }, + (request) => client.labrinth.analytics_v3.fetch(request), + ) + + const [byProjectVersion, byCountry, byGameVersion] = await Promise.all([ + fetchDownloadCounts(['project_id', 'version_id']), + fetchDownloadCounts(['country']), + fetchDownloadCounts(['game_version']), + ]) + return { byProjectVersion, byCountry, byGameVersion } }, enabled: computed( () => hasFetchedAnalyticsFilterOptions.value && isAnalyticsFetchRequestReady(analyticsFacetsRequest.value), ), - placeholderData: [], + placeholderData: null, gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS, refetchOnWindowFocus: false, }) @@ -1749,24 +1761,33 @@ export function createAnalyticsDashboardContext( } return versionProjectOrganizationNames }) - const downloadCountTimeSlices = computed(() => { - const countTimeSlices = analyticsDownloadCountTimeSlices.value ?? [] - return countTimeSlices.length > 0 ? countTimeSlices : timeSlices.value - }) + function downloadCountsOrCurrentTimeSlices( + countTimeSlices: Labrinth.Analytics.v3.TimeSlice[] | undefined, + ): Labrinth.Analytics.v3.TimeSlice[] { + return countTimeSlices && countTimeSlices.length > 0 ? countTimeSlices : timeSlices.value + } const availableProjectDownloadsById = computed(() => getProjectDownloadsByIdFromTimeSlices(availableProjectDownloadCountTimeSlices.value ?? []), ) const projectDownloadsById = computed(() => - getProjectDownloadsByIdFromTimeSlices(downloadCountTimeSlices.value), + getProjectDownloadsByIdFromTimeSlices( + downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byProjectVersion), + ), ) const projectVersionDownloadsById = computed(() => - getProjectVersionDownloadsByIdFromTimeSlices(downloadCountTimeSlices.value), + getProjectVersionDownloadsByIdFromTimeSlices( + downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byProjectVersion), + ), ) const countryDownloadsByCode = computed(() => - getCountryDownloadsByCodeFromTimeSlices(downloadCountTimeSlices.value), + getCountryDownloadsByCodeFromTimeSlices( + downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byCountry), + ), ) const gameVersionDownloadsByVersion = computed(() => - getGameVersionDownloadsByVersionFromTimeSlices(downloadCountTimeSlices.value), + getGameVersionDownloadsByVersionFromTimeSlices( + downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byGameVersion), + ), ) const selectedProjectIdSet = computed(() => new Set(selectedProjectIds.value)) diff --git a/apps/labrinth/src/clickhouse/mod.rs b/apps/labrinth/src/clickhouse/mod.rs index fd87142d8d1..8d08566350e 100644 --- a/apps/labrinth/src/clickhouse/mod.rs +++ b/apps/labrinth/src/clickhouse/mod.rs @@ -261,5 +261,20 @@ pub async fn run_migrations_on_database( .execute() .await?; + // Modpack playtime is looked up by `parent`, which isn't in the primary + // key. Existing parts need a one-off `MATERIALIZE PROJECTION` to use it. + client + .query(&format!( + " + ALTER TABLE {database}.{PLAYTIME} {cluster_line} + ADD PROJECTION IF NOT EXISTS by_parent ( + SELECT recorded, parent, seconds, version_id, loader, game_version, country + ORDER BY (parent, recorded) + ) + " + )) + .execute() + .await?; + Ok(()) } From c7c9d75c9607c466bc114c58a3b85753d3ff55de Mon Sep 17 00:00:00 2001 From: "Michael H." Date: Fri, 13 Nov 2026 21:30:48 +0700 Subject: [PATCH 2/2] fix: isolate analytics download count queries per breakdown Use a separate query for each download count breakdown so one failed request no longer hides the other counts, and each keeps its own retry. --- .../src/providers/analytics/analytics.ts | 82 ++++++++++--------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/apps/frontend/src/providers/analytics/analytics.ts b/apps/frontend/src/providers/analytics/analytics.ts index fe71feecf66..034da56923b 100644 --- a/apps/frontend/src/providers/analytics/analytics.ts +++ b/apps/frontend/src/providers/analytics/analytics.ts @@ -1276,46 +1276,48 @@ export function createAnalyticsDashboardContext( refetchOnWindowFocus: false, }) - const { data: analyticsDownloadCounts } = useQuery({ - queryKey: computed(() => [ - 'analytics', - 'dashboard', - analyticsQueryUserId.value, - 'filter-options', - 'download-counts-fallback', - analyticsFacetsRequest.value, - queryRefreshTimestamp.value, - ]), - queryFn: async () => { - const nextRequest = analyticsFacetsRequest.value - if (!isAnalyticsFetchRequestReady(nextRequest)) { - return null - } + // One query per breakdown: a combined bucket_by multiplies the group count and pushes + // most country buckets under the privacy floor. Separate queries also keep one failed + // breakdown from hiding the others. + function useDownloadCountTimeSlices(bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[]) { + return useQuery({ + queryKey: computed(() => [ + 'analytics', + 'dashboard', + analyticsQueryUserId.value, + 'filter-options', + 'download-counts-fallback', + bucketBy, + analyticsFacetsRequest.value, + queryRefreshTimestamp.value, + ]), + queryFn: () => { + const nextRequest = analyticsFacetsRequest.value + if (!isAnalyticsFetchRequestReady(nextRequest)) { + return [] + } - // One request per breakdown: a combined bucket_by multiplies the group count - // and pushes most country buckets under the privacy floor. - const fetchDownloadCounts = (bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[]) => - fetchAnalyticsTimeSlices( + return fetchAnalyticsTimeSlices( { ...nextRequest, return_metrics: { project_downloads: { bucket_by: bucketBy } } }, (request) => client.labrinth.analytics_v3.fetch(request), ) - - const [byProjectVersion, byCountry, byGameVersion] = await Promise.all([ - fetchDownloadCounts(['project_id', 'version_id']), - fetchDownloadCounts(['country']), - fetchDownloadCounts(['game_version']), - ]) - return { byProjectVersion, byCountry, byGameVersion } - }, - enabled: computed( - () => - hasFetchedAnalyticsFilterOptions.value && - isAnalyticsFetchRequestReady(analyticsFacetsRequest.value), - ), - placeholderData: null, - gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS, - refetchOnWindowFocus: false, - }) + }, + enabled: computed( + () => + hasFetchedAnalyticsFilterOptions.value && + isAnalyticsFetchRequestReady(analyticsFacetsRequest.value), + ), + placeholderData: [], + gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS, + refetchOnWindowFocus: false, + }).data + } + const projectVersionDownloadCountTimeSlices = useDownloadCountTimeSlices([ + 'project_id', + 'version_id', + ]) + const countryDownloadCountTimeSlices = useDownloadCountTimeSlices(['country']) + const gameVersionDownloadCountTimeSlices = useDownloadCountTimeSlices(['game_version']) const analyticsFacetsFilterOptionSummary = computed(() => getAnalyticsFacetsFilterOptionSummary(analyticsFacetsData.value?.facets), @@ -1771,22 +1773,22 @@ export function createAnalyticsDashboardContext( ) const projectDownloadsById = computed(() => getProjectDownloadsByIdFromTimeSlices( - downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byProjectVersion), + downloadCountsOrCurrentTimeSlices(projectVersionDownloadCountTimeSlices.value), ), ) const projectVersionDownloadsById = computed(() => getProjectVersionDownloadsByIdFromTimeSlices( - downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byProjectVersion), + downloadCountsOrCurrentTimeSlices(projectVersionDownloadCountTimeSlices.value), ), ) const countryDownloadsByCode = computed(() => getCountryDownloadsByCodeFromTimeSlices( - downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byCountry), + downloadCountsOrCurrentTimeSlices(countryDownloadCountTimeSlices.value), ), ) const gameVersionDownloadsByVersion = computed(() => getGameVersionDownloadsByVersionFromTimeSlices( - downloadCountsOrCurrentTimeSlices(analyticsDownloadCounts.value?.byGameVersion), + downloadCountsOrCurrentTimeSlices(gameVersionDownloadCountTimeSlices.value), ), )