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.
This commit is contained in:
Princi Vershwal 2024-07-25 23:15:13 +05:30 committed by GitHub
parent 74b5677e3d
commit 23458c664a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -36,6 +36,7 @@ class MembersStatsService {
*/ */
async fetchAllStatusDeltas() { async fetchAllStatusDeltas() {
const knex = this.knex; 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') const rows = await knex('members_status_events')
.select(knex.raw('DATE(created_at) as date')) .select(knex.raw('DATE(created_at) as date'))
.select(knex.raw(`SUM( .select(knex.raw(`SUM(
@ -56,8 +57,8 @@ class MembersStatsService {
WHEN from_status='free' THEN -1 WHEN from_status='free' THEN -1
ELSE 0 END ELSE 0 END
) as free_delta`)) ) as free_delta`))
.groupByRaw('DATE(created_at)') .where('created_at', '>=', ninetyDaysAgo)
.orderByRaw('DATE(created_at)'); .groupByRaw('DATE(created_at)');
return rows; return rows;
} }
@ -77,6 +78,7 @@ class MembersStatsService {
const cumulativeResults = []; const cumulativeResults = [];
rows.sort((a, b) => new Date(a.date) - new Date(b.date));
// Loop in reverse order (needed to have correct sorted result) // Loop in reverse order (needed to have correct sorted result)
for (let i = rows.length - 1; i >= 0; i -= 1) { for (let i = rows.length - 1; i >= 0; i -= 1) {
const row = rows[i]; const row = rows[i];