The Script Aggregator function inherits all functions and variables from the Script function and also has an object of its own called session
, which is a representation of a group of data records matching the configured conditions.
You can use the following properties/objects when configuring
:
Properties | Methods |
---|
session.key : The id of the session constructed based on the group by rules.
session.data : The configurable data block of the session used for storing the aggregated data. The default value is { }.
session.meta : Meta data collected by the application containing:
count : Number of records matching the current session. The count is increased every time a record passes.
firstEvent : Timestamp for when the first record matched the session.
lastEvent : Timestamp for when the last record matched the session.
flushType : The flushType is set as part of the meta downstream once the session is either pushed or flushed.
mycustomProperty : Allows you to add a custom property in the meta data, that will also be pushed downstream along with the payload.
Meta properties
meta = {
count: number,
firstEvent: timestamp,
lastEvent: timestamp,
flushType: string,
myCustomProperty: any,
}
session.timeoutAt : Timestamp for when a session should be flushed. Use the methods setTimeout and clearTimeout when configuring the timeoutAt property.
| Syntax
await session.push(
excludeCurrentPayload?: boolean, flushType?: string
)
Example
if (!session.data.myCounter) {
session.data.myCounter = 0;
}
session.data.myCounter += 1;
await session.push();
/*
## 1 ##
OUTPUT:
payload -> { myCounter: 1 }
meta -> {
count: 1,
firstEvent: '2022-06-10T08:57:06.242Z',
lastEvent: '2022-06-10T08:57:06.242Z'
}
SESSION in store: {
data: {
myCounter: 1,
}
meta: {
count: 1,
firstEvent: '2022-06-10T08:57:06.242Z',
lastEvent: '2022-06-10T08:57:06.242Z'
}
}
## 2 ##
OUTPUT:
payload -> { myCounter: 2 }
meta -> {
count: 2,
firstEvent: '2022-06-10T08:57:06.242Z',
lastEvent: '2022-06-10T08:57:06.553Z'
}
SESSION in store: {
data: {
myCounter: 2,
}
meta: {
count: 2,
firstEvent: '2022-06-10T08:57:06.242Z',
lastEvent: '2022-06-10T08:57:06.553Z'
}
}
*\
delete : Deletes the current session. You will not be able to perform any operation on the session object after it has been deleted.
Syntax
await session.delete();
// Will not push!
await session.push();
// Will not be saved!
session.data.myProperty = 1337;
flush(excludeCurrentPayload?: boolean) : Flushes the data and deletes the session. You will not be able to perform any operation on the session object after it has been deleted.
Note!Flush does not happen automatically, you have to add it in your JavaScript configuration. It is recommended to keep flush in the On Timeout block. If you have a batch stream, On Timeout is triggered for sessions that have timed out when the stream is executed. If you have a real-time stream, the On Timeout is triggered for sessions that have timed out every 60 seconds.
If excludeCurrentPayload is set as true , the flushed session will exclude the current input as part of the . excludeCurrentPayload is set to false by default. The flushType argument defines the flushType property for the and is set to flush by default. Syntax
await session.flush(
excludeCurrentPayload?: boolean, flushType?: string
)
clear(): Clears the session but it is not deleted and can still be used and viewed in the Aggregation Inspector.
setTimeout : A timeout can be set to trigger delayed actions. Setting a timeout on a session sets or updates the current timeout in milliseconds. The value can either be in UTC date format or string format.
Example
session.setTimeout(new Date('2022-06-10T08:57:06.242Z'));
const moment = require('moment');
session.setTimeout(moment().add(1, 'days').toDate());
|