These docs are for v2.0. Click to read the latest docs for v3.3.

Cloud Code Jobs

Jobs are tasks executed once every hour or once a day. You can use the jobs to generate your own kpis and analytics aggregating the data from the events.

Each job must contain a function job(params, callback)

Where:

params is an empty object for now.

callback is a function to be called when the job ends and can contain a parameter string error if the trigger needs to report an error.

The execution of a job is limited to 5 minutes. If the execution takes longer, the server will end the job.

The common use of jobs is to get the data from the events, process it and send to a kpi. But any of the available services can be used at any time.

Average Temperature

The following code takes all events with the type temperature and separates them in two groups, with temperature higher than 0 and lower than 0. Then computes the average and stores to a kpi called avg-temperature:

function job(params, callback){
  analytics.events.getValuesByName('temperature', function(error, data){
    if(error) return callback(error)
    var high0 = data.filter(function(val){return val > 0}).avg()
    var low0 = data.filter(function(val){return val < 0}).avg()
    analytics.kpis.create('avg-temperature',{high:high0,low:low0})
    callback()
  })
}

Top 10 Scores

Sort the data and get the top ten values.

function job(params, callback){
  analytics.events.getValuesByName('score', function(error, data){
    if(error) return callback(error)
    var top10 = data.sort(function(a, b){return b-a}).take(10).collect()
    analytics.kpis.create('top-10', top10)
    callback()
  })
}

Percentiles

Splits the data in 10 parts and gets the median of each one i.e. the values in 5%, 15%, 25%, ... , 95%.

function job(params, callback){
  analytics.events.getByName('noise', function(error, data){
    if(error) return callback(error)
    var noisePercentiles = data.sort().percentiles(10)
    analytics.kpis.create('noise-percentiles',noisePercentiles)
    callback()
  })
}

Square of Sum of Squares

function job(params, callback){
  analytics.events.getValuesByName('myevent', function(error, data){
    if(error) return callback(error)
    var res = Math.sqrt(data.map(function(val){return val*val}).sum())
    analytics.kpis.create('my-kpi',res)
    callback()
  })
}

Get all the things of a product

function job(params, callback){
  thethingsAPI.getProductThings(function(err, things) {
     async.each(things, function(t, cb) {
        console.log('thingToken: ', t.thingToken);
      },

      function(err) {
      callback();
    });
  });
}