From b04452fdb3a508fae30b2c80165b5cf1e0411947 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Wed, 17 Jul 2024 07:16:39 +0530 Subject: [PATCH] Added 90 days filter to Top Sources Admin Dashboard query (#20609) ref: https://linear.app/tryghost/issue/SLO-184/add-90-days-filter-to-top-sources-admin-dashboard-query --- .../lib/ReferrersStatsService.js | 4 +++ .../stats-service/test/lib/referrers.test.js | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ghost/stats-service/lib/ReferrersStatsService.js b/ghost/stats-service/lib/ReferrersStatsService.js index 345f4896bb..aebb06b667 100644 --- a/ghost/stats-service/lib/ReferrersStatsService.js +++ b/ghost/stats-service/lib/ReferrersStatsService.js @@ -101,10 +101,12 @@ class ReferrersStatsService { **/ async fetchAllPaidConversionSources() { const knex = this.knex; + const ninetyDaysAgo = moment.utc().subtract(90, 'days').startOf('day').utc().format('YYYY-MM-DD HH:mm:ss'); const rows = await knex('members_subscription_created_events') .select(knex.raw(`DATE(created_at) as date`)) .select(knex.raw(`COUNT(*) as paid_conversions`)) .select(knex.raw(`referrer_source as source`)) + .where('created_at', '>=', ninetyDaysAgo) .groupBy('date', 'referrer_source') .orderBy('date'); @@ -116,10 +118,12 @@ class ReferrersStatsService { **/ async fetchAllSignupSources() { const knex = this.knex; + const ninetyDaysAgo = moment.utc().subtract(90, 'days').startOf('day').utc().format('YYYY-MM-DD HH:mm:ss'); const rows = await knex('members_created_events') .select(knex.raw(`DATE(created_at) as date`)) .select(knex.raw(`COUNT(*) as signups`)) .select(knex.raw(`referrer_source as source`)) + .where('created_at', '>=', ninetyDaysAgo) .groupBy('date', 'referrer_source') .orderBy('date'); diff --git a/ghost/stats-service/test/lib/referrers.test.js b/ghost/stats-service/test/lib/referrers.test.js index 25d26b228c..0b2fd2e9fd 100644 --- a/ghost/stats-service/test/lib/referrers.test.js +++ b/ghost/stats-service/test/lib/referrers.test.js @@ -1,6 +1,7 @@ const knex = require('knex').default; const assert = require('assert/strict'); const ReferrersStatsService = require('../../lib/ReferrersStatsService'); +const {DateTime} = require('luxon'); describe('ReferrersStatsService', function () { describe('getReferrerHistory', function () { @@ -35,12 +36,13 @@ describe('ReferrersStatsService', function () { }); async function insertEvents(sources) { - const {DateTime} = require('luxon'); const signupInsert = []; const paidInsert = []; + const startDate = DateTime.now().minus({months: 1}); + for (let index = 0; index < sources.length; index++) { - const day = DateTime.fromISO('1970-01-01').plus({days: index}).toISODate(); + const day = startDate.plus({days: index}).toISODate(); if (index > 0) { signupInsert.push({ referrer_source: sources[index], @@ -64,13 +66,13 @@ describe('ReferrersStatsService', function () { referrer_source: null, referrer_medium: null, referrer_url: null, - created_at: '1970-01-09' + created_at: startDate.plus({days: sources.length}).toISODate() }, { referrer_source: null, referrer_medium: null, referrer_url: null, - created_at: '1970-01-09' + created_at: startDate.plus({days: sources.length}).toISODate() } ]); @@ -80,13 +82,13 @@ describe('ReferrersStatsService', function () { referrer_source: null, referrer_medium: null, referrer_url: null, - created_at: '1970-01-09' + created_at: startDate.plus({days: sources.length}).toISODate() }, { referrer_source: null, referrer_medium: null, referrer_url: null, - created_at: '1970-01-09' + created_at: startDate.plus({days: sources.length}).toISODate() } ]); await db('members_created_events').insert(signupInsert); @@ -105,13 +107,19 @@ describe('ReferrersStatsService', function () { return result.date === date && result.source === source; }; - // Is sorted by date - assert.deepEqual(results.data.map(result => result.date), ['1970-01-01', '1970-01-02', '1970-01-03', '1970-01-04', '1970-01-05', '1970-01-06', '1970-01-07', '1970-01-08', '1970-01-09', '1970-01-09']); + const startDate = DateTime.now().minus({months: 1}); - const firstDayCounts = results.data.find(finder('Twitter', '1970-01-01')); - const secondDayCounts = results.data.find(finder('Ghost Newsletter', '1970-01-02')); - const thirdDayCounts = results.data.find(finder('Ghost Explore', '1970-01-03')); - const nullReferrerCounts = results.data.find(finder(null, '1970-01-09')); + const expectedDates = []; + for (let i = 0; i < 10; i++) { + expectedDates.push(startDate.plus({days: i}).toISODate()); + } + + assert.deepEqual(results.data.map(result => result.date), expectedDates); + + const firstDayCounts = results.data.find(finder('Twitter', expectedDates[0])); + const secondDayCounts = results.data.find(finder('Ghost Newsletter', expectedDates[1])); + const thirdDayCounts = results.data.find(finder('Ghost Explore', expectedDates[2])); + const nullReferrerCounts = results.data.find(finder(null, expectedDates[9])); assert(firstDayCounts.signups === 0); assert(firstDayCounts.paid_conversions === 1);