Skip to content
Open
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
21 changes: 10 additions & 11 deletions apps/api/src/guild/leaderboards/guild-leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,27 @@ export class GuildLeaderboardService extends LeaderboardService {
protected async getAdditionalStats(
ids: string[],
fields: string[]
): Promise<LeaderboardAdditionalStats[]> {
): Promise<Map<string, LeaderboardAdditionalStats>> {
const selector = fields.reduce((acc, key) => {
acc[key] = true;
return acc;
}, {} as Record<string, boolean>);

selector.id = true;
selector.nameFormatted = true;

return await Promise.all(
ids.map(async (id) => {
const guild = await this.guildModel
.findOne()
.where("id")
.equals(id)
.select(selector)
.lean()
.exec();
const guilds = await this.guildModel
.find({ id: { $in: ids } })
.select(selector)
.lean()
.exec();

return new Map(
guilds.map((guild) => {
const additionalStats = flatten(guild) as LeaderboardAdditionalStats;
additionalStats.name = additionalStats.nameFormatted;

return additionalStats;
return [guild.id, additionalStats];
})
);
}
Expand Down
22 changes: 17 additions & 5 deletions apps/api/src/leaderboards/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { InjectRedis } from "#redis";
import { Injectable, InternalServerErrorException } from "@nestjs/common";
import { type LeaderboardEnabledMetadata, getLeaderboardField, getLeaderboardFields } from "@statsify/schemas";
import { LeaderboardQuery } from "@statsify/api-client";
import { Logger } from "@statsify/logger";
import { Redis } from "ioredis";

const DAYS_IN_WEEK = {
Expand All @@ -29,6 +30,8 @@ export type LeaderboardAdditionalStats = Record<string, any> & { name: string };

@Injectable()
export abstract class LeaderboardService {
private readonly logger = new Logger("LeaderboardService");

public constructor(@InjectRedis() protected readonly redis: Redis) {}

public async addLeaderboards<T>(
Expand Down Expand Up @@ -143,8 +146,9 @@ export abstract class LeaderboardService {
]
);

const data = leaderboard.map((doc, index) => {
const stats = additionalStats[index];
const data = leaderboard.flatMap((doc) => {
const stats = additionalStats.get(doc.id);
if (!stats) return [];

if (extraDisplay) {
const extraDisplayValue = stats[extraDisplay] ?? extraDisplayMetadata?.default;
Expand All @@ -168,15 +172,23 @@ export abstract class LeaderboardService {
if (!hidden) fields.push(field);
fields.push(...additionalValues);

return {
return [{
id: doc.id,
fields,
name: stats.name,
position: doc.index + 1,
highlight: doc.index === highlight,
};
}];
});

const missing = leaderboard.length - data.length;

if (missing) {
this.logger.warn(
`${missing} of ${leaderboard.length} rows on ${constructor.name} ${field} page ${top / PAGE_SIZE} have no document`
);
}

const fields = [];
if (!hidden) fields.push(fieldName);
fields.push(...additionalFieldMetadata.map(({ fieldName }) => fieldName));
Expand Down Expand Up @@ -265,7 +277,7 @@ export abstract class LeaderboardService {
protected abstract getAdditionalStats(
ids: string[],
fields: string[]
): Promise<LeaderboardAdditionalStats[]>;
): Promise<Map<string, LeaderboardAdditionalStats>>;

private async getLeaderboardFromRedis<T>(
constructor: Constructor<T>,
Expand Down
21 changes: 10 additions & 11 deletions apps/api/src/player/leaderboards/player-leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,27 @@ export class PlayerLeaderboardService extends LeaderboardService {
protected async getAdditionalStats(
ids: string[],
fields: string[]
): Promise<LeaderboardAdditionalStats[]> {
): Promise<Map<string, LeaderboardAdditionalStats>> {
const selector = fields.reduce((acc, key) => {
acc[key] = true;
return acc;
}, {} as Record<string, boolean>);

selector.displayName = true;
selector.uuid = true;

return await Promise.all(
ids.map(async (id) => {
const player = await this.playerModel
.findOne()
.where("uuid")
.equals(id)
.select(selector)
.lean()
.exec();
const players = await this.playerModel
.find({ uuid: { $in: ids } })
.select(selector)
.lean()
.exec();

return new Map(
players.map((player) => {
const additionalStats = flatten(player) as LeaderboardAdditionalStats;
additionalStats.name = additionalStats.displayName;

return additionalStats;
return [player.uuid, additionalStats];
})
);
}
Expand Down
Loading