From ced14ce36a37a7ecb9fd66e93650f3579955516a Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 10 Feb 2023 23:32:44 +0000 Subject: [PATCH] fix: response & resolution time stats --- src/lib/tickets/manager.js | 14 +++++++++++--- src/listeners/client/ready.js | 3 ++- .../api/admin/guilds/[guild]/categories/index.js | 2 +- src/routes/api/admin/guilds/[guild]/index.js | 5 +++-- src/routes/api/client.js | 3 ++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index 6b2a74b..910839c 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -406,9 +406,17 @@ module.exports = class TicketManager { const statsCacheKey = `cache/category-stats/${categoryId}`; let stats = await this.client.keyv.get(statsCacheKey); if (!stats) { - const { tickets } = await this.client.prisma.category.findUnique({ - select: { tickets: { where: { open: false } } }, - where: { id: categoryId }, + const tickets = await this.client.prisma.ticket.findMany({ + select: { + closedAt: true, + createdAt: true, + firstResponseAt: true, + }, + where: { + categoryId: category.id, + firstResponseAt: { not: null }, + open: false, + }, }); stats = { avgResolutionTime: ms(tickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / tickets.length), diff --git a/src/listeners/client/ready.js b/src/listeners/client/ready.js index f19baba..97b0a97 100644 --- a/src/listeners/client/ready.js +++ b/src/listeners/client/ready.js @@ -33,11 +33,12 @@ module.exports = class extends Listener { if (!cached) { const tickets = await client.prisma.ticket.findMany({ select: { + closedAt: true, createdAt: true, firstResponseAt: true, }, }); - const closedTickets = tickets.filter(t => t.closedAt); + const closedTickets = tickets.filter(t => t.firstResponseAt && t.closedAt); cached = { avgResolutionTime: ms(closedTickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / closedTickets.length), avgResponseTime: ms(closedTickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / closedTickets.length), diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js index e7d1c38..318914f 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/index.js @@ -29,7 +29,7 @@ module.exports.get = fastify => ({ where: { id: req.params.guild }, }); categories = categories.map(c => { - const closedTickets = c.tickets.filter(t => t.closedAt); + const closedTickets = c.tickets.filter(t => t.firstResponseAt && t.closedAt); c = { ...c, stats: { diff --git a/src/routes/api/admin/guilds/[guild]/index.js b/src/routes/api/admin/guilds/[guild]/index.js index c3b22a0..913d6ce 100644 --- a/src/routes/api/admin/guilds/[guild]/index.js +++ b/src/routes/api/admin/guilds/[guild]/index.js @@ -23,12 +23,13 @@ module.exports.get = fastify => ({ }); const tickets = await client.prisma.ticket.findMany({ select: { + closedAt: true, createdAt: true, firstResponseAt: true, }, where: { guildId: id }, }); - const closedTickets = tickets.filter(t => t.closedAt); + const closedTickets = tickets.filter(t => t.firstResponseAt && t.closedAt); cached = { createdAt: settings.createdAt, id: guild.id, @@ -46,7 +47,7 @@ module.exports.get = fastify => ({ tickets: tickets.length, }, }; - await client.keyv.set(cacheKey, cached, ms('5m')); + // await client.keyv.set(cacheKey, cached, ms('5m')); } return cached; diff --git a/src/routes/api/client.js b/src/routes/api/client.js index 8d89eec..4c793fb 100644 --- a/src/routes/api/client.js +++ b/src/routes/api/client.js @@ -10,11 +10,12 @@ module.exports.get = () => ({ if (!cached) { const tickets = await client.prisma.ticket.findMany({ select: { + closedAt: true, createdAt: true, firstResponseAt: true, }, }); - const closedTickets = tickets.filter(t => t.closedAt); + const closedTickets = tickets.filter(t => t.firstResponseAt && t.closedAt); const users = await client.prisma.user.findMany({ select: { messageCount: true } }); cached = { avatar: client.user.avatarURL(),