Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions apps/frontend/src/providers/analytics/analytics-data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
},
},
}
Expand Down
95 changes: 59 additions & 36 deletions apps/frontend/src/providers/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ export function createAnalyticsDashboardContext(
return buildAnalyticsFacetsRequest(
sortedAvailableProjectIds.value,
nextFetchRequest.time_range,
['project_id'],
)
},
)
Expand Down Expand Up @@ -1275,35 +1276,48 @@ export function createAnalyticsDashboardContext(
refetchOnWindowFocus: false,
})

const { data: analyticsDownloadCountTimeSlices } = useQuery({
queryKey: computed(() => [
'analytics',
'dashboard',
analyticsQueryUserId.value,
'filter-options',
'download-counts-fallback',
analyticsFacetsRequest.value,
queryRefreshTimestamp.value,
]),
queryFn: () => {
const nextRequest = analyticsFacetsRequest.value
if (!isAnalyticsFetchRequestReady(nextRequest)) {
return []
}
// 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 []
}

return fetchAnalyticsTimeSlices(nextRequest, (request) =>
client.labrinth.analytics_v3.fetch(request),
)
},
enabled: computed(
() =>
hasFetchedAnalyticsFilterOptions.value &&
isAnalyticsFetchRequestReady(analyticsFacetsRequest.value),
),
placeholderData: [],
gcTime: ANALYTICS_FILTER_OPTIONS_GC_TIME_MS,
refetchOnWindowFocus: false,
})
return fetchAnalyticsTimeSlices(
{ ...nextRequest, return_metrics: { project_downloads: { bucket_by: bucketBy } } },
(request) => client.labrinth.analytics_v3.fetch(request),
)
},
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),
Expand Down Expand Up @@ -1749,24 +1763,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(projectVersionDownloadCountTimeSlices.value),
),
)
const projectVersionDownloadsById = computed(() =>
getProjectVersionDownloadsByIdFromTimeSlices(downloadCountTimeSlices.value),
getProjectVersionDownloadsByIdFromTimeSlices(
downloadCountsOrCurrentTimeSlices(projectVersionDownloadCountTimeSlices.value),
),
)
const countryDownloadsByCode = computed(() =>
getCountryDownloadsByCodeFromTimeSlices(downloadCountTimeSlices.value),
getCountryDownloadsByCodeFromTimeSlices(
downloadCountsOrCurrentTimeSlices(countryDownloadCountTimeSlices.value),
),
)
const gameVersionDownloadsByVersion = computed(() =>
getGameVersionDownloadsByVersionFromTimeSlices(downloadCountTimeSlices.value),
getGameVersionDownloadsByVersionFromTimeSlices(
downloadCountsOrCurrentTimeSlices(gameVersionDownloadCountTimeSlices.value),
),
)

const selectedProjectIdSet = computed(() => new Set(selectedProjectIds.value))
Expand Down
15 changes: 15 additions & 0 deletions apps/labrinth/src/clickhouse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Loading