node-red-viseo-helper
Advanced tools
Comparing version 0.2.0-alpha.3 to 0.2.0-alpha.6
@@ -0,0 +0,0 @@ # Contributor Covenant Code of Conduct |
@@ -0,0 +0,0 @@ # Contributing to the Project |
129
index.js
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const vm = require('vm'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const vm = require('vm'); | ||
const rp = require('request-promise'); | ||
const EventEmitter = require('events'); | ||
@@ -110,2 +111,33 @@ | ||
const setContextValue = exports.setContextValue = (RED, node, data, str, value, type) => { | ||
try { | ||
if (type === "msg") { | ||
RED.util.setMessageProperty(data,str,value); | ||
return true; | ||
} | ||
else if (type === 'global' || type === 'flow') { | ||
let contextKey = RED.util.parseContextStore(str); | ||
let target = node.context()[type]; | ||
target.set(contextKey.key, value, contextKey.store, null); | ||
return true; | ||
} | ||
return false; | ||
} | ||
catch(err) { | ||
error(err); | ||
return false; | ||
} | ||
} | ||
const getContextValue = exports.getContextValue = (RED, node, data, str, type) => { | ||
try { | ||
let value = RED.util.evaluateNodeProperty(str, type, node, data, null); | ||
return value; | ||
} | ||
catch(err) { | ||
error(err); | ||
return null; | ||
} | ||
} | ||
const resolve = exports.resolve = (str, obj, def) => { | ||
@@ -198,2 +230,93 @@ if (str === undefined) return str; | ||
callback(data); | ||
} | ||
// ------------------------------------------ | ||
// TRACKING | ||
// ------------------------------------------ | ||
let activities = {}; | ||
exports.trackActivities = async function trackActivities(node) { | ||
function unauth() { | ||
node.status({fill:"red", shape:"ring", text: 'Missing VISEO Bot Maker key'}); | ||
node.error("Missing VISEO Bot Maker key - Read the documentation.") | ||
return false; | ||
} | ||
let conf = CONFIG || global.CONFIG; | ||
let key = conf.key || ''; | ||
let time = Date.now(); | ||
if (!key) return unauth(); | ||
// First pass : set start time | ||
if (!activities.info) { | ||
activities = { | ||
info : { | ||
start : time, | ||
key : key | ||
} | ||
}; | ||
} | ||
else if (time - activities.info.start > 1000 ){ // 24h : 24 * 3600 * 1000 = 86400000 | ||
try { | ||
node.warn(activities) | ||
await sendActivities(activities); | ||
} catch(err) { | ||
if (err.statusCode === 401) return unauth(); | ||
return true; | ||
} | ||
activities = { | ||
info : { | ||
start : time, | ||
key : key | ||
} | ||
}; | ||
} | ||
else { | ||
// set the last time | ||
activities.info.end = time; | ||
} | ||
if (!activities[node.type]) { | ||
activities[node.type] = { | ||
"type": node.type, | ||
"start": time, | ||
"nodes": [], | ||
"calls": 0 | ||
} | ||
} | ||
if (activities[node.type].nodes.indexOf(node.id) === -1) { | ||
activities[node.type].nodes.push(node.id); | ||
} | ||
activities[node.type].calls += 1; | ||
activities[node.type].end = time; | ||
activities.info.end = time; | ||
return true; | ||
} | ||
async function sendActivities(obj) { | ||
let body = { | ||
info: obj.info, | ||
data: [] | ||
} | ||
for (let key in obj) { | ||
if (key === 'info') continue; | ||
obj[key].nodes = obj[key].nodes.length; | ||
body.data.push(obj[key]); | ||
} | ||
// send data | ||
let request = { | ||
url: "https://nodedatafunction.azurewebsites.net/api/sendActivities?code=rriqcWx/cCsUQ0oxcVqTTaxsXWj5gf2AOCqhSayw8vaGaQKf6SgaQw==", | ||
resolveWithFullResponse: true, | ||
method: "post", | ||
json: true, | ||
body: body | ||
} | ||
console.log(request) | ||
return rp(request); | ||
} |
@@ -0,0 +0,0 @@ const extend = require('extend'); |
{ | ||
"name" : "node-red-viseo-helper", | ||
"version" : "0.2.0-alpha.3", | ||
"version" : "0.2.0-alpha.6", | ||
"description" : "Utility libarary for Node-RED", | ||
@@ -5,0 +5,0 @@ "dependencies" : { |
@@ -0,0 +0,0 @@ # Node-RED |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26878
284