Skip to content

refactor(lb): refactor lb update query (@fehmer) #6738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion backend/__tests__/dal/leaderboards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function expectedLbEntry(
// @ts-expect-error
user.lbPersonalBests?.time[Number.parseInt(time)].english;

return {
const entry = {
rank,
uid: user.uid,
name: user.name,
Expand All @@ -294,6 +294,15 @@ function expectedLbEntry(
badgeId,
isPremium,
};

if (badgeId === undefined) {
delete entry.badgeId;
}
if (isPremium === undefined) {
delete entry.isPremium;
}

return entry;
}

async function createUser(
Expand Down
166 changes: 87 additions & 79 deletions backend/src/dal/leaderboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,96 +123,104 @@ export async function update(
const lbCollectionName = `leaderboards.${language}.${mode}.${mode2}`;
const minTimeTyping = (await getCachedConfiguration(true)).leaderboards
.minTimeTyping;
const lb = db.collection<DBUser>("users").aggregate<LeaderboardEntry>(
[
{
$match: {
[`${key}.wpm`]: {
$gt: 0,
},
[`${key}.acc`]: {
$gt: 0,
},
[`${key}.timestamp`]: {
$gt: 0,
},
banned: {
$ne: true,
},
lbOptOut: {
$ne: true,
},
needsToChangeName: {
$ne: true,
},
timeTyping: {
$gt: isDevEnvironment() ? 0 : minTimeTyping,
},
},
const pipeline = [
{
$match: {
[`${key}.wpm`]: { $gt: 0 },
[`${key}.acc`]: { $gt: 0 },
[`${key}.timestamp`]: { $gt: 0 },
banned: { $ne: true },
lbOptOut: { $ne: true },
needsToChangeName: { $ne: true },
timeTyping: { $gt: isDevEnvironment() ? 0 : minTimeTyping },
},
{
$sort: {
[`${key}.wpm`]: -1,
[`${key}.acc`]: -1,
[`${key}.timestamp`]: -1,
},

{
$project: {
_id: 0,
[`${key}.wpm`]: 1,
[`${key}.acc`]: 1,
[`${key}.raw`]: 1,
[`${key}.consistency`]: {
$ifNull: [`$${key}.consistency`, "$$REMOVE"],
},
},
{
$project: {
_id: 0,
[`${key}.wpm`]: 1,
[`${key}.acc`]: 1,
[`${key}.raw`]: 1,
[`${key}.consistency`]: 1,
[`${key}.timestamp`]: 1,
uid: 1,
name: 1,
discordId: 1,
discordAvatar: 1,
inventory: 1,
premium: 1,
[`${key}.timestamp`]: 1,
uid: 1,
name: 1,
discordId: 1,
discordAvatar: 1,
inventory: 1,
premium: 1,
sortKey: {
$add: [
{ $multiply: [`${key}.wpm`, 1e19] }, //maybe use $convert: {input: `${key}.wpm`,to: "double",onError: null,},
{ $multiply: [`${key}.acc`, 1e16] },
[`${key}.timestamp`],
],
},
},
},
//sort by wpm, acc, timestamp descending, add rank number
{
$setWindowFields: {
sortBy: { sortKey: -1 },
output: { "user.rank": { $documentNumber: {} } },
},
},
{
$addFields: {
"user.uid": "$uid",
"user.name": "$name",
"user.discordId": { $ifNull: ["$discordId", "$$REMOVE"] },
"user.discordAvatar": { $ifNull: ["$discordAvatar", "$$REMOVE"] },
"user.badgeId": {
$ifNull: [
{
$first: {
$map: {
input: {
$filter: {
input: "$inventory.badges",
as: "badge",
cond: { $eq: ["$$badge.selected", true] },
},
},
as: "selectedBadge",
in: "$$selectedBadge.id",
},
},
},
"$$REMOVE",
],
},

{
$addFields: {
"user.uid": "$uid",
"user.name": "$name",
"user.discordId": { $ifNull: ["$discordId", "$$REMOVE"] },
"user.discordAvatar": { $ifNull: ["$discordAvatar", "$$REMOVE"] },
[`${key}.consistency`]: {
$ifNull: [`$${key}.consistency`, "$$REMOVE"],
},
calculated: {
$function: {
lang: "js",
args: [
"$premium.expirationTimestamp",
"$$NOW",
"$inventory.badges",
"user.isPremium": {
$cond: {
if: {
$or: [
{ $eq: ["$premium.expirationTimestamp", -1] },
{ $gt: ["$premium.expirationTimestamp", { $toLong: "$$NOW" }] },
],
body: `function(expiration, currentTime, badges) {
try {row_number+= 1;} catch (e) {row_number= 1;}
var badgeId = undefined;
if(badges)for(let i=0; i<badges.length; i++){
if(badges[i].selected){ badgeId = badges[i].id; break}
}
var isPremium = expiration !== undefined && (expiration === -1 || new Date(expiration)>currentTime) || undefined;
return {rank:row_number,badgeId, isPremium};
}`,
},
// oxlint-disable-next-line no-thenable
then: true,
else: "$$REMOVE",
},
},
},
{
$replaceWith: {
$mergeObjects: [`$${key}`, "$user", "$calculated"],
},
},
{
$replaceWith: {
$mergeObjects: [`$${key}`, "$user"],
},
{ $out: lbCollectionName },
],
{ allowDiskUse: true }
);
},
{ $out: lbCollectionName },
];

const lb = db
.collection<DBUser>("users")
.aggregate<LeaderboardEntry>(pipeline, { allowDiskUse: true });

const start1 = performance.now();
await lb.toArray();
Expand Down