Add stats posting

This commit is contained in:
Isaac 2022-09-11 21:11:12 +01:00
parent 50d7e016ed
commit 7eabce1a83
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
6 changed files with 54 additions and 6 deletions

View File

@ -6,11 +6,7 @@ menu question max length cannot be higher than question options
ports <1024 require root ports <1024 require root
- TODO: topic and question answer values not encrypted?
- TODO: post stats
- TODO: settings bundle download
- TODO: update notifications - TODO: update notifications
- TODO: check inline to-dos
creation requires an interaction: creation requires an interaction:

View File

@ -55,6 +55,7 @@
"keyv": "^4.5.0", "keyv": "^4.5.0",
"leeks.js": "^0.2.4", "leeks.js": "^0.2.4",
"leekslazylogger": "^4.1.7", "leekslazylogger": "^4.1.7",
"md5": "^2.3.0",
"ms": "^2.1.3", "ms": "^2.1.3",
"node-dir": "^0.1.17", "node-dir": "^0.1.17",
"node-emoji": "^1.11.0", "node-emoji": "^1.11.0",

View File

@ -63,7 +63,7 @@ const config = YAML.parse(fs.readFileSync('./user/config.yml', 'utf8'));
const log = logger(config); const log = logger(config);
process.on('unhandledRejection', error => { process.on('unhandledRejection', error => {
log.notice(`Discord Tickets v${pkg.version} on Node.js v${process.versions.node} (${process.platform})`); log.notice(`Discord Tickets v${pkg.version} on Node.js ${process.version} (${process.platform})`);
log.notice('An error was not caught'); log.notice('An error was not caught');
if (error instanceof Error) log.warn(`Uncaught ${error.name}`); if (error instanceof Error) log.warn(`Uncaught ${error.name}`);
log.error(error); log.error(error);

1
src/lib/misc.js Normal file
View File

@ -0,0 +1 @@
module.exports.msToMins = ms => Number((ms / 1000 / 60).toFixed(1));

View File

@ -1,5 +1,8 @@
const { Listener } = require('@eartharoid/dbf'); const { Listener } = require('@eartharoid/dbf');
const md5 = require('md5');
const ms = require('ms'); const ms = require('ms');
const { version } = require('../../../package.json');
const { msToMins } = require('../../lib/misc');
module.exports = class extends Listener { module.exports = class extends Listener {
constructor(client, options) { constructor(client, options) {
@ -114,5 +117,51 @@ module.exports = class extends Listener {
setPresence(); setPresence();
if (client.config.presence.activities.length > 1) setInterval(() => setPresence(), client.config.presence.interval * 1000); if (client.config.presence.activities.length > 1) setInterval(() => setPresence(), client.config.presence.interval * 1000);
if (client.config.stats) {
const send = async () => {
const tickets = await client.prisma.ticket.findMany({
select: {
createdAt: true,
firstResponseAt: true,
},
});
const closedTickets = tickets.filter(t => t.closedAt);
const users = await client.prisma.user.findMany({ select: { messageCount: true } });
const stats = {
activated_users: users.length,
arch: process.arch,
avg_resolution_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / closedTickets.length),
avg_response_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / closedTickets.length),
categories: await client.prisma.category.count(),
database: process.env.DB_PROVIDER,
guilds: client.guilds.cache.size,
id: md5(client.user.id),
members: client.guilds.cache.reduce((t, g) => t + g.memberCount, 0),
messages: users.reduce((total, user) => total + user.messageCount, 0), // don't count archivedMessage table rows, they can be deleted,
node: process.version,
os: process.platform,
tags: await client.prisma.tag.count(),
tickets: tickets.length,
version,
};
try {
const res = await fetch('https://stats.discordtickets.app/api/v3/houston', {
body: JSON.stringify(stats),
headers: { 'content-type': 'application/json' },
method: 'POST',
});
if (!res.ok) throw res;
client.log.success('Posted client stats');
client.log.verbose(stats);
client.log.debug(res);
} catch (error) {
client.log.error('An error occurred whilst posting stats', stats, error);
}
};
send();
setInterval(() => send(), ms('12h'));
}
} }
}; };

View File

@ -32,3 +32,4 @@ presence:
status: online # online|idle|invisible|dnd status: online # online|idle|invisible|dnd
overrides: overrides:
disableArchives: false disableArchives: false
stats: true