Using Events from Other Streams

When implementing Cloud Actions, it's often useful to programmatically find specific Events from Streams such as individual Events, or multiple Events that meet certain conditions, so that you can base your logic around the information contained in those Events. You can invoke Octave's cloud APIs from within your Cloud Action's code, to find and access Events from Streams.

Performing a Simple Query

You can run a simple query to find specific events using the Octave.Event.* methods such as find etc.

The following steps illustrate this process:

  1. Create a Cloud Action.
  2. Add code to the Cloud Action that invokes the Octave.Event.find method. For example, the following code finds events from stream s5b7c2258c4eaa25486be2ed1:

Request:

var events = Octave.Event.find('s5b7c2258c4eaa25486be2ed1')

Response:

[
   {
      elems:{
         x:1
      },
      creationDate:1534865286261,
      ...
   },
   {
      elems:{
         x:1
      },
      ...
   }
]

Querying with a Filter

You can pass in options to the Octave.Event.* methods to run the query with a filter.

For additional information about the fields of the options object, see Analyzing Events.

The following steps illustrate this process:

  1. Create a Cloud Action.
  2. Add code to the Cloud Action's logic that invokes the Octave.Event.find method. For example, the following code finds the most recent event where x is greater than 1:

Request:

function(event) {
  var event = Octave.Event.findOne('s5b7c2258c4eaa25486be2ed1', {
   filter: 'x > 1',
    sort: 'creationDate',
  })
}

Response:

{
   elems:{
      x:2
   },
   creationDate:1534865286261,
   ...
}

Analyzing Events Using Aggregation

Events can be analyzed using the aggregate method and passing in options to define the aggregation. For example, the following code filters events that have a CPU temperature above 50 degrees. It then groups the results by the month in which they were created, and outputs information about the temperature values:

var myAggregationResults = Octave.Event.aggregate(myStreamId, {
  filter: 'EXISTS cpu_temp',
  rules: { x: 'cpu_temp > 50' },
  groupBy: ['$month'],
  output: [
    '$avg:cpu_temp',
    '$min:cpu_temp',
    '$max:cpu_temp',
    '$avg:x',
    '$count',
  ],
  sorts: ['$avg:x:desc', '$avg:cpu_temp:asc'],
})