923c522778
fixes https://github.com/TryGhost/Team/issues/2562 New event fetching loops: - Reworked the analytics fetching algorithm. Instead of starting again where we stopped during the last fetching minus 30 minutes, we now just continue where we stopped. But with ms precision (because no longer database dependent after first fetch), and we stop at NOW - 1 minute to reduce chance of missing events. - Apart from that, a missing fetching loop is introduced. This fetches events that are older than 30 minutes, and just processes all events a second time to make sure we didn't skip any because of storage delays in the Mailgun API. - A new scheduled fetching loop, that allows us to schedule between a given start/end date (currently only persisted in memory, so stops after a reboot) UI and endpoint changes: - New UI to show the state of the analytics 'loops' - New endpoint to request the analytics loop status - New endpoint to schedule analytics - New endpoint to cancel scheduled analytics - Some number formatting improvements, and introduction of 'opened' count in debug screen - Live reload of data in the debug screen Other changes: - This also improves the support for maxEvents. We can now stop a fetching loop after x events without worrying about lost events. This is used to reduce the fetched events in the missing and scheduled event loop (e.g. when the main one is fetching lots of events, we skip the other loops). - Prevents fetching the same events over and over again if no new events come in (because we always started at the same begin timestamp). The code increases the begin timestamp with 1 second if it is safe to do so, to prevent the API from returning the same events over and over again. - Some optimisations in handing the processing results (less merges to reduce CPU usage in cases we have lots of events). Testing: - You can test with lots of events using the new mailgun mocking server (Toolbox repo `scripts/mailgun-mock-server`). This can also simulate events that are only returned after x minutes because of storage delays.
160 lines
6.1 KiB
JavaScript
160 lines
6.1 KiB
JavaScript
const sinon = require('sinon');
|
|
|
|
const {EventProcessingResult} = require('@tryghost/email-analytics-service');
|
|
|
|
// module under test
|
|
const EmailAnalyticsProviderMailgun = require('../');
|
|
|
|
const SAMPLE_EVENTS = [
|
|
new EventProcessingResult({
|
|
delivered: 4,
|
|
opened: 2,
|
|
temporaryFailed: 0,
|
|
permanentFailed: 0,
|
|
unsubscribed: 0,
|
|
complained: 0,
|
|
unhandled: 0,
|
|
unprocessable: 0,
|
|
processingFailures: 0,
|
|
emailIds: [
|
|
'62f3aebaaa887b504a40519f',
|
|
'62f3c200e8e74e677ab5e1fa',
|
|
'62f3c606de193a6d00433dfc'
|
|
],
|
|
memberIds: ['62ed25f69ae8f1a8c22d1a2f']
|
|
})
|
|
];
|
|
|
|
describe('EmailAnalyticsProviderMailgun', function () {
|
|
let config, settings;
|
|
|
|
beforeEach(function () {
|
|
// options objects that can be stubbed or spied
|
|
config = {get() {}};
|
|
settings = {get() {}};
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
describe('fetchLatest()', function () {
|
|
const LATEST_TIMESTAMP = new Date('Thu Feb 25 2021 12:00:00 GMT+0000');
|
|
const END_EXAMPLE = new Date('Thu Feb 25 2021 14:00:00 GMT+0000');
|
|
const MAILGUN_OPTIONS = {
|
|
event: 'delivered OR opened OR failed OR unsubscribed OR complained',
|
|
limit: 300,
|
|
tags: 'bulk-email',
|
|
begin: 1614254400,
|
|
end: undefined,
|
|
ascending: 'yes'
|
|
};
|
|
|
|
it('passes the correct parameters to mailgun-client', async function () {
|
|
const configStub = sinon.stub(config, 'get');
|
|
configStub.withArgs('bulkEmail').returns({
|
|
mailgun: {
|
|
apiKey: 'apiKey',
|
|
domain: 'domain.com',
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
}
|
|
});
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
const batchHandler = sinon.spy();
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP});
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, MAILGUN_OPTIONS, batchHandler, {maxEvents: undefined});
|
|
});
|
|
|
|
it('can use end timestamp', async function () {
|
|
const configStub = sinon.stub(config, 'get');
|
|
configStub.withArgs('bulkEmail').returns({
|
|
mailgun: {
|
|
apiKey: 'apiKey',
|
|
domain: 'domain.com',
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
}
|
|
});
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
const batchHandler = sinon.spy();
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP, end: END_EXAMPLE});
|
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: undefined});
|
|
});
|
|
|
|
it('can use end without begin', async function () {
|
|
const configStub = sinon.stub(config, 'get');
|
|
configStub.withArgs('bulkEmail').returns({
|
|
mailgun: {
|
|
apiKey: 'apiKey',
|
|
domain: 'domain.com',
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
}
|
|
});
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
const batchHandler = sinon.spy();
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {end: END_EXAMPLE});
|
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, begin: undefined, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: undefined});
|
|
});
|
|
|
|
it('can use max events', async function () {
|
|
const configStub = sinon.stub(config, 'get');
|
|
configStub.withArgs('bulkEmail').returns({
|
|
mailgun: {
|
|
apiKey: 'apiKey',
|
|
domain: 'domain.com',
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
}
|
|
});
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
const batchHandler = sinon.spy();
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP, end: END_EXAMPLE, maxEvents: 1000});
|
|
|
|
const END_EXAMPLE_UNIX = END_EXAMPLE.getTime() / 1000;
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {...MAILGUN_OPTIONS, end: END_EXAMPLE_UNIX}, batchHandler, {maxEvents: 1000});
|
|
});
|
|
|
|
it('uses custom tags when supplied', async function () {
|
|
const configStub = sinon.stub(config, 'get');
|
|
configStub.withArgs('bulkEmail').returns({
|
|
mailgun: {
|
|
apiKey: 'apiKey',
|
|
domain: 'domain.com',
|
|
baseUrl: 'https://api.mailgun.net/v3'
|
|
}
|
|
});
|
|
configStub.withArgs('bulkEmail:mailgun:tag').returns('custom-tag');
|
|
|
|
const mailgunProvider = new EmailAnalyticsProviderMailgun({config, settings});
|
|
|
|
const batchHandler = sinon.spy();
|
|
const mailgunFetchEventsStub = sinon.stub(mailgunProvider.mailgunClient, 'fetchEvents').returns(SAMPLE_EVENTS);
|
|
|
|
await mailgunProvider.fetchLatest(batchHandler, {begin: LATEST_TIMESTAMP});
|
|
|
|
sinon.assert.calledWithExactly(mailgunFetchEventsStub, {
|
|
...MAILGUN_OPTIONS,
|
|
tags: 'bulk-email AND custom-tag'
|
|
}, batchHandler, {maxEvents: undefined});
|
|
});
|
|
});
|
|
});
|