From 82053e0c7c4bea8921cc186bf57599cd4849d568 Mon Sep 17 00:00:00 2001 From: Keith Williams Date: Wed, 11 Sep 2024 11:16:46 -0300 Subject: [PATCH] perf: Include no shows in main status query (#16594) * perf: Include no shows in main status query * Removed the Promise.all call --- packages/features/insights/server/events.ts | 18 +++++++--------- .../features/insights/server/trpc-router.ts | 21 +++++++------------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/features/insights/server/events.ts b/packages/features/insights/server/events.ts index 42ba2a077b..bdd0e2044c 100644 --- a/packages/features/insights/server/events.ts +++ b/packages/features/insights/server/events.ts @@ -11,7 +11,7 @@ class EventsInsights { static countGroupedByStatus = async (where: Prisma.BookingTimeStatusWhereInput) => { const data = await prisma.bookingTimeStatus.groupBy({ where, - by: ["timeStatus"], + by: ["timeStatus", "noShowHost"], _count: { _all: true, }, @@ -22,6 +22,10 @@ class EventsInsights { if (typeof item.timeStatus === "string" && item) { aggregate[item.timeStatus] += item?._count?._all ?? 0; aggregate["_all"] += item?._count?._all ?? 0; + + if (item.noShowHost) { + aggregate["noShowHost"] += item?._count?._all ?? 0; + } } return aggregate; }, @@ -29,6 +33,7 @@ class EventsInsights { completed: 0, rescheduled: 0, cancelled: 0, + noShowHost: 0, _all: 0, } ); @@ -48,15 +53,6 @@ class EventsInsights { }); }; - static getTotalNoShows = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => { - return await prisma.bookingTimeStatus.count({ - where: { - ...whereConditional, - noShowHost: true, - }, - }); - }; - static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => { const result = await prisma.bookingTimeStatus.findMany({ where: { @@ -173,9 +169,11 @@ class EventsInsights { return 0; } const result = (differenceActualVsPrevious * 100) / previousMetric; + if (isNaN(result) || !isFinite(result)) { return 0; } + return result; }; diff --git a/packages/features/insights/server/trpc-router.ts b/packages/features/insights/server/trpc-router.ts index e2953eae0d..f821318ab0 100644 --- a/packages/features/insights/server/trpc-router.ts +++ b/packages/features/insights/server/trpc-router.ts @@ -278,20 +278,16 @@ export const insightsRouter = router({ const [ countGroupedByStatus, totalRatingsAggregate, - totalNoShow, totalCSAT, lastPeriodCountGroupedByStatus, lastPeriodTotalRatingsAggregate, - lastPeriodTotalNoShow, lastPeriodTotalCSAT, ] = await Promise.all([ EventsInsights.countGroupedByStatus(baseWhereCondition), EventsInsights.getAverageRating(baseWhereCondition), - EventsInsights.getTotalNoShows(baseWhereCondition), EventsInsights.getTotalCSAT(baseWhereCondition), EventsInsights.countGroupedByStatus(lastPeriodBaseCondition), EventsInsights.getAverageRating(lastPeriodBaseCondition), - EventsInsights.getTotalNoShows(lastPeriodBaseCondition), EventsInsights.getTotalCSAT(lastPeriodBaseCondition), ]); @@ -299,6 +295,7 @@ export const insightsRouter = router({ const totalCompleted = countGroupedByStatus["completed"]; const totalRescheduled = countGroupedByStatus["rescheduled"]; const totalCancelled = countGroupedByStatus["cancelled"]; + const totalNoShow = countGroupedByStatus["noShowHost"]; const averageRating = totalRatingsAggregate._avg.rating ? parseFloat(totalRatingsAggregate._avg.rating.toFixed(1)) @@ -307,6 +304,7 @@ export const insightsRouter = router({ const lastPeriodBaseBookingsCount = lastPeriodCountGroupedByStatus["_all"]; const lastPeriodTotalRescheduled = lastPeriodCountGroupedByStatus["rescheduled"]; const lastPeriodTotalCancelled = lastPeriodCountGroupedByStatus["cancelled"]; + const lastPeriodTotalNoShow = lastPeriodCountGroupedByStatus["noShowHost"]; const lastPeriodAverageRating = lastPeriodTotalRatingsAggregate._avg.rating ? parseFloat(lastPeriodTotalRatingsAggregate._avg.rating.toFixed(1)) @@ -542,16 +540,13 @@ export const insightsRouter = router({ }, }; - const promisesResult = await Promise.all([ - EventsInsights.countGroupedByStatus(whereConditional), - EventsInsights.getTotalNoShows(whereConditional), - ]); + const countsByStatus = await EventsInsights.countGroupedByStatus(whereConditional); - EventData["Created"] = promisesResult[0]["_all"]; - EventData["Completed"] = promisesResult[0]["completed"]; - EventData["Rescheduled"] = promisesResult[0]["rescheduled"]; - EventData["Cancelled"] = promisesResult[0]["cancelled"]; - EventData["No-Show (Host)"] = promisesResult[1]; + EventData["Created"] = countsByStatus["_all"]; + EventData["Completed"] = countsByStatus["completed"]; + EventData["Rescheduled"] = countsByStatus["rescheduled"]; + EventData["Cancelled"] = countsByStatus["cancelled"]; + EventData["No-Show (Host)"] = countsByStatus["noShowHost"]; result.push(EventData); }