mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-23 19:02:29 +03:00
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.
62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
const MailgunClient = require('@tryghost/mailgun-client');
|
|
|
|
const EVENT_FILTER = 'delivered OR opened OR failed OR unsubscribed OR complained';
|
|
const PAGE_LIMIT = 300;
|
|
const DEFAULT_TAGS = ['bulk-email'];
|
|
|
|
class EmailAnalyticsProviderMailgun {
|
|
mailgunClient;
|
|
|
|
constructor({config, settings}) {
|
|
this.mailgunClient = new MailgunClient({config, settings});
|
|
this.tags = [...DEFAULT_TAGS];
|
|
|
|
if (config.get('bulkEmail:mailgun:tag')) {
|
|
this.tags.push(config.get('bulkEmail:mailgun:tag'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fetch from the last known timestamp-TRUST_THRESHOLD then work forwards
|
|
* through pages until we get a blank response. This lets us get events
|
|
* quicker than the TRUST_THRESHOLD
|
|
*
|
|
* @param {Function} batchHandler
|
|
* @param {Object} [options]
|
|
* @param {Number} [options.maxEvents] Not a strict maximum. We stop fetching after we reached the maximum AND received at least one event after begin (not equal) to prevent deadlocks.
|
|
* @param {Date} [options.begin]
|
|
* @param {Date} [options.end]
|
|
*/
|
|
fetchLatest(batchHandler, options) {
|
|
const mailgunOptions = {
|
|
limit: PAGE_LIMIT,
|
|
event: EVENT_FILTER,
|
|
tags: this.tags.join(' AND '),
|
|
begin: options.begin ? options.begin.getTime() / 1000 : undefined,
|
|
end: options.end ? options.end.getTime() / 1000 : undefined,
|
|
ascending: 'yes'
|
|
};
|
|
|
|
return this.#fetchAnalytics(mailgunOptions, batchHandler, {
|
|
maxEvents: options.maxEvents
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {Object} mailgunOptions
|
|
* @param {Number} mailgunOptions.limit
|
|
* @param {String} mailgunOptions.event
|
|
* @param {String} mailgunOptions.tags
|
|
* @param {String} [mailgunOptions.begin]
|
|
* @param {String} [mailgunOptions.ascending]
|
|
* @param {Function} batchHandler
|
|
* @param {Object} [options]
|
|
* @param {Number} [options.maxEvents] Not a strict maximum. We stop fetching after we reached the maximum AND received at least one event after begin (not equal) to prevent deadlocks.
|
|
*/
|
|
async #fetchAnalytics(mailgunOptions, batchHandler, options) {
|
|
return await this.mailgunClient.fetchEvents(mailgunOptions, batchHandler, options);
|
|
}
|
|
}
|
|
|
|
module.exports = EmailAnalyticsProviderMailgun;
|