Hey there, I'm currently exploring the most efficient method to accomplish a task. The data I am dealing with looks something like this:
[
{
name: 'email.delivered',
programme: 'Email One',
timestamp: 2022-03-24T18:06:02.000Z,
"payload":{
"id":"bcabca7c-a5d5-4e02-b247-2292240ffc77",
},
},
{
name: 'interaction.click',
programme: 'Email One',
timestamp: 2022-03-24T18:06:02.000Z,
"payload":{
"correlation":{
"channel":"email",
"messageId":"bcabca7c-a5d5-4e02-b247-2292240ffc77",
},
"metadata":{
"customerId":"9999999111",
"programName":"Email One"
}
},
},
...
]
The event names include delivered, failed, expired, or click. The programmes serve as categories for organization. Currently, my code snippet (simplified) performs certain checks in the following manner:
emails.forEach((record) => {
const data = JSON.parse(record.data);
if (data?.name === 'email.delivered') {
const id = data?.payload?.id;
if (!deliverDates[id]) deliverDates[id] = record.timestamp;
deliveredMap.set(programme, (deliveredMap.get(programme) || 0) + 1);
return;
}
if (data?.name === 'email.click') {
const id = data?.payload?.correlation?.messageId;
if (id) {
const deliveryDate = new Date(deliverDates[id]);
if (deliveryDate.getTime() > Date.now() - 1209600000) {
const programme = record?.programme;
clicksMap.set(programme, (clicksMap.get(programme) || 0) + 1);
}
}
}
});
The issue lies in having two separate Maps instead of returning a single Object with the programme as the key. My goal is to tally up all event types for each programme, resembling the desired structure below:
{
'Email One': {
delivered: 315,
failed: 18,
expired: 14,
click: 27,
},
'Email Two': {
delivered: 542,
failed: 322,
expired: 33,
click: 22,
}
...
}
Any suggestions on how to achieve this outcome efficiently would be greatly appreciated.
Cheers!