diff --git a/ghost/core/core/server/services/email-analytics/lib/queries.js b/ghost/core/core/server/services/email-analytics/lib/queries.js index c641496db8a6..fbe2019fdc3a 100644 --- a/ghost/core/core/server/services/email-analytics/lib/queries.js +++ b/ghost/core/core/server/services/email-analytics/lib/queries.js @@ -41,12 +41,14 @@ module.exports = { }, async aggregateEmailStats(emailId) { - const [deliveredCount] = await db.knex('email_recipients').count('id as count').whereRaw('email_id = ? AND delivered_at IS NOT NULL', [emailId]); + const {totalCount} = await db.knex('emails').select(db.knex.raw('email_count as totalCount')).where('id', emailId).first() || {totalCount: 0}; + // use IS NULL here because that will typically match far fewer rows than IS NOT NULL making the query faster + const [undeliveredCount] = await db.knex('email_recipients').count('id as count').whereRaw('email_id = ? AND delivered_at IS NULL', [emailId]); const [openedCount] = await db.knex('email_recipients').count('id as count').whereRaw('email_id = ? AND opened_at IS NOT NULL', [emailId]); const [failedCount] = await db.knex('email_recipients').count('id as count').whereRaw('email_id = ? AND failed_at IS NOT NULL', [emailId]); await db.knex('emails').update({ - delivered_count: deliveredCount.count, + delivered_count: totalCount - undeliveredCount.count, opened_count: openedCount.count, failed_count: failedCount.count }).where('id', emailId); diff --git a/ghost/core/test/utils/fixtures/data-generator.js b/ghost/core/test/utils/fixtures/data-generator.js index d671503b014b..6a2d650fa037 100644 --- a/ghost/core/test/utils/fixtures/data-generator.js +++ b/ghost/core/test/utils/fixtures/data-generator.js @@ -731,7 +731,7 @@ DataGenerator.Content = { id: ObjectId().toHexString(), uuid: '6b6afda6-4b5e-4893-bff6-f16859e8349a', status: 'submitted', - email_count: 2, + email_count: 6, // match the number of email_recipients relations below recipient_filter: 'all', subject: 'You got mailed!', html: '

Look! I\'m an email

', @@ -745,7 +745,7 @@ DataGenerator.Content = { uuid: '365daa11-4bf0-4614-ad43-6346387ffa00', status: 'failed', error: 'Everything went south', - email_count: 3, + email_count: 0, // match the number of email_recipient relations below subject: 'You got mailed! Again!', html: '

What\'s that? Another email!

', plaintext: 'yes this is an email',