Ghost/ghost/email-analytics-service/test/email-analytics-service.test.js
Steve Larson ae15e12ffc Reverted email analytics jobs commits (#20835)
ref https://linear.app/tryghost/issue/ENG-1518

After releasing the analytics job improvements, it appears for large
sites we're awfully close to missing some Mailgun events because of an
unexpected behavior of the aggregateStats call for just the opened
events job. This is taking 2-5x(+) the amount of time that the aggregate
queries take for the other jobs, despite not being dependent on the
events.

To err on the side of caution, we're going to roll this back and look to
optimize the aggregation queries before re-implementing. And we may be a
bit more cautious in giving _some_ but not _all_ priority to the
`opened` events.
2024-08-27 16:16:07 -05:00

104 lines
3.1 KiB
JavaScript

// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const sinon = require('sinon');
const {
EmailAnalyticsService
} = require('..');
const EventProcessingResult = require('../lib/EventProcessingResult');
describe('EmailAnalyticsService', function () {
let eventProcessor;
beforeEach(function () {
eventProcessor = {};
eventProcessor.handleDelivered = sinon.stub().callsFake(({emailId}) => {
return {
emailId,
emailRecipientId: emailId,
memberId: 1
};
});
eventProcessor.handleOpened = sinon.stub().callsFake(({emailId}) => {
return {
emailId,
emailRecipientId: emailId,
memberId: 1
};
});
});
describe('fetchLatest', function () {
});
describe('processEventBatch', function () {
it('uses passed-in event processor', async function () {
const service = new EmailAnalyticsService({
eventProcessor
});
const result = new EventProcessingResult();
const fetchData = {
};
await service.processEventBatch([{
type: 'delivered',
emailId: 1,
timestamp: new Date(1)
}, {
type: 'delivered',
emailId: 2,
timestamp: new Date(2)
}, {
type: 'opened',
emailId: 1,
timestamp: new Date(3)
}], result, fetchData);
eventProcessor.handleDelivered.callCount.should.eql(2);
result.should.deepEqual(new EventProcessingResult({
delivered: 2,
opened: 1,
unprocessable: 0,
emailIds: [1, 2],
memberIds: [1]
}));
fetchData.should.deepEqual({
lastEventTimestamp: new Date(3)
});
});
});
describe('aggregateStats', function () {
let service;
beforeEach(function () {
service = new EmailAnalyticsService({
queries: {
aggregateEmailStats: sinon.spy(),
aggregateMemberStats: sinon.spy()
}
});
});
it('calls appropriate query for each email id and member id', async function () {
await service.aggregateStats({
emailIds: ['e-1', 'e-2'],
memberIds: ['m-1', 'm-2']
});
service.queries.aggregateEmailStats.calledTwice.should.be.true();
service.queries.aggregateEmailStats.calledWith('e-1').should.be.true();
service.queries.aggregateEmailStats.calledWith('e-2').should.be.true();
service.queries.aggregateMemberStats.calledTwice.should.be.true();
service.queries.aggregateMemberStats.calledWith('m-1').should.be.true();
service.queries.aggregateMemberStats.calledWith('m-2').should.be.true();
});
});
});