Instantly share code, notes, and snippets.

@gekola /custDate.js Secret
Last active Sep 6, 2016

Embed
What would you like to do?
Predix Analytics Node.js Client App
const BASE_DATE = +new Date('2014-01-01T00:00:00.000Z');
module.exports = {
intToDate: function(int, base = BASE_DATE) {
const date = new Date(int*1000 + base);
let parts = date.toUTCString().slice(5, 25).split(' ');
return `${parts[0]}-${parts[1]}-${parts[2]} ${parts[3]}`;
},
dateToInt: function(str, base = BASE_DATE) {
const date = new Date(str + 'Z');
return (date - base)/1000;
}
};
const Server = require('socket.io');
const request = require('request');
const { intToDate, dateToInt } = require('./custDate');
const ANALYTICS_URI = process.env.ANALYTICS_URI;
const ANALYTICS_ZONE = process.env.ANALYTICS_ZONE;
const PORT = process.env.PORT || 8090;
const UAA_BASE_URL = process.env.UAA_BASE_URL;
const UAA_CLIENT = process.env.UAA_CLIENT;
const UAA_SECRET = process.env.UAA_SECRET;
const Y_PADDING = 3;
const AUTH_URL = `${UAA_BASE_URL}/oauth/token?grant_type=client_credentials&client_id=${UAA_CLIENT}&client_secret=${UAA_SECRET}`;
const server = new Server().attach(PORT);
server.on('connection', (socket) => {
socket.on('analyze', (inputData, callback) => {
let analyticsData = {Time: [], Value: []};
inputData.forEach((el) => {
analyticsData.Time.push(intToDate(el._id));
analyticsData.Value.push(el.value);
});
request
.get(AUTH_URL, function(err, response, body) {
if (err) {
console.error('Error: ', err);
callback({success: false, message: err});
} else {
let token = JSON.parse(body);
if (token && token.access_token) {
let opts = {
method: 'POST',
uri: ANALYTICS_URI,
body: JSON.stringify(analyticsData),
headers: {
'Authorization': `Bearer ${token.access_token}`,
'Predix-Zone-Id': ANALYTICS_ZONE,
'Content-Type': 'application/json'
}
};
request(opts, function(err, response, body) {
if (err) {
console.error('Error: ', err);
callback({success: false, message: err});
} else {
const respData = JSON.parse(body);
if (respData && respData.result) {
const result = JSON.parse(respData.result);
let borders = [];
if (result.Beg) {
result.Beg.forEach((btime, i) => {
const x = dateToInt(btime);
const x2 = dateToInt(result.End[i]);
let y, y2;
data.forEach((el) => {
if (el._id >= x && el._id <= x2) {
if (!y || y < el.value)
y = el.value;
if (!y2 || y2 > el.value)
y2 = el.value;
}
});
y += Y_PADDING;
y2 -= Y_PADDING;
borders.push({x, x2, y: y, y2});
});
}
callback({success: true, result: borders});
} else {
callback({success: false, message: respData.message});
}
}
});
} else {
callback({success: false, message: 'No token from UAA'});
}
}
})
.auth(UAA_CLIENT, UAA_SECRET);
});
});
{
"name": "app-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"request": "^2.74.0",
"socket.io": "^1.4.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment