From bd9ec8f3436ead0c32ca0af255a2336778821659 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Mon, 20 Jan 2020 21:35:26 +0100 Subject: [PATCH] feat: create ace command to edit detailed matches --- server/app/Commands/EditDetailedMatch.js | 84 +++++++++++++++++++++ server/app/Transformers/MatchTransformer.js | 2 + server/start/app.js | 1 + 3 files changed, 87 insertions(+) create mode 100644 server/app/Commands/EditDetailedMatch.js diff --git a/server/app/Commands/EditDetailedMatch.js b/server/app/Commands/EditDetailedMatch.js new file mode 100644 index 0000000..1c8c717 --- /dev/null +++ b/server/app/Commands/EditDetailedMatch.js @@ -0,0 +1,84 @@ +'use strict' + +const { Command } = require('@adonisjs/ace') +const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') +const Database = use('Database') +const Jax = use('Jax') +const DetailedMatch = use('App/Models/DetailedMatch') +const Queue = use('Bee/Queue') + +class EditDetailedMatch extends Command { + static get signature() { + return ` + edit:detailed:match + { concurrent?=10 : Number of concurrent jobs } + ` + } + + static get description() { + return 'Edit DetailedMatches in the db with the new Transformer version' + } + + /** + * Create edit-detailed-matches Queue + */ + createQueue(concurrent) { + Queue.get('edit-detailed-matches').process(concurrent, async (job) => { + // Get stats from Riot API + const matchRiot = await Jax.Match.get(job.data.gameId, job.data.region) + // Transform raw matches data + const transformedMatch = await DetailedMatchTransformer.transform(matchRiot) + + // Update match in DB + await DetailedMatch.where('_id', job.data._id).update(transformedMatch) + + return + }) + + /** + * Job (edit detailed match) finished with success + */ + Queue.get('edit-detailed-matches').on('succeeded', (job, result) => { + console.log(`Job ${job.id} succeeded`) + }) + } + + async handle(args, options) { + console.time('EditDetailedMatches') + + this.createQueue(args.concurrent) + + // All detailed matches from the db + const matches = await DetailedMatch.all() + const matchesArray = matches.toJSON() + + // Create jobs + const jobs = [] + for (const match of matchesArray) { + const matchInfos = { + _id: match._id, + gameId: match.gameId, + region: match.region, + } + const job = await Queue + .get('edit-detailed-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('EditDetailedMatches') + this.success(`${this.icon('success')} Edit ${matchesArray.length} DetailedMatches completed`) + } +} + +module.exports = EditDetailedMatch diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index b8782ac..15a14f9 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -138,6 +138,8 @@ class MatchTransformer { return { name, + accountId: identity.player.currentAccountId, + summonerId: identity.player.summonerId, champion, role, primaryRune, diff --git a/server/start/app.js b/server/start/app.js index d5c3fe5..5a23729 100644 --- a/server/start/app.js +++ b/server/start/app.js @@ -60,6 +60,7 @@ const aliases = {} */ const commands = [ 'App/Commands/DeleteMatch', + 'App/Commands/EditDetailedMatch', 'App/Commands/EditMatch', ]