perf(stats): don't duplicate work

This commit is contained in:
Isaac 2025-01-30 03:06:36 +00:00
parent 118b685f8e
commit 08300504b1
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33

View File

@ -1,3 +1,5 @@
/* eslint-disable no-underscore-dangle */
const { version } = require('../../package.json'); const { version } = require('../../package.json');
const { const {
md5, md5,
@ -26,46 +28,51 @@ module.exports.sendToHouston = async client => {
}, },
}, },
}); });
const users = await client.prisma.user.findMany({ select: { messageCount: true } }); const users = (await client.prisma.user.aggregate({
_count: true,
_sum: { messageCount: true },
}));
const messages = users._sum.messageCount ?? 0;
const stats = { const stats = {
activated_users: users.length, activated_users: users._count,
arch: process.arch, arch: process.arch,
database: process.env.DB_PROVIDER, database: process.env.DB_PROVIDER,
guilds: guilds.filter(guild => { guilds: guilds
if (!client.guilds.cache.has(guild.id)) { .filter(guild => client.guilds.cache.has(guild.id))
client.log.warn('Guild %s is in the database but is not cached and might not exist. It will be excluded from the stats report.', guild.id); .map(guild => {
return false; const closedTickets = guild.tickets.filter(t => t.firstResponseAt && t.closedAt);
} return {
return true; avg_resolution_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / closedTickets.length),
}).map(guild => { avg_response_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / closedTickets.length),
const closedTickets = guild.tickets.filter(t => t.firstResponseAt && t.closedAt); categories: guild.categories.length,
return { features: {
avg_resolution_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / closedTickets.length), auto_close: msToMins(guild.autoClose),
avg_response_time: msToMins(closedTickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / closedTickets.length), claiming: guild.categories.filter(c => c.claiming).length,
categories: guild.categories.length, feedback: guild.categories.filter(c => c.enableFeedback).length,
features: { logs: !!guild.logChannel,
auto_close: msToMins(guild.autoClose), questions: guild.categories.filter(c => c._count.questions).length,
claiming: guild.categories.filter(c => c.claiming).length, tags: guild.tags.length,
feedback: guild.categories.filter(c => c.enableFeedback).length, tags_regex: guild.tags.filter(t => t.regex).length,
logs: !!guild.logChannel, topic: guild.categories.filter(c => c.requireTopic).length,
// eslint-disable-next-line no-underscore-dangle },
questions: guild.categories.filter(c => c._count.questions).length, id: md5(guild.id),
tags: guild.tags.length, locale: guild.locale,
tags_regex: guild.tags.filter(t => t.regex).length, members: client.guilds.cache.get(guild.id).memberCount,
topic: guild.categories.filter(c => c.requireTopic).length, messages, // * global not guild, don't count archivedMessage table rows, they can be deleted
}, tickets: guild.tickets.length,
id: md5(guild.id), };
locale: guild.locale, }),
members: client.guilds.cache.get(guild.id).memberCount,
messages: users.reduce((total, user) => total + user.messageCount, 0), // global not guild, don't count archivedMessage table rows, they can be deleted
tickets: guild.tickets.length,
};
}),
id: md5(client.user.id), id: md5(client.user.id),
node: process.version, node: process.version,
os: process.platform, os: process.platform,
version, version,
}; };
const delta = guilds.length - stats.guilds.length;
if (delta !== 0) {
client.log.warn('%d guilds are not cached and were excluded from the stats report', delta);
}
try { try {
client.log.verbose('Reporting to Houston:', stats); client.log.verbose('Reporting to Houston:', stats);
const res = await fetch('https://stats.discordtickets.app/api/v4/houston', { const res = await fetch('https://stats.discordtickets.app/api/v4/houston', {