Advanced Accumulator code editor
The Advanced Accumulator code editor processes multiple events together, typically within time-based windows or grouped contexts. It is best suited for aggregation, summation, and grouped analytics.
Example
Suppose you collect daily transactions from different customers. With this code editor, you can calculate the total sales per customer each day or find the average order value per week. It works for batch or time-based processing where data is grouped and processed together.
exports.step = function(events, context) {
if (events.length > 0) {
// Use the first event as a base for the output
const result = { ...events[0] };
let totalSales = 0;
let totalOrders = events.length;
// Calculate total sales for this customer within the time window
events.forEach(event => {
const price = Number(event.price) || 0;
const quantity = Number(event.quantity) || 0;
totalSales += price * quantity;
});
// Add calculated fields to the result
result.totalSales = totalSales;
result.averageOrderValue = totalOrders > 0 ? totalSales / totalOrders : 0;
result.customerId = result.customerId; // Group identifier
result.eventCount = totalOrders;
result.windowStart = context.windowStart;
result.windowEnd = context.windowEnd;
result.status = "aggregated";
return result;
}
return null;
}
Capabilities
The code editor works with many events at once, usually within a time window (like every hour, day, or week). It is useful great for finding totals, averages, or counts. For example, summing up sales for each customer every day. It can also group data by fields (like customer ID or region) and analyze patterns within each group. The system collects events for a set period and then processes them together once the time is up. The code editor helps you summarize and analyze groups of data over time.
Since the code editor waits for a batch or time window to finish, results might not appear instantly. It also uses more memory since it stores multiple records until processing time. The code editor cannot look back at previous windows or handle ongoing state and only works with the events currently in its batch. Use it when you want time-based summaries or grouped calculations rather than real-time updates.
Configuration Metadata
|
Field |
Mandatory? |
Description |
|---|---|---|
|
|
Conditional |
Required only if |
|
|
Conditional |
Also required when |
|
|
Conditional |
Required only if |
|
|
Optional (but recommended) |
Defines how to group events (e.g., by |
Example:
{
"triggerType": "Timeout",
"timeoutType": "EventTime",
"timeoutDuration": "1 day",
"eventTimeField": "eventTime",
"groupFields": ["customerId"]
}