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
This commit is contained in:
Princi Vershwal 2024-07-17 07:16:39 +05:30 committed by GitHub
parent 2e3eb1da71
commit b04452fdb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 12 deletions

View File

@ -101,10 +101,12 @@ class ReferrersStatsService {
**/ **/
async fetchAllPaidConversionSources() { async fetchAllPaidConversionSources() {
const knex = this.knex; 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') const rows = await knex('members_subscription_created_events')
.select(knex.raw(`DATE(created_at) as date`)) .select(knex.raw(`DATE(created_at) as date`))
.select(knex.raw(`COUNT(*) as paid_conversions`)) .select(knex.raw(`COUNT(*) as paid_conversions`))
.select(knex.raw(`referrer_source as source`)) .select(knex.raw(`referrer_source as source`))
.where('created_at', '>=', ninetyDaysAgo)
.groupBy('date', 'referrer_source') .groupBy('date', 'referrer_source')
.orderBy('date'); .orderBy('date');
@ -116,10 +118,12 @@ class ReferrersStatsService {
**/ **/
async fetchAllSignupSources() { async fetchAllSignupSources() {
const knex = this.knex; 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') const rows = await knex('members_created_events')
.select(knex.raw(`DATE(created_at) as date`)) .select(knex.raw(`DATE(created_at) as date`))
.select(knex.raw(`COUNT(*) as signups`)) .select(knex.raw(`COUNT(*) as signups`))
.select(knex.raw(`referrer_source as source`)) .select(knex.raw(`referrer_source as source`))
.where('created_at', '>=', ninetyDaysAgo)
.groupBy('date', 'referrer_source') .groupBy('date', 'referrer_source')
.orderBy('date'); .orderBy('date');

View File

@ -1,6 +1,7 @@
const knex = require('knex').default; const knex = require('knex').default;
const assert = require('assert/strict'); const assert = require('assert/strict');
const ReferrersStatsService = require('../../lib/ReferrersStatsService'); const ReferrersStatsService = require('../../lib/ReferrersStatsService');
const {DateTime} = require('luxon');
describe('ReferrersStatsService', function () { describe('ReferrersStatsService', function () {
describe('getReferrerHistory', function () { describe('getReferrerHistory', function () {
@ -35,12 +36,13 @@ describe('ReferrersStatsService', function () {
}); });
async function insertEvents(sources) { async function insertEvents(sources) {
const {DateTime} = require('luxon');
const signupInsert = []; const signupInsert = [];
const paidInsert = []; const paidInsert = [];
const startDate = DateTime.now().minus({months: 1});
for (let index = 0; index < sources.length; index++) { 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) { if (index > 0) {
signupInsert.push({ signupInsert.push({
referrer_source: sources[index], referrer_source: sources[index],
@ -64,13 +66,13 @@ describe('ReferrersStatsService', function () {
referrer_source: null, referrer_source: null,
referrer_medium: null, referrer_medium: null,
referrer_url: null, referrer_url: null,
created_at: '1970-01-09' created_at: startDate.plus({days: sources.length}).toISODate()
}, },
{ {
referrer_source: null, referrer_source: null,
referrer_medium: null, referrer_medium: null,
referrer_url: 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_source: null,
referrer_medium: null, referrer_medium: null,
referrer_url: null, referrer_url: null,
created_at: '1970-01-09' created_at: startDate.plus({days: sources.length}).toISODate()
}, },
{ {
referrer_source: null, referrer_source: null,
referrer_medium: null, referrer_medium: null,
referrer_url: null, referrer_url: null,
created_at: '1970-01-09' created_at: startDate.plus({days: sources.length}).toISODate()
} }
]); ]);
await db('members_created_events').insert(signupInsert); await db('members_created_events').insert(signupInsert);
@ -105,13 +107,19 @@ describe('ReferrersStatsService', function () {
return result.date === date && result.source === source; return result.date === date && result.source === source;
}; };
// Is sorted by date const startDate = DateTime.now().minus({months: 1});
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 firstDayCounts = results.data.find(finder('Twitter', '1970-01-01')); const expectedDates = [];
const secondDayCounts = results.data.find(finder('Ghost Newsletter', '1970-01-02')); for (let i = 0; i < 10; i++) {
const thirdDayCounts = results.data.find(finder('Ghost Explore', '1970-01-03')); expectedDates.push(startDate.plus({days: i}).toISODate());
const nullReferrerCounts = results.data.find(finder(null, '1970-01-09')); }
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.signups === 0);
assert(firstDayCounts.paid_conversions === 1); assert(firstDayCounts.paid_conversions === 1);