remove analytics, update stats command

This commit is contained in:
derpystuff 2023-01-11 17:04:51 +01:00
parent 72789e644f
commit a24cbf5a36
4 changed files with 55 additions and 106 deletions

View file

@ -1,15 +1,26 @@
const { codeblock } = require('../../../labscore/utils/markdown')
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { highlight, iconPill } = require('../../../labscore/utils/markdown')
const { createEmbed } = require('../../../labscore/utils/embed')
const { paginator } = require('../../../labscore/client');
const { editOrReply } = require('../../../labscore/utils/message');
const { getCommandStatistics } = require('../../../labscore/analytics');
function format(seconds){
function pad(s){
return (s < 10 ? '0' : '') + s;
}
var hours = Math.floor(seconds / (60*60));
var minutes = Math.floor(seconds % (60*60) / 60);
var seconds = Math.floor(seconds % 60);
if(hours == 0) return pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds'
return pad(hours) + ' Hours, ' + pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds'
}
module.exports = {
name: 'stats',
aliases: ['usage', 'uptime'],
metadata: {
description: 'Lists command usage statistics.',
description_short: 'Shows command usage stats.',
description: 'Shows statistics about the bot.',
description_short: 'Shows bot stats.',
examples: ['stats'],
category: 'core',
usage: 'stats'
@ -17,39 +28,51 @@ module.exports = {
run: async (context) => {
context.triggerTyping();
try{
let stats = await getCommandStatistics();
let pages = [];
let ranking = 1;
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 = `${ranking}`
if(`${ranking}`.length == 1){dispnum = ` ${ranking}`}
list.push(`${dispnum} | ${elem} - ${stats[elem]}`)
ranking++
if(context.manager){
const globalStats = await context.manager.broadcastEval((cluster) => {
const shardUsage = process.memoryUsage();
let stats = {
usage: shardUsage.heapTotal + shardUsage.external + shardUsage.arrayBuffers,
guilds: 0
}
for (const [shardId, shard] of cluster.shards) {
stats.guilds += shard.guilds.length;
}
return stats
})
pages.push({embeds:[
createEmbed("default", context, {
description: codeblock("autohotkey", list)
})
]})
let formatted = {
guilds: 0,
usage: 0
}
for (let cid in globalStats) {
const cstats = globalStats[cid];
formatted.guilds += cstats.guilds
formatted.usage += cstats.usage
}
const display = [
`${iconPill("house", "Servers ")} ${highlight(` ${formatted.guilds} `)}`,
`${iconPill("robot", "Shard ")} ${highlight(` ${context.shardId + 1}/${context.manager.cluster.shardCount} `)}`,
`${iconPill("connection", "Memory Usage")} ${highlight(` ${Math.round(formatted.usage / 1024 / 1024)}MB `)}`,
`${iconPill("timer", "Uptime ")} ${highlight(` ${format(process.uptime())} `)}`
]
return editOrReply(context, createEmbed("default", context, {
//description: codeblock("autohotkey", [`Guilds: ${formatted.guilds}`, `Shard: ${context.shardId + 1}/${context.manager.cluster.shardCount}`, `Memory Usage: ${Math.round(formatted.usage / 1024 / 1024)}MB`])
description: display.join('\n')
}))
} else {
return editOrReply(context, createEmbed("error", context, "Bot is not running in cluster mode."))
}
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.")]})
return editOrReply(context, {embeds: [createEmbed("error", context, "Unable to fetch bot statistics.")]})
}
}
};

View file

@ -1,36 +0,0 @@
const { editOrReply } = require('../../../labscore/utils/message')
const { highlight } = require('../../../labscore/utils/markdown')
function format(seconds){
function pad(s){
return (s < 10 ? '0' : '') + s;
}
var hours = Math.floor(seconds / (60*60));
var minutes = Math.floor(seconds % (60*60) / 60);
var seconds = Math.floor(seconds % 60);
return pad(hours) + ' Hours, ' + pad(minutes) + ' Minutes, ' + pad(seconds) + ' Seconds'
}
module.exports = {
name: "uptime",
metadata: {
description: 'Displays the bots uptime.',
description_short: 'Bot uptime',
examples: ['uptime'],
category: 'dev',
usage: 'uptime'
},
onBefore: context => context.user.isClientOwner,
onCancel: context =>
context.reply(
`${context.user.mention}, you are lacking the permission \`BOT_OWNER\`.`
),
run: async (context, args) => {
await context.triggerTyping();
return await editOrReply(context, {
"content": `up for ${highlight(format(process.uptime()))}`
})
}
};

View file

@ -1,34 +0,0 @@
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."
}
}

View file

@ -55,10 +55,6 @@ 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();