User Defined Scripts

Apply external analytical scripts within a real-time streaming context


Overview

The analytics processor feature set is extended to host external scripts within the Joule processing context. Therefore enabling existing scripting assets to be leveraged within a streaming context.

Currently only Javascript and Python scripts are supported

Key features

  • Script and expression execution

  • Script and method execution

  • Stateful analytics

  • Analytic result memory


Execution models

  • Script and expression

  • Script and function execution

Script and Expression

Use this option when you have pre-existing scripts which you want to leverage within a stream processing context.

analytic:
  script: ./scripts/js/myCustomFunctions.js
  expression: bid - squareRoot(`${ask}` / `${bid}`)
  assign to: new_event_variable

Javascript function

export function squareRoot(num) {
    if (num < 0) return NaN;
    return Math.sqrt(num);
}

The above example would apply the bid and ask event attributes to the expression. Note the current implementation requires the script to be provided using the js extension due to the way the above expression is defined. All other variables are provided as global within the execution context.

Script and Function

This option build upon the above but uses a method which is supplied with a StreamEvent which can further call imported scripts. All other variables are provided as global within the execution context.

analytic:
  script: ./scripts/js/usedDefinedCustomModuleFunctions.mjs
  function: totalCompensationPackage
  assign to: total_compensation

Javascript function

The event is automatically passed as a method parameter per execution call.

export function totalCompensationPackage(event) {
    return event.getValue("salary") +
           event.getValue("bonus") + 
           event.getValue("benefits");
}

This method uses the Javascript modules which are defined using the mjs file extension.


Attributes

The same attributes described in User Defined Analytics are applicable, see documentation.

AttributeDescriptionData Type

script

Path of the script to loaded within the Joule processing context

String

expression

Math expression without an assignment variable. Required if method has not been provided.

String

function

Function to execute within the provided script using a StreamEvent as a parameter

String

Last updated