From 23075b7bf8a3b4f292db75661998ffe12e4e34d6 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Tue, 2 Jul 2024 11:34:04 +0200 Subject: [PATCH] Optimized aggregating member attribution statistics - the existing code creates a new moment instance, takes away some days and then formats the result - this is run for every entry of the member attribution stats, which means dashboards for big sites with a lot of attribution data become slow - this value doesn't change across each iteration of the filter, so we can just extract it out and calculate it once - this commit removes this code block from the flamegraph completely --- ghost/admin/app/services/dashboard-stats.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ghost/admin/app/services/dashboard-stats.js b/ghost/admin/app/services/dashboard-stats.js index 8ffcb7d54b..c55d5f5363 100644 --- a/ghost/admin/app/services/dashboard-stats.js +++ b/ghost/admin/app/services/dashboard-stats.js @@ -242,11 +242,14 @@ export default class DashboardStatsService extends Service { if (!this.memberAttributionStats) { return []; } + + const firstChartDay = moment().add(-this.chartDays, 'days').format('YYYY-MM-DD'); + return this.memberAttributionStats.filter((stat) => { if (this.chartDays === 'all') { return true; } - return stat.date >= moment().add(-this.chartDays, 'days').format('YYYY-MM-DD'); + return stat.date >= firstChartDay; }).reduce((acc, stat) => { const statSource = stat.source ?? ''; const existingSource = acc.find(s => s.source.toLowerCase() === statSource.toLowerCase());