Added !stats command

This commit is contained in:
dbroqua 2019-12-25 13:10:13 +01:00
parent 08b2d46a43
commit ef694b116f
7 changed files with 201 additions and 78 deletions

View File

@ -4,7 +4,7 @@
* ~~!artist <add/del> {nom de l'artiste}~~
* ~~!song <add/del> {nom du morceau}~~
* !stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>
* ~~!stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~
* ~~!when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~
* ~~!help {artist/song/stats/when}~~
* !notifications <on/off/state>

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

52
libs/Queries.js Normal file
View File

@ -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

68
libs/Statistics.js Normal file
View File

@ -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

View File

@ -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)
}
}