setup memcache
This commit is contained in:
@@ -43,20 +43,30 @@ function monthIdToEndSeconds(monthId: number): number {
|
||||
return Number.MAX_SAFE_INTEGER
|
||||
}
|
||||
|
||||
export function addRushStatsApis(app, jsonParser) {
|
||||
export function addRushStatsApis(app, jsonParser, memcache) {
|
||||
app.post('/api/serverstats/gamestats', jsonParser, async (req, res) => {
|
||||
const monthId = req.body.monthId
|
||||
let startSeconds = monthIdToStartSeconds(monthId)
|
||||
let endSeconds = monthIdToEndSeconds(monthId)
|
||||
|
||||
const serverGameStats = await database.query(serverGameStatsQuery, {
|
||||
type: QueryTypes.SELECT,
|
||||
replacements: {
|
||||
startSeconds,
|
||||
endSeconds
|
||||
}
|
||||
})
|
||||
res.send(serverGameStats[0])
|
||||
let cachedResponse = await memcache.get('gamestats' + req.body.monthId)
|
||||
|
||||
if (!cachedResponse) {
|
||||
console.log('cache miss')
|
||||
const serverGameStats = await database.query(serverGameStatsQuery, {
|
||||
type: QueryTypes.SELECT,
|
||||
replacements: {
|
||||
startSeconds,
|
||||
endSeconds
|
||||
}
|
||||
})
|
||||
|
||||
await memcache.set('gamestats' + req.body.monthId, serverGameStats[0])
|
||||
res.send(serverGameStats[0])
|
||||
} else {
|
||||
console.log('cache hit')
|
||||
res.send(cachedResponse)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/api/serverstats/rolestats', jsonParser, async (req, res) => {
|
||||
@@ -64,52 +74,60 @@ export function addRushStatsApis(app, jsonParser) {
|
||||
let startSeconds = monthIdToStartSeconds(monthId)
|
||||
let endSeconds = monthIdToEndSeconds(monthId)
|
||||
|
||||
const games = await Game.findAll({
|
||||
include: [
|
||||
{ model: Character, as: 'characterPickedForGame' },
|
||||
{ model: Character, as: 'characterAppliedForGame' }
|
||||
],
|
||||
where: { postdate: { [Op.between]: [startSeconds, endSeconds] } }
|
||||
})
|
||||
let cachedResponse = await memcache.get('rolestats' + req.body.monthId)
|
||||
|
||||
// count active roles
|
||||
let activeCharacters = new Map()
|
||||
let pickedCharacterCount = new Map()
|
||||
let appedCharacterCount = new Map()
|
||||
|
||||
games.forEach((game, gameNum) => {
|
||||
const picks = game.dataValues.characterPickedForGame
|
||||
const appls = game.dataValues.characterAppliedForGame
|
||||
|
||||
picks.forEach((character, characterNum) => {
|
||||
const role = character.dataValues.role
|
||||
// Count role application
|
||||
pickedCharacterCount.set(role, (pickedCharacterCount.get(role) || 0) + 1)
|
||||
if (!cachedResponse) {
|
||||
const games = await Game.findAll({
|
||||
include: [
|
||||
{ model: Character, as: 'characterPickedForGame' },
|
||||
{ model: Character, as: 'characterAppliedForGame' }
|
||||
],
|
||||
where: { postdate: { [Op.between]: [startSeconds, endSeconds] } }
|
||||
})
|
||||
|
||||
appls.forEach((character, characterNum) => {
|
||||
const role = character.dataValues.role
|
||||
const charId = character.dataValues.id
|
||||
// Add apllied characters to active list
|
||||
if (!activeCharacters.has(role)) {
|
||||
activeCharacters.set(role, new Set())
|
||||
// count active roles
|
||||
let activeCharacters = new Map()
|
||||
let pickedCharacterCount = new Map()
|
||||
let appedCharacterCount = new Map()
|
||||
|
||||
games.forEach((game, gameNum) => {
|
||||
const picks = game.dataValues.characterPickedForGame
|
||||
const appls = game.dataValues.characterAppliedForGame
|
||||
|
||||
picks.forEach((character, characterNum) => {
|
||||
const role = character.dataValues.role
|
||||
// Count role application
|
||||
pickedCharacterCount.set(role, (pickedCharacterCount.get(role) || 0) + 1)
|
||||
})
|
||||
|
||||
appls.forEach((character, characterNum) => {
|
||||
const role = character.dataValues.role
|
||||
const charId = character.dataValues.id
|
||||
// Add apllied characters to active list
|
||||
if (!activeCharacters.has(role)) {
|
||||
activeCharacters.set(role, new Set())
|
||||
}
|
||||
activeCharacters.get(role).add(charId)
|
||||
// Count role application
|
||||
appedCharacterCount.set(role, (appedCharacterCount.get(role) || 0) + 1)
|
||||
})
|
||||
})
|
||||
|
||||
const result = {}
|
||||
|
||||
roleNames.forEach((roleName) => {
|
||||
result[roleName] = {
|
||||
apps: appedCharacterCount.get(roleName) || 0,
|
||||
picks: pickedCharacterCount.get(roleName) || 0,
|
||||
active: activeCharacters.has(roleName) ? activeCharacters.get(roleName).size : 0
|
||||
}
|
||||
activeCharacters.get(role).add(charId)
|
||||
// Count role application
|
||||
appedCharacterCount.set(role, (appedCharacterCount.get(role) || 0) + 1)
|
||||
})
|
||||
})
|
||||
|
||||
const result = {}
|
||||
|
||||
roleNames.forEach((roleName) => {
|
||||
result[roleName] = {
|
||||
apps: appedCharacterCount.get(roleName) || 0,
|
||||
picks: pickedCharacterCount.get(roleName) || 0,
|
||||
active: activeCharacters.has(roleName) ? activeCharacters.get(roleName).size : 0
|
||||
}
|
||||
})
|
||||
|
||||
res.send(result)
|
||||
await memcache.set('rolestats' + req.body.monthId, result)
|
||||
res.send(result)
|
||||
} else {
|
||||
console.log('cache hit')
|
||||
res.send(cachedResponse)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user