mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-09 06:33:05 -04:00
remove analytics, update stats command
This commit is contained in:
parent
72789e644f
commit
a24cbf5a36
4 changed files with 55 additions and 106 deletions
|
@ -1,15 +1,26 @@
|
||||||
const { codeblock } = require('../../../labscore/utils/markdown')
|
const { highlight, iconPill } = require('../../../labscore/utils/markdown')
|
||||||
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
|
const { createEmbed } = require('../../../labscore/utils/embed')
|
||||||
|
|
||||||
const { paginator } = require('../../../labscore/client');
|
|
||||||
const { editOrReply } = require('../../../labscore/utils/message');
|
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 = {
|
module.exports = {
|
||||||
name: 'stats',
|
name: 'stats',
|
||||||
|
aliases: ['usage', 'uptime'],
|
||||||
metadata: {
|
metadata: {
|
||||||
description: 'Lists command usage statistics.',
|
description: 'Shows statistics about the bot.',
|
||||||
description_short: 'Shows command usage stats.',
|
description_short: 'Shows bot stats.',
|
||||||
examples: ['stats'],
|
examples: ['stats'],
|
||||||
category: 'core',
|
category: 'core',
|
||||||
usage: 'stats'
|
usage: 'stats'
|
||||||
|
@ -17,39 +28,51 @@ module.exports = {
|
||||||
run: async (context) => {
|
run: async (context) => {
|
||||||
context.triggerTyping();
|
context.triggerTyping();
|
||||||
try{
|
try{
|
||||||
let stats = await getCommandStatistics();
|
if(context.manager){
|
||||||
|
const globalStats = await context.manager.broadcastEval((cluster) => {
|
||||||
|
const shardUsage = process.memoryUsage();
|
||||||
|
|
||||||
let pages = [];
|
let stats = {
|
||||||
let ranking = 1;
|
usage: shardUsage.heapTotal + shardUsage.external + shardUsage.arrayBuffers,
|
||||||
|
guilds: 0
|
||||||
for (var i = 0; i < Object.keys(stats).length; i += 20) {
|
}
|
||||||
list = []
|
for (const [shardId, shard] of cluster.shards) {
|
||||||
if(pages.length == 0){list.push(` | Total - ${Object.values(stats).reduce((a, b) => a + b, 0)}`)}
|
stats.guilds += shard.guilds.length;
|
||||||
|
}
|
||||||
Object.keys(stats).forEach(function(elem){
|
return stats
|
||||||
dispnum = `${ranking}`
|
|
||||||
if(`${ranking}`.length == 1){dispnum = ` ${ranking}`}
|
|
||||||
list.push(`${dispnum} | ${elem} - ${stats[elem]}`)
|
|
||||||
ranking++
|
|
||||||
})
|
})
|
||||||
|
|
||||||
pages.push({embeds:[
|
let formatted = {
|
||||||
createEmbed("default", context, {
|
guilds: 0,
|
||||||
description: codeblock("autohotkey", list)
|
usage: 0
|
||||||
})
|
|
||||||
]})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
for (let cid in globalStats) {
|
||||||
const paging = await paginator.createPaginator({
|
const cstats = globalStats[cid];
|
||||||
context,
|
formatted.guilds += cstats.guilds
|
||||||
pages
|
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."))
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(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.")]})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -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()))}`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -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."
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -55,10 +55,6 @@ const { maintower } = require('./logging');
|
||||||
const { icon } = require('./utils/markdown');
|
const { icon } = require('./utils/markdown');
|
||||||
const { editOrReply } = require('./utils/message');
|
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
|
// Delete command responses if the user chooses to delete their trigger or edits the command away
|
||||||
commandClient.on('commandDelete', async ({context, reply}) => {
|
commandClient.on('commandDelete', async ({context, reply}) => {
|
||||||
if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete();
|
if(context.message?.deleted && !reply.deleted || !context.message.content.startsWith(commandPrefix)) reply.delete();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue