feat: add ace command to edit all matches with the updated transformer

This commit is contained in:
Valentin Kaelin 2019-12-15 20:52:04 +01:00
parent 4989306ba8
commit 1a87bea46d
3 changed files with 92 additions and 1 deletions

View file

@ -0,0 +1,88 @@
'use strict'
const { Command } = require('@adonisjs/ace')
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
const Database = use('Database')
const Jax = use('Jax')
const Match = use('App/Models/Match')
const Queue = use('Bee/Queue')
class EditMatch extends Command {
static get signature() {
return 'edit:match'
}
static get description() {
return 'Edit matches in the db with the new Transformer version'
}
/**
* Create edit-matches Queue
*/
async createQueue() {
Queue.get('edit-matches').process(2, async (job) => {
// Get stats from Riot API
const matchRiot = await Jax.Match.get(job.data.gameId, job.data.region)
const account = {
accountId: (matchRiot.participantIdentities.find(s => s.player.summonerName === job.data.name)).player.currentAccountId,
puuid: job.data.summoner_puuid
}
// Transform raw matches data
const transformedMatch = await BasicMatchTransformer.transform(matchRiot, { account })
// Update match in DB
await Match.where('_id', job.data._id).update(transformedMatch)
return
})
/**
* Job (edit match) finished with success
*/
Queue.get('edit-matches').on('succeeded', (job, result) => {
console.log(`Job ${job.id} succeeded`)
})
}
async handle(args, options) {
console.time('EditMatches')
this.createQueue()
// All matches from the db
const matches = await Match.all()
const matchesArray = matches.toJSON()
// Create jobs
const jobs = []
for (const match of matchesArray) {
const matchInfos = {
_id: match._id,
gameId: match.gameId,
name: match.name,
region: match.region,
summoner_puuid: match.summoner_puuid
}
const job = await Queue
.get('edit-matches')
.createJob(matchInfos)
.save()
jobs.push(job)
}
// Wait that all jobs are done
const finalResult = await new Promise((resolve, reject) => {
const lastJob = jobs[jobs.length - 1]
lastJob.on('succeeded', result => {
resolve(`FINAL RESULT for job ${lastJob.id}`)
})
})
Database.close()
console.timeEnd('EditMatches')
this.success(`${this.icon('success')} Edit ${matchesArray.length} Matches completed`)
}
}
module.exports = EditMatch

View file

@ -19,10 +19,12 @@ class BasicMatchTransformer extends MatchTransformer {
if (Array.isArray(matches)) { if (Array.isArray(matches)) {
matches.forEach((match, index) => { matches.forEach((match, index) => {
matches[index] = this.transformOneMatch(match, account) matches[index] = this.transformOneMatch(match, account)
}); })
} else { } else {
matches = this.transformOneMatch(matches, account) matches = this.transformOneMatch(matches, account)
} }
return matches
} }
/** /**

View file

@ -60,6 +60,7 @@ const aliases = {}
*/ */
const commands = [ const commands = [
'App/Commands/DeleteMatch', 'App/Commands/DeleteMatch',
'App/Commands/EditMatch',
] ]
module.exports = { providers, aceProviders, aliases, commands } module.exports = { providers, aceProviders, aliases, commands }