Advanced Aggregator code editor
The Advanced Aggregator code editor provides stateful, memory-based processing across multiple events. It enables custom release conditions, allowing flexible accumulation of data until business-defined triggers are met.
Example
You can use the code editor to track how many units a customer has purchased. Once their total reaches 1,000 units, the code can automatically release a record showing that they have reached the threshold and then reset the counter. This is useful for usage tracking, threshold alerts, or custom business triggers.
exports.step = function(events, context) {
const state = context.state;
let results = [];
events.forEach(event => {
// Get the current accumulated quantity
let currentTotal = state.get("totalQuantity") || 0;
// Add the quantity from this event
currentTotal += Number(event.quantity) || 0;
state.set("totalQuantity", currentTotal);
// Check if the threshold is reached
if (currentTotal >= 1000) {
const result = {
customerId: event.customerId,
totalQuantity: currentTotal,
releaseReason: "threshold_reached",
status: "released",
timestamp: new Date().toISOString()
};
// Reset the accumulator for this group
state.set("totalQuantity", 0);
// Store the result
results.push(result);
}
});
// Return the result if a threshold was reached
return results.length > 0 ? results : null;
}
Capabilities
The code editor is designed to collect and remember data over time. It keeps a running total or state, and you can decide when to release the results. For example, when a total reaches a certain value or a condition is met. This makes it perfect for tracking progress, detecting thresholds, or managing custom rules (like sending an alert when a customer’s usage hits a limit). It is flexible because you define when and how the accumulated data should be processed. The accumulator code editor helps you hold onto data until something important happens.
Since the code editor stores data in the memory, you need to manage it carefully to avoid overloading the system. It does not automatically handle time-based windows and the logic can be more complex since you decide when to release results. Use it when you need custom rules, ongoing tracking, or stateful processing instead of fixed time-based summaries.
Configuration Metadata
Example:
{
"groupFields": ["customerId"],
"releaseTrigger": "custom_condition"
}
groupFields
groupFields defines how incoming events should be grouped for accumulation. All events that share the same value for the specified field(s) are accumulated together, they share the same internal state.
For example, if you set "groupFields": ["customerId"], then:
-
Each customer will have their own separate accumulator.
-
All events with the same customerId will be added to that customer’s running total (or whatever state you track in your code).
Example:
If you receive these three events:
{ "customerId": "A123", "quantity": 200 }
{ "customerId": "A123", "quantity": 300 }
{ "customerId": "B456", "quantity": 500 }
-
Customer A123 accumulates 200 + 300 = 500 units.
-
Customer B456 accumulates 500 units separately.
-
Each customer’s total is tracked independently.