aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-02-28 16:55:40 -0600
committermat <27899617+mat-1@users.noreply.github.com>2021-02-28 16:55:40 -0600
commitf5a8a08022e5748cf8c582bf2486520e27d73f06 (patch)
tree075c7d338eb98c51b0c5a4c645f26a083f23e5f4
parentd9d557a095d09f0d4e643ee6b59a28520a003b20 (diff)
downloadskyblock-api-f5a8a08022e5748cf8c582bf2486520e27d73f06.tar.gz
skyblock-api-f5a8a08022e5748cf8c582bf2486520e27d73f06.tar.bz2
skyblock-api-f5a8a08022e5748cf8c582bf2486520e27d73f06.zip
Make leaderboard stuff use less memory
-rw-r--r--build/database.js17
-rw-r--r--build/util.js6
-rw-r--r--src/database.ts20
-rw-r--r--src/util.ts4
4 files changed, 12 insertions, 35 deletions
diff --git a/build/database.js b/build/database.js
index d400677..208c56a 100644
--- a/build/database.js
+++ b/build/database.js
@@ -39,7 +39,6 @@ const recentlyUpdated = new node_cache_1.default({
useClones: false,
});
const cachedRawLeaderboards = new Map();
-const cachedLeaderboards = new Map();
const leaderboardMax = 100;
const reversedStats = [
'first_join'
@@ -121,8 +120,6 @@ async function fetchMemberLeaderboardRaw(name) {
return leaderboardRaw;
}
async function fetchMemberLeaderboard(name) {
- if (cachedLeaderboards.has(name))
- return cachedLeaderboards.get(name);
const leaderboardRaw = await fetchMemberLeaderboardRaw(name);
const fetchLeaderboardPlayer = async (item) => {
return {
@@ -135,7 +132,6 @@ async function fetchMemberLeaderboard(name) {
promises.push(fetchLeaderboardPlayer(item));
}
const leaderboard = await Promise.all(promises);
- cachedLeaderboards.set(name, leaderboard);
return leaderboard;
}
exports.fetchMemberLeaderboard = fetchMemberLeaderboard;
@@ -177,18 +173,8 @@ async function updateDatabaseMember(member, profile) {
}
}, { upsert: true });
for (const [attributeName, attributeValue] of Object.entries(leaderboardAttributes)) {
- const existingLeaderboard = await fetchMemberLeaderboard(attributeName);
const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName);
const leaderboardReverse = isLeaderboardReversed(attributeName);
- const newLeaderboard = existingLeaderboard
- // remove the player from the leaderboard, if they're there
- .filter(value => value.player.uuid !== member.uuid)
- .concat([{
- player: await cached.fetchPlayer(member.uuid),
- value: attributeValue
- }])
- .sort((a, b) => leaderboardReverse ? a.value - b.value : b.value - a.value)
- .slice(0, 100);
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.uuid !== member.uuid)
@@ -199,7 +185,6 @@ async function updateDatabaseMember(member, profile) {
}])
.sort((a, b) => leaderboardReverse ? a.stats[attributeName] - b.stats[attributeName] : b.stats[attributeName] - a.stats[attributeName])
.slice(0, 100);
- cachedLeaderboards.set(attributeName, newLeaderboard);
cachedRawLeaderboards.set(attributeName, newRawLeaderboard);
}
}
@@ -212,7 +197,7 @@ async function removeBadMemberLeaderboardAttributes() {
// shuffle so if the application is restarting many times itll still be useful
for (const leaderboard of util_1.shuffle(leaderboards)) {
// wait 10 seconds so it doesnt use as much ram
- await new Promise(resolve => setTimeout(resolve, 10000));
+ await util_1.sleep(100000);
const unsetValue = {};
unsetValue[leaderboard] = '';
const filter = {};
diff --git a/build/util.js b/build/util.js
index 3b21db4..785c195 100644
--- a/build/util.js
+++ b/build/util.js
@@ -3,7 +3,7 @@
* Random utility functions that are not related to Hypixel
*/
Object.defineProperty(exports, "__esModule", { value: true });
-exports.colorCodeFromName = exports.minecraftColorCodes = exports.shuffle = exports.jsonToQuery = exports.queryToJson = exports.undashUuid = void 0;
+exports.sleep = exports.colorCodeFromName = exports.minecraftColorCodes = exports.shuffle = exports.jsonToQuery = exports.queryToJson = exports.undashUuid = void 0;
function undashUuid(uuid) {
return uuid.replace(/-/g, '').toLowerCase();
}
@@ -78,3 +78,7 @@ function colorCodeFromName(colorName) {
}
}
exports.colorCodeFromName = colorCodeFromName;
+async function sleep(ms) {
+ await new Promise(resolve => setTimeout(resolve, ms));
+}
+exports.sleep = sleep;
diff --git a/src/database.ts b/src/database.ts
index afbcf08..83360c3 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -8,7 +8,7 @@ import { Collection, Db, MongoClient } from 'mongodb'
import NodeCache from 'node-cache'
import { CleanMember } from './cleaners/skyblock/member'
import { CleanPlayer } from './cleaners/player'
-import { shuffle } from './util'
+import { shuffle, sleep } from './util'
import { CleanFullProfile } from './cleaners/skyblock/profile'
import { categorizeStat, StatCategory } from './cleaners/skyblock/stats'
@@ -31,7 +31,6 @@ interface LeaderboardItem {
}
const cachedRawLeaderboards: Map<string, DatabaseLeaderboardItem[]> = new Map()
-const cachedLeaderboards: Map<string, LeaderboardItem[]> = new Map()
const leaderboardMax = 100
const reversedStats = [
@@ -133,9 +132,6 @@ async function fetchMemberLeaderboardRaw(name: string): Promise<DatabaseLeaderbo
}
export async function fetchMemberLeaderboard(name: string) {
- if (cachedLeaderboards.has(name))
- return cachedLeaderboards.get(name)
-
const leaderboardRaw = await fetchMemberLeaderboardRaw(name)
const fetchLeaderboardPlayer = async(item: DatabaseLeaderboardItem): Promise<LeaderboardItem> => {
return {
@@ -148,7 +144,6 @@ export async function fetchMemberLeaderboard(name: string) {
promises.push(fetchLeaderboardPlayer(item))
}
const leaderboard = await Promise.all(promises)
- cachedLeaderboards.set(name, leaderboard)
return leaderboard
}
@@ -198,18 +193,8 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu
)
for (const [ attributeName, attributeValue ] of Object.entries(leaderboardAttributes)) {
- const existingLeaderboard = await fetchMemberLeaderboard(attributeName)
const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName)
const leaderboardReverse = isLeaderboardReversed(attributeName)
- const newLeaderboard = existingLeaderboard
- // remove the player from the leaderboard, if they're there
- .filter(value => value.player.uuid !== member.uuid)
- .concat([{
- player: await cached.fetchPlayer(member.uuid),
- value: attributeValue
- }])
- .sort((a, b) => leaderboardReverse ? a.value - b.value : b.value - a.value)
- .slice(0, 100)
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.uuid !== member.uuid)
@@ -220,7 +205,6 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu
}])
.sort((a, b) => leaderboardReverse ? a.stats[attributeName] - b.stats[attributeName] : b.stats[attributeName] - a.stats[attributeName])
.slice(0, 100)
- cachedLeaderboards.set(attributeName, newLeaderboard)
cachedRawLeaderboards.set(attributeName, newRawLeaderboard)
}
}
@@ -234,7 +218,7 @@ async function removeBadMemberLeaderboardAttributes() {
// shuffle so if the application is restarting many times itll still be useful
for (const leaderboard of shuffle(leaderboards)) {
// wait 10 seconds so it doesnt use as much ram
- await new Promise(resolve => setTimeout(resolve, 10000))
+ await sleep(100000)
const unsetValue = {}
unsetValue[leaderboard] = ''
diff --git a/src/util.ts b/src/util.ts
index 2ff55a8..e0c7699 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -79,4 +79,8 @@ export function colorCodeFromName(colorName: string): string {
if (key.length === 1 && value === hexColor)
return key
}
+}
+
+export async function sleep(ms: number) {
+ await new Promise(resolve => setTimeout(resolve, ms))
} \ No newline at end of file