Skip to content

Commit 46211a1

Browse files
authored
perf: speed up playtime and download count analytics queries (#8318)
* 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. * 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.
1 parent 342ab04 commit 46211a1

3 files changed

Lines changed: 86 additions & 47 deletions

File tree

‎apps/frontend/src/providers/analytics/analytics-data-utils.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ export function buildDailyAnalyticsFetchRequest(
326326
export function buildAnalyticsFacetsRequest(
327327
projectIds: string[],
328328
timeRange: Labrinth.Analytics.v3.TimeRange,
329+
bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[] = [
330+
'project_id',
331+
'domain',
332+
'user_agent',
333+
'version_id',
334+
'monetized',
335+
'country',
336+
'reason',
337+
'game_version',
338+
'loader',
339+
],
329340
): Labrinth.Analytics.v3.FetchRequest {
330341
return {
331342
time_range: {
@@ -338,17 +349,7 @@ export function buildAnalyticsFacetsRequest(
338349
project_ids: projectIds,
339350
return_metrics: {
340351
project_downloads: {
341-
bucket_by: [
342-
'project_id',
343-
'domain',
344-
'user_agent',
345-
'version_id',
346-
'monetized',
347-
'country',
348-
'reason',
349-
'game_version',
350-
'loader',
351-
],
352+
bucket_by: bucketBy,
352353
},
353354
},
354355
}

‎apps/frontend/src/providers/analytics/analytics.ts‎

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ export function createAnalyticsDashboardContext(
11841184
return buildAnalyticsFacetsRequest(
11851185
sortedAvailableProjectIds.value,
11861186
nextFetchRequest.time_range,
1187+
['project_id'],
11871188
)
11881189
},
11891190
)
@@ -1275,35 +1276,48 @@ export function createAnalyticsDashboardContext(
12751276
refetchOnWindowFocus: false,
12761277
})
12771278

1278-
const { data: analyticsDownloadCountTimeSlices } = useQuery({
1279-
queryKey: computed(() => [
1280-
'analytics',
1281-
'dashboard',
1282-
analyticsQueryUserId.value,
1283-
'filter-options',
1284-
'download-counts-fallback',
1285-
analyticsFacetsRequest.value,
1286-
queryRefreshTimestamp.value,
1287-
]),
1288-
queryFn: () => {
1289-
const nextRequest = analyticsFacetsRequest.value
1290-
if (!isAnalyticsFetchRequestReady(nextRequest)) {
1291-
return []
1292-
}
1279+
// One query per breakdown: a combined bucket_by multiplies the group count and pushes
1280+
// most country buckets under the privacy floor. Separate queries also keep one failed
1281+
// breakdown from hiding the others.
1282+
function useDownloadCountTimeSlices(bucketBy: Labrinth.Analytics.v3.ProjectDownloadsField[]) {
1283+
return useQuery({
1284+
queryKey: computed(() => [
1285+
'analytics',
1286+
'dashboard',
1287+
analyticsQueryUserId.value,
1288+
'filter-options',
1289+
'download-counts-fallback',
1290+
bucketBy,
1291+
analyticsFacetsRequest.value,
1292+
queryRefreshTimestamp.value,
1293+
]),
1294+
queryFn: () => {
1295+
const nextRequest = analyticsFacetsRequest.value
1296+
if (!isAnalyticsFetchRequestReady(nextRequest)) {
1297+
return []
1298+
}
12931299

1294-
return fetchAnalyticsTimeSlices(nextRequest, (request) =>
1295-
client.labrinth.analytics_v3.fetch(request),
1296-
)
1297-
},
1298-
enabled: computed(
1299-
() =>
1300-
hasFetchedAnalyticsFilterOptions.value &&
1301-
isAnalyticsFetchRequestReady(analyticsFacetsRequest.value),
1302-
),
1303-
placeholderData: [],
1304-
gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS,
1305-
refetchOnWindowFocus: false,
1306-
})
1300+
return fetchAnalyticsTimeSlices(
1301+
{ ...nextRequest, return_metrics: { project_downloads: { bucket_by: bucketBy } } },
1302+
(request) => client.labrinth.analytics_v3.fetch(request),
1303+
)
1304+
},
1305+
enabled: computed(
1306+
() =>
1307+
hasFetchedAnalyticsFilterOptions.value &&
1308+
isAnalyticsFetchRequestReady(analyticsFacetsRequest.value),
1309+
),
1310+
placeholderData: [],
1311+
gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS,
1312+
refetchOnWindowFocus: false,
1313+
}).data
1314+
}
1315+
const projectVersionDownloadCountTimeSlices = useDownloadCountTimeSlices([
1316+
'project_id',
1317+
'version_id',
1318+
])
1319+
const countryDownloadCountTimeSlices = useDownloadCountTimeSlices(['country'])
1320+
const gameVersionDownloadCountTimeSlices = useDownloadCountTimeSlices(['game_version'])
13071321

13081322
const analyticsFacetsFilterOptionSummary = computed(() =>
13091323
getAnalyticsFacetsFilterOptionSummary(analyticsFacetsData.value?.facets),
@@ -1749,24 +1763,33 @@ export function createAnalyticsDashboardContext(
17491763
}
17501764
return versionProjectOrganizationNames
17511765
})
1752-
const downloadCountTimeSlices = computed(() => {
1753-
const countTimeSlices = analyticsDownloadCountTimeSlices.value ?? []
1754-
return countTimeSlices.length > 0 ? countTimeSlices : timeSlices.value
1755-
})
1766+
function downloadCountsOrCurrentTimeSlices(
1767+
countTimeSlices: Labrinth.Analytics.v3.TimeSlice[] | undefined,
1768+
): Labrinth.Analytics.v3.TimeSlice[] {
1769+
return countTimeSlices && countTimeSlices.length > 0 ? countTimeSlices : timeSlices.value
1770+
}
17561771
const availableProjectDownloadsById = computed(() =>
17571772
getProjectDownloadsByIdFromTimeSlices(availableProjectDownloadCountTimeSlices.value ?? []),
17581773
)
17591774
const projectDownloadsById = computed(() =>
1760-
getProjectDownloadsByIdFromTimeSlices(downloadCountTimeSlices.value),
1775+
getProjectDownloadsByIdFromTimeSlices(
1776+
downloadCountsOrCurrentTimeSlices(projectVersionDownloadCountTimeSlices.value),
1777+
),
17611778
)
17621779
const projectVersionDownloadsById = computed(() =>
1763-
getProjectVersionDownloadsByIdFromTimeSlices(downloadCountTimeSlices.value),
1780+
getProjectVersionDownloadsByIdFromTimeSlices(
1781+
downloadCountsOrCurrentTimeSlices(projectVersionDownloadCountTimeSlices.value),
1782+
),
17641783
)
17651784
const countryDownloadsByCode = computed(() =>
1766-
getCountryDownloadsByCodeFromTimeSlices(downloadCountTimeSlices.value),
1785+
getCountryDownloadsByCodeFromTimeSlices(
1786+
downloadCountsOrCurrentTimeSlices(countryDownloadCountTimeSlices.value),
1787+
),
17671788
)
17681789
const gameVersionDownloadsByVersion = computed(() =>
1769-
getGameVersionDownloadsByVersionFromTimeSlices(downloadCountTimeSlices.value),
1790+
getGameVersionDownloadsByVersionFromTimeSlices(
1791+
downloadCountsOrCurrentTimeSlices(gameVersionDownloadCountTimeSlices.value),
1792+
),
17701793
)
17711794

17721795
const selectedProjectIdSet = computed(() => new Set(selectedProjectIds.value))

‎apps/labrinth/src/clickhouse/mod.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,20 @@ pub async fn run_migrations_on_database(
261261
.execute()
262262
.await?;
263263

264+
// Modpack playtime is looked up by `parent`, which isn't in the primary
265+
// key. Existing parts need a one-off `MATERIALIZE PROJECTION` to use it.
266+
client
267+
.query(&format!(
268+
"
269+
ALTER TABLE {database}.{PLAYTIME} {cluster_line}
270+
ADD PROJECTION IF NOT EXISTS by_parent (
271+
SELECT recorded, parent, seconds, version_id, loader, game_version, country
272+
ORDER BY (parent, recorded)
273+
)
274+
"
275+
))
276+
.execute()
277+
.await?;
278+
264279
Ok(())
265280
}

0 commit comments

Comments
 (0)