irc-radio-bot/libs/Statistics.js

108 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-12-25 13:10:13 +01:00
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
2020-01-07 16:14:50 +01:00
if (line.length < 3) {
botSay(where, 'Tu as oublié la période !')
2019-12-25 13:10:13 +01:00
return false
}
2020-01-09 11:24:40 +01:00
this.setBaseFilter(line, 3, true)
2019-12-25 13:10:13 +01:00
const period = line[2]
const filter = this.getFilter()
switch (period) {
case 'day':
case 'week':
case 'month':
case 'year':
case 'lastday':
2020-01-13 14:26:18 +01:00
case 'yesterday':
2019-12-25 13:10:13 +01:00
case 'lastweek':
case 'lastmonth':
case 'lastyear':
2019-12-25 13:47:42 +01:00
this.setPeriod(period)
filter.createdAt = this.period
2019-12-25 13:10:13 +01:00
break
default:
isOk = false
}
if (!isOk) {
botSay(where, 'Période invalide !')
return false
}
2020-01-07 16:14:50 +01:00
if (line.length === 3) {
this.getCountGroups(botSay, where)
} else {
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é...')
})
}
}
getCountGroups (botSay, where) {
const artists = []
const songs = []
2019-12-25 13:10:13 +01:00
this.models.Histories
2020-01-07 16:29:17 +01:00
.find({
2020-01-07 16:33:03 +01:00
radio: process.env.RADIO_ALIAS,
2020-01-07 16:29:17 +01:00
createdAt: this.filter.createdAt
})
2019-12-25 13:10:13 +01:00
.sort({
2020-01-07 16:14:50 +01:00
artist: 'desc'
2019-12-25 13:10:13 +01:00
})
.then(items => {
if (items.length === '0') {
2020-01-07 16:14:50 +01:00
botSay(where, 'Je n\'ai absolu rien entendu pendant cette période !')
2019-12-25 13:10:13 +01:00
return true
}
2020-01-07 16:26:09 +01:00
for (let i = 0; i < items.length; i += 1) {
2020-01-07 16:14:50 +01:00
if (artists.indexOf(items[i].artist) === -1) {
artists.push(items[i].artist)
}
if (songs.indexOf(items[i].title) === -1) {
songs.push(items[i].title)
}
}
2020-01-07 16:26:09 +01:00
botSay(where, `Pour cette période tu as pu entendre ${artists.length} artistes différents pour un total de ${songs.length} morceaux différents !`)
2019-12-25 13:10:13 +01:00
})
.catch(err => {
console.error('ERR:', err)
botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...')
})
}
}
module.exports = When