Example - Performing analysis with Pylon

How to perform Pylon API functions, such as creating recordings and performing analysis.

Require the DataSift library - choose one of these:

var DataSift = require('datasift-node'); // When running from NPM package
var DataSift = require('../lib/datasift'); // When running within datasift-node repository

Create a DataSift client object - insert your API credentials:

var ds = new DataSift('YOUR_USERNAME', 'YOUR_IDENTITY_APIKEY');

Variable to store the hash for the recording:

var hash;

The CSDL we’ll use for the recording:

var csdl = 'fb.content contains_any "BMW, Mercedes, Cadillac"';

Declare Utility Methods

Gets a list of all the recordings currently in your account:

function getRecordings() {
	ds.pylon.get(function(err, response) {
		if(err)
			console.log(err);
		else
		{
			console.log("Current recordings: \n" + JSON.stringify(response));
			validate();
		}
	});
}

Validates the CSDL we’ll use for the recording:

function validate() {
	ds.pylon.validate({ "csdl": csdl },
		function(err, response) {
			if (err) 
				console.log(err);
			else
			{
				console.log("CSDL is valid");
				compile();
			}
		});
}

Compiles the CSDL, to give a hash for the recording:

function compile() {
	ds.pylon.compile({ "csdl": csdl },
		function(err, response) {
			if (err) 
				console.log(err);
			else
			{
				hash = response.hash;
				console.log("Filter hash: " + response.hash);
				start();
			}
		});
}

Starts the recording:

function start() {
	ds.pylon.start({ "hash": hash, "name": "Example recording" },
		function(err, response) {
			if (err) 
				console.log(err);
			else
			{
				console.log("Recording started successfully");
				getRecording();
			}
		});
}

Gets details for the recording, once it’s created:

function getRecording() {
	ds.pylon.get({ "hash": hash }, function(err, response) {
		if(err)
			console.log(err);
		else
		{
			listId = response.id;
			console.log("Current recording: \n" + JSON.stringify(response));
			analyze();
		}
	});
}

Performs a basic analysis on the recording data:

function analyze() {

	var parameters = {
		   "analysis_type": "freqDist",
		   "parameters": {
		     "threshold": 5,
		     "target": "fb.author.age"
		   }
		 };

	ds.pylon.analyze({ "hash": hash, 
			"parameters": parameters
	 }, function(err, response) {
		if(err)
			console.log(err);
		else
		{
			listId = response.id;
			console.log("Analysis result: \n" + JSON.stringify(response));
			analyzeWithFilter();
		}
	});
}

Performs analysis, but with the addition of an analysis filter:

function analyzeWithFilter() {

	var parameters = {
		   "analysis_type": "freqDist",
		   "parameters": {
		     "threshold": 5,
		     "target": "fb.author.age"
		   }
		 };

	ds.pylon.analyze({ "hash": hash, 
			"filter": "fb.author.gender == \"male\"",
			"parameters": parameters
	 }, function(err, response) {
		if(err)
			console.log(err);
		else
		{
			listId = response.id;
			console.log("Analysis with filter result: \n" + JSON.stringify(response));
			analyzeWithNesting();
		}
	});
}

Performs analysis, but this time with a nested query:

function analyzeWithNesting() {

	var parameters = {
                "analysis_type": "freqDist",
                "parameters": {
                    "threshold": 3,
                    "target": "fb.author.gender"
                },
                "child": {
                    "analysis_type": "freqDist",
                    "parameters": {
                        "threshold": 3,
                        "target": "fb.author.age"
                    }
                }
            };

	ds.pylon.analyze({ "hash": hash, 
			"parameters": parameters
	 }, function(err, response) {
		if(err)
			console.log(err);
		else
		{
			listId = response.id;
			console.log("Analysis with nesting result: \n" + JSON.stringify(response));
			stop();
		}
	});
}

Stops the recording running:

function stop() {
	ds.pylon.stop({ "hash": hash },
		function(err, response) {
			if (err) 
				console.log(err);
			else
			{
				console.log("Recording stopped successfully");
			}
		});
}

Initiate Process

Finally we start the process creating a list. This will lead to:

getRecordings();
h