From ef694b116f3d6c022a51060fa687a8094fd18d3d Mon Sep 17 00:00:00 2001 From: dbroqua Date: Wed, 25 Dec 2019 13:10:13 +0100 Subject: [PATCH] Added !stats command --- TODO.md | 2 +- helpers/dates.js | 98 ++++++++++++++++++++++++---------------------- helpers/strings.js | 18 ++++++--- index.js | 5 +++ libs/Queries.js | 52 ++++++++++++++++++++++++ libs/Statistics.js | 68 ++++++++++++++++++++++++++++++++ libs/When.js | 36 ++++++----------- 7 files changed, 201 insertions(+), 78 deletions(-) create mode 100644 libs/Queries.js create mode 100644 libs/Statistics.js diff --git a/TODO.md b/TODO.md index 349a40b..192a353 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,7 @@ * ~~!artist {nom de l'artiste}~~ * ~~!song {nom du morceau}~~ -* !stats +* ~~!stats ~~ * ~~!when ~~ * ~~!help {artist/song/stats/when}~~ * !notifications \ No newline at end of file diff --git a/helpers/dates.js b/helpers/dates.js index d5f43b8..41eaba2 100644 --- a/helpers/dates.js +++ b/helpers/dates.js @@ -1,50 +1,56 @@ const moment = require('moment') -module.exports = { - setPeriod: (period) => { - switch (period) { - case 'day': - return { - $gte: moment().startOf('day'), - $lte: moment().endOf('day') - } - case 'week': - return { - $gte: moment().startOf('week'), - $lte: moment().endOf('day') - } - case 'month': - return { - $gte: moment().startOf('month'), - $lte: moment().endOf('day') - } - case 'year': - return { - $gte: moment().startOf('year'), - $lte: moment().endOf('day') - } - case 'lastday': - return { - $gte: moment().subtract('day').startOf('day'), - $lte: moment().subtract('day').endOf('day') - } - case 'lastweek': - return { - $gte: moment().subtract(1, 'week').startOf('week'), - $lte: moment().subtract(1, 'week').endOf('week') - } - case 'lastmonth': - return { - $gte: moment().subtract(1, 'month').startOf('month'), - $lte: moment().subtract(1, 'month').endOf('month') - } - case 'lastyear': - return { - $gte: moment().subtract(1, 'year').startOf('year'), - $lte: moment().subtract(1, 'year').endOf('year') - } - default: - return {} - } +/** + * Fonction permettant de générer le filtre de date sur les commandes !stats et !when + * @param {String} period + */ +function setPeriod (period) { + switch (period) { + case 'day': + return { + $gte: moment().startOf('day'), + $lte: moment().endOf('day') + } + case 'week': + return { + $gte: moment().startOf('week'), + $lte: moment().endOf('day') + } + case 'month': + return { + $gte: moment().startOf('month'), + $lte: moment().endOf('day') + } + case 'year': + return { + $gte: moment().startOf('year'), + $lte: moment().endOf('day') + } + case 'lastday': + return { + $gte: moment().subtract('day').startOf('day'), + $lte: moment().subtract('day').endOf('day') + } + case 'lastweek': + return { + $gte: moment().subtract(1, 'week').startOf('week'), + $lte: moment().subtract(1, 'week').endOf('week') + } + case 'lastmonth': + return { + $gte: moment().subtract(1, 'month').startOf('month'), + $lte: moment().subtract(1, 'month').endOf('month') + } + case 'lastyear': + return { + $gte: moment().subtract(1, 'year').startOf('year'), + $lte: moment().subtract(1, 'year').endOf('year') + } + default: + return {} } } + +module.exports = { + setPeriod: setPeriod +} diff --git a/helpers/strings.js b/helpers/strings.js index 7fa2bc9..8e08c0f 100644 --- a/helpers/strings.js +++ b/helpers/strings.js @@ -1,8 +1,14 @@ -module.exports = { - formatString: function (string) { - if (string !== undefined && string !== null) { - return new RegExp('^' + string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$', 'i') - } - return ';' +/** + * Fonction permettant d'être insensible à la casse + * @param {String} string + */ +function formatString (string) { + if (string !== undefined && string !== null) { + return new RegExp('^' + string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$', 'i') } + return ';' +} + +module.exports = { + formatString: formatString } diff --git a/index.js b/index.js index 8ee99dc..d711f69 100644 --- a/index.js +++ b/index.js @@ -5,12 +5,14 @@ const models = require('./models').models const Help = require('./libs/Help') const ArtistSong = require('./libs/ArtistSong') const When = require('./libs/When') +const Statistics = require('./libs/Statistics') const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications'] // Init des librairies const db = new Db(models) const artistSong = new ArtistSong(models) const when = new When(models) +const stats = new Statistics(models) // Stockage du morceau en cours de lecture let currentSong = { @@ -66,6 +68,9 @@ const actions = (where, message, from) => { case '!when': when.action(botSay, where, exploded) break + case '!stats': + stats.action(botSay, where, exploded) + break default: break } diff --git a/libs/Queries.js b/libs/Queries.js new file mode 100644 index 0000000..7e579bd --- /dev/null +++ b/libs/Queries.js @@ -0,0 +1,52 @@ +const formatString = require('../helpers/strings').formatString + +class Queries { + constructor () { + this.filter = null + this.value = null + } + + /** + * Fonction permettant de générer la query sur artist et title + * @param {Array} line + * @param {Integer} start + */ + setBaseFilter (line, start) { + let value = '' + for (let i = start; i < line.length; i += 1) { + value += ' ' + line[i] + } + value = value.replace(' ', '') + const filter = { + $or: [ + { + artist: formatString(value) + }, + { + title: formatString(value) + } + ] + } + + this.filter = filter + this.value = value + } + + /** + * Getter pour le filter + * @return {Object} + */ + getFilter () { + return this.filter + } + + /** + * Getter pour le texte filtré + * @return {String} + */ + getValue () { + return this.value + } +} + +module.exports = Queries diff --git a/libs/Statistics.js b/libs/Statistics.js new file mode 100644 index 0000000..1a3a6a1 --- /dev/null +++ b/libs/Statistics.js @@ -0,0 +1,68 @@ +const setPeriod = require('../helpers/dates').setPeriod +const Queries = require('./Queries') + +class When extends Queries { + constructor (models) { + super() + this.models = models + } + + /** + * Point d'entrée + * @param {Function} botSay + * @param {String} where + * @param {Array} line + */ + action (botSay, where, line) { + let isOk = true + if (line.length < 4) { + botSay(where, 'Tu as oublié la période et ou l\'artiste/titre !') + return false + } + + this.setBaseFilter(line, 3) + + const period = line[2] + const filter = this.getFilter() + + switch (period) { + case 'day': + case 'week': + case 'month': + case 'year': + case 'lastday': + case 'lastweek': + case 'lastmonth': + case 'lastyear': + filter.createdAt = setPeriod(period) + break + default: + isOk = false + } + + if (!isOk) { + botSay(where, 'Période invalide !') + return false + } + + this.models.Histories + .find(filter) + .sort({ + createdAt: 'desc' + }) + .then(items => { + if (items.length === '0') { + botSay(where, `${this.getValue()} n'a pas était joué pour cette période sur ${process.env.RADIO_ALIAS}`) + return true + } + + botSay(where, `Pour cette période ${this.getValue()} a été entendu ${items.length} fois`) + }) + .catch(err => { + console.error('ERR:', err) + botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...') + }) + } +} + +module.exports = When diff --git a/libs/When.js b/libs/When.js index cedecaf..8eb401e 100644 --- a/libs/When.js +++ b/libs/When.js @@ -1,9 +1,10 @@ const setPeriod = require('../helpers/dates').setPeriod -const formatString = require('../helpers/strings').formatString +const Queries = require('./Queries') const moment = require('moment') -class When { +class When extends Queries { constructor (models) { + super() this.models = models } @@ -19,23 +20,11 @@ class When { return false } - const period = line[2] - let value = '' - for (let i = 3; i < line.length; i += 1) { - value += ' ' + line[i] - } - value = value.replace(' ', '') let isOk = true - const filter = { - $or: [ - { - artist: formatString(value) - }, - { - title: formatString(value) - } - ] - } + this.setBaseFilter(line, 3) + + const period = line[2] + const filter = this.getFilter() switch (period) { case 'day': @@ -65,23 +54,20 @@ class When { .limit(1) .then(item => { if (!item) { - botSay(where, `${value} n'a pas était joué pour cette période sur ${process.env.RADIO_ALIAS}`) + botSay(where, `${this.getValue()} n'a pas était joué pour cette période sur ${process.env.RADIO_ALIAS}`) return true } - if (item.artist.toLowerCase() === value.toLowerCase()) { - botSay(where, `Pour cette période ${value} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')} avec ${item.title}`) + if (item.artist.toLowerCase() === this.getValue().toLowerCase()) { + botSay(where, `Pour cette période ${this.getValue()} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')} avec ${item.title}`) } else { - botSay(where, `Pour cette période ${value} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')}`) + botSay(where, `Pour cette période ${this.getValue()} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')}`) } }) .catch(err => { console.log('ERR:', err) botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...') }) - - console.log('PERIOD:', period, 'SONG:', value) - console.log('FILTER:', filter) } }