From 8bf8516f840a1956d24f2cbc4cf60b1b32cf64b3 Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Tue, 10 Jan 2023 20:03:30 +0100 Subject: [PATCH] command stats --- commands/message/core/stats.js | 54 ++++++++++++++++++++++++++++++++++ labscore/analytics.js | 34 +++++++++++++++++++++ labscore/client.js | 4 +++ 3 files changed, 92 insertions(+) create mode 100644 commands/message/core/stats.js create mode 100644 labscore/analytics.js diff --git a/commands/message/core/stats.js b/commands/message/core/stats.js new file mode 100644 index 0000000..4cb9ce0 --- /dev/null +++ b/commands/message/core/stats.js @@ -0,0 +1,54 @@ +const { codeblock } = require('../../../labscore/utils/markdown') +const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') + +const { paginator } = require('../../../labscore/client'); +const { editOrReply } = require('../../../labscore/utils/message'); +const { getCommandStatistics } = require('../../../labscore/analytics'); + +module.exports = { + name: 'stats', + metadata: { + description: 'Lists command usage statistics.', + description_short: 'Shows command usage stats.', + examples: ['stats'], + category: 'core', + usage: 'stats' + }, + run: async (context) => { + try{ + let stats = await getCommandStatistics(); + + let pages = []; + num = 0; + + for (var i = 0; i < Object.keys(stats).length; i += 20) { + list = [] + if(pages.length == 0){list.push(` | Total - ${Object.values(stats).reduce((a, b) => a + b, 0)}`)} + + Object.keys(stats).forEach(function(elem){ + dispnum = `${num}` + if(`${num}`.length == 1){dispnum = ` ${num}`} + list.push(`${dispnum} | ${elem} - ${stats[elem]}`) + num++ + }) + + pages.push({embeds:[ + createEmbed("default", context, { + description: codeblock("autohotkey", list) + }) + ]}) + } + + pages = formatPaginationEmbeds(pages) + const paging = await paginator.createPaginator({ + context, + pages + }); + + return; + }catch(e){ + console.log(e) + return editOrReply(context, {embeds: [createEmbed("error", context, "Unable to fetch command statistics.")]}) + } + } +}; \ No newline at end of file diff --git a/labscore/analytics.js b/labscore/analytics.js new file mode 100644 index 0000000..d9a8b67 --- /dev/null +++ b/labscore/analytics.js @@ -0,0 +1,34 @@ +const superagent = require('superagent') + +const ANALYTICS_BASE_URL = process.env.ANALYTICS_SERVER + +let analyticsClient = "prod" +if(process.env.ANALYTICS_OVERRIDE) analyticsClient = process.env.ANALYTICS_OVERRIDE + +module.exports.track = async function (command){ + try{ + let res = await superagent.get(ANALYTICS_BASE_URL + '/stats/collect/' + command) + .set({ + "Authorization": process.env.ANALYTICS_KEY, + "x-nx-client": analyticsClient + }) + }catch(e){ + console.log('Analytics report failed.') + console.log(e) + } +} + +module.exports.getCommandStatistics = async function (time){ + if(!time) time = analyticsClient + '_' + new Date().getFullYear() + '/' + new Date().getMonth() + try{ + let res = await superagent.get(ANALYTICS_BASE_URL + '/stats/' + encodeURIComponent(time)) + .set({ + "Authorization": process.env.ANALYTICS_KEY, + "x-nx-client": analyticsClient + }) + return res.body.collection + }catch(e){ + console.log(e) + throw "Analytics request failed." + } +} \ No newline at end of file diff --git a/labscore/client.js b/labscore/client.js index 5233754..a9bbdc0 100644 --- a/labscore/client.js +++ b/labscore/client.js @@ -55,6 +55,10 @@ const { maintower } = require('./logging'); const { icon } = require('./utils/markdown'); const { editOrReply } = require('./utils/message'); +const { track } = require('./analytics') +// analytics +commandClient.on('commandRan', async ({context, command}) => track(command.name)) + // Delete command responses if the user chooses to delete their trigger or edits the command away commandClient.on('commandDelete', async ({context, reply}) => { if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete();