Skip to content

Commit

Permalink
Revert "fix: try to close mongo db connections after every query"
Browse files Browse the repository at this point in the history
This reverts commit 82df025.
  • Loading branch information
Geczy committed Aug 27, 2023
1 parent 82df025 commit 311250f
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 476 deletions.
50 changes: 21 additions & 29 deletions packages/dota/src/dota/lib/getAccountsFromMatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { delayedGames } from '@dotabod/prisma/dist/mongo/index.js'

import MongoDBSingleton from '../../steam/MongoDBSingleton.js'
import { mongoClient } from '../../steam/index.js'
import { Packet } from '../../types.js'
import { getCurrentMatchPlayers } from './getCurrentMatchPlayers.js'

Expand All @@ -26,34 +26,26 @@ export async function getAccountsFromMatch({
}

const matchId = searchMatchId || gsi?.map?.matchid
const response = await mongoClient
.collection<delayedGames>('delayedGames')
.findOne({ 'match.match_id': matchId })

let matchPlayers = [] as { heroid: number; accountid: number }[]
if (Array.isArray(response?.teams) && response?.teams.length === 2) {
matchPlayers = [
...response.teams[0].players.map((a) => ({
heroid: a.heroid,
accountid: Number(a.accountid),
})),
...response.teams[1].players.map((a) => ({
heroid: a.heroid,
accountid: Number(a.accountid),
})),
]
}

const mongo = new MongoDBSingleton()
const db = await mongo.connect()

try {
const response = await db
.collection<delayedGames>('delayedGames')
.findOne({ 'match.match_id': matchId })

const matchPlayers =
Array.isArray(response?.teams) && response?.teams.length === 2
? [
...response.teams[0].players.map((a) => ({
heroid: a.heroid,
accountid: Number(a.accountid),
})),
...response.teams[1].players.map((a) => ({
heroid: a.heroid,
accountid: Number(a.accountid),
})),
]
: ([] as { heroid: number; accountid: number }[])

return {
matchPlayers,
accountIds: matchPlayers.map((player) => player.accountid),
}
} finally {
await mongo.close()
return {
matchPlayers,
accountIds: matchPlayers.map((player) => player.accountid),
}
}
50 changes: 22 additions & 28 deletions packages/dota/src/dota/lib/getPlayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { delayedGames } from '@dotabod/prisma/dist/mongo'
import { t } from 'i18next'

import Dota from '../../steam/index.js'
import MongoDBSingleton from '../../steam/MongoDBSingleton.js'
import Mongo from '../../steam/mongo.js'
import CustomError from '../../utils/customError.js'
import { getAccountsFromMatch } from './getAccountsFromMatch.js'

const mongo = await Mongo.connect()
const dota = Dota.getInstance()

export async function getPlayers(
Expand All @@ -21,32 +22,25 @@ export async function getPlayers(
throw new CustomError(t('gameNotFound', { lng: locale }))
}

const mongo = new MongoDBSingleton()
const db = await mongo.connect()

try {
const response = await db
.collection<delayedGames>('delayedGames')
.findOne({ 'match.match_id': currentMatchId })

if (!response && !players?.length) {
throw new CustomError(t('missingMatchData', { emote: 'PauseChamp', lng: locale }))
}

const { matchPlayers, accountIds } = await getAccountsFromMatch({
searchMatchId: currentMatchId,
searchPlayers: players,
})

const cards = await dota.getCards(accountIds)

return {
gameMode: response ? Number(response.match.game_mode) : undefined,
matchPlayers,
accountIds,
cards,
}
} finally {
await mongo.close()
const response = await mongo
.collection<delayedGames>('delayedGames')
.findOne({ 'match.match_id': currentMatchId })

if (!response && !players?.length) {
throw new CustomError(t('missingMatchData', { emote: 'PauseChamp', lng: locale }))
}

const { matchPlayers, accountIds } = await getAccountsFromMatch({
searchMatchId: currentMatchId,
searchPlayers: players,
})

const cards = await dota.getCards(accountIds)

return {
gameMode: response ? Number(response.match.game_mode) : undefined,
matchPlayers,
accountIds,
cards,
}
}
138 changes: 57 additions & 81 deletions packages/dota/src/steam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ import { GCMatchData } from '../types.js'
import CustomError from '../utils/customError.js'
import { retryCustom } from '../utils/index.js'
import { logger } from '../utils/logger.js'
import MongoDBSingleton from './MongoDBSingleton.js'
import Mongo from './mongo.js'

export const mongoClient = await Mongo.connect()

// Fetches data from MongoDB
const fetchDataFromMongo = async (match_id: string) => {
const mongo = new MongoDBSingleton()
const db = await mongo.connect()

try {
return await db.collection<delayedGames>('delayedGames').findOne({ 'match.match_id': match_id })
} finally {
await mongo.close()
}
return await mongoClient
.collection<delayedGames>('delayedGames')
.findOne({ 'match.match_id': match_id })
}

// Constructs the API URL
Expand All @@ -47,22 +44,15 @@ const saveMatch = async ({
game: delayedGames
refetchCards?: boolean
}) => {
const mongo = new MongoDBSingleton()
const db = await mongo.connect()
await mongoClient
.collection<delayedGames>('delayedGames')
.updateOne({ 'match.match_id': match_id }, { $set: game }, { upsert: true })

try {
await db
.collection<delayedGames>('delayedGames')
.updateOne({ 'match.match_id': match_id }, { $set: game }, { upsert: true })

if (refetchCards) {
const { accountIds } = await getAccountsFromMatch({
searchMatchId: game.match.match_id,
})
await dota.getCards(accountIds, true)
}
} finally {
await mongo.close()
if (refetchCards) {
const { accountIds } = await getAccountsFromMatch({
searchMatchId: game.match.match_id,
})
await dota.getCards(accountIds, true)
}
}

Expand Down Expand Up @@ -411,69 +401,55 @@ class Dota {
return Dota.instance
}

fetchAndUpdateCard = async (accountId: number) => {
const fetchedCard = accountId
? await retryCustom({
retries: 10,
fn: () => this.getCard(accountId),
minTimeout: 1000,
}).catch(() => ({
rank_tier: -10,
leaderboard_rank: 0,
}))
: undefined

const card = {
...fetchedCard,
account_id: accountId,
createdAt: new Date(),
rank_tier: fetchedCard?.rank_tier ?? 0,
leaderboard_rank: fetchedCard?.leaderboard_rank ?? 0,
} as cards

if (!accountId) return card

if (fetchedCard?.rank_tier !== -10) {
const mongo = new MongoDBSingleton()
const db = await mongo.connect()

try {
await db
public async getCards(accounts: number[], refetchCards = false): Promise<cards[]> {
const cardsFromDb = await mongoClient
.collection<cards>('cards')
.find({ account_id: { $in: accounts.filter((a) => !!a) } })
.sort({ createdAt: -1 })
.toArray()

const cardsMap = new Map(cardsFromDb.map((card) => [card.account_id, card]))

const fetchAndUpdateCard = async (accountId: number) => {
const fetchedCard = accountId
? await retryCustom({
retries: 10,
fn: () => this.getCard(accountId),
minTimeout: 1000,
}).catch(() => ({
rank_tier: -10,
leaderboard_rank: 0,
}))
: undefined

const card = {
...fetchedCard,
account_id: accountId,
createdAt: new Date(),
rank_tier: fetchedCard?.rank_tier ?? 0,
leaderboard_rank: fetchedCard?.leaderboard_rank ?? 0,
} as cards

if (!accountId) return card

if (fetchedCard?.rank_tier !== -10) {
await mongoClient
.collection<cards>('cards')
.updateOne({ account_id: accountId }, { $set: card }, { upsert: true })
} finally {
await mongo.close()
}
}

return card
}

public async getCards(accounts: number[], refetchCards = false): Promise<cards[]> {
const mongo = new MongoDBSingleton()
const db = await mongo.connect()
return card
}

try {
const cardsFromDb = await db
.collection<cards>('cards')
.find({ account_id: { $in: accounts.filter((a) => !!a) } })
.sort({ createdAt: -1 })
.toArray()

const cardsMap = new Map(cardsFromDb.map((card) => [card.account_id, card]))

const promises = accounts.map(async (accountId) => {
const existingCard = cardsMap.get(accountId)
if (refetchCards || !existingCard || typeof existingCard.rank_tier !== 'number') {
return this.fetchAndUpdateCard(accountId)
}
return existingCard
})
const promises = accounts.map(async (accountId) => {
const existingCard = cardsMap.get(accountId)
if (refetchCards || !existingCard || typeof existingCard.rank_tier !== 'number') {
return fetchAndUpdateCard(accountId)
}
return existingCard
})

return Promise.all(promises)
} finally {
await mongo.close()
}
return Promise.all(promises)
}

public async getCard(account: number): Promise<cards> {
Expand Down
Loading

0 comments on commit 311250f

Please sign in to comment.