Fix performance of FindChartsOnPopularity

This commit is contained in:
zkldi
2021-09-19 19:12:19 +01:00
parent d4a3ca5b56
commit 93b6b32c60
3 changed files with 49 additions and 3 deletions
+8 -1
View File
@@ -24,11 +24,13 @@ const staticIndexes: Partial<Record<Databases, Index[]>> = {
index({ scoreID: 1 }, UNIQUE),
index({ chartID: 1, userID: 1 }),
index({ game: 1, playtype: 1, userID: 1 }),
index({ chartID: 1 }),
],
"personal-bests": [
index({ chartID: 1, userID: 1 }, UNIQUE),
index({ chartID: 1, "scoreData.percent": 1 }),
index({ game: 1, playtype: 1, userID: 1 }),
index({ chartID: 1 }),
],
sessions: [
// lol
@@ -83,6 +85,7 @@ const staticIndexes: Partial<Record<Databases, Index[]>> = {
"user-private-information": [index({ userID: 1 }, UNIQUE), index({ email: 1 }, UNIQUE)],
"fer-settings": [index({ userID: 1 }, UNIQUE)],
counters: [index({ counterName: 1 }, UNIQUE)],
"class-achievements": [index({ game: 1, playtype: 1, timeAchieved: 1 })],
};
const indexes: Partial<Record<Databases, Index[]>> = staticIndexes;
@@ -94,12 +97,16 @@ for (const game of ServerTypeInfo.supportedGames) {
index(
{ songID: 1, difficulty: 1, playtype: 1, isPrimary: 1 },
{ unique: true, partialFilterExpression: { isPrimary: { $eq: true } } }
)
),
index({ songID: 1 }),
index({ playtype: 1 })
);
} else {
indexes[`charts-${game}` as Databases] = [
index({ chartID: 1 }, UNIQUE),
index({ songID: 1, difficulty: 1, playtype: 1, isPrimary: 1 }, UNIQUE),
index({ songID: 1 }),
index({ playtype: 1 }),
];
}
-2
View File
@@ -236,8 +236,6 @@ export function FindChartOnARCID(game: "iidx" | "ddr" | "jubeat" | "sdvx", arcID
* Returns the N most popular charts for this game + playtype.
* Popularity is determined by how many scores match in the score
* collection.
*
* Performance of this is untested.
*/
export function FindChartsOnPopularity(
game: Game,
+41
View File
@@ -0,0 +1,41 @@
import { Game, Playtypes, gameClasses, IDStrings, integer } from "tachi-common";
import NodeCache from "node-cache";
import db from "external/mongo/db";
import { ONE_HOUR } from "lib/constants/time";
const classDistCache = new NodeCache();
export async function GetClassDistribution(
game: Game,
playtype: Playtypes[Game],
className: gameClasses.GameClassSets[IDStrings]
) {
const cacheKey = `${game}:${playtype}:${className}`;
const cache = classDistCache.get(cacheKey);
if (!cache) {
const distribution = (await db["game-stats"].aggregate([
{
$match: {
game,
playtype,
},
},
{
$group: {
_id: `$classes.${className}`,
count: { $sum: 1 },
},
},
])) as { _id: string; count: integer }[];
// Converts {_id: "kaiden", count: 3} to {"kaiden": 3}, more or less.
const convert = Object.fromEntries(distribution.map((e) => [e._id, e.count]));
classDistCache.set(cacheKey, convert, ONE_HOUR);
return convert;
} else {
return cache;
}
}