-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphSlicer.js
70 lines (55 loc) · 2.33 KB
/
GraphSlicer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module.exports = function (RED) {
var util = require('util');
function DCloudGraphSlicer(cfg) {
"use strict";
RED.nodes.createNode(this, cfg);
var node = this;
node.config = cfg;
var g = {};
//Handle input
this.on('input', function (msg) {
//this.log('input:' + util.inspect(msg)+ ' typeof:' + typeof(msg));
try {
var data = msg.payload.data;
//this.log('data identified:' + util.inspect(data));
var lnk_varid_name = {}; //create inverse lookup having varids (when looping through data)
for (var ptr = 0; ptr < data.variablesRequested.length; ptr++) {
var v = data.variablesRequested[ptr];
g[v.name] = [];
for (var ptrV = 0; ptrV < v.varids.length; ptrV++) {
lnk_varid_name[v.varids[ptrV].varid] = v.name;
}
}
//console.log('lnk:' + util.inspect(lnk_varid_name));
//loop through data
for (var ptr = 0; ptr < data.data.length; ptr++) {
var d = data.data[ptr];
var ts = Date.parse(data.sessions[d.sessionid].tsB);
var val = d.val;
var name = lnk_varid_name[d.varid];
//console.log('name:' + name + ' val:' + val + ' varid:' + d.varid);
if (g.hasOwnProperty(name) == true) {
g[name].push([ts, val]);
}
}
//filter - future, remove overloading data :-)
//build resulting graph arrays
var msgResult = {};
msgResult.topic = 'GraphSlicer';
msgResult.payload = [];
Object.keys(g).forEach(function (n) {
var p = {};
p.key = n;
p.values = g[n];
msgResult.payload.push(p);
});
//console.log('msgResult:' + util.inspect(msgResult));
node.send(msgResult);
}
catch (err) {
console.error(err);
}
});
}
RED.nodes.registerType("DCloud-GraphSlicer", DCloudGraphSlicer);
}