From 6208a03a11b8d2f658e297b709aab2d2df9cff76 Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Mon, 28 Mar 2022 14:20:07 +0200 Subject: [PATCH] Improved email open rate chart in dashboard 5.0 refs https://github.com/TryGhost/Team/issues/1443 - Now fetches email models instead of posts - Fetches up to 8 emails - Autofills missing emails, so we always have 8 emails - Changed data structure of emailOpenRateStats variable in dashboard stats service --- app/components/dashboard/v5/chart-email.js | 4 +-- app/services/dashboard-mocks.js | 27 +++++--------- app/services/dashboard-stats.js | 42 ++++++++++++++++++---- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/app/components/dashboard/v5/chart-email.js b/app/components/dashboard/v5/chart-email.js index a429f9360..05c5d956b 100644 --- a/app/components/dashboard/v5/chart-email.js +++ b/app/components/dashboard/v5/chart-email.js @@ -40,8 +40,8 @@ export default class ChartEmailOpenRate extends Component { get chartData() { const stats = this.dashboardStats.emailOpenRateStats; - const labels = stats.map(stat => stat.title); - const data = stats.map(stat => stat.email.openRate); + const labels = stats.map(stat => stat.subject); + const data = stats.map(stat => stat.openRate); return { labels, diff --git a/app/services/dashboard-mocks.js b/app/services/dashboard-mocks.js index 011d8e989..52c004a93 100644 --- a/app/services/dashboard-mocks.js +++ b/app/services/dashboard-mocks.js @@ -215,29 +215,20 @@ export default class DashboardMocksService extends Service { this.emailOpenRateStats = [ { - id: '23424', - title: '💸 The best way to get paid to create', - email: { - openedCount: 518, - deliveredCount: 1234 - } + subject: '💸 The best way to get paid to create', + openRate: 58, + submittedAt: new Date() }, { - id: '23425', - title: '🎒How to start a blog and make money', - email: { - openedCount: 100, - deliveredCount: 900 - } + subject: '🎒How to start a blog and make money', + openRate: 42, + submittedAt: new Date() } , { - id: '23426', - title: 'How to turn your amateur blogging into a real business', - email: { - openedCount: 500, - deliveredCount: 1600 - } + subject: 'How to turn your amateur blogging into a real business', + openRate: 89, + submittedAt: new Date() } ]; diff --git a/app/services/dashboard-stats.js b/app/services/dashboard-stats.js index ad3aa6fc0..cbea8d103 100644 --- a/app/services/dashboard-stats.js +++ b/app/services/dashboard-stats.js @@ -30,12 +30,11 @@ import {tracked} from '@glimmer/tracking'; */ /** - * @todo: THIS ONE IS TEMPORARY - * @typedef EmailOpenRateStat (Will be the same as post model probably) + * @typedef EmailOpenRateStat * @type {Object} - * @property {string} id Post id - * @property {string} title Post title - * @property {?Object} Email model + * @property {string} subject Email title + * @property {number} openRate Email openRate + * @property {Date} submittedAt Date */ /** @@ -400,8 +399,37 @@ export default class DashboardStatsService extends Service { return; } - const posts = yield this.store.query('post', {limit: 5, filter: 'status:published', order: 'published_at desc'}); - this.emailOpenRateStats = posts; + const limit = 8; + let query = { + filter: 'email_count:-0', + order: 'submitted_at desc', + limit: limit + }; + const results = yield this.store.query('email', query); + const data = results.toArray(); + let stats = data.map((d) => { + return { + subject: d.subject, + submittedAt: moment(d.submittedAtUTC).format('YYYY-MM-DD'), + openRate: d.openRate + }; + }); + + const paddedResults = []; + if (data.length < limit) { + const pad = limit - data.length; + const lastSubmittedAt = data.length > 0 ? data[results.length - 1].submittedAtUTC : moment(); + for (let i = 0; i < pad; i++) { + paddedResults.push({ + subject: '', + submittedAt: moment(lastSubmittedAt).subtract(i + 1, 'days').format('YYYY-MM-DD'), + openRate: 0 + }); + } + } + stats = stats.concat(paddedResults); + stats.reverse(); + this.emailOpenRateStats = stats; } /**