stat stuff

This commit is contained in:
Lio Young 2021-05-23 05:59:25 +02:00
parent aece129f04
commit 67bd79fcd5
No known key found for this signature in database
GPG Key ID: 789795A11879E169
3 changed files with 40 additions and 2 deletions

View File

@ -3,6 +3,19 @@ import { Context, Usage } from '../../utils/types';
import lingua from '../../utils/lingua';
import { MessageEmbed } from 'discord.js';
import CommandUsage from '../../utils/command.usage';
import { commitHash } from "../../utils/git"
function uptime(ms: number) {
function pad(s: number) {
return (s < 10 ? '0' : '') + s;
}
var days = Math.floor(ms / (24 * ((60 * 60))));
var hours = Math.floor(ms / (60 * 60));
var minutes = Math.floor((ms % (60 * 60)) / 60);
var seconds = Math.floor(ms % 60);
return `${pad(days)}d ${pad(hours)}h ${pad(minutes)}m ${pad(seconds)}s`
// return pad(hours) + 'h ' + pad(minutes) + 'm ' + pad(seconds) + 's';
}
export = class Stats extends Command {
constructor() {
@ -15,8 +28,24 @@ export = class Stats extends Command {
}
async command(ctx: Context) {
let Stats = await CommandUsage(ctx.client.commands)
console.log(Stats)
// @ts-ignore
const guilds = (await ctx.client.shard?.fetchClientValues(`guilds.cache.size`)).reduce((a, b) => a + b, 0)
const shards = ctx.client.shard?.count
const users = (await ctx.client.shard?.fetchClientValues(`users.cache.size`))?.reduce((a, b) => a + b, 0)
let MiscValues = [
`Uptime: **${uptime(process.uptime())}**`,
`Guilds: **${guilds}**`,
`Shards: **${shards}**`,
`Users: **${users}**`
]
let embed = new MessageEmbed().setColor(ctx.config.variables.color).setFooter(`${ctx.config.variables.name} v${ctx.config.pkg.version} [${commitHash}]`, ctx.config.variables.avatar)
.setTitle(`Statistics`)
.setDescription(`Visit [our System Dashboard](https://system.thaldr.in) for more in-depth stats`)
.addField(`Misc`, MiscValues.map((value) => `${value}`))
ctx.channel.send(embed)
}
}

3
src/utils/git.ts Normal file
View File

@ -0,0 +1,3 @@
import { execSync } from 'child_process';
export const commitHash = execSync('git rev-parse HEAD', { encoding: 'utf8' }).slice(0, 8);

View File

@ -9,6 +9,7 @@ export default class Prometheus {
public messagesSeen!: prom.Counter<string>
public guildCount!: prom.Gauge<string>
public totalGuilds!: prom.Gauge<string>
public uptime!: prom.Gauge<string>
// public commmandsRan: prom.Counter<string>
#server!: ReturnType<typeof createServer>
@ -32,6 +33,10 @@ export default class Prometheus {
name: "thaldrin_guilds_total",
help: "Total Number of Guilds Thaldrin is in"
})
this.uptime = new prom.Gauge({
name: "thaldrin_uptime",
help: "Thaldrin's Uptime"
})
this.#server = createServer(this.onRequest.bind(this));
this.#server.once('listening', () => Logger.info({ type: 'event:prometheusStart', message: `Prometheus: Listening at http://localhost:${vars.prometheus.port}` }));
@ -39,6 +44,7 @@ export default class Prometheus {
this.#server.listen(vars.prometheus.port);
}
private async onRequest(req: IncomingMessage, res: ServerResponse) {
this.uptime.set(process.uptime())
if (req.url! === '/metrics') {
res.writeHead(200, { 'Content-Type': prom.register.contentType });
res.write(await prom.register.metrics());