aboutsummaryrefslogtreecommitdiff
path: root/src/database.ts
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-05-30 21:43:25 -0500
committermat <github@matdoes.dev>2021-05-30 21:43:25 -0500
commit3b69df999f91a34580b4a75b1e95211e225c9fbb (patch)
treec8ac628ec7e11ea22c730291f57011025a9739a3 /src/database.ts
parent09abf9c1fc875e95dc58c0ad0b59a107d800f9d4 (diff)
downloadskyblock-api-3b69df999f91a34580b4a75b1e95211e225c9fbb.tar.gz
skyblock-api-3b69df999f91a34580b4a75b1e95211e225c9fbb.tar.bz2
skyblock-api-3b69df999f91a34580b4a75b1e95211e225c9fbb.zip
i think i found out why it's using so much memory.
Diffstat (limited to 'src/database.ts')
-rw-r--r--src/database.ts35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/database.ts b/src/database.ts
index 2acfee5..0939e79 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -280,11 +280,16 @@ async function fetchMemberLeaderboardRaw(name: string): Promise<DatabaseMemberLe
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1
fetchingRawLeaderboardNames.add(name)
- const leaderboardRaw: DatabaseMemberLeaderboardItem[] = await memberLeaderboardsCollection
+ const leaderboardRaw: DatabaseMemberLeaderboardItem[] = (await memberLeaderboardsCollection
.find(query)
.sort(sortQuery)
.limit(leaderboardMax)
- .toArray()
+ .toArray())
+ .map((i: DatabaseMemberLeaderboardItem): DatabaseMemberLeaderboardItem => {
+ const stats = {}
+ stats[name] = i.stats[name]
+ return { ...i, stats }
+ })
fetchingRawLeaderboardNames.delete(name)
cachedRawLeaderboards.set(name, leaderboardRaw)
return leaderboardRaw
@@ -311,11 +316,16 @@ async function fetchProfileLeaderboardRaw(name: string): Promise<DatabaseProfile
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1
fetchingRawLeaderboardNames.add(name)
- const leaderboardRaw: DatabaseProfileLeaderboardItem[] = await profileLeaderboardsCollection
+ const leaderboardRaw: DatabaseProfileLeaderboardItem[] = (await profileLeaderboardsCollection
.find(query)
.sort(sortQuery)
.limit(leaderboardMax)
- .toArray()
+ .toArray())
+ .map((i: DatabaseProfileLeaderboardItem): DatabaseProfileLeaderboardItem => {
+ const stats = {}
+ stats[name] = i.stats[name]
+ return { ...i, stats }
+ })
fetchingRawLeaderboardNames.delete(name)
cachedRawLeaderboards.set(name, leaderboardRaw)
@@ -537,12 +547,16 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu
for (const [ attributeName, attributeValue ] of Object.entries(leaderboardAttributes)) {
const existingRawLeaderboard = await fetchMemberLeaderboardRaw(attributeName)
const leaderboardReverse = isLeaderboardReversed(attributeName)
+
+ const thisLeaderboardAttributes = {}
+ thisLeaderboardAttributes[attributeName] = attributeValue
+
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.uuid !== member.uuid || value.profile !== profile.uuid)
.concat([{
last_updated: new Date(),
- stats: leaderboardAttributes,
+ stats: thisLeaderboardAttributes,
uuid: member.uuid,
profile: profile.uuid
}])
@@ -592,12 +606,16 @@ export async function updateDatabaseProfile(profile: CleanFullProfile): Promise<
for (const [ attributeName, attributeValue ] of Object.entries(leaderboardAttributes)) {
const existingRawLeaderboard = await fetchProfileLeaderboardRaw(attributeName)
const leaderboardReverse = isLeaderboardReversed(attributeName)
+
+ const thisLeaderboardAttributes = {}
+ thisLeaderboardAttributes[attributeName] = attributeValue
+
const newRawLeaderboard = existingRawLeaderboard
// remove the player from the leaderboard, if they're there
.filter(value => value.uuid !== profile.uuid)
.concat([{
last_updated: new Date(),
- stats: leaderboardAttributes,
+ stats: thisLeaderboardAttributes,
uuid: profile.uuid,
players: profile.members.map(p => p.uuid)
}])
@@ -730,3 +748,8 @@ if (!globalThis.isTest) {
setInterval(fetchAllLeaderboards, 4 * 60 * 60 * 1000)
})
}
+
+setInterval(() => {
+ console.log(cachedRawLeaderboards.size)
+ console.log(Array.from(cachedRawLeaderboards.values())[cachedRawLeaderboards.size - 1])
+}, 5000) \ No newline at end of file