refactor(api): cleaner way to filter next matchIds to fetch

This commit is contained in:
Valentin Kaelin 2022-02-01 00:13:44 +01:00
parent f6cd5e19a3
commit 87972e4f4d
4 changed files with 29 additions and 34 deletions

View file

@ -1,6 +1,7 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Match from 'App/Models/Match' import Match from 'App/Models/Match'
import MatchPlayerRankParser from 'App/Parsers/MatchPlayerRankParser' import MatchPlayerRankParser from 'App/Parsers/MatchPlayerRankParser'
import MatchRepository from 'App/Repositories/MatchRepository'
import DetailedMatchSerializer from 'App/Serializers/DetailedMatchSerializer' import DetailedMatchSerializer from 'App/Serializers/DetailedMatchSerializer'
import MatchPlayerRankSerializer from 'App/Serializers/MatchPlayerRankSerializer' import MatchPlayerRankSerializer from 'App/Serializers/MatchPlayerRankSerializer'
import { SerializedMatch } from 'App/Serializers/SerializedTypes' import { SerializedMatch } from 'App/Serializers/SerializedTypes'
@ -16,7 +17,7 @@ export default class MatchesController {
*/ */
public async index({ request, response }: HttpContextContract) { public async index({ request, response }: HttpContextContract) {
const { puuid, region, lastMatchId, season } = await request.validate(MatchesIndexValidator) const { puuid, region, lastMatchId, season } = await request.validate(MatchesIndexValidator)
const matchIds = await MatchService.getMatchIdsToFetch(lastMatchId, puuid, season) const matchIds = await MatchRepository.getNextMatchIds({ lastMatchId, puuid, season })
let matches: SerializedMatch[] = [] let matches: SerializedMatch[] = []
if (matchIds.length > 0) { if (matchIds.length > 0) {

View file

@ -89,19 +89,7 @@ export default class SummonersController {
const { puuid, region, season } = await request.validate(SummonerOverviewValidator) const { puuid, region, season } = await request.validate(SummonerOverviewValidator)
const finalJSON: any = {} const finalJSON: any = {}
// Summoner in DB const matchIds = await MatchRepository.getNextMatchIds({ puuid, season })
const summonerDB = await Summoner.firstOrCreate({ puuid: puuid })
// MATCHES BASIC
const matchListQuery = summonerDB.related('matchList').query().select('matchId')
if (season) {
matchListQuery
.join('matches', 'summoner_matchlist.match_id', 'matches.id')
.where('matches.season', season)
}
const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(10)
const matchIds = matchlist.map((m) => m.matchId)
finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, puuid) finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, puuid)
console.time('STATS') console.time('STATS')

View file

@ -1,10 +1,12 @@
import Database from '@ioc:Adonis/Lucid/Database' import Database from '@ioc:Adonis/Lucid/Database'
import SummonerMatchlist from 'App/Models/SummonerMatchlist'
export interface SelectFilters { export interface SelectFilters {
puuid: string puuid: string
season?: number season?: number
limit?: number limit?: number
queue?: number queue?: number
lastMatchId?: string
} }
class MatchRepository { class MatchRepository {
@ -26,6 +28,30 @@ class MatchRepository {
return query return query
} }
/**
* Get the list of matchIds from the next matches to fetch for a specific Summoner
*/
public async getNextMatchIds(filters: SelectFilters) {
const matchListQuery = SummonerMatchlist.query()
.select('matchId')
.where('summoner_puuid', filters.puuid)
if (filters.lastMatchId) {
matchListQuery.andWhere('match_id', '<', filters.lastMatchId)
}
if (filters.season) {
matchListQuery
.join('matches', 'summoner_matchlist.match_id', 'matches.id')
.where('matches.season', filters.season)
}
const limit = filters.limit ?? 10
const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(limit)
return matchlist.map((m) => m.matchId)
}
public async gamemodes(puuid: string) { public async gamemodes(puuid: string) {
const query = ` const query = `
SELECT DISTINCT SELECT DISTINCT

View file

@ -8,7 +8,6 @@ import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer'
import { SerializedMatch } from 'App/Serializers/SerializedTypes' import { SerializedMatch } from 'App/Serializers/SerializedTypes'
import Match from 'App/Models/Match' import Match from 'App/Models/Match'
import { notEmpty, tutorialQueues } from 'App/helpers' import { notEmpty, tutorialQueues } from 'App/helpers'
import SummonerMatchlist from 'App/Models/SummonerMatchlist'
class MatchService { class MatchService {
/** /**
@ -83,25 +82,6 @@ class MatchService {
return currentMatchListIds.reverse() return currentMatchListIds.reverse()
} }
/**
* Get the list of matchIds from the next matches to fetch for a specific Summoner
*/
public async getMatchIdsToFetch(lastMatchId: string, puuid: string, season?: number) {
const matchListQuery = SummonerMatchlist.query()
.select('matchId')
.where('summoner_puuid', puuid)
.andWhere('match_id', '<', lastMatchId)
if (season) {
matchListQuery
.join('matches', 'summoner_matchlist.match_id', 'matches.id')
.where('matches.season', season)
}
const matchlist = await matchListQuery.orderBy('matchId', 'desc').limit(10)
return matchlist.map((m) => m.matchId)
}
/** /**
* Fetch list of matches for a specific Summoner * Fetch list of matches for a specific Summoner
*/ */