Skip to content

Commit

Permalink
Add full analytics support. Needs further testing and stabilization.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Apr 14, 2019
1 parent 05986e5 commit 2d197e2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/bot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default class CommandParser {
this.evaluatedMessages = []
this.onMessage = this.onMessage.bind(this)
this.onMessageUpdate = this.onMessageUpdate.bind(this)
setInterval(() => this.sendAnalytics(), 30000)
}

registerCommand = (command: IveBotCommand) => { // eslint-disable-line no-undef
Expand Down Expand Up @@ -184,6 +185,34 @@ export default class CommandParser {
this.analytics[this.analytics.indexOf(commandInfo)].averageExecTime = averageExecTime
}

async sendAnalytics () {
const analytics = this.db.collection('analytics')
// Iterate over every command we have information stored locally.
for (let index in this.analytics) {
const command = this.analytics[index]
// Get the data for the selected command.
const statistics = await analytics.findOne({ name: command.name })
// If the command was not stored, we store our analytics directly.
if (!statistics) analytics.insertOne(command)
// Else, we update existing command data in the database.
else {
// Calculate the average execution time and update the database.
const averageExecTime = statistics.averageExecTime.map((i: number, index: number) => (
(
(i * statistics.totalUse) + (command.averageExecTime[index] * command.totalUse)
) / (statistics.totalUse + command.totalUse)
))
analytics.updateOne({ name: command.name }, {
$inc: { totalUse: command.totalUse },
$set: { averageExecTime }
})
}
// Clear analytics for the specific index.
this.analytics[index].totalUse = 0
this.analytics[index].averageExecTime = [0, 0]
}
}

async onMessage (message: Message) {
if (message.content && !message.content.startsWith('/')) {
botCallback(message, this.client, this.tempDB, this.db)
Expand Down

0 comments on commit 2d197e2

Please sign in to comment.