From dac3fff90f7142a6c05f1d389de5d65afe58fc84 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Tue, 6 Sep 2022 12:08:16 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20incorrect=20member=20cou?= =?UTF-8?q?nt=20on=20sidebar=20(#15330)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refshttps://github.com/TryGhost/Team/issues/1738 - Changed sidebar member count to use new API endpoint data. - Added separate function for getting member count. - changed `_fetchCountsTask` to use new `/stats/member_count/` endpoint - updated @task to calculate total members from endpoint data. --- .../admin/app/components/gh-nav-menu/main.js | 8 +++--- ghost/admin/app/services/members-stats.js | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ghost/admin/app/components/gh-nav-menu/main.js b/ghost/admin/app/components/gh-nav-menu/main.js index cba10dc028..bea2247839 100644 --- a/ghost/admin/app/components/gh-nav-menu/main.js +++ b/ghost/admin/app/components/gh-nav-menu/main.js @@ -110,12 +110,12 @@ export default class Main extends Component.extend(ShortcutsMixin) { @task(function* () { try { this.set('memberCountLoading', true); - const stats = yield this.membersStats.fetchCounts(); + const stats = yield this.membersStats.fetchMemberCount(); this.set('memberCountLoading', false); if (stats) { - const statsDateObj = this.membersStats.fillCountDates(stats.data) || {}; - const dateValues = Object.values(statsDateObj); - this.set('memberCount', dateValues.length ? dateValues[dateValues.length - 1].total : 0); + const {free, paid, comped} = stats.meta.totals; + const total = free + paid + comped || 0; + this.set('memberCount', total); } } catch (e) { return false; diff --git a/ghost/admin/app/services/members-stats.js b/ghost/admin/app/services/members-stats.js index 2d276dd84f..4c8f0d7a44 100644 --- a/ghost/admin/app/services/members-stats.js +++ b/ghost/admin/app/services/members-stats.js @@ -16,6 +16,7 @@ export default class MembersStatsService extends Service { @tracked countStats = null; @tracked mrrStats = null; @tracked newsletterStats = null; + @tracked totalMemberCount = null; fetch() { let daysChanged = this._lastFetchedDays !== this.days; @@ -50,6 +51,22 @@ export default class MembersStatsService extends Service { return this._fetchCountsTask.perform(); } + fetchMemberCount() { + let staleData = this._lastFetchedMemberCounts && (new Date() - this._lastFetchedMemberCounts) > ONE_MINUTE; + + // return an already in-progress promise unless params have changed + if (this._fetchMemberCountsTask.isRunning) { + return this._fetchMemberCountsTask.last; + } + + // return existing stats unless data is > 1 min old + if (this.totalMemberCount && !this._forceRefresh && !staleData) { + return Promise.resolve(this.totalMemberCount); + } + + return this._fetchMemberCountsTask.perform(); + } + fetchNewsletterStats() { let staleData = this._lastFetchedNewsletterStats && (new Date() - this._lastFetchedNewsletterStats) > ONE_MINUTE; @@ -196,6 +213,16 @@ export default class MembersStatsService extends Service { return stats; } + @task + *_fetchMemberCountsTask() { + this._lastFetchedMemberCounts = new Date(); + + let statsUrl = this.ghostPaths.url.api('stats/member_count/'); + let stats = yield this.ajax.request(statsUrl); + this.totalMemberCount = stats; + return stats; + } + @task *_fetchMRRTask() { this._lastFetchedMRR = new Date();