From 23458c664a93bf6381d119255d675873ba929e60 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Thu, 25 Jul 2024 23:15:13 +0530 Subject: [PATCH] Added 90 days filter to Member Count Admin Dashboard query (#20665) Ref https://linear.app/tryghost/issue/SLO-192/add-90-days-filter-to-member-count-admin-dashboard-query This API used to fetch the data for all time. We need data for only 90 days for our dashboard. This will optimise the performance of this API. Also, sorting the rows in memory is lot more efficient than orderBy in db in this case. --- ghost/stats-service/lib/MembersStatsService.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ghost/stats-service/lib/MembersStatsService.js b/ghost/stats-service/lib/MembersStatsService.js index ce46b16e04..9cfad8d4c0 100644 --- a/ghost/stats-service/lib/MembersStatsService.js +++ b/ghost/stats-service/lib/MembersStatsService.js @@ -36,6 +36,7 @@ class MembersStatsService { */ async fetchAllStatusDeltas() { const knex = this.knex; + const ninetyDaysAgo = moment.utc().subtract(91, 'days').startOf('day').utc().format('YYYY-MM-DD HH:mm:ss'); const rows = await knex('members_status_events') .select(knex.raw('DATE(created_at) as date')) .select(knex.raw(`SUM( @@ -56,8 +57,8 @@ class MembersStatsService { WHEN from_status='free' THEN -1 ELSE 0 END ) as free_delta`)) - .groupByRaw('DATE(created_at)') - .orderByRaw('DATE(created_at)'); + .where('created_at', '>=', ninetyDaysAgo) + .groupByRaw('DATE(created_at)'); return rows; } @@ -77,6 +78,7 @@ class MembersStatsService { const cumulativeResults = []; + rows.sort((a, b) => new Date(a.date) - new Date(b.date)); // Loop in reverse order (needed to have correct sorted result) for (let i = rows.length - 1; i >= 0; i -= 1) { const row = rows[i];