Ghost/ghost/email-analytics-service/lib/event-processing-result.js
Simon Backx f4fdb4fa6c
Added new email event processor (#15879)
fixes https://github.com/TryGhost/Team/issues/2310

This moves the processing of the events from the event-processor to a
new email-event-processor in the email-service package.

- The `EmailEventProcessor` only translates events from
providerId/emailId to their known emailId, memberId and recipientId, and
dispatches the corresponding events.
- Since `EmailEventProcessor` runs in a separate worker thread, we can't
listen for the dispatched events on the main thread. To accomplish this
communication, the events dispatched from the `EmailEventProcessor`
class are 'posted' via the postMessage method and redispatched on the
main thread.
- A new `EmailEventStorage` class reacts to the email events and stores
it in the database. This code mostly corresponds to the (now deleted)
subclass of the old `EmailEventProcessor`
- Updating a members last_seen_at timestamp has moved to the
lastSeenAtUpdater.
- Email events no longer store `ObjectID` because these are not
encodable across threads via postMessage
- Includes new E2E tests that test the storage of all supported Mailgun
events. Note that in these tests we run the processing on the main
thread instead of on a separate thread (couldn't do this because
stubbing is not possible across threads)

There are some missing pieces that will get added in later PRs (this PR
focuses on porting the existing functionality):
- Handling temporary failures/bounces
- Capturing the error messages of bounce events
2022-11-29 11:15:19 +01:00

68 lines
2.1 KiB
JavaScript

const _ = require('lodash');
class EventProcessingResult {
/**
* @param {object} result
* @param {number} [result.delivered]
* @param {number} [result.opened]
* @param {number} [result.temporaryFailed]
* @param {number} [result.permanentFailed]
* @param {number} [result.unsubscribed]
* @param {number} [result.complained]
* @param {number} [result.unhandled]
* @param {number} [result.unprocessable]
* @param {number} [result.processingFailures]
* @param {string[]} [result.emailIds]
* @param {string[]} [result.memberIds]
*/
constructor(result = {}) {
// counts
this.delivered = 0;
this.opened = 0;
this.temporaryFailed = 0;
this.permanentFailed = 0;
this.unsubscribed = 0;
this.complained = 0;
this.unhandled = 0;
this.unprocessable = 0;
// processing failures are counted separately in addition to event type counts
this.processingFailures = 0;
// ids seen whilst processing ready for passing to the stats aggregator
this.emailIds = [];
this.memberIds = [];
this.merge(result);
}
get totalEvents() {
return this.delivered
+ this.opened
+ this.temporaryFailed
+ this.permanentFailed
+ this.unsubscribed
+ this.complained
+ this.unhandled
+ this.unprocessable;
}
merge(other = {}) {
this.delivered += other.delivered || 0;
this.opened += other.opened || 0;
this.temporaryFailed += other.temporaryFailed || 0;
this.permanentFailed += other.permanentFailed || 0;
this.unsubscribed += other.unsubscribed || 0;
this.complained += other.complained || 0;
this.unhandled += other.unhandled || 0;
this.unprocessable += other.unprocessable || 0;
this.processingFailures += other.processingFailures || 0;
this.emailIds = _.compact(_.union(this.emailIds, other.emailIds || []));
this.memberIds = _.compact(_.union(this.memberIds, other.memberIds || []));
}
}
module.exports = EventProcessingResult;