Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

⚡️ Scope space member counts and the orphaned guest scan

Luke Vellafcc601c32f7e
diff --git a/apps/web/src/features/space/data.ts b/apps/web/src/features/space/data.ts
index 64d2ed567..8676592a4 100644
--- a/apps/web/src/features/space/data.ts
+++ b/apps/web/src/features/space/data.ts
@@ -217,7 +217,6 @@ export const listSpacesForUser = cache(async (userId: string) => {
showBranding: true,
hideAttribution: true,
shared: true,
- _count: { select: { members: true } },
subscriptions: {
where: { active: true },
select: { quantity: true },
@@ -229,14 +228,26 @@ export const listSpacesForUser = cache(async (userId: string) => {
},
});
- const policy = await getInstancePolicy();
+ // Counted for these spaces only: a relation _count over more than one
+ // space compiles to a GROUP BY over the whole space_members table
+ const [memberCounts, policy] = await Promise.all([
+ prisma.spaceMember.groupBy({
+ by: ["spaceId"],
+ where: { spaceId: { in: result.map(({ space }) => space.id) } },
+ _count: { _all: true },
+ }),
+ getInstancePolicy(),
+ ]);
+ const memberCountBySpaceId = new Map(
+ memberCounts.map((row) => [row.spaceId, row._count._all]),
+ );
return result.map((spaceMember) =>
createSpaceDTO({
space: {
...spaceMember.space,
role: spaceMember.role,
- memberCount: spaceMember.space._count.members,
+ memberCount: memberCountBySpaceId.get(spaceMember.space.id) ?? 0,
seatCount: spaceMember.space.subscriptions[0]?.quantity ?? 1,
},
policy,
diff --git a/apps/web/src/features/user/mutations.ts b/apps/web/src/features/user/mutations.ts
index 9c9ebd782..edfdb58e3 100644
--- a/apps/web/src/features/user/mutations.ts
+++ b/apps/web/src/features/user/mutations.ts
@@ -230,6 +230,10 @@ export async function deleteOrphanedAnonymousUsers() {
const batch = await prisma.user.findMany({
where: orphanedAnonymousFilter,
select: { id: true },
+ // Without an order Prisma sorts by id, and the planner walks the whole
+ // users primary key expecting orphans to be common. Ordering on
+ // last_seen_at scans only the idle anonymous range of its index.
+ orderBy: { lastSeenAt: "asc" },
take: Math.min(
DELETE_ORPHANED_ANONYMOUS_USERS_BATCH_SIZE,
DELETE_ORPHANED_ANONYMOUS_USERS_MAX_PER_RUN - deleted,