Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
To use JavaScript in Usage Engine, there are some mandatory variables and APIs that you require.
Variables/Functions
Description
Examples
Variables/Functions
Description
Examples
audit
To control the custom user audit function. The supported types are:
count: Increments the string key value by 1
Syntax
audit.count(key);
sum: Sums up the total amount
Syntax
audit.sum(key,value);
set: Sets an input value. Data entry using the set function can be used with any data type. The output of the sum function will show the accumulated amount.
Syntax
audit.set(key,value);
Example - audit.count
audit.count('Tracking-Records-Changes'): - This function can be issued to increase the count of tracked records or a collection of data after an audit review.
Example - audit.sum
audit.sum('KEY_NAME', payload.fieldName): - This function is useful when presenting amounts of data that is to be saved or forwarded to a destination source.
Example - audit.set
audit.set('Collection 1', JSON.stringify(object)): – This audit function can be used to enter any string value and save it to a specified key.
log
An object that contains logging functionality for the severity levels: info, warning, and error.
Large log messages are truncated, for example, messages that are greater than 100000 characters.
The object takes one or more arguments and concatenates them to a single string.
sprintf/printf placeholders are supported in the string for digits, json and strings.
If you have more arguments than placeholders, the remaining arguments are concatenated at the end of the string in the order that they are provided.
Note! Currently, the console object in JavaScript, which provides access to the browser's debugging console, is disabled.
Example - log variable
log.info('This is %s object: %j, it contains %d greeting phrases',"my", { greeting: 'hello', message: 'world' }, 1);
This results in the message:"This is my object: {\"greeting\": \"hello\", \"message\": \"world\"}, it contains 1 greeting phrases".
payload
The payload variable contains all the information of the current record/object.
Example - payload variable
payload.foo = "bar"; - Sets the property foo in the payload to the string "bar"
delete payload.foo; - Deletes the property foo from the payload
payload.baz = { a: 1, b: 2, c: 2 }; - Sets the property baz in the payload to an object
meta
The meta variable provides generic information (metadata) about a payload (a record). It summarizes basic information about data, making it easier to find and work with specific instances of data.
You can also use the meta variable to capture validation errors for functions.
The following metadata is accessible using the Script Flush tab:
lastCall: A boolean flag set to true for the final transaction. lastCall can be used to create a summary or audit for the processed data at the final transaction. To initiate action(s) triggered by the recent transaction:
if (meta.lastCall)
{
<action(s) needed>
}
Hint! You can add meta properties in a script function by assigning values to the meta variable like this: meta.myField = "Hello World". This will then be enriched in the meta field downstream.
Example - Using S3 Forwarder
If you choose to add properties to meta, you can do so in the Script function and refer it to a different function, for example, Amazon S3 forwarder.
In the Script function,
const moment = require("moment");
if (!meta.simDate)
{
meta.simDate = moment(new Date()).format('YYYYMMDD');
}
In the Amazon S3 forwarder, add this in the Filename field: passages-${meta.simDate}.csv
To capture validation error, use the following syntax:
{"0":
{
message: "value must be > 100",path: "value"
}
}
where message: states the message provided and path: states the current value.
push(<event>)
To send data downstream. You need to invoke this function using await syntax.
To send data to the next function(s) in the stream.
Example - push variable
await push(data);
state
This keeps the state across records. The state is only available during one stream execution. The information stored in the state variable is not persisted between executions and is cleared at the end of each stream execution.
The state variable contains two scopes:
node - To save the state locally for this function.
stream - To save the state globally for the stream.
state.node - This state remains across one stream execution and is only available in this function.
const foo = state.node.foo; - Retrieves the variable foo from function
delete state.node.foo; - Removes the variable foo from function
Example - stream variable
state.stream - This state is shared only within the Script functions in a stream.
const foo = state.stream.foo; - Retrieves the variable foo from the stream
delete state.stream.foo; - Removes the variable foo from the stream
store
The following three operations function as key-value stores. The key must be a string. The value can be of any type, for example, string, number, date, JSON object, or array.
You need to invoke this function using await syntax:
get: To get the value
set: To set the value of a key
Syntax
await store.set("<key>", <value>);
And, to set the duration (in seconds) of how long the key is stored.
Syntax
await store.set("<key>", <value>,{ttl:<storage duration in number of seconds>});
Note! Sets the value for a key for the defined duration. The TTL parameter is optional, and the default value is 30 days, that is, 2,592,000 seconds. A key can be stored for a maximum of 60 days (5,184,000 seconds), however, this duration gets extended every time store.set is called within these 60 days.
Note! If a value exceeding the maximum limit is entered, the value is automatically set to the designed maximum.
del - To delete the value
Syntax
await store.del(<key>)
Example - store variable
await store.get("foo") - Returns the value for the key "foo".
Example - await store
await store.set("foo", "hello"); - Sets the value for the key "foo" to the string "hello". The value is stored for 30 days, which is the default time for store.set.
Example - await store tel
await store.set("foo", "hello", {ttl:600000}); - Sets the value for the key "foo" to the string "hello". The ttl for this value is 600000 seconds.
sharedStore
With shared persistent storage, streams can access the same data. For example, one stream writes data to the shared persistent storage that can be used by one or more streams. The key must be a string. Value can be of any type, for example, string, number, date, JSON object, and array. See https://infozone.atlassian.net/wiki/spaces/DAZ/pages/586678318 for more information.
Note! Streams sharing data must belong to the same solution.
You must use the await syntax for the function:
sharedStore.get - To get a value.
Syntax
await sharedStore.get("<key>");
sharedStore.set - To set a value of a key and the duration (in seconds) of how long the key is stored.
Syntax
await sharedStore.set("<key>", <value>);
And, to set the duration (in seconds) of how long the key is stored.
Syntax
await sharedStore.set("<key>", <value>,{ttl:<storage duration in number of seconds>});
Note! If a value exceeding the maximum limit is entered, the value is automatically set to the designed maximum.
sharedStore.del - To delete a value.
Syntax
await sharedStore.del("<key>");
Example - shared storage
await sharedStore.get("foo") - Returns the value from the shared persistent storage for the key foo.
await sharedStore.set("Team", "hello"); - Sets the value for key "Team" to the string "hello". The ttl for this value is set to default, 2592000 seconds.
const getVal = await sharedStore.get("Team");
await sharedStore.del("Team");
await sharedStore.set("Team",{ foo: 42 }, {ttl: 6000}); - Sets the value for key "Team" to the object { foo: 42 }. The ttl for this value is 6000 seconds.
// This script simulates daily record for number of games downloaded by a user
// Write the input record from previous function into log
log.info("Receive data: " + JSON.stringify(payload));
// Create sample daily record
var usageEvent = {
eventId: payload.value,
userId: "USER-1001",
downloadedGame: 2 * payload.value,
dateTime: simDate(),
}
// Send the record to next function
await push(usageEvent)
// Simulate different date for each record
function simDate() {
var moment = require("moment");
var date = moment().add(payload.value, 'days');
return date.format('YYYY-MM-DD');
}