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();