Analysis¶
It’s possible to run analysis scripts on your computer, or inside TagoIO server. In the follow pages, you will be instructed on how to setup an analysis on your computer, use our services, and manage any data from Tago.
If you want to get instructions about how to upload your script or how to use third-party packages inside our server, take a look at admin analysis documentation
Setting Up Analysis¶
Through analysis, it is possible to insert any calculation and manage your data from TagoIO in any way you want. We provide some services, such as SMS and email, but you are free to use any third party packages that you need.
To setup an analysis, you first need a analysis token. That can be retrieved from the admin analysis section..
'use strict';
const Analysis = require('tago/analysis');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
console.log('my context:', context);
console.log('my scope:', scope);
//Do anything you want here
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
context¶
As you can setup some predefined parameters in your analysis, it’s possible to get these value from the context variable defined in the admin. It is a object, and it comes with follow properties:
PROPERTY VALUE environment All environment variables token Token of the analysis .log(/msg/) Print a message to the admin console
scope¶
Every time an action triggers a script, the variable scope will be generated. This scope will bring all others variables generated at the same time by the same event. For example, if you submit a form, together with the variable that the script is reading, the scope will return a list of all values/variable input in that form. This allows you to manipulate data in real time, and more easily the new values inserted in your bucket.
Runtime Timeout¶
TagoIO Analysis has a mechanism that prevents scripts from being locked in their executions by applying a timeout of 30 seconds. It means that if a script takes more than 30 seconds to be completed, TagoIO will abort it, and the script will not be completed.
This limitation doesn’t apply when running the analyze from your own machine. Check the information below to learn how to run scripts from an external server (e.g. from your own computer).
Running in your machine¶
You always have the option to run your script from your own machine or from TagoIO server without any technical difference. When running the script from your machine, you will need to install all the packages used by your analysis by using the command npm install mypackage.
Be sure to set your analysis configuration with the option to run the script from “external”. And finally, get the analysis token from the same configuration screen, and put it on the second parameter when calling new Analysis. Check out this example:
module.exports = new Analysis(myanalysis, ‘c89f0d50-38e2-11e6-966e-b94d760acc7d’);
Tago-Builder and Using Another Packages¶
When you are programming, it can be useful to use another packages inside your code; Or you may want to organize your project using require and subfoulders.
Also, TagoIO only accepts one single .js file when uploading your script to our servers. ago provides a builder CLI that can build your entire project and generate a single .js file with the whole code. You can access the repository clicking here
To use our Tago-Builder, follow the following steps:
- Type in your terminal `npm install -g tago-builder`
- Wait it for the installation to be completed
- Type in your terminal `tago-builder ‘my script’.js ‘new name’.tago.js (the last parameter is optional).
- Upload the generated ‘my script’.tago.js file to Tago.
If everything is okay, a new file called ‘my script’.tago.js will be generated. Now you can upload this file to TagoIO!
Services¶
We provide some functions that can greatly help your application. When creating a analysis, you are can use TagoIO services on your own, just make sure you understand the policies and cost associate with the usage.
When setting up a service, you need to pass an analysis-token. For convenience, the context returns a property token that you can use to setup a service object.
'use strict';
const Analysis = require('tago/analysis');
const Services = require('tago/services');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
//Setting up a SMS service
const sms = new Services(context.token).sms;
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
sms¶
You can configure the system to send SMS directly from your analysis to yourself or your customers. Another option is to use the Actions to send SMS.
Some costs may occur when using the SMS service, which varies based on the country of operation. Check pricing, terms of use, and your plan before using the SMS service.
.send¶
Whenever you need to send a sms, use .send function.
'use strict';
const Analysis = require('tago/analysis');
const Services = require('tago/services');
//Main function to be executed when analysis are called
function myanalysis(context, scope) {
const sms = new Services(context.token).sms;
const to = '2693856214';
const message = 'Hi! This is a sms example sent from TagoIO. \nWith a breakline in the sms message.';
sms.send(to, message).then(console.log).catch(console.log);
//Print "Sending";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
email¶
Email service allows you to send e-mail through your analysis. Cost may occur when using the e-mail service.
.send¶
Whenever you need to send an email, use .send function.
'use strict';
const Analysis = require('tago/analysis');
const Services = require('tago/services');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
const email = new Services(context.token).email;
const to = 'myuser@gmail.com';
const subject = 'E-mail example';
const message = 'Hi! This is an email example. \nWith a breakline in the email message.';
const from = 'me@gmail.com';
const attachment = {
archive: 'This is a txt archive',
filename: 'document.txt'
};
email.send(to, subject, message, from, attachment).then(console.log).catch(console.log);
//Print "Sending";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
MQTT¶
This option gives you a lot of flexibility to interpret any kind of data depending on your application. You can send any data format with any content to this topic, your data will go directly to your Analysis inside the scope on the first position of the array. The data will not be stored automatically, your script need to take care of it.
You can read more about MQTT on TagoIO in our MQTT documentation
.send¶
Use this topic when you want to send a payload data in any format to be first parsed by a specific script.
'use strict';
const Analysis = require('tago/analysis');
const Services = require('tago/services');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
const MQTT = new Services(context.token).MQTT;
const topic = 'my topic';
const message = 'new message';
MQTT.publish(topic, message).then(console.log).catch(console.log);
//Print "Sending";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
Notification to myself¶
Sometimes you may want to send an alert to the account through notification system. You can do it in three ways: pointing to a dashboard, to a bucket or just a notification to the account itself.
When pointing to a dashboard or a bucket, the account owner and anyone he shared the dashboard/bucket will receive the notification.
.send¶
Use this topic to send a notification.
'use strict';
const Analysis = require('tago/analysis');
const Services = require('tago/services');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
const Notification = new Services(context.token).Notification;
const title = 'my title';
const message = 'new message';
const ref_id = '5915e4a302a0a7002f2a0960'; //bucket id
Notification.send(title, message, ref_id).then(console.log).catch(console.log);
//Print "Notification sent";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
Utils¶
This library provides some helping functions commonly used while writing analysis code. Using them can be time saving most times.
.env_to_obj¶
Convert the Environment Variables from array to an object variable, making it easier to access through methods. It is not compatible with Environment Variables using same keys.
'use strict';
const Analysis = require('tago/analysis');
const Utils = require('tago/utils');
//Main function to be executed when the analysis are called
function myanalysis(context, scope) {
const environment_variables = Utils.env_to_obj(context.environment);
context.log(environment_variables);
//Print "{ key: value }";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');
.getTokenByName¶
Easy method to get tokens from a device. If a token name is not provided, returns the frist token of the device.
'use strict';
const Analysis = require('tago/analysis');
const Account = require('tago/account');
const Utils = require('tago/utils');
//Main function to be executed when the analysis are called
async function myanalysis(context, scope) {
const tago_account = new Account('a13c0c50-38o2-11u6-966n-c34d980acc88')
const device_id = '5cdecbbf3474b7001deca551';
const token = await Utils.getTokenByName(tago_account, device_id);
context.log(token);
//Print device token "'d44e8650-3ae2-11u6-126n-c34dd80fc91'";
}
module.exports = new Analysis(myanalysis, 'c89f0d50-38e2-11e6-966e-b94d760acc7d');