From c620a0a36656199d2076241a1cd19c9eb8ca4110 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Tue, 12 Feb 2019 15:00:17 +0100 Subject: [PATCH 01/76] Fixed issue in lib/appContext.js --- lib/appContext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/appContext.js b/lib/appContext.js index c662912e..e28a0c19 100644 --- a/lib/appContext.js +++ b/lib/appContext.js @@ -37,8 +37,8 @@ function SetOrionDB(db) { orionDatabase = db; } -function OrionDB(database) { - return orionDatabase.db(database); +function OrionDB(db) { + return orionDatabase.db(db); } /** From b1eb8fa5351e901edae42462ba4918315fffdf32 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:27:34 +0100 Subject: [PATCH 02/76] Fixed issues in lib/db.js --- lib/db.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/db.js b/lib/db.js index 8027ed17..3a4e2eee 100644 --- a/lib/db.js +++ b/lib/db.js @@ -75,8 +75,8 @@ function getDbAux(url, component, callback) { }); checkDbHealthFunc = function checkDbHealth() { - pingAux(db, component, function(err, result) { - logger.debug('ping (%s) %j', component, err || result); + pingAux(db, component, function(error, result) { + logger.debug('ping (%s) %j', component, error || result); }); }; setInterval(checkDbHealthFunc, delay); @@ -97,20 +97,20 @@ function getOrionDb(callback) { } function ensureIndex(collection, fields, callback) { - database.collection(collection, function(err, collection) { - myutils.logErrorIf(err, collection, context); - collection.ensureIndex(fields, {unique: true}, function(err, indexName) { - myutils.logErrorIf(err, 'ensureIndex ' + collection, context); - callback(err, indexName); + database.collection(collection, function(err, collectionVar) { + myutils.logErrorIf(err, collectionVar, context); + collectionVar.ensureIndex(fields, {unique: true}, function(error, indexName) { + myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); + callback(error, indexName); }); }); } function ensureIndexTTL(collection, fields, ttl, callback) { - database.collection(collection, function(err, collection) { - myutils.logErrorIf(err, collection); - collection.ensureIndex(fields, {expireAfterSeconds: ttl}, function(err, indexName) { - myutils.logErrorIf(err, 'ensureIndex ' + collection, context); - callback(err, indexName); + database.collection(collection, function(err, collectionVar) { + myutils.logErrorIf(err, collectionVar); + collectionVar.ensureIndex(fields, {expireAfterSeconds: ttl}, function(error, indexName) { + myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); + callback(error, indexName); }); }); } From d38ffcc9f3f1792c0782149072ddeaa522f40cf8 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:33:54 +0100 Subject: [PATCH 03/76] Fixed issue in lib/models/actions.js --- lib/models/actions.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/models/actions.js b/lib/models/actions.js index 3257d99f..202d6c27 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -241,7 +241,7 @@ function DoAction(event, callback) { } actionStore.Find(service, subservice, ruleName, function(err, actions) { - var localError, + var localErr, queue; if (err) { return callback(err, null); @@ -251,10 +251,10 @@ function DoAction(event, callback) { } // check all axns are right for (var i = 0; i < actions.length; i++) { - localError = validateAction(actions[i]); - if (localError !== null) { - myutils.logErrorIf(localError); - return callback(localError, null); + localErr = validateAction(actions[i]); + if (localErr !== null) { + myutils.logErrorIf(localErr); + return callback(localErr, null); } } From f18f17bdd7caec601cc5d422a6975fc819575a59 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:37:05 +0100 Subject: [PATCH 04/76] Fixed issues in lib/models/executionsStore.js --- lib/models/executionsStore.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/models/executionsStore.js b/lib/models/executionsStore.js index 565ef654..90f349c2 100644 --- a/lib/models/executionsStore.js +++ b/lib/models/executionsStore.js @@ -45,10 +45,10 @@ module.exports = { col.findOne({ '$query': {'name': ruleName, 'subservice': subservice, 'service': service, 'id': id, index: index}, '$orderby': {'lastTime': -1} - }, function(err, data) { - myutils.logErrorIf(err); - if (err) { - return callback(err, null); + }, function(error, data) { + myutils.logErrorIf(error); + if (error) { + return callback(error, null); } return callback(null, (data && data.lastTime && data.lastTime.getTime()) || 0); }); @@ -73,10 +73,10 @@ module.exports = { 'id': id, 'notice': noticeId, 'index': index}, - function(err, data) { - myutils.logErrorIf(err); - if (err) { - return callback(err, null); + function(error, data) { + myutils.logErrorIf(error); + if (error) { + return callback(error, null); } return callback(null, data); }); From fb9cb758bc1c1c718650585a8e5cf8617c5c3693 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:43:04 +0100 Subject: [PATCH 05/76] Fixed issue in lib/models/notices.js --- lib/models/notices.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/models/notices.js b/lib/models/notices.js index f0ff8459..f3f7c107 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -234,12 +234,12 @@ function DoNotice(orionN, callback) { notices.push(notice); } } - async.each(notices, function(notice, cbEach) { + async.each(notices, function(noticeVar, cbEach) { var h = {}; - h[constants.SUBSERVICE_HEADER] = notice.subservice; + h[constants.SUBSERVICE_HEADER] = noticeVar.subservice; myutils.requestHelperWOMetrics('post', { url: config.perseoCore.noticesURL, - json: notice, + json: noticeVar, headers: h }, function(err, data) { @@ -254,7 +254,7 @@ function DoNotice(orionN, callback) { if (config.nextCore && config.nextCore.noticesURL) { myutils.requestHelperWOMetrics('post', { url: config.nextCore.noticesURL, - json: notice, + json: noticeVar, headers: h }, myutils.logErrorIf); } From d9d800dd5277e73adf97e82aabef063401eba5fc Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:46:22 +0100 Subject: [PATCH 06/76] Fixed issue in lib/models/updateAction.js --- lib/models/updateAction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index a5f68152..700ec07a 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -187,8 +187,8 @@ function makeTokenListenerFunc(action, event, callback) { return callback(error); } else { logger.debug('tokenHandlerFunc retrying with', token); - return doRequest(action, event, token, function cbDoReqUpdAxn(error, data) { - callback(error, data); + return doRequest(action, event, token, function cbDoReqUpdAxn(err, data) { + callback(err, data); }); } }; From e66602799346fbc27b4a72d3f9645c22a9f04f41 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 09:48:27 +0100 Subject: [PATCH 07/76] Fixed issue in lib/models/perseo.js --- lib/perseo.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/perseo.js b/lib/perseo.js index 1b865903..b12a7ef8 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -115,15 +115,15 @@ function start(callbackStart) { }, db.orionPing, function(callback) { - var domain = require('domain').create(); - domain.context = {}; - domain.context.op = 'initialRefresh'; - domain.context.comp = constants.COMPONENT_NAME; - domain.context.trans = 'n/a'; - domain.context.corr = domain.context.trans; - domain.context.srv = 'n/a'; - domain.context.subsrv = 'n/a'; - domain.run(function() { + var domainVar = require('domain').create(); + domainVar.context = {}; + domainVar.context.op = 'initialRefresh'; + domainVar.context.comp = constants.COMPONENT_NAME; + domainVar.context.trans = 'n/a'; + domainVar.context.corr = domainVar.context.trans; + domainVar.context.srv = 'n/a'; + domainVar.context.subsrv = 'n/a'; + domainVar.run(function() { rules.Refresh(callback); }); }, From 5f799c92f64cd80b479d2a41eb63c0d5f7a0f6e5 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Wed, 13 Feb 2019 11:10:46 +0100 Subject: [PATCH 08/76] Fixed issue in lib/models/actionsController.js --- lib/routes/actionsController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/routes/actionsController.js b/lib/routes/actionsController.js index fbf717ad..bf1d1965 100644 --- a/lib/routes/actionsController.js +++ b/lib/routes/actionsController.js @@ -129,9 +129,9 @@ function PostAction(req, resp) { setTimeout(function() { nextDomain.run( function() { - for (var p in firedEvent.ev) { - if (firedEvent[p] === undefined) { - firedEvent[p] = firedEvent.ev[p]; + for (var pVar in firedEvent.ev) { + if (firedEvent[pVar] === undefined) { + firedEvent[pVar] = firedEvent.ev[pVar]; } } actions.Do(firedEvent, function(err) { From 24bbb3fe916a9f6ac2caf74e9800f67a8cd992ee Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 14 Feb 2019 12:54:37 +0100 Subject: [PATCH 09/76] Fixed issue #284635 in lib/models/actions.js lib/models/actionsStore.js lib/models/entitiesStore.js lib/models/paths.js lib/models/postAction.js lib/models/rules.js lib/models/visualRules.js lib/myutils.js --- lib/models/actions.js | 3 +++ lib/models/actionsStore.js | 5 +++++ lib/models/entitiesStore.js | 3 +++ lib/models/paths.js | 4 ++++ lib/models/postAction.js | 8 ++++++++ lib/models/rules.js | 3 +++ lib/models/visualRules.js | 6 ++++++ lib/myutils.js | 3 +++ 8 files changed, 35 insertions(+) diff --git a/lib/models/actions.js b/lib/models/actions.js index 202d6c27..47234495 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -84,6 +84,9 @@ function validateAction(axn) { return new errors.IdAsAttribute(JSON.stringify(axnElem.parameters)); } else if (axnElem.parameters.name === 'type') { return new errors.IdAsAttribute(JSON.stringify(axnElem.parameters)); + } else{ + //Added default else clause + return null; } } } diff --git a/lib/models/actionsStore.js b/lib/models/actionsStore.js index 0a4b8ed1..6902cb23 100644 --- a/lib/models/actionsStore.js +++ b/lib/models/actionsStore.js @@ -42,6 +42,11 @@ module.exports = { myutils.logErrorIf(localError); return callback(localError, null); } + else { + //Added default else clause + return callback(null, data.action); + } + return callback(null, data.action); }); diff --git a/lib/models/entitiesStore.js b/lib/models/entitiesStore.js index a743c858..b3d712ab 100644 --- a/lib/models/entitiesStore.js +++ b/lib/models/entitiesStore.js @@ -53,6 +53,9 @@ function findSilentEntities(service, subservice, ruleData, func, callback) { catch (e) { return callback(e, null); } + } else { + //Added default else clause + } if (ruleData.type) { criterion['_id.type'] = ruleData.type; diff --git a/lib/models/paths.js b/lib/models/paths.js index df8bc325..aaa36654 100644 --- a/lib/models/paths.js +++ b/lib/models/paths.js @@ -40,6 +40,10 @@ function validComponent(component) { else if (!componentPattern.test(component)) { err = new errors.InvalidCharacter(component); } + else { + //Added default else clause + return err; + } return err; } function validService(service) { diff --git a/lib/models/postAction.js b/lib/models/postAction.js index 243dc6d2..ac0700d7 100644 --- a/lib/models/postAction.js +++ b/lib/models/postAction.js @@ -42,6 +42,10 @@ function buildPostOptions(action, event) { else if (action.template) { options.text = myutils.expandVar(action.template, event); } + else { + //Added default else clause + return options; + } return options; } @@ -62,6 +66,10 @@ function doIt(action, event, callback) { else if (options.text) { requestOptions.body = options.text; } + else { + //Added default else clause + + } metrics.IncMetrics(event.service, event.subservice, metrics.actionHttpPost); diff --git a/lib/models/rules.js b/lib/models/rules.js index 5e9ec496..c66cb2dd 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -54,6 +54,9 @@ function validRule(rule) { return new errors.InvalidRuleName(rule.name); } else if (!rule.text && !rule.nosignal) { return new errors.EmptyRule(JSON.stringify(rule)); + } else { + //Added default else clause + } if (rule.nosignal) { //Specific checks for no-signal rules err = validNSRule(rule); diff --git a/lib/models/visualRules.js b/lib/models/visualRules.js index ddc35d3e..98659113 100644 --- a/lib/models/visualRules.js +++ b/lib/models/visualRules.js @@ -130,6 +130,9 @@ function vr2rule(cardRule) { } } else if (card.conditionList[0].parameterName === 'type') { type = card.conditionList[0].parameterValue; + } else { + //Added default else clause + } break; case 'OBSERVATION': @@ -141,6 +144,9 @@ function vr2rule(cardRule) { return new errors.IdAsAttribute(''); } else if (card.sensorData.measureName === 'type') { return new errors.TypeAsAttribute(''); + } else { + //Added default else clause + } conditions.push(makeObservationCondition(card.sensorData.measureName, card.conditionList[0].operator, diff --git a/lib/myutils.js b/lib/myutils.js index 7cc12c28..4898c138 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -71,6 +71,9 @@ function expandObject(templateObj, dictionary) { res[expandVar(key, dictionary)] = expandVar(templateObj[key], dictionary); } else if (typeof templateObj[key] === 'object') { res[expandVar(key, dictionary)] = expandObject(templateObj[key], dictionary); + } else { + //Added default else clause + } }); } From 26a081ef7de88de3c59d13cd5de6f1664acb87b0 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 25 Feb 2019 13:45:44 +0100 Subject: [PATCH 10/76] Fixed issue #284625 in lib/routes/actionsController.js --- lib/routes/actionsController.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/routes/actionsController.js b/lib/routes/actionsController.js index bf1d1965..7a965265 100644 --- a/lib/routes/actionsController.js +++ b/lib/routes/actionsController.js @@ -110,7 +110,6 @@ function PostAction(req, resp) { else { delay = config.slaveDelay; } - // TODO: Tricky ... To refactor? var cleanDomain, domainErrorHandler; domainErrorHandler = function(err) { From ea6a05d107a25362d923c0de50731e347d6f7dc8 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Tue, 19 Mar 2019 11:53:37 +0100 Subject: [PATCH 11/76] Merged lib/db.js and lib/routes/actionsController.js from telefonicaid/master --- lib/db.js | 24 +++++++++--------------- lib/routes/actionsController.js | 7 +++---- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/db.js b/lib/db.js index 09288b0a..85d22c2c 100644 --- a/lib/db.js +++ b/lib/db.js @@ -67,19 +67,13 @@ function getDbAux(url, component, callback) { return callback(err, null); } - - // This event is emitted only by Server topology (standalone) - // The driver has given up getting a connection, so we will die (restart perseo usually) - // and re-try from scratch. - // The ReplSet does not emit 'reconnectFailed' - db.serverConfig.on('reconnectFailed', function() { - logger.fatal('too many tries to reconnect to database, dying ...'); - process.exit(-2); - }); - - checkDbHealthFunc = function checkDbHealth() { - pingAux(db, component, function(error, result) { - logger.debug('ping (%s) %j', component, error || result); + // This event is emitted only by Server topology (standalone) + // The driver has given up getting a connection, so we will die (restart perseo usually) + // and re-try from scratch. + // The ReplSet does not emit 'reconnectFailed' + db.serverConfig.on('reconnectFailed', function() { + logger.fatal('too many tries to reconnect to database, dying ...'); + process.exit(-2); }); checkDbHealthFunc = function checkDbHealth() { @@ -108,7 +102,7 @@ function getOrionDb(callback) { function ensureIndex(collection, fields, callback) { database.collection(collection, function(err, collectionVar) { myutils.logErrorIf(err, collectionVar, context); - collectionVar.ensureIndex(fields, {unique: true}, function(error, indexName) { + collectionVar.ensureIndex(fields, { unique: true }, function(error, indexName) { myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); callback(error, indexName); }); @@ -117,7 +111,7 @@ function ensureIndex(collection, fields, callback) { function ensureIndexTTL(collection, fields, ttl, callback) { database.collection(collection, function(err, collectionVar) { myutils.logErrorIf(err, collectionVar); - collectionVar.ensureIndex(fields, {expireAfterSeconds: ttl}, function(error, indexName) { + collectionVar.ensureIndex(fields, { expireAfterSeconds: ttl }, function(error, indexName) { myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); callback(error, indexName); }); diff --git a/lib/routes/actionsController.js b/lib/routes/actionsController.js index 7398e321..3476a2e6 100644 --- a/lib/routes/actionsController.js +++ b/lib/routes/actionsController.js @@ -129,10 +129,9 @@ function PostAction(req, resp) { firedEvent.fiwarePerseoContext = nextDomain.context; setTimeout(function() { nextDomain.run(function() { - for (var pVar in firedEvent.ev) { - if (firedEvent[pVar] === undefined) { - firedEvent[pVar] = firedEvent.ev[pVar]; - } + for (var pVar in firedEvent.ev) { + if (firedEvent[pVar] === undefined) { + firedEvent[pVar] = firedEvent.ev[pVar]; } } actions.Do(firedEvent, function(err) { From 88344c842811275e8093852f5667988a2a1b3fe9 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:34:59 +0100 Subject: [PATCH 12/76] Resolver issues Db.js #292343 --- lib/alarm.js | 18 ++++++++++-------- lib/db.js | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/alarm.js b/lib/alarm.js index a9fe7f09..c55733de 100644 --- a/lib/alarm.js +++ b/lib/alarm.js @@ -29,27 +29,29 @@ var util = require('util'), function raise(alarm, context, message) { var state = alarms[alarm]; - context = (process.domain && process.domain.context) || {}; + var contextTemp = (process.domain && process.domain.context) || {}; + var messageTemp = ''; if (typeof message === 'object' && message) { - message = util.format('%j', message); + messageTemp = util.format('%j', message); } - message = message || ''; + messageTemp = messageTemp || ''; if (state !== true) { // emit ERROR alarm ON - logger.error(context, 'ALARM-ON [' + alarm + '] ' + message); + logger.error(contextTemp, 'ALARM-ON [' + alarm + '] ' + messageTemp); } alarms[alarm] = true; } function release(alarm, context, message) { var state = alarms[alarm]; - context = (process.domain && process.domain.context) || {}; + var contextTemp = (process.domain && process.domain.context) || {}; + var messageTemp = ''; if (typeof message === 'object' && message) { - message = util.format('%j', message); + messageTemp = util.format('%j', message); } - message = message || ''; + messageTemp = messageTemp || ''; if (state === true) { // emit ERROR alarm OFF - logger.error(context, 'ALARM-OFF [' + alarm + '] ' + message); + logger.error(contextTemp, 'ALARM-OFF [' + alarm + '] ' + messageTemp); } alarms[alarm] = false; } diff --git a/lib/db.js b/lib/db.js index 85d22c2c..0675fb4b 100644 --- a/lib/db.js +++ b/lib/db.js @@ -77,8 +77,8 @@ function getDbAux(url, component, callback) { }); checkDbHealthFunc = function checkDbHealth() { - pingAux(db, component, function(err, result) { - logger.debug('ping (%s) %j', component, err || result); + pingAux(db, component, function(err2, result) { + logger.debug('ping (%s) %j', component, err2 || result); }); }; setInterval(checkDbHealthFunc, delay); From 2e1025f20a44fdfa0641ce67a6e13719c9cecba8 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:35:53 +0100 Subject: [PATCH 13/76] Resolver issues actions.js #292346 --- lib/models/actions.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/models/actions.js b/lib/models/actions.js index dbdbdd9a..78719692 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -81,9 +81,9 @@ function validateAction(axn) { return new errors.IdAsAttribute(JSON.stringify(axnElem.parameters)); } else if (axnElem.parameters.name === 'type') { return new errors.IdAsAttribute(JSON.stringify(axnElem.parameters)); - } else{ - //Added default else clause - return null; + } else { + //Added default else clause + return null; } } } @@ -229,16 +229,17 @@ function DoAction(event, callback) { } actionStore.Find(service, subservice, ruleName, function(err, actions) { - var localError, queue; + var queue; + var arrayAction = actions; if (err) { return callback(err, null); } - if (!util.isArray(actions)) { - actions = [actions]; + if (!util.isArray(arrayAction)) { + arrayAction = [arrayAction]; } // check all axns are right - for (var i = 0; i < actions.length; i++) { - localErr = validateAction(actions[i]); + for (var i = 0; i < arrayAction.length; i++) { + localErr = validateAction(arrayAction[i]); if (localErr !== null) { myutils.logErrorIf(localErr); return callback(localErr, null); @@ -246,10 +247,10 @@ function DoAction(event, callback) { } queue = getInProcessArray(service, subservice, ruleName, id, type); - for (i = 0; i < actions.length; i++) { - actions[i].index = i; + for (i = 0; i < arrayAction.length; i++) { + arrayAction[i].index = i; queue.push({ - action: actions[i], + action: arrayAction[i], event: event, context: event.fiwarePerseoContext }); From e75d07d22dcbf5cece6ec5c371b0ed31821477dc Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:36:56 +0100 Subject: [PATCH 14/76] Resolve issues actionsStore.js #292348 --- lib/models/actionsStore.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/models/actionsStore.js b/lib/models/actionsStore.js index 00cbdee9..680a169b 100644 --- a/lib/models/actionsStore.js +++ b/lib/models/actionsStore.js @@ -40,13 +40,10 @@ module.exports = { localError = new errors.NotFoundAction(data); myutils.logErrorIf(localError); return callback(localError, null); + } else { + //Added default else clause + return callback(null, data.action); } - else { - //Added default else clause - return callback(null, data.action); - } - - return callback(null, data.action); }); } }; From 2cd53b5abb83dd88b0b119ebaf1a5f40b4b5c472 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:37:56 +0100 Subject: [PATCH 15/76] Resolve issues executionsStore.js --- lib/models/executionsStore.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/models/executionsStore.js b/lib/models/executionsStore.js index 8dd9fdd2..61685e5d 100644 --- a/lib/models/executionsStore.js +++ b/lib/models/executionsStore.js @@ -47,10 +47,10 @@ module.exports = { $query: { name: ruleName, subservice: subservice, service: service, id: id, index: index }, $orderby: { lastTime: -1 } }, - function(err, data) { - myutils.logErrorIf(err); + function(error2, data) { + myutils.logErrorIf(error2); if (err) { - return callback(err, null); + return callback(error2, null); } return callback(null, (data && data.lastTime && data.lastTime.getTime()) || 0); } @@ -79,10 +79,10 @@ module.exports = { notice: noticeId, index: index }, - function(err, data) { - myutils.logErrorIf(err); - if (err) { - return callback(err, null); + function(error2, data) { + myutils.logErrorIf(error2); + if (error2) { + return callback(error2, null); } return callback(null, data); } From a3c1cc1a271b603cd04e738595d5aa535f83aa91 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:38:59 +0100 Subject: [PATCH 16/76] Resolver issues Metrics.js #292352 --- lib/models/metrics.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/models/metrics.js b/lib/models/metrics.js index b6d752ab..25deb9a1 100644 --- a/lib/models/metrics.js +++ b/lib/models/metrics.js @@ -56,14 +56,15 @@ function deleteMetrics() { function incMetrics(service, servicePath, key, value) { var m = getMetrics(service, servicePath); - if (value === undefined) { - value = 1; + var result = value; + if (result === undefined) { + result = 1; } if (m[key] === undefined) { logger.error('invalid key for metrics %s', key); return; } - m[key] += value; + m[key] += result; } function decoratedMetrics(reset) { From c012cdcf1e55680c47c98eefe16d6d5c67528c67 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:39:58 +0100 Subject: [PATCH 17/76] Resolver issues Notices.js #292353 --- lib/models/notices.js | 83 +++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/lib/models/notices.js b/lib/models/notices.js index 2bb79bea..3c90436f 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -248,35 +248,34 @@ function processCBv2Notice(service, subservice, ncr, ix) { var attrList = ncr.data[ix]; for (var attr in attrList) { // Exclude id and type. NGSIv2 - if (attr === 'id' || attr === 'type') { - continue; - } - // each atttribute - var attrInfo = attrList[attr]; - n[attr + '__type'] = attrInfo.type; - - // NGSIv1 location attribute (only one should be present) - // see links in issues/198 - if (attrInfo.type === 'geo:point') { - location = parseLocation(attrInfo.value); - n[attr] = location; - } else if (attrInfo.type === 'DateTime') { - date = parseDate(attrInfo.value); - n[attr] = date; - } else { - n[attr] = attrInfo.value; - } + if (attr !== 'id' && attr !== 'type') { + // each atttribute + var attrInfo = attrList[attr]; + n[attr + '__type'] = attrInfo.type; - for (var metaKey in attrInfo.metadata) { - n[attr + '__metadata__' + metaKey + '__type'] = attrInfo.metadata[metaKey].type; - if (attrInfo.metadata[metaKey].type === 'DateTime') { - metaDate = parseDate(attrInfo.metadata[metaKey].value); - n[attr + '__metadata__' + metaKey] = metaDate; - } else if (attrInfo.metadata[metaKey].type === 'geo:point') { - metalocation = parseLocation(attrInfo.metadata[metaKey].value); - n[attr + '__metadata__' + metaKey] = metalocation; + // NGSIv1 location attribute (only one should be present) + // see links in issues/198 + if (attrInfo.type === 'geo:point') { + location = parseLocation(attrInfo.value); + n[attr] = location; + } else if (attrInfo.type === 'DateTime') { + date = parseDate(attrInfo.value); + n[attr] = date; } else { - n[attr + '__metadata__' + metaKey] = attrInfo.metadata[metaKey].value; + n[attr] = attrInfo.value; + } + + for (var metaKey in attrInfo.metadata) { + n[attr + '__metadata__' + metaKey + '__type'] = attrInfo.metadata[metaKey].type; + if (attrInfo.metadata[metaKey].type === 'DateTime') { + metaDate = parseDate(attrInfo.metadata[metaKey].value); + n[attr + '__metadata__' + metaKey] = metaDate; + } else if (attrInfo.metadata[metaKey].type === 'geo:point') { + metalocation = parseLocation(attrInfo.metadata[metaKey].value); + n[attr + '__metadata__' + metaKey] = metalocation; + } else { + n[attr + '__metadata__' + metaKey] = attrInfo.metadata[metaKey].value; + } } } } @@ -376,17 +375,17 @@ function DoNotice(orionN, callback) { } } async.each( - notices, - function(noticeVar, cbEach) { + notices, + function(noticeVar, cbEach) { var h = {}; h[constants.SUBSERVICE_HEADER] = noticeVar.subservice; myutils.requestHelperWOMetrics( - 'post', - { - url: config.perseoCore.noticesURL, - json: noticeVar, - headers: h - }, + 'post', + { + url: config.perseoCore.noticesURL, + json: noticeVar, + headers: h + }, function(err, data) { if (err) { alarm.raise(alarm.POST_EVENT); @@ -398,13 +397,13 @@ function DoNotice(orionN, callback) { // Don't wait propagation to next core to finish, asynchronously ... if (config.nextCore && config.nextCore.noticesURL) { myutils.requestHelperWOMetrics( - 'post', - { - url: config.nextCore.noticesURL, - json: noticeVar, - headers: h - }, - myutils.logErrorIf + 'post', + { + url: config.nextCore.noticesURL, + json: noticeVar, + headers: h + }, + myutils.logErrorIf ); } cbEach(); From 326a6b4578a194c5c0cd9ce46ca8722660904fac Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:41:43 +0100 Subject: [PATCH 18/76] Resolver issues Rules.js #292363 --- lib/models/rules.js | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/models/rules.js b/lib/models/rules.js index 816b5eb4..84c6c950 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -59,8 +59,7 @@ function validRule(rule) { } else if (!rule.text && !rule.nosignal) { return new errors.EmptyRule(JSON.stringify(rule)); } else { - //Added default else clause - + //Added default else clause } if (rule.nosignal) { //Specific checks for no-signal rules @@ -187,6 +186,7 @@ module.exports = { }, Save: function(rule, callback) { var localError; + var normalizeRule; localError = validRule(rule); if (localError !== null) { myutils.logErrorIf(localError); @@ -194,26 +194,31 @@ module.exports = { } // Normalize the rule text - rule = normalizeRuleName(rule); + normalizeRule = normalizeRuleName(rule); async.series( [ function(localCallback) { - rulesStore.Exists(rule, function rsSaveExistsCb(err, exists) { + rulesStore.Exists(normalizeRule, function rsSaveExistsCb(err, exists) { if (err) { return localCallback(err); } if (exists) { - return localCallback(new errors.RuleExists(rule.name)); + return localCallback(new errors.RuleExists(normalizeRule.name)); } return localCallback(err, exists); }); }, - postR2core.bind(null, rule), - rulesStore.Save.bind(null, rule), + postR2core.bind(null, normalizeRule), + rulesStore.Save.bind(null, normalizeRule), function(cb) { if (rule.nosignal) { - noSignal.AddNSRule(rule.service, rule.subservice, rule.name, rule.nosignal); + noSignal.AddNSRule( + normalizeRule.service, + normalizeRule.subservice, + normalizeRule.name, + normalizeRule.nosignal + ); } cb(null); } @@ -256,25 +261,34 @@ module.exports = { } // Normalize the rule text - rule = normalizeRuleName(rule); + putNormalizeRule = normalizeRuleName(rule); async.series( [ - delR2core.bind(null, rule), + delR2core.bind(null, putNormalizeRule), function(cb) { if (rule.nosignal) { - noSignal.DeleteNSRule(rule.service, rule.subservice, rule.name); + noSignal.DeleteNSRule( + putNormalizeRule.service, + putNormalizeRule.subservice, + putNormalizeRule.name + ); } cb(null); }, - postR2core.bind(null, rule), + postR2core.bind(null, putNormalizeRule), function(cb) { - if (rule.nosignal) { - noSignal.AddNSRule(rule.service, rule.subservice, rule.name, rule.nosignal); + if (putNormalizeRule.nosignal) { + noSignal.AddNSRule( + putNormalizeRule.service, + putNormalizeRule.subservice, + putNormalizeRule.name, + putNormalizeRule.nosignal + ); } cb(null); }, - rulesStore.Update.bind(null, id, rule) + rulesStore.Update.bind(null, id, putNormalizeRule) ], callback ); From 9e873b8c59542bdd6736a283ad0c8c24bb9568ff Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:42:39 +0100 Subject: [PATCH 19/76] Resolver issues RulesStore.js #292365 --- lib/models/rulesStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/models/rulesStore.js b/lib/models/rulesStore.js index b73fdf54..62066010 100644 --- a/lib/models/rulesStore.js +++ b/lib/models/rulesStore.js @@ -95,9 +95,9 @@ function search(rule, callback) { ); } -function findAll(service, subservice, callback) { +function findAll(service, subservice, uselessCallback) { var criterion = {}; - + var callback = uselessCallback; if (typeof service === 'function') { callback = service; } else { From 9b1527787814f259ce7a45d193e8b831a0bdec84 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:44:09 +0100 Subject: [PATCH 20/76] Resolver issues smppAction.js #292366 --- lib/models/smppAction.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/models/smppAction.js b/lib/models/smppAction.js index 54330914..056810bc 100644 --- a/lib/models/smppAction.js +++ b/lib/models/smppAction.js @@ -62,14 +62,14 @@ function doIt(action, event, callback) { var sendSMPP = function(from, to, text) { /*jshint camelcase:false */ - from = from.toString(); - to = to.toString(); - logger.debug('smpp sms from: %s, to: %s, text: %s', from, to, text); + var fromString = from.toString(); + var toString = to.toString(); + logger.debug('smpp sms from: %s, to: %s, text: %s', fromString, toString, text); session.submit_sm( { - source_addr: from, - destination_addr: to, + source_addr: fromString, + destination_addr: toString, short_message: text }, function(pdu) { From 0695fef2059763492fca28921114095bcafc9243 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:44:55 +0100 Subject: [PATCH 21/76] Resolver issus updateAction.js #292368 --- lib/models/updateAction.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index 8d3a5278..fb2ddb96 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -175,9 +175,11 @@ function doRequest(action, event, token, callback) { alarm.release(alarm.ORION); } if (!err && data.body && data.body.orionError) { - err = new errors.OrionError(JSON.stringify(data.body.orionError)); + var errOrion = new errors.OrionError(JSON.stringify(data.body.orionError)); + callback(errOrion, data); + } else { + callback(err, data); } - callback(err, data); } ); } @@ -280,6 +282,8 @@ function processOptionParams(action, event) { case 'None': theValue = null; break; + default: + //nothing to do. } var key = myutils.expandVar(attr.name, event); changes[key] = { @@ -311,7 +315,7 @@ function doRequestV2(action, event, callback) { servicepath: event.subservice }; - // TODO NGSIv2 update action with trust token (similar to doItWithToken for NGSIv1) + // NGSIv2 update action with trust token (similar to doItWithToken for NGSIv1) var connection = new NGSI.Connection(config.orion.URL, options); From c0b8505acd45dae4333de8c53cf0a58e3e8c3f81 Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:46:03 +0100 Subject: [PATCH 22/76] Resolver issues visualRules.js #292387 --- lib/models/visualRules.js | 60 +++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/models/visualRules.js b/lib/models/visualRules.js index f9178b1a..c0b265a7 100644 --- a/lib/models/visualRules.js +++ b/lib/models/visualRules.js @@ -45,36 +45,37 @@ function errorOperator(op) { } function castVar(str, isNumber) { - str = util.format('cast(`%s`?, String)', str); + castStr = util.format('cast(`%s`?, String)', str); if (isNumber === true) { - str = util.format('cast(%s, float)', str); + castStr = util.format('cast(%s, float)', str); } - return str; + return castStr; } function adaptExp(str, isNumber) { + var adaptString; if (str.indexOf('${') !== 0) { if (isNumber !== true) { - str = util.format('%j', str); + adaptString = util.format('%j', str); } - return str; + return adaptString; } //Remove starting "${" and trailing "}" - str = str.slice(2, -1); + adaptString = str.slice(2, -1); //Change dot to double underscore - str = str.replace(/\./g, '__'); - return castVar(str, isNumber); + adaptString = adaptString.replace(/\./g, '__'); + return castVar(adaptString, isNumber); } function makeUserPropCondition(exprL, op, exprR, isNumber) { - exprL = adaptExp(exprL, isNumber); - exprR = adaptExp(exprR, isNumber); - return util.format('(%s %s %s)', exprL, operatorMap[op], exprR); + var adaptExprL = adaptExp(exprL, isNumber); + var adaptExprR = adaptExp(exprR, isNumber); + return util.format('(%s %s %s)', adaptExprL, operatorMap[op], adaptExprR); } function makeObservationCondition(exprL, op, exprR, isNumber) { - exprL = castVar(exprL, isNumber); - exprR = adaptExp(exprR, isNumber); - return util.format('(%s %s %s)', exprL, operatorMap[op], exprR); + var castExprL = castVar(exprL, isNumber); + var adaptExprR = adaptExp(exprR, isNumber); + return util.format('(%s %s %s)', castExprL, operatorMap[op], adaptExprR); } function plainParams(p) { var i, @@ -142,10 +143,9 @@ function vr2rule(cardRule) { } } else if (card.conditionList[0].parameterName === 'type') { type = card.conditionList[0].parameterValue; - } else { - //Added default else clause - - } + } else { + //Added default else clause + } break; case 'OBSERVATION': errOp = errorOperator(card.conditionList[0].operator); @@ -157,8 +157,8 @@ function vr2rule(cardRule) { } else if (card.sensorData.measureName === 'type') { return new errors.TypeAsAttribute(''); } else { - //Added default else clause - } + //Added default else clause + } conditions.push( makeObservationCondition( card.sensorData.measureName, @@ -229,10 +229,15 @@ function vr2rule(cardRule) { case 'timeElapsed': // Minimal interval for actions action.interval = card.timeData.interval; break; + default: + //nothing to do } } else { return new errors.MissingConfigDataInTimeCard(JSON.stringify(card)); } + break; + default: + //nothing to do } } @@ -278,12 +283,11 @@ module.exports = { }, Find: function Find(rule, callback) { rules.Find(rule, function(err, data) { + var vr = (data && data.VR) || {}; if (err) { return callback(err, null); } - data = data || {}; - data.VR = data.VR || {}; - return callback(err, data.VR); + return callback(err, vr); }); }, Save: function Save(vrrule, callback) { @@ -303,12 +307,12 @@ module.exports = { rules.Remove(id, callback); }, Put: function(id, rule, callback) { - rule = vr2rule(rule); - if (rule instanceof Error) { - myutils.logErrorIf(rule); - return callback(rule, null); + var reformatRule = vr2rule(rule); + if (reformatRule instanceof Error) { + myutils.logErrorIf(reformatRule); + return callback(reformatRule, null); } - rules.Put(id, rule, function(err, data) { + rules.Put(id, reformatRule, function(err, data) { if (err) { return callback(err, null); } From c11d45d019ce722eda37f3411e93e49ae5e16d6c Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:50:04 +0100 Subject: [PATCH 23/76] Resolver issues myutils.js #292390 --- lib/myutils.js | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/myutils.js b/lib/myutils.js index b1db8c4f..495c17c9 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -31,34 +31,37 @@ var request = require('request'), function logErrorIf(err, message, context) { var level = 'error'; - if (context === undefined) { - context = process.domain && process.domain.context; + var contextTemp = context; + var messageTemp; + if (contextTemp === undefined) { + contextTemp = process.domain && process.domain.context; } if (err) { - message = message || ''; - if (context) { - context.op = - context.op || + messageTemp = message || ''; + if (contextTemp) { + contextTemp.op = + contextTemp.op || new Error().stack .split('\n')[2] .trim() .substr(3); - context.comp = context.comp || constants.COMPONENT_NAME; - logger[level](context, message, err.message || JSON.stringify(err)); + contextTemp.comp = contextTemp.comp || constants.COMPONENT_NAME; + logger[level](context, messageTemp, err.message || JSON.stringify(err)); } else { - logger[level](message, err.message || JSON.stringify(err)); + logger[level](messageTemp, err.message || JSON.stringify(err)); } } } function expandVar(val, mapping) { - if (typeof val === 'string') { + var valTemp = val; + if (typeof valTemp === 'string') { Object.keys(mapping).forEach(function(p) { - val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]); + valTemp = valTemp.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]); }); - val = val.replace(/\$\{\w*\}/g, '[?]'); + valTemp = valTemp.replace(/\$\{\w*\}/g, '[?]'); } - return val; + return valTemp; } function expandObject(templateObj, dictionary) { @@ -70,8 +73,7 @@ function expandObject(templateObj, dictionary) { } else if (typeof templateObj[key] === 'object') { res[expandVar(key, dictionary)] = expandObject(templateObj[key], dictionary); } else { - //Added default else clause - + //Added default else clause } }); } @@ -199,10 +201,11 @@ function respondAux(resp, err, data, withCount, raw, withMetrics) { respObj, respStr, domain = process.domain; + var dataTemp = data; if (err) { errMsg = err.message; statusCode = err.httpCode || 500; - data = null; + dataTemp = null; } if (raw === true) { @@ -210,13 +213,13 @@ function respondAux(resp, err, data, withCount, raw, withMetrics) { respObj = err; delete respObj.httpCode; } else { - respObj = data; + respObj = dataTemp; } } else { // non-raw - respObj = { error: errMsg, data: data }; - if (withCount === true && data && util.isArray(data)) { - respObj.count = data.length; + respObj = { error: errMsg, data: dataTemp }; + if (withCount === true && dataTemp && util.isArray(dataTemp)) { + respObj.count = dataTemp.length; } } logger.info('sending response: %s %j', statusCode, respObj); @@ -247,10 +250,11 @@ function respondWOMetrics(resp, err, data, withCount, raw) { function firstChars(result) { var text; - if (result === undefined) { - result = 'undefined'; + var resultTemp = result; + if (resultTemp === undefined) { + resultTemp = 'undefined'; } - text = JSON.stringify(result); + text = JSON.stringify(resultTemp); if (text.length > 125) { text = text.substr(0, 125) + ' [...]'; } From 05a520849019f177c47decd4697ebcd4ec9f57ea Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 12:52:33 +0100 Subject: [PATCH 24/76] Resolver issues visualRulesController.js #292395 --- lib/routes/visualRulesController.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 538be6af..0e42f8cc 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -31,13 +31,13 @@ var util = require('util'), function GetAllVR(req, resp) { visualRules.FindAll(req.service, req.subservice, function(err, data) { if (!err && util.isArray(data)) { - data = data.map(function(e) { + var reformatData = data.map(function(e) { delete e.subservice; delete e.service; return e; }); } - myutils.respond(resp, err, data, true); + myutils.respond(resp, err, reformatData, true); }); } function GetVR(req, resp) { @@ -119,7 +119,6 @@ function PutVR(req, resp) { return myutils.respond(resp, err, data); } if (data && data.name) { - //resp.location(req.url.replace(new RegExp(req.params.id), '')+encodeURIComponent(data.name)); resp.location(req.url.substr(0, req.url.lastIndexOf('/')) + '/' + encodeURIComponent(data.name)); } resp.status(200); From 53c605204b0a0c04388584e0d2b742bf08c56c8e Mon Sep 17 00:00:00 2001 From: fiqare Date: Wed, 27 Mar 2019 13:56:28 +0100 Subject: [PATCH 25/76] Update version of docker image --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 50a6190b..ed5432a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ # For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es # -FROM node:8.12.0-slim +FROM node:8.15-slim MAINTAINER FIWARE Perseo Team. Telefónica I+D From e4e9bca24b7b0e4a7e9c76b7c5987f003b27173b Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 11:37:57 +0200 Subject: [PATCH 26/76] Resolver issues db.js #292343 --- .snyk | 4 ++++ lib/db.js | 14 +++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 .snyk diff --git a/.snyk b/.snyk new file mode 100644 index 00000000..700d8894 --- /dev/null +++ b/.snyk @@ -0,0 +1,4 @@ +# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. +version: v1.13.4 +ignore: {} +patch: {} diff --git a/lib/db.js b/lib/db.js index 0675fb4b..b03848b1 100644 --- a/lib/db.js +++ b/lib/db.js @@ -51,15 +51,11 @@ function getDbAux(url, component, callback) { client.connect( url, { - db: { - bufferMaxEntries: config.checkDB.bufferMaxEntries, - domainsEnabled: true - }, - server: { - reconnectTries: config.checkDB.reconnectTries, - reconnectInterval: config.checkDB.reconnectInterval, - domainsEnabled: true - } + bufferMaxEntries: config.checkDB.bufferMaxEntries, + domainsEnabled: true, + reconnectTries: config.checkDB.reconnectTries, + reconnectInterval: config.checkDB.reconnectInterval, + domainsEnabled: true }, function(err, db) { if (err) { From 01ec8314daa272c30db1b787c37a606f12c204cb Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 11:40:11 +0200 Subject: [PATCH 27/76] Resolver issues action.js #292346 --- lib/models/actions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/models/actions.js b/lib/models/actions.js index 78719692..f35f133f 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -231,6 +231,7 @@ function DoAction(event, callback) { actionStore.Find(service, subservice, ruleName, function(err, actions) { var queue; var arrayAction = actions; + var localErr; if (err) { return callback(err, null); } From 805219bf223805f11ea77b61c2dc7d3be04bf4e4 Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 11:46:46 +0200 Subject: [PATCH 28/76] Resolver issues emailAction.js #293783 --- lib/models/emailAction.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/models/emailAction.js b/lib/models/emailAction.js index 061459b9..3a5ff5e2 100644 --- a/lib/models/emailAction.js +++ b/lib/models/emailAction.js @@ -25,13 +25,12 @@ var util = require('util'), nodemailer = require('nodemailer'), logger = require('logops'), - smtpTransport = require('nodemailer-smtp-transport'), config = require('../../config'), myutils = require('../myutils'), alarm = require('../alarm'), metrics = require('./metrics'); -var transporter = nodemailer.createTransport(smtpTransport(config.smtp)); +var transporter = nodemailer.createTransport(config.smtp); function buildMailOptions(action, event) { return { @@ -50,7 +49,7 @@ function SendMail(action, event, callback) { metrics.IncMetrics(event.service, event.subservice, metrics.actionEmail); - transporter.sendMail(mailOptions, function(err, info) { + transporter.sendMail(mailOptions, (err, info) => { logger.debug('emailAction.SendMail %j %j %j', mailOptions, err, info); // Not an HTTP request, so outgoingTransacion hasn't already counted and must be counted now metrics.IncMetrics(event.service, event.subservice, metrics.outgoingTransactions); From 32662f46123b53bf45d6017e1b43328d82bae904 Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 11:47:38 +0200 Subject: [PATCH 29/76] Resolver issues rules.js #292363 --- lib/models/rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/rules.js b/lib/models/rules.js index 84c6c950..752dd35d 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -261,7 +261,7 @@ module.exports = { } // Normalize the rule text - putNormalizeRule = normalizeRuleName(rule); + var putNormalizeRule = normalizeRuleName(rule); async.series( [ From 4cade58d419ee8b196659728c9773ea5b8df9458 Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 11:48:51 +0200 Subject: [PATCH 30/76] Resolver issues visualRules.js #292388 --- lib/models/visualRules.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/models/visualRules.js b/lib/models/visualRules.js index c0b265a7..08b4221a 100644 --- a/lib/models/visualRules.js +++ b/lib/models/visualRules.js @@ -45,23 +45,24 @@ function errorOperator(op) { } function castVar(str, isNumber) { - castStr = util.format('cast(`%s`?, String)', str); + var castStr = util.format('cast(`%s`?, String)', str); + if (isNumber === true) { - castStr = util.format('cast(%s, float)', str); + castStr = util.format('cast(%s, float)', castStr); } return castStr; } function adaptExp(str, isNumber) { - var adaptString; - if (str.indexOf('${') !== 0) { + var adaptString = str; + if (adaptString.indexOf('${') !== 0) { if (isNumber !== true) { - adaptString = util.format('%j', str); + adaptString = util.format('%j', adaptString); } return adaptString; } //Remove starting "${" and trailing "}" - adaptString = str.slice(2, -1); + adaptString = adaptString.slice(2, -1); //Change dot to double underscore adaptString = adaptString.replace(/\./g, '__'); return castVar(adaptString, isNumber); @@ -149,9 +150,11 @@ function vr2rule(cardRule) { break; case 'OBSERVATION': errOp = errorOperator(card.conditionList[0].operator); + if (errOp) { return errOp; } + if (card.sensorData.measureName === 'id') { return new errors.IdAsAttribute(''); } else if (card.sensorData.measureName === 'type') { @@ -159,6 +162,7 @@ function vr2rule(cardRule) { } else { //Added default else clause } + conditions.push( makeObservationCondition( card.sensorData.measureName, @@ -167,6 +171,7 @@ function vr2rule(cardRule) { card.sensorData.dataType === 'Quantity' ) ); + break; case 'LAST_MEASURE': checkInterval = parseInt(card.timeData.interval, 10); From ef334a6b823f9d389e792ee5bb458d383e8d9c6f Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 12:47:07 +0200 Subject: [PATCH 31/76] Resolver issues config.js --- config.js | 14 ++++++-------- package.json | 12 ++++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/config.js b/config.js index 12e966c0..9574ed83 100644 --- a/config.js +++ b/config.js @@ -29,7 +29,7 @@ var config = {}; * Default log level. Can be one of: 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' * @type {string} */ -config.logLevel = 'INFO'; +config.logLevel = 'DEBUG'; /** * Configures the exposed API. @@ -105,16 +105,14 @@ config.nextCore = { * SMTP endpoint options */ config.smtp = { - port: 25, - host: 'smtpserver', + port: 9292, + host: '127.0.0.1', secure: false, - /* - , - auth: { + /*auth: { user: 'abc', pass: 'xyz' - } - */ + }, */ + tls: { // keep rejectUnauthorized to false when secure is also false rejectUnauthorized: false diff --git a/package.json b/package.json index e071b692..8d007979 100644 --- a/package.json +++ b/package.json @@ -21,14 +21,14 @@ }, "scripts": { "clean": "rm -rf package-lock.json && rm -rf node_modules && rm -rf coverage", - "test": "mocha --recursive 'test/**/*.js' --reporter spec --timeout 3000 --ui bdd --exit", + "test": "snyk test && mocha --recursive --reporter spec --timeout 3000 --ui bdd --exit", "test:watch": "npm run test -- -w ./lib", "lint": "jshint lib/ --config .jshintrc && jshint test/ --config test/.jshintrc", "lint:md": "remark 'README.md' documentation", "lint:text": "textlint 'README.md' 'documentation/*.md' 'documentation/**/*.md'", "prettier": "prettier --config .prettierrc.json --write ./**/**/*.js **/*.js *.js", "prettier:text": "prettier 'README.md' 'documentation/*.md' 'documentation/**/*.md' --no-config --tab-width 4 --print-width 120 --write --prose-wrap always", - "test:coverage": "istanbul cover _mocha -- --recursive 'test/**/*.js' --reporter spec --exit", + "test:coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --recursive --reporter spec --exit", "test:coveralls": "npm run test:coverage && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", "watch": "watch 'npm test && npm run lint' ./lib ./test" }, @@ -53,7 +53,8 @@ "textlint-rule-common-misspellings": "~1.0.1", "textlint-rule-no-dead-link": "~4.4.1", "textlint-rule-terminology": "~1.1.30", - "textlint-rule-write-good": "~1.6.2" + "textlint-rule-write-good": "~1.6.2", + "snyk": "^1.145.0" }, "keywords": [], "dependencies": { @@ -63,8 +64,7 @@ "logops": "2.1.0", "mongodb": "~2.2.31", "ngsijs": "~1.2.0", - "nodemailer": "~1.11.0", - "nodemailer-smtp-transport": "~0.1.13", + "nodemailer": "~6.0.0", "request": "~2.83.0", "twitter": "~1.7.1", "utm-converter": "~0.1.1", @@ -86,4 +86,4 @@ "git add" ] } -} +} \ No newline at end of file From 07c164ee1c4b90bf79729e9ebf2f49f5271277db Mon Sep 17 00:00:00 2001 From: fiqare Date: Thu, 4 Apr 2019 13:59:56 +0200 Subject: [PATCH 32/76] Fix ip for mongo in docker --- test/utils/utilsT.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/utilsT.js b/test/utils/utilsT.js index 4025f9ec..ae020606 100644 --- a/test/utils/utilsT.js +++ b/test/utils/utilsT.js @@ -220,7 +220,7 @@ function addEntity(tenant, entity, callback) { ); } function configTest() { - config.mongo.url = 'mongodb://localhost:27017/perseo_testing'; + config.mongo.url = 'mongodb://172.17.0.1:27017/perseo_testing'; config.endpoint.port = 9182; config.perseoCore.noticesURL = 'http://localhost:' + fakeServerPort; config.perseoCore.rulesURL = 'http://localhost:' + fakeServerPort; @@ -232,7 +232,7 @@ function configTest() { config.logLevel = 'fatal'; config.nextCore = {}; - config.orionDb.url = 'mongodb://localhost:27017/test'; + config.orionDb.url = 'mongodb://172.17.0.1:27017/test'; config.orionDb.prefix = 'oriontest'; config.perseoCore.interval = 10 * 60e3; //Do not refresh in the middle of a long test } From e6b731a932d7b84d04d7b421e89a194e9e7e8e2f Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Tue, 24 Sep 2019 12:19:13 +0200 Subject: [PATCH 33/76] Added Rules Swagger --- lib/perseo.js | 20 ++++++- lib/routes/rulesController.js | 109 ++++++++++++++++++++++++++++++++++ package.json | 6 +- 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/lib/perseo.js b/lib/perseo.js index 93a83abe..54294a7a 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -48,7 +48,9 @@ var domain = require('domain'), serviceMiddleware = require('./middleware/service'), errorMiddleware = require('./middleware/error'), myutils = require('./myutils'), - d = domain.create(); + d = domain.create(), + swaggerJsdoc = require('swagger-jsdoc'), + swaggerUi = require('swagger-ui-express'); function start(callbackStart) { var context = { op: 'start', comp: constants.COMPONENT_NAME }; @@ -98,6 +100,22 @@ function start(callbackStart) { versionRoutes.AddTo(app); metricsRoutes.AddTo(app); + const options = { + definition: { + swagger: '2.0', // Specification (optional, defaults to swagger: '2.0') + info: { + title: 'Perseo Front-End', // Title (required) + version: '1.7.0-fiqare' // Version (required) + } + }, + // Path to the API docs + apis: ['./lib/routes/*.js'] + }; + + const specs = swaggerJsdoc(options); + + app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs)); + async.series( [ function(callback) { diff --git a/lib/routes/rulesController.js b/lib/routes/rulesController.js index 82a5d0ef..62841017 100644 --- a/lib/routes/rulesController.js +++ b/lib/routes/rulesController.js @@ -75,9 +75,118 @@ function DelRules(req, resp) { } function AddTo(app) { + /** + * @swagger + * + * /rules: + * get: + * description: Get all rules + * produces: + * - application/json + * parameters: + * + * responses: + * 200: + * description: rules + */ app.get(config.endpoint.rulesPath, GetAllRules); + /** + * @swagger + * + * /rules/:id: + * get: + * description: Get rule by id + * produces: + * - application/json + * parameters: + * - name: id + * description: Name of the rule. + * in: path + * required: true + * type: string + * responses: + * 200: + * description: rules + */ app.get(config.endpoint.rulesPath + '/:id', GetRules); + /** + * @swagger + * + * /rules: + * post: + * description: Add rule + * produces: + * - application/json + * parameters: + * - name: body + * in: body + * schema: + * type: object + * properties: + * name: + * type: string + * description: Rule's name + * example: + * - foo + * text: + * type: string + * description: EPL sentence + * example: + * - select *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")] + * action: + * type: object + * properties: + * type: + * type: string + * description: Action type + * example: + * - update + * parameters: + * type: object + * properties: + * name: + * type: string + * description: Parameter name + * example: + * - foo + * value: + * type: string + * description: Parameter value + * example: + * - true + * type: + * type: string + * description: Parameter type + * example: + * - boolean + * required: + * - name + * - text + * - action + * responses: + * 200: + * description: rules + */ app.post(config.endpoint.rulesPath, PostRules); + + /** + * @swagger + * + * /rules/:id: + * delete: + * description: Delete rule by id + * produces: + * - application/json + * parameters: + * - name: id + * description: Rule id. + * in: path + * required: true + * type: string + * responses: + * 200: + * description: rules + */ app.delete(config.endpoint.rulesPath + '/:id', DelRules); } diff --git a/package.json b/package.json index e071b692..edaccfbe 100644 --- a/package.json +++ b/package.json @@ -66,10 +66,12 @@ "nodemailer": "~1.11.0", "nodemailer-smtp-transport": "~0.1.13", "request": "~2.83.0", + "smpp": "0.3.1", + "swagger-jsdoc": "^3.4.0", + "swagger-ui-express": "^4.1.1", "twitter": "~1.7.1", "utm-converter": "~0.1.1", - "uuid": "~1.4.2", - "smpp": "0.3.1" + "uuid": "~1.4.2" }, "husky": { "hooks": { From 862b17613bde655e78c383cd095cfb794355c5bd Mon Sep 17 00:00:00 2001 From: fiqare-emergya-dev <47174790+fiqare-emergya-dev@users.noreply.github.com> Date: Tue, 24 Sep 2019 12:22:58 +0200 Subject: [PATCH 34/76] Revert "Third iteration" --- lib/perseo.js | 20 +------ lib/routes/rulesController.js | 109 ---------------------------------- package.json | 6 +- 3 files changed, 3 insertions(+), 132 deletions(-) diff --git a/lib/perseo.js b/lib/perseo.js index 54294a7a..93a83abe 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -48,9 +48,7 @@ var domain = require('domain'), serviceMiddleware = require('./middleware/service'), errorMiddleware = require('./middleware/error'), myutils = require('./myutils'), - d = domain.create(), - swaggerJsdoc = require('swagger-jsdoc'), - swaggerUi = require('swagger-ui-express'); + d = domain.create(); function start(callbackStart) { var context = { op: 'start', comp: constants.COMPONENT_NAME }; @@ -100,22 +98,6 @@ function start(callbackStart) { versionRoutes.AddTo(app); metricsRoutes.AddTo(app); - const options = { - definition: { - swagger: '2.0', // Specification (optional, defaults to swagger: '2.0') - info: { - title: 'Perseo Front-End', // Title (required) - version: '1.7.0-fiqare' // Version (required) - } - }, - // Path to the API docs - apis: ['./lib/routes/*.js'] - }; - - const specs = swaggerJsdoc(options); - - app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs)); - async.series( [ function(callback) { diff --git a/lib/routes/rulesController.js b/lib/routes/rulesController.js index 62841017..82a5d0ef 100644 --- a/lib/routes/rulesController.js +++ b/lib/routes/rulesController.js @@ -75,118 +75,9 @@ function DelRules(req, resp) { } function AddTo(app) { - /** - * @swagger - * - * /rules: - * get: - * description: Get all rules - * produces: - * - application/json - * parameters: - * - * responses: - * 200: - * description: rules - */ app.get(config.endpoint.rulesPath, GetAllRules); - /** - * @swagger - * - * /rules/:id: - * get: - * description: Get rule by id - * produces: - * - application/json - * parameters: - * - name: id - * description: Name of the rule. - * in: path - * required: true - * type: string - * responses: - * 200: - * description: rules - */ app.get(config.endpoint.rulesPath + '/:id', GetRules); - /** - * @swagger - * - * /rules: - * post: - * description: Add rule - * produces: - * - application/json - * parameters: - * - name: body - * in: body - * schema: - * type: object - * properties: - * name: - * type: string - * description: Rule's name - * example: - * - foo - * text: - * type: string - * description: EPL sentence - * example: - * - select *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")] - * action: - * type: object - * properties: - * type: - * type: string - * description: Action type - * example: - * - update - * parameters: - * type: object - * properties: - * name: - * type: string - * description: Parameter name - * example: - * - foo - * value: - * type: string - * description: Parameter value - * example: - * - true - * type: - * type: string - * description: Parameter type - * example: - * - boolean - * required: - * - name - * - text - * - action - * responses: - * 200: - * description: rules - */ app.post(config.endpoint.rulesPath, PostRules); - - /** - * @swagger - * - * /rules/:id: - * delete: - * description: Delete rule by id - * produces: - * - application/json - * parameters: - * - name: id - * description: Rule id. - * in: path - * required: true - * type: string - * responses: - * 200: - * description: rules - */ app.delete(config.endpoint.rulesPath + '/:id', DelRules); } diff --git a/package.json b/package.json index a3911bc4..8d007979 100644 --- a/package.json +++ b/package.json @@ -66,12 +66,10 @@ "ngsijs": "~1.2.0", "nodemailer": "~6.0.0", "request": "~2.83.0", - "smpp": "0.3.1", - "swagger-jsdoc": "^3.4.0", - "swagger-ui-express": "^4.1.1", "twitter": "~1.7.1", "utm-converter": "~0.1.1", - "uuid": "~1.4.2" + "uuid": "~1.4.2", + "smpp": "0.3.1" }, "husky": { "hooks": { From 334a59e6b89e330eb087516ae58def771afdae17 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 30 Sep 2019 08:51:37 +0200 Subject: [PATCH 35/76] Added documentation for developers --- README.md | 1 + documentation/eslint.md | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 documentation/eslint.md diff --git a/README.md b/README.md index e2b35e6f..1a98c642 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ CEP implementation for IoT platform - [Data model](documentation/models.md) - [Available actions for PEP](documentation/pep_actions.md) - [Documentation for developers of perseo](documentation/development.md) +- [Development documentation (Third Iteration)](documentation/eslint.md) | :dart: [Roadmap](documentation/roadmap.md) | | ------------------------------------------ | diff --git a/documentation/eslint.md b/documentation/eslint.md new file mode 100644 index 00000000..01c91d6f --- /dev/null +++ b/documentation/eslint.md @@ -0,0 +1,74 @@ +# Development documentation (Third Iteration) + +## ESLint + +ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. In many ways, it is +similar to JSLint and JSHint with a few exceptions: + +- ESLint uses Espree for JavaScript parsing. +- ESLint uses an AST to evaluate patterns in code. +- ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime. + +### Installation and Usage + +#### Command-line + +Prerequisites: [Node.js](https://nodejs.org/) (`^8.10.0`, `^10.13.0`, or `>=11.10.1`), npm version 3+. + +You can install ESLint using npm: + +``` +$ npm install eslint --save-dev +``` + +You should then set up a configuration file: + +``` +$ ./node_modules/.bin/eslint --init +``` + +After that, you can run ESLint on any file or directory like this: + +```` +$ ./node_modules/.bin/eslint yourfile.js + +After running `eslint --init`, you'll have a `.eslintrc` file in your directory. In it, you'll see some rules configured like this: + +```json +{ + "rules": { + "semi": ["error", "always"], + "quotes": ["error", "double"] + } +} +```` + +The names `"semi"` and `"quotes"` are the names of rules in ESLint. The first value is the error level of the rule and +can be one of these values: + +- `"off"` or `0` - turn the rule off +- `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code) +- `"error"` or `2` - turn the rule on as an error (exit code will be 1) + +The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and +details, see the [configuration docs](https://eslint.org/docs/user-guide/configuring)). + +#### Integrations with code editors + +With ESLint properly configured in your editor, your development experience can be careful. it’ll help you produce +better quality code and teach you as you work. + +Below we will list the main IDEs and their link to the configuration of the ESLint plugin for the improvement of your +code. + +- Sublime Text 3: [SublimeLinter-eslint](https://github.com/SublimeLinter/SublimeLinter-eslint) +- Vim: [ALE](https://github.com/dense-analysis/ale) +- Emacs: [FlyChecks](http://www.flycheck.org/en/latest/) +- Eclipse Orion: [ESLint](https://www.eclipse.org/lists/orion-dev/msg02718.html) +- Eclipse IDE: [Tern ESLint linter](https://github.com/angelozerr/tern.java/wiki/Tern-Linter-ESLint) +- TextMate 2: [ESLint Bundle](https://github.com/natesilva/javascript-eslint.tmbundle) +- Atom: [linter-eslint](https://atom.io/packages/linter-eslint) +- IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, PyCharm, AppCode, Android Studio, 0xDBE: + [ESLint Plugin](https://plugins.jetbrains.com/plugin/7494-eslint) +- Visual Studio Code: [ESLint Extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) +- Brackets: [Included and Brackets ESLint](https://github.com/brackets-userland/brackets-eslint) From fe26c9013a915b793b233cdec9531192d0cae71b Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 30 Sep 2019 11:38:30 +0200 Subject: [PATCH 36/76] Added full Swagger definition --- lib/routes/noticesController.js | 69 +++++++++++++++ lib/routes/rulesController.js | 143 +++++++++++++++++++++----------- lib/routes/versionController.js | 73 ++++++++++++++++ 3 files changed, 237 insertions(+), 48 deletions(-) diff --git a/lib/routes/noticesController.js b/lib/routes/noticesController.js index 8501dc43..e14d007f 100644 --- a/lib/routes/noticesController.js +++ b/lib/routes/noticesController.js @@ -45,6 +45,75 @@ function PostNotice(req, resp) { } function AddTo(app) { + /** + * @swagger + * + * /notices: + * post: + * tags: + * - Notices + * description: Send an event/notification to perseo + * produces: + * - application/json + * parameters: + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string + * - name: body + * in: body + * schema: + * type: object + * properties: + * subscriptionId: + * type: string + * description: Subscription Id + * example: + * - 5c7e63939859f248791984e0 + * data: + * type: object + * properties: + * id: + * type: string + * description: Entity Id + * example: + * - urn:ngsi-ld:Door:001 + * type: + * type: string + * description: Entity type + * example: + * - Door + * state: + * type: object + * properties: + * type: + * type: string + * description: State type + * example: + * - Text + * value: + * type: string + * description: State value + * example: + * - OPEN + * metadata: + * type: object + * description: State metadata + * example: + * - {"TimeInstant":{"type":"ISO8601", "value":"2019-03-05T10:49:16.402Z"}} + * required: + * - subscriptionId + * - data + * responses: + * 200: + * description: rules + */ app.post(config.endpoint.noticesPath, PostNotice); } diff --git a/lib/routes/rulesController.js b/lib/routes/rulesController.js index 62841017..ed565d0c 100644 --- a/lib/routes/rulesController.js +++ b/lib/routes/rulesController.js @@ -80,11 +80,22 @@ function AddTo(app) { * * /rules: * get: + * tags: + * - Rules * description: Get all rules * produces: * - application/json * parameters: - * + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string * * responses: * 200: * description: rules @@ -95,10 +106,22 @@ function AddTo(app) { * * /rules/:id: * get: + * tags: + * - Rules * description: Get rule by id * produces: * - application/json * parameters: + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string * - name: id * description: Name of the rule. * in: path @@ -114,58 +137,70 @@ function AddTo(app) { * * /rules: * post: + * tags: + * - Rules * description: Add rule * produces: * - application/json * parameters: - * - name: body - * in: body - * schema: - * type: object - * properties: - * name: - * type: string - * description: Rule's name - * example: - * - foo - * text: - * type: string - * description: EPL sentence - * example: - * - select *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")] - * action: - * type: object - * properties: - * type: - * type: string - * description: Action type - * example: - * - update - * parameters: - * type: object - * properties: - * name: - * type: string - * description: Parameter name - * example: - * - foo - * value: - * type: string - * description: Parameter value - * example: - * - true - * type: - * type: string - * description: Parameter type - * example: - * - boolean - * required: - * - name - * - text - * - action + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string + * - name: body + * in: body + * schema: + * type: object + * properties: + * name: + * type: string + * description: Rule's name + * example: + * - foo + * text: + * type: string + * description: EPL sentence + * example: + * - select *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")] + * action: + * type: object + * properties: + * type: + * type: string + * description: Action type + * example: + * - update + * parameters: + * type: object + * properties: + * name: + * type: string + * description: Parameter name + * example: + * - foo + * value: + * type: string + * description: Parameter value + * example: + * - true + * type: + * type: string + * description: Parameter type + * example: + * - boolean + * required: + * - name + * - text + * - action * responses: - * 200: - * description: rules + * 200: + * description: rules */ app.post(config.endpoint.rulesPath, PostRules); @@ -174,10 +209,22 @@ function AddTo(app) { * * /rules/:id: * delete: + * tags: + * - Rules * description: Delete rule by id * produces: * - application/json * parameters: + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string * - name: id * description: Rule id. * in: path diff --git a/lib/routes/versionController.js b/lib/routes/versionController.js index 2e794e0a..e4b7653a 100644 --- a/lib/routes/versionController.js +++ b/lib/routes/versionController.js @@ -64,8 +64,81 @@ function getLogLevel(req, resp) { myutils.respondWOMetrics(resp, null, { level: currentLevel }, /* withCount */ false, /* raw */ true); } function AddTo(app) { + /** + * @swagger + * + * /version: + * get: + * tags: + * - Version + * description: Get version + * produces: + * - application/json + * parameters: + * + * responses: + * 200: + * description: version + */ app.get(config.endpoint.versionPath, version); + /** + * @swagger + * + * /admin/log: + * put: + * tags: + * - Log + * description: Send an event/notification to perseo + * produces: + * - application/json + * parameters: + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string + * - name: level + * description: Log level + * in: query + * required: true + * type: string + * example: + * - INFO + * responses: + * 200: + * description: logLevel + */ app.put(config.endpoint.logPath, changeLogLevel); + /** + * @swagger + * + * /admin/log: + * get: + * tags: + * - Log + * description: Get Log Level + * produces: + * - application/json + * parameters: + * - name: fiware-service + * description: Fiware service. + * in: header + * required: true + * type: string + * - name: fiware-servicepath + * description: Fiware service path. + * in: header + * required: true + * type: string + * responses: + * 200: + * description: logLevel + */ app.get(config.endpoint.logPath, getLogLevel); } From 7f7ad601c6a197ab05b56d18219b60c87347c75e Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 30 Sep 2019 14:26:20 +0200 Subject: [PATCH 37/76] Fixed Swagger --- lib/perseo.js | 20 +++++++++++++++++++- package.json | 8 +++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/perseo.js b/lib/perseo.js index 93a83abe..54294a7a 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -48,7 +48,9 @@ var domain = require('domain'), serviceMiddleware = require('./middleware/service'), errorMiddleware = require('./middleware/error'), myutils = require('./myutils'), - d = domain.create(); + d = domain.create(), + swaggerJsdoc = require('swagger-jsdoc'), + swaggerUi = require('swagger-ui-express'); function start(callbackStart) { var context = { op: 'start', comp: constants.COMPONENT_NAME }; @@ -98,6 +100,22 @@ function start(callbackStart) { versionRoutes.AddTo(app); metricsRoutes.AddTo(app); + const options = { + definition: { + swagger: '2.0', // Specification (optional, defaults to swagger: '2.0') + info: { + title: 'Perseo Front-End', // Title (required) + version: '1.7.0-fiqare' // Version (required) + } + }, + // Path to the API docs + apis: ['./lib/routes/*.js'] + }; + + const specs = swaggerJsdoc(options); + + app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs)); + async.series( [ function(callback) { diff --git a/package.json b/package.json index 8d007979..08345981 100644 --- a/package.json +++ b/package.json @@ -66,10 +66,12 @@ "ngsijs": "~1.2.0", "nodemailer": "~6.0.0", "request": "~2.83.0", + "smpp": "0.3.1", + "swagger-jsdoc": "^3.4.0", + "swagger-ui-express": "^4.1.1", "twitter": "~1.7.1", "utm-converter": "~0.1.1", - "uuid": "~1.4.2", - "smpp": "0.3.1" + "uuid": "~1.4.2" }, "husky": { "hooks": { @@ -86,4 +88,4 @@ "git add" ] } -} \ No newline at end of file +} From b54bc43b0dba749bf6c1bb44a0ade3e1dc06d2ce Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 10:08:51 +0100 Subject: [PATCH 38/76] Updated to 1.10 release from telefonicaid/perseo-fe --- .travis.yml | 7 +- Dockerfile | 4 +- bin/perseo | 25 +- config.js | 5 + documentation/admin.md | 38 +- documentation/api.md | 12 + documentation/configuration.md | 8 + documentation/plain_rules.md | 303 ++++++++++++--- lib/constants.js | 1 + lib/models/executionsStore.js | 32 +- lib/models/metrics.js | 1 + lib/models/notices.js | 2 + lib/models/postAction.js | 2 +- lib/models/smsAction.js | 13 +- lib/models/updateAction.js | 365 ++++++++++++++---- lib/myutils.js | 23 +- lib/routes/noticesController.js | 2 + lib/routes/rulesController.js | 15 +- lib/routes/visualRulesController.js | 16 +- package.json | 9 +- rpm/SPECS/cep.spec | 24 ++ .../Rules/EPL/epl_create_rules.feature | 15 + .../bad_rules/rule_update_action_id_attr.json | 1 + .../rule_update_action_type_attr.json | 1 + .../error_in_axn_rules/blood_rule_update.json | 1 + test/data/good_rules/blood_rule_update.json | 1 + .../good_rules/blood_rule_update_trust.json | 1 + test/unit/updateAction.js | 309 +++++++++------ test/utils/clients.js | 7 +- 29 files changed, 931 insertions(+), 312 deletions(-) diff --git a/.travis.yml b/.travis.yml index 33e109e5..01bc0af2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: node_js node_js: - "8" - "10" + # FIXME: something is precluding perseo-fe to work in Node v12. Some tests are failing + #- "12" branches: only: @@ -14,11 +16,14 @@ services: addons: apt: sources: - - mongodb-3.2-precise + - mongodb-3.6-precise packages: - mongodb-org-server install: + # It seems to be a good idea to clean before install + # see https://travis-ci.community/t/i-needed-an-explicit-cleanup-before-starting-my-tests-to-have-then-working-again-is-that-normal/5512/2 + - npm run clean - npm install before_install: diff --git a/Dockerfile b/Dockerfile index ed5432a8..ec5aa1be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,9 +16,9 @@ # For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es # -FROM node:8.15-slim +FROM node:8.16.1-slim -MAINTAINER FIWARE Perseo Team. Telefónica I+D +MAINTAINER FIWARE Perseo Team. Telefónica I+D COPY . /opt/perseo-fe/ WORKDIR /opt/perseo-fe diff --git a/bin/perseo b/bin/perseo index 9148cd8a..9cbf9218 100755 --- a/bin/perseo +++ b/bin/perseo @@ -49,6 +49,8 @@ function loadConfiguration() { 'PERSEO_MONGO_REPLICASET', 'PERSEO_MONGO_USER', 'PERSEO_MONGO_PASSWORD', + 'PERSEO_IS_MASTER', + 'PERSEO_SLAVE_DELAY', 'PERSEO_CORE_URL', 'PERSEO_NEXT_URL', 'PERSEO_ORION_URL', @@ -58,7 +60,6 @@ function loadConfiguration() { 'PERSEO_SMTP_SECURE', 'PERSEO_SMTP_AUTH_USER', 'PERSEO_SMTP_AUTH_PASS', - 'PERSEO_SMTP_AUTH_PASS', 'PERSEO_SMTP_TLS_REJECTUNAUTHORIZED', 'PERSEO_SMS_URL', 'PERSEO_SMS_API_KEY', @@ -71,8 +72,12 @@ function loadConfiguration() { 'PERSEO_SMPP_FROM', 'PERSEO_SMPP_ENABLED', 'PERSEO_NOTICES_PATH', - 'PERSEO_RULES_PATH' - + 'PERSEO_RULES_PATH', + 'PERSEO_CAST_TYPES', + 'PERSEO_AUTHENTICATION_HOST', + 'PERSEO_AUTHENTICATION_PORT', + 'PERSEO_AUTHENTICATION_USER', + 'PERSEO_AUTHENTICATION_PASSWORD' ]; for (var i = 0; i < environmentValues.length; i++) { @@ -97,6 +102,14 @@ function loadConfiguration() { process.env.PERSEO_MONGO_ENDPOINT + '/orion' + ( process.env.PERSEO_MONGO_REPLICASET ? '?replicaSet=' + process.env.PERSEO_MONGO_REPLICASET : ''); } + if (process.env.PERSEO_IS_MASTER === 'true') { + config.isMaster = true; + } else if (process.env.PERSEO_IS_MASTER === 'false') { + config.isMaster = false; + } + if (process.env.PERSEO_SLAVE_DELAY) { + config.slaveDelay = process.env.PERSEO_SLAVE_DELAY; + } if (process.env.PERSEO_CORE_URL) { config.perseoCore.rulesURL = process.env.PERSEO_CORE_URL + '/perseo-core/rules'; config.perseoCore.noticesURL = process.env.PERSEO_CORE_URL + '/perseo-core/events'; @@ -145,6 +158,12 @@ function loadConfiguration() { config.smpp.enabled = process.env.PERSEO_SMPP_ENABLED || config.smpp.enabled; config.endpoint.noticesPath = process.env.PERSEO_NOTICES_PATH || config.endpoint.noticesPath; config.endpoint.rulesPath = process.env.PERSEO_RULES_PATH || config.endpoint.rulesPath; + config.castTypes = process.env.PERSEO_CAST_TYPES || config.castTypes; + config.authentication = config.authentication || {}; + config.authentication.host = process.env.PERSEO_AUTHENTICATION_HOST || config.authentication.host; + config.authentication.port = process.env.PERSEO_AUTHENTICATION_PORT || config.authentication.port; + config.authentication.user = process.env.PERSEO_AUTHENTICATION_USER || config.authentication.user; + config.authentication.password = process.env.PERSEO_AUTHENTICATION_PASSWORD || config.authentication.password; } loadConfiguration(); diff --git a/config.js b/config.js index 9574ed83..168400c0 100644 --- a/config.js +++ b/config.js @@ -212,4 +212,9 @@ config.checkDB = { */ config.restBase = null; +/** + * Cast attribute values in updateAction using NGSIv2 types + */ +config.castTypes = false; + module.exports = config; diff --git a/documentation/admin.md b/documentation/admin.md index 1c7afef2..d3097fd2 100644 --- a/documentation/admin.md +++ b/documentation/admin.md @@ -117,28 +117,28 @@ The different configuration parameters introduced above are described also in th - 'subservice' is the subservice associated to the subscription ```bash -(curl http://orion-machine:1026/v1/subscribeContext -s -S --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Fiware-Service: service' –header 'Fiware-ServicePath: subservice' -d @- | python -mjson.tool) < ## Actions @@ -131,6 +201,13 @@ This substitution can be used in the following fields: - `template` for `twitter` action - `id`, `type`, `name`, `value`, `ìsPattern` for `update` action +Attribute value of `update` action and template of `post` action are expanded to numerical, boolean or JSON stringyfied +values instead of string values when is possible. For example, if we have `{"a": "${x}"}`: + +- If the value of attribute `x` is `42` then it will expand do `{"a": 42}` and not to `{"a": "42"}` +- If the value of attribute `x` is `{"hello": "world"}` then it will expand to `{"a": "{\"hello\":\"world\"}"}` + (expand to native JSON, i.e. `{"a": {"hello": "world"}}`, is not supported) + ### SMS action Sends a SMS to a number set as an action parameter with the body of the message built from the template @@ -145,7 +222,8 @@ Sends a SMS to a number set as an action parameter with the body of the message } ``` -The field `parameters` include a field `to` with the number to send the message to. +The field `parameters` include a field `to` with the number, or numbers separated by whiestpace charaters, to send the +message to. The `template` and `to` fields perform [attribute substitution](#string-substitution-syntax). @@ -171,15 +249,15 @@ The `template`, `from`, `to` and `subject` fields perform [string substitution]( ### update attribute action -Updates one or more attributes of a given entity (in the Context Broker instance specified in the Perseo configuration). -The `parameters` map includes the following fields: +Updates one or more attributes of a given entity or as a result of filter (in the Context Broker instance specified in +the Perseo configuration). The `parameters` map includes the following fields: - id: optional, the ID of the entity which attribute is to be updated (by default the ID of the entity that triggers the rule is used, i.e. `${id}`) - type: optional, the type of the entity which attribute is to be updated (by default the type of the entity that triggers the rule is usedi.e. `${type}`) - version: optional, The NGSI version for the update action. Set this attribute to `2` or `"2"` if you want to use - NGSv2 format. `1` by default + NGSv2 format. `2` by default. - isPattern: optional, `false` by default. (Only for NGSIv1. If `version` is set to 2, this attribute will be ignored) - attributes: _mandatory_, array of target attributes to update. Each element of the array must contain the fields - **name**: _mandatory_, attribute name to set @@ -189,6 +267,9 @@ The `parameters` map includes the following fields: - actionType: optional, type of CB action: APPEND or UPDATE. By default is APPEND. - trust: optional, trust token for getting an access token from Auth Server which can be used to get to a Context Broker behind a PEP. +- filter: optional, a NGSIv2 filter. If provided then updateAction is done over result of query. This overrides the + `id` field (in other words, if you use `filter` then `id` field is ignored, in fact you should not use `id` and + `filter` in the same rule). Needs `version: 2` option (if `version` is `1` the filter is ignored). NGSIv1 example: @@ -239,17 +320,20 @@ NGSIv2 example: "name":"abnormal", "type":"Number", "value": 7 - } + }, + { + "name": "locationCopy", + "type": "MyCustomTypo", + "value": "{\"type\":\"Point\",\"coordinates\":[${Lat},${Lon}]}" + } ] } } ``` -**Note:** NGSIv2 update actions ignore the trust token for now. - When using NGSIv2 in the update actions, the value field perform [string substitution](#string-substitution-syntax). If -`value` is a String, Perseo will parse the value taking into account the `type` field, this only applies to _`Number`_, -_`Boolean`_ and _`None`_ types. +`value` is a String, Perseo will try cast value to number, boolean or null (without paying attention to the attribute +type). If the casting fails then String is used. _`Boolean`_ and _`None`_ types. **Data Types for NGSIv2:** @@ -448,6 +532,32 @@ This attribute will take `null` as value. Note that using NGSIv2 the BloodPressure attribute is a Number and therefore it is not necessary to use `cast()`. +**Complete example using NGSv2 update action with filter in a rule:** + +```json +{ + "name": "blood_rule_update", + "text": "select *,\"blood_rule_update\" as ruleName, *, ev.BloodPressure? as Pressure from pattern [every ev=iotEvent(BloodPressure? > 1.5 and type=\"BloodMeter\")]", + "action": { + "type": "update", + "parameters": { + "filter": { + "type": "SensorMetter", + "q": "status:on" + }, + "version": 2, + "attributes": [ + { + "name": "pressure", + "type": "Number", + "value": "${Pressure}" + } + ] + } + } +} +``` + ### HTTP request action Makes an HTTP request to an URL specified in `url` inside `parameters`, sending a body built from `template`. The @@ -556,38 +666,40 @@ For example: The metadata in an event/notice like ```json { - "subscriptionId" : "51c04a21d714fb3b37d7d5a7", - "originator" : "localhost", - "contextResponses" : [ - { - "contextElement" : { - "attributes" : [ - { - "name" : "BloodPressure", - "type" : "centigrade", - "value" : "2", - "metadatas": [{ - "crs": { - "value": {"system": "WGS84"} - }] + "subscriptionId": "51c04a21d714fb3b37d7d5a7", + "originator": "localhost", + "contextResponses": [ + { + "contextElement": { + "attributes": [ + { + "name": "BloodPressure", + "type": "centigrade", + "value": "2", + "metadatas": [ + { + "crs": { + "value": { "system": "WGS84" } + } + } + ] + }, + { + "name": "TimeInstant", + "type": "urn:x-ogc:def:trs:IDAS:1.0:ISO8601", + "value": "2014-04-29T13:18:05Z" + } + ], + "type": "BloodMeter", + "isPattern": "false", + "id": "bloodm1" + }, + "statusCode": { + "code": "200", + "reasonPhrase": "OK" } - }, - { - "name" : "TimeInstant", - "type" : "urn:x-ogc:def:trs:IDAS:1.0:ISO8601", - "value" : "2014-04-29T13:18:05Z" - } - ], - "type" : "BloodMeter", - "isPattern" : "false", - "id" : "bloodm1" - }, - "statusCode" : { - "code" : "200", - "reasonPhrase" : "OK" - } - } - ] + } + ] } ``` @@ -626,8 +738,9 @@ respectively. The formats are -- [NGSV1 deprecated format](https://forge.fiware.org/plugins/mediawiki/wiki/fiware/index.php/Publish/Subscribe_Broker_-_Orion_Context_Broker_-_User_and_Programmers_Guide_R3#Defining_location_attribute) -- [NGSIV1 current format](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/user/geolocation.md#defining-location-attribute) +- [NGSIv1 deprecated format](https://fiware-orion.readthedocs.io/en/1.15.1/user/geolocation/index.html#defining-location-attribute) +- [NGSIv2 current format](http://telefonicaid.github.io/fiware-orion/api/v2/stable/), section "Geospatial properties + of entities" So, a notification in the deprecated format like @@ -752,8 +865,12 @@ that will send an email when the entity with attribute `position` is less than 5 circle equation, `(x - a)^2 + (y - b)^2 = d^2`, being `(a, b)` 618618.8286057833 and 9764160.736945232 the UTMC coordinates of Cuenca and `d` the distance of 5 000 m. -Note: for long distances the precision of the computations and the distortion of the projection can introduce some -degree of inaccuracy. +Notes: + +- NGSIv2 allows several geo location formats (geo:point, geo:line, geo:box, geo:polygon and geo:json). At the present + moment, Perseo only supports geo:point. +- For long distances the precision of the computations and the distortion of the projection can introduce some degree + of inaccuracy. ## Time fields @@ -931,3 +1048,81 @@ A rule that will check if the employee has been hired in the last half hour, cou } } ``` + +## JSON and Array fields in attributes + +Some attributes like JSON and Array based, will generate a pseudo-attribute with the same name as the attribute and a +suffix "\_\_" followed by element name (for the case of JSON) or the ordinal (for the case of arrays), with the parsed +value. This value makes easier to write the EPL text which involves time comparisons. + +So, an incoming notification like this: + +```json +{ + "subscriptionId": "51c04a21d714fb3b37d7d5a7", + "data": [ + { + "id": "John Doe", + "type": "employee", + "myJsonValue": { + "type": "myType1", + "value": { "color": "blue" } + }, + "myArrayValue": { + "type": "myType2", + "value": ["green", "blue"] + } + } + ] +} +``` + +will send to core the "event" + +```json +{ + "noticeId": "799635b0-914f-11e6-836b-bf1691c99768", + "noticeTS": 1476368120971, + "id": "John Doe", + "type": "employee", + "isPattern": "false", + "subservice": "/", + "service": "unknownt", + "myJsonValue__color": "blue", + "myArrayValue__0": "green", + "myArrayValue__1": "black" +} +``` + +Additionally all attributes are also included in non flatten format in the event into the `stripped` section: + +```json +{ + "noticeId": "799635b0-914f-11e6-836b-bf1691c99768", + "noticeTS": 1476368120971, + "id": "John Doe", + "type": "employee", + "isPattern": "false", + "subservice": "/", + "service": "unknownt", + "myJsonValue__color": "blue", + "myArrayValue__0": "green", + "myArrayValue__1": "black", + "stripped": [ + { + "myJsonValue": { + "type": "myType1", + "value": { + "color": "blue" + } + } + }, + { + "myArrayValue": { + "type": "myType2", + "value": ["green", "black"] + } + } + ] +} +``` diff --git a/lib/constants.js b/lib/constants.js index 8116c013..fad425d9 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -32,5 +32,6 @@ module.exports = { CORRELATOR_HEADER: 'fiware-correlator', AUTH_HEADER: 'X-Auth-Token', REALIP_HEADER: 'X-Real-IP', + TOTAL_COUNT_HEADER: 'fiware-total-count', COMPONENT_NAME: 'perseo-fe' }; diff --git a/lib/models/executionsStore.js b/lib/models/executionsStore.js index 61685e5d..0fd8a55b 100644 --- a/lib/models/executionsStore.js +++ b/lib/models/executionsStore.js @@ -42,19 +42,27 @@ module.exports = { myutils.logErrorIf(err); return callback(err, null); } - col.findOne( - { - $query: { name: ruleName, subservice: subservice, service: service, id: id, index: index }, - $orderby: { lastTime: -1 } - }, - function(error2, data) { - myutils.logErrorIf(error2); - if (err) { - return callback(error2, null); - } - return callback(null, (data && data.lastTime && data.lastTime.getTime()) || 0); + var cursor = col + .find( + { + name: ruleName, + subservice: subservice, + service: service, + id: id, + index: index + }, + {} + ) + .sort({ lastTime: -1 }); + cursor = cursor.limit(1); + cursor.toArray(function(err, results) { + myutils.logErrorIf(err); + if (err) { + return callback(err, null); } - ); + var data = results.length ? results[0] : null; + return callback(null, (data && data.lastTime && data.lastTime.getTime()) || 0); + }); }); }, AlreadyDone: function AlreadyDone(task, callback) { diff --git a/lib/models/metrics.js b/lib/models/metrics.js index 25deb9a1..6df70001 100644 --- a/lib/models/metrics.js +++ b/lib/models/metrics.js @@ -140,6 +140,7 @@ module.exports.failedRuleDelete = 'failedRuleDelete'; module.exports.firedRules = 'firedRules'; module.exports.errAction = 'errAction'; +module.exports.actionEntityFilter = 'actionEntityFilter'; module.exports.actionEntityUpdate = 'actionEntityUpdate'; module.exports.okActionEntityUpdate = 'okActionEntityUpdate'; module.exports.failedActionEntityUpdate = 'failedActionEntityUpdate'; diff --git a/lib/models/notices.js b/lib/models/notices.js index 3c90436f..446a9a86 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -206,6 +206,7 @@ function processCBNotice(service, subservice, ncr, ix) { }); n = myutils.flattenMap('', n); + n.stripped = ncr.contextResponses[ix].contextElement.attributes; } catch (ex) { // SHOULD BE ex instanceof TypeError. Do not do anything else inside try localError = new errors.InvalidNotice(JSON.stringify(ncr)); @@ -305,6 +306,7 @@ function processCBv2Notice(service, subservice, ncr, ix) { }); n = myutils.flattenMap('', n); + n.stripped = ncr.data[ix]; } catch (ex) { // Should never reach this catch var localError = new errors.InvalidV2Notice(ex + ' (' + JSON.stringify(ncr) + ')'); diff --git a/lib/models/postAction.js b/lib/models/postAction.js index 37f6f192..71ff5cef 100644 --- a/lib/models/postAction.js +++ b/lib/models/postAction.js @@ -37,7 +37,7 @@ function buildPostOptions(action, event) { if (action.parameters.json) { options.json = myutils.expandObject(action.parameters.json, event); } else if (action.template) { - options.text = myutils.expandVar(action.template, event); + options.text = myutils.expandVar(action.template, event, true); } else { //Added default else clause diff --git a/lib/models/smsAction.js b/lib/models/smsAction.js index caeabf3b..c5cb5b2e 100644 --- a/lib/models/smsAction.js +++ b/lib/models/smsAction.js @@ -35,11 +35,22 @@ function buildSMSOptions(action, event) { to: myutils.expandVar(action.parameters.to, event) }; } + +function buildTo(to) { + const splitChar = ' '; + var tos = to.split(splitChar); + var r = []; + tos.forEach(function(dest) { + r.push('tel:' + dest); + }); + return r; +} + function doIt(action, event, callback) { try { var options, msg; options = buildSMSOptions(action, event); - msg = { to: ['tel:' + options.to], message: options.text, from: config.sms.from }; + msg = { to: buildTo(options.to), message: options.text, from: config.sms.from }; metrics.IncMetrics(event.service, event.subservice, metrics.actionSMS); diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index fb2ddb96..f7eef130 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -103,7 +103,7 @@ function buildUpdateOptions(action, event) { for (var i = 0; i < action.parameters.attributes.length; i++) { attr = { name: myutils.expandVar(action.parameters.attributes[i].name, event), - value: myutils.expandVar(action.parameters.attributes[i].value, event) + value: myutils.expandVar(action.parameters.attributes[i].value, event, true) }; //The portal does not provide 'type' for attribute, "plain" rules could @@ -119,6 +119,18 @@ function buildUpdateOptions(action, event) { return options; } + +function buildQueryOptionsParams(action, event) { + var filter = {}; + + Object.keys(action.parameters.filter).forEach(function(key) { + filter[key] = myutils.expandVar(action.parameters.filter[key], event); + }); + + logger.debug('filter built: %j', filter); + return filter; +} + function doRequest(action, event, token, callback) { var subservice, service, headers, options, updateOrion; @@ -198,42 +210,6 @@ function makeTokenListenerFunc(action, event, callback) { }; } -function doItWithToken(action, event, callback) { - var subservice, service, ruleName, cached, newTokenListener; - - subservice = event.subservice; - service = event.service; - ruleName = event.ruleName; - newTokenListener = makeTokenListenerFunc(action, event, callback); - - cached = getCachedToken(service, subservice, ruleName); - if (cached.token === null) { - // First time token is generated from trust - cached.emitter.once(newTokenEventName, newTokenListener); - if (cached.generating === false) { - logger.debug('generating token for %s %s %s', service, subservice, ruleName); - generateToken(action.parameters.trust, cached); - } - } else if (cached.generating === true) { - // In the middle of getting a new one - logger.debug('waiting token to be generated %s %s %s', service, subservice, ruleName); - cached.emitter.once(newTokenEventName, newTokenListener); - } else { - doRequest(action, event, cached.token, function cbDoReqUpdAxn(error, data) { - logger.debug('cbDoReqUpdAxn %s %s', error, data); - // If authorization error, we should call generateToken and addListener to retry - if (error && data.code === 401) { - cached.emitter.once(newTokenEventName, newTokenListener); - if (cached.generating === false) { - generateToken(action.parameters.trust, cached); - } - } else { - return callback(error, data); - } - }); - } -} - /** * Process NGSIv2 action do request option Params * @@ -251,39 +227,50 @@ function processOptionParams(action, event) { action.parameters.attributes = action.parameters.attributes || []; var changes = {}; + // old format, with only one attribute to update + if (action.parameters.name) { + action.parameters.attributes.push({ + name: action.parameters.name, + value: action.parameters.value, + type: action.parameters.attrType + }); + } action.parameters.attributes.forEach(function(attr) { // Direct value for Text, DateTime, and others. Apply expandVar for strings - let theValue = myutils.expandVar(attr.value, event); + let theValue = myutils.expandVar(attr.value, event, true); let theType = myutils.expandVar(attr.type, event); // Metadata should be null or object let theMeta = attr.metadata; - var date; - - switch (theType) { - case 'Text': - theValue = theValue.toString(); - break; - case 'Number': - theValue = parseFloat(theValue); - break; - case 'Boolean': - if (typeof theValue === 'string') { - theValue = theValue.toLowerCase().trim() === 'true'; - } - break; - case 'DateTime': - if (parseInt(theValue).toString() === theValue) { - // Parse String with number (timestamp__ts in Perseo events) - theValue = parseInt(theValue); - } - date = new Date(theValue); - theValue = isNaN(date.getTime()) ? theValue : date.toISOString(); - break; - case 'None': - theValue = null; - break; - default: - //nothing to do. + + if (config.castTypes) { + // Cast using NGSIv2 types + var date; + switch (theType) { + case 'Text': + theValue = theValue.toString(); + break; + case 'Number': + theValue = parseFloat(theValue); + break; + case 'Boolean': + if (typeof theValue === 'string') { + theValue = theValue.toLowerCase().trim() === 'true'; + } + break; + case 'DateTime': + if (parseInt(theValue).toString() === theValue) { + // Parse String with number (timestamp__ts in Perseo events) + theValue = parseInt(theValue); + } + date = new Date(theValue); + theValue = isNaN(date.getTime()) ? theValue : date.toISOString(); + break; + case 'None': + theValue = null; + break; + default: + //Nothing to do + } } var key = myutils.expandVar(attr.name, event); changes[key] = { @@ -300,6 +287,68 @@ function processOptionParams(action, event) { return changes; } +/** + * Process NGSIv2 action do request option Params + * + * @param action The request update action information + * @param entity NGSI entity + * + * @return options object + */ +function processUpdateOptionParams(action, entity) { + action.parameters.attributes = action.parameters.attributes || []; + + var options = {}; + action.parameters.attributes.forEach(function(attr) { + let theValue = attr.value; + let theType = attr.type; + + // Metadata should be null or object + let theMeta = attr.metadata; + + if (config.castTypes) { + // Cast using NGSIv2 types + var date; + switch (theType) { + case 'Text': + theValue = theValue.toString(); + break; + case 'Number': + theValue = parseFloat(theValue); + break; + case 'Boolean': + if (typeof theValue === 'string') { + theValue = theValue.toLowerCase().trim() === 'true'; + } + break; + case 'DateTime': + if (parseInt(theValue).toString() === theValue) { + // Parse String with number (timestamp__ts in Perseo events) + theValue = parseInt(theValue); + } + date = new Date(theValue); + theValue = isNaN(date.getTime()) ? theValue : date.toISOString(); + break; + case 'None': + theValue = null; + break; + } + } + var key = attr.name; + options[key] = { + value: theValue, + type: theType + }; + if (attr.metadata !== undefined) { + options[key].metadata = theMeta; + } + }); + options.id = entity.id; + options.type = entity.type; + logger.debug('options built: %j', options); + return options; +} + /** * Manage update action request for NGSv2 request * @@ -309,39 +358,187 @@ function processOptionParams(action, event) { * * @return changes object */ -function doRequestV2(action, event, callback) { +function doRequestV2(action, event, token, callback) { var options = { service: event.service, servicepath: event.subservice }; + options.headers = {}; + if (token !== null) { + options.headers[constants.AUTH_HEADER] = token; + } + // Add correlator + var domain = process.domain; + if (domain && domain.context) { + options.headers[constants.CORRELATOR_HEADER] = domain.context.corr; + // Add other headers + if (domain.context.srv && options.headers[constants.SERVICE_HEADER] === undefined) { + options.headers[constants.SERVICE_HEADER] = domain.context.srv; + } + if (domain.context.subsrv && options.headers[constants.SUBSERVICE_HEADER] === undefined) { + options.headers[constants.SUBSERVICE_HEADER] = domain.context.subsrv; + } + if (domain.context.from && options.headers[constants.REALIP_HEADER] === undefined) { + options.headers[constants.REALIP_HEADER] = domain.context.from; + } + } + var connection = new NGSI.Connection(config.orion.URL, options); - // NGSIv2 update action with trust token (similar to doItWithToken for NGSIv1) + // check if filter in action + if (action && action.parameters && action.parameters.filter) { + // UpdateAction with filter + logger.debug('action: %j, event: %j', action, event); + + var queryOptions = buildQueryOptionsParams(action, event); + + // + // 1. Make query with filter + // + connection.v2.listEntities(queryOptions).then( + (response) => { + // Entities retrieved successfully + // response.results is an array with the retrieved entities + // response.correlator transaction id associated with the server response + // response.count contains the total number of entities selected + // by this query + // response.offset contains the offset used in the request + + metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityFilter); + alarm.release(alarm.ORION); - var connection = new NGSI.Connection(config.orion.URL, options); + logger.debug('full query response: %j', response); - var changes = processOptionParams(action, event); - metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate); + var changes = { + actionType: 'append', + entities: [] + }; - connection.v2.createEntity(changes, { upsert: true }).then( - (response) => { - metrics.IncMetrics(event.service, event.subservice, metrics.okActionEntityUpdate); - alarm.release(alarm.ORION); - callback(null, response); - }, - (error) => { - metrics.IncMetrics(event.service, event.subservice, metrics.failedActionEntityUpdate); - alarm.raise(alarm.ORION, null, error); - callback(error, null); + if (action.parameters.actionType !== undefined) { + options.actionType = myutils.expandVar(action.parameters.actionType, event); + } + + if (response && response.results) { + // Process response (array of entities) + response.results.forEach(function(entity) { + var updateOptions = processUpdateOptionParams(action, entity); + changes.entities.push(updateOptions); + }); // For each + } + + logger.debug('entities to update: %j', changes); + metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate); + // + // 2. Apply update to all entities in a batch + // + connection.v2.batchUpdate(changes).then( + (response) => { + // Entities updated successfully + // response.correlator transaction id associated with the server response + logger.debug('response batchUpdate: %j', response); + metrics.IncMetrics(event.service, event.subservice, metrics.okActionEntityUpdate); + alarm.release(alarm.ORION); + callback(null, response); + }, + (error) => { + // Error updating entities + // If the error was reported by Orion, error.correlator will be + // filled with the associated transaction id + metrics.IncMetrics(event.service, event.subservice, metrics.failedActionEntityUpdate); + logger.warn('error batchUpdate%j', error); + alarm.raise(alarm.ORION, null, error); + } + ); // batchUpdate + }, + (error) => { + // Error retrieving entities + // If the error was reported by Orion, error.correlator will be + // filled with the associated transaction id + metrics.IncMetrics(event.service, event.subservice, metrics.failedActionEntityUpdate); + logger.warn('error filtering %j', error); + alarm.raise(alarm.ORION, null, error); + callback(error, null); + } + ); + } else { + // classical updateAction (without filter) + + var changes = { + actionType: 'append', + entities: [processOptionParams(action, event)] + }; + if (action.parameters.actionType !== undefined) { + changes.actionType = myutils.expandVar(action.parameters.actionType, event); } - ); + metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate); + logger.debug('entity to update: %j', changes); + connection.v2.batchUpdate(changes).then( + (response) => { + // response.correlator transaction id associated with the server response + metrics.IncMetrics(event.service, event.subservice, metrics.okActionEntityUpdate); + alarm.release(alarm.ORION); + callback(null, response); + }, + (error) => { + metrics.IncMetrics(event.service, event.subservice, metrics.failedActionEntityUpdate); + alarm.raise(alarm.ORION, null, error); + callback(error, null); + } + ); + } +} + +function doItWithToken(action, event, version, callback) { + var subservice, service, ruleName, cached, newTokenListener; + + function cbDoReqUpdAxn(error, data) { + logger.debug('cbDoReqUpdAxn %s %s', error, data); + // If authorization error, we should call generateToken and addListener to retry + if (error && data.code === 401) { + cached.emitter.once(newTokenEventName, newTokenListener); + if (cached.generating === false) { + generateToken(action.parameters.trust, cached); + } + } else { + return callback(error, data); + } + } + + subservice = event.subservice; + service = event.service; + ruleName = event.ruleName; + newTokenListener = makeTokenListenerFunc(action, event, callback); + + cached = getCachedToken(service, subservice, ruleName); + if (cached.token === null) { + // First time token is generated from trust + cached.emitter.once(newTokenEventName, newTokenListener); + if (cached.generating === false) { + logger.debug('generating token for %s %s %s', service, subservice, ruleName); + generateToken(action.parameters.trust, cached); + } + } else if (cached.generating === true) { + // In the middle of getting a new one + logger.debug('waiting token to be generated %s %s %s', service, subservice, ruleName); + cached.emitter.once(newTokenEventName, newTokenListener); + } else { + if (version === 1) { + doRequest(action, event, cached.token, cbDoReqUpdAxn); + } else { + doRequestV2(action, event, cached.token, cbDoReqUpdAxn); + } + } } function doIt(action, event, callback) { try { - if (action.parameters.version === '2' || action.parameters.version === 2) { - return doRequestV2(action, event, callback); + if (action.parameters.version !== '1' && action.parameters.version !== 1) { + if (action.parameters.trust) { + return doItWithToken(action, event, 2, callback); + } else { + return doRequestV2(action, event, null, callback); + } } else if (action.parameters.trust) { - return doItWithToken(action, event, callback); + return doItWithToken(action, event, 1, callback); } else { return doRequest(action, event, null, callback); } diff --git a/lib/myutils.js b/lib/myutils.js index 495c17c9..8977a6d6 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -53,13 +53,32 @@ function logErrorIf(err, message, context) { } } -function expandVar(val, mapping) { +function expandVar(val, mapping, trycast) { var valTemp = val; if (typeof valTemp === 'string') { Object.keys(mapping).forEach(function(p) { valTemp = valTemp.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]); }); - valTemp = valTemp.replace(/\$\{\w*\}/g, '[?]'); + var expanded = false; + if (trycast) { + try { + // Check if "${num}" and expand it as real value, non string + var n = JSON.parse(valTemp); + switch (typeof n) { + case 'number': + case 'boolean': + case 'object': // for a json in a string like: "{\"type\":\"Point\"}" + valTemp = n; + expanded = true; + break; + } + } catch (e) { + // keep val value + } + } + if (!expanded) { + valTemp = valTemp.replace(/\$\{\w*\}/g, '[?]'); + } } return valTemp; } diff --git a/lib/routes/noticesController.js b/lib/routes/noticesController.js index e14d007f..90724d11 100644 --- a/lib/routes/noticesController.js +++ b/lib/routes/noticesController.js @@ -25,6 +25,7 @@ var notices = require('../models/notices'), metrics = require('../models/metrics'), config = require('../../config'), + logger = require('logops'), myutils = require('../myutils'); function PostNotice(req, resp) { @@ -33,6 +34,7 @@ function PostNotice(req, resp) { notice = req.body; notice.subservice = req.subservice; notice.service = req.service; + logger.info({}, 'posting notice ' + notice); metrics.IncMetrics(req.service, req.subservice, metrics.notifications); notices.Do(notice, function(err, data) { if (err) { diff --git a/lib/routes/rulesController.js b/lib/routes/rulesController.js index ed565d0c..5e393996 100644 --- a/lib/routes/rulesController.js +++ b/lib/routes/rulesController.js @@ -24,12 +24,22 @@ var rules = require('../models/rules'), metrics = require('../models/metrics'), + constants = require('../constants'), config = require('../../config'), + logger = require('logops'), + util = require('util'), myutils = require('../myutils'); function GetAllRules(req, resp) { + var limit = req.query && req.query.limit ? req.query.limit : 1000; + var offset = req.query && req.query.offset ? req.query.offset : 0; + var count = req.query && req.query.options ? req.query.options === 'count' : true; rules.FindAll(req.service, req.subservice, function(err, data) { - myutils.respond(resp, err, data); + if (util.isArray(data)) { + resp.set(constants.TOTAL_COUNT_HEADER, data.length); + data = data.splice(offset, limit); + } + myutils.respond(resp, err, data, count); }); } function GetRules(req, resp) { @@ -38,6 +48,7 @@ function GetRules(req, resp) { subservice: req.subservice, service: req.service }; + logger.info({}, 'getting rule %j', rule); rules.Find(rule, function(err, data) { myutils.respond(resp, err, data); }); @@ -46,6 +57,7 @@ function PostRules(req, resp) { req.body = req.body || {}; req.body.subservice = req.subservice; req.body.service = req.service; + logger.info({}, 'posting rule %j', req.body); metrics.IncMetrics(req.service, req.subservice, metrics.ruleCreation); rules.Save(req.body, function(err, data) { if (err) { @@ -62,6 +74,7 @@ function DelRules(req, resp) { subservice: req.subservice, service: req.service }; + logger.info({}, 'deleting rule %j', rule); metrics.IncMetrics(req.service, req.subservice, metrics.ruleDelete); rules.Remove(rule, function(err, data) { if (err) { diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 0e42f8cc..7e84ffe5 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -26,9 +26,14 @@ var util = require('util'), visualRules = require('../models/visualRules'), metrics = require('../models/metrics'), config = require('../../config'), + constants = require('../constants'), + logger = require('logops'), myutils = require('../myutils'); function GetAllVR(req, resp) { + var limit = req.query.limit ? req.query.limit : 1000; + var offset = req.query.offset ? req.query.offset : 0; + var count = req.query && req.query.options ? req.query.options === 'count' : true; visualRules.FindAll(req.service, req.subservice, function(err, data) { if (!err && util.isArray(data)) { var reformatData = data.map(function(e) { @@ -36,8 +41,10 @@ function GetAllVR(req, resp) { delete e.service; return e; }); + resp.set(constants.TOTAL_COUNT_HEADER, data.length); + data = data.splice(offset, limit); } - myutils.respond(resp, err, reformatData, true); + myutils.respond(resp, err, data, count); }); } function GetVR(req, resp) { @@ -46,6 +53,7 @@ function GetVR(req, resp) { subservice: req.subservice, service: req.service }; + logger.info({}, 'getting Vrule ' + rule); visualRules.Find(rule, function(err, data) { if (!err && data) { delete data.subservice; @@ -58,7 +66,7 @@ function PostVR(req, resp) { req.body = req.body || {}; req.body.subservice = req.subservice; req.body.service = req.service; - + logger.info({}, 'posting Vrule ' + req.body); metrics.IncMetrics(req.service, req.subservice, metrics.ruleCreation); visualRules.Save(req.body, function(err, data) { @@ -84,7 +92,7 @@ function DelVR(req, resp) { subservice: req.subservice, service: req.service }; - + logger.info({}, 'deleting Vrule ' + rule); metrics.IncMetrics(req.service, req.subservice, metrics.ruleDelete); visualRules.Remove(rule, function(err, data) { @@ -105,7 +113,7 @@ function PutVR(req, resp) { req.body = req.body || {}; req.body.subservice = req.subservice; req.body.service = req.service; - + logger.info({}, 'putting Vrule ' + req.body); metrics.IncMetrics(req.service, req.subservice, metrics.ruleUpdate); visualRules.Put(req.params.id, req.body, function(err, data) { diff --git a/package.json b/package.json index 08345981..7efe3ece 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "perseo", "description": "IOT CEP front End", "license": "AGPL-3.0-only", - "version": "1.8.0-next", + "version": "1.10.0", "homepage": "", "author": { "name": "crbrox", @@ -62,9 +62,10 @@ "body-parser": "~1.18.2", "express": "~4.16.1", "logops": "2.1.0", - "mongodb": "~2.2.31", - "ngsijs": "~1.2.0", - "nodemailer": "~6.0.0", + "mongodb": "~2.2.35", + "ngsijs": "~1.2.1", + "nodemailer": "~1.11.0", + "nodemailer-smtp-transport": "~0.1.13", "request": "~2.83.0", "smpp": "0.3.1", "swagger-jsdoc": "^3.4.0", diff --git a/rpm/SPECS/cep.spec b/rpm/SPECS/cep.spec index 14e8236e..20cf1bc5 100644 --- a/rpm/SPECS/cep.spec +++ b/rpm/SPECS/cep.spec @@ -164,6 +164,30 @@ rm -rf $RPM_BUILD_ROOT %{_install_dir} %changelog +* Tue Oct 29 2019 Fermin Galan 1.10.0 +- Add: include stripped (non flatten) data into event (#377) +- Add: castType (PERSEO_CAST_TYPE) to enable NGSIv2 type based casting (default is to use JSON native types in values) +- Add: authentication config env vars (#349) +- Add: full support for pagination in APIs /rules and /vrules (#364) +- Add: Fiware-Total-Count header to response when count +- Add: missed correlatorid headers in updateAction with NGSIv2 +- Add: try expandVars for numeric, boolean and json in attributes of updateAction and text of postAction (#362) +- Add: NGSI filter to updateAction (#335) +- Add: count to response to get all rules (cep + vr) +- Add: update entity using NGSIv2 with a trust token (#317) +- Add: new logs about rule provision, deleting, etc (#346) +- Add: config env vars for isMaster and slaveDelay +- Set default version to 2 (NGSIv2) for updateAction by default +- Refactor updateAction doItWithToken to include version 1 and 2 +- Upgrade NodeJS version from 8.16.0 to 8.16.1 in Dockerfile due to security issues +- Upgrade ngsijs dependency from 1.2.0 to 1.2.1 +- Upgrade mongodb dependency from ~2.2.31 to ~2.2.35 + +* Tue Jun 04 2019 Fermin Galan 1.9.0 +- Upgrade from node:8.12.0-slim to node:8.16.0-slim as base image in Dockerfile +- Add: allow SMS actions with multiple phone destinations (#337) +- Fix: replace findOne + orderby to find + sort + limit in LastTime executionsStore (#339) + * Fri Feb 08 2019 Fermin Galan 1.8.0 - Add: NGSIv2 support in both notification reception and CB update action - Change on the PERSEO_ORION_URL env var behaviour. Now it represents Context Broker base URL instead of the diff --git a/test/acceptance/integration/Rules/EPL/epl_create_rules.feature b/test/acceptance/integration/Rules/EPL/epl_create_rules.feature index 3367ad31..f8e65c1e 100644 --- a/test/acceptance/integration/Rules/EPL/epl_create_rules.feature +++ b/test/acceptance/integration/Rules/EPL/epl_create_rules.feature @@ -71,6 +71,21 @@ Feature: Append a new rule in Perseo manager Then I receive an "200" http code in rules request And Validate that rule name is created successfully in db + @happy_path + Scenario: Append a sms multiple rule in Perseo manager + # Gen EPL + Given an EPL sentence with name "sms_rule" + And the entity_type "Room" for the EPL + And the attributes for the EPL + | attribute_id | attribute_value_type | attribute_operation | attribute_value | + | temperature | float | > | 1.5 | + And generate the epl sentence with the data defined before + # Create the Rule + And set the sms action with text "the new temperature is ${temperature}" and number "666999666 777777" + And with the epl generated and the action, append a new rule in perseo with name "sms_rule" + # Validations + Then I receive an "200" http code in rules request + And Validate that rule name is created successfully in db @happy_path Scenario: Append a post rule in Perseo manager diff --git a/test/data/bad_rules/rule_update_action_id_attr.json b/test/data/bad_rules/rule_update_action_id_attr.json index 685d9267..bba0c0e5 100644 --- a/test/data/bad_rules/rule_update_action_id_attr.json +++ b/test/data/bad_rules/rule_update_action_id_attr.json @@ -5,6 +5,7 @@ "type": "update", "template": "Meter ${Meter} has pression ${Pression}: ${ev__complejo__saludo} (GEN RULE)", "parameters": { + "version": "1", "name": "id", "value": "ID_VALUE" } diff --git a/test/data/bad_rules/rule_update_action_type_attr.json b/test/data/bad_rules/rule_update_action_type_attr.json index d8a84373..a7eb7e69 100644 --- a/test/data/bad_rules/rule_update_action_type_attr.json +++ b/test/data/bad_rules/rule_update_action_type_attr.json @@ -5,6 +5,7 @@ "type": "update", "template": "Meter ${Meter} has pression ${Pression}: ${ev__complejo__saludo} (GEN RULE)", "parameters": { + "version": "1", "name": "type", "value": "TYPE_VALUE" } diff --git a/test/data/error_in_axn_rules/blood_rule_update.json b/test/data/error_in_axn_rules/blood_rule_update.json index 591aa2ac..2d41bf5c 100644 --- a/test/data/error_in_axn_rules/blood_rule_update.json +++ b/test/data/error_in_axn_rules/blood_rule_update.json @@ -4,6 +4,7 @@ "action": { "type": "update", "parameters": { + "version": "1", "name": "abnormal", "value": "true", "type": "boolean" diff --git a/test/data/good_rules/blood_rule_update.json b/test/data/good_rules/blood_rule_update.json index 591aa2ac..2d41bf5c 100644 --- a/test/data/good_rules/blood_rule_update.json +++ b/test/data/good_rules/blood_rule_update.json @@ -4,6 +4,7 @@ "action": { "type": "update", "parameters": { + "version": "1", "name": "abnormal", "value": "true", "type": "boolean" diff --git a/test/data/good_rules/blood_rule_update_trust.json b/test/data/good_rules/blood_rule_update_trust.json index f6dc549f..2a3a3dbf 100644 --- a/test/data/good_rules/blood_rule_update_trust.json +++ b/test/data/good_rules/blood_rule_update_trust.json @@ -4,6 +4,7 @@ "action": { "type": "update", "parameters": { + "version": "1", "name": "abnormal", "value": "true", "type": "boolean", diff --git a/test/unit/updateAction.js b/test/unit/updateAction.js index 06f901fd..35cc44eb 100644 --- a/test/unit/updateAction.js +++ b/test/unit/updateAction.js @@ -106,7 +106,7 @@ var action1 = { { name: 'isBool2', type: 'Boolean', - value: 'TRUE' + value: 'true' }, { name: 'isBool3', @@ -116,7 +116,7 @@ var action1 = { { name: 'isBool4', type: 'Boolean', - value: 'False' + value: 'false' }, { name: 'isBool5', @@ -156,7 +156,7 @@ var action1 = { { name: 'refNone', type: '${refNoneType}', - value: 'futureNull' + value: null }, { name: 'refNone2', @@ -182,7 +182,7 @@ var event1 = { addressLocality: 'Stockholm', laststatus: 'allright', powerState: 'on', - stringDate: '2018-12-05T11:31:39.00Z', + stringDate: '2018-12-05T11:31:39.000Z', stringDateMs: '1548843060657', numberDateMs: 1548843229832, subservice: '/', @@ -199,105 +199,116 @@ var event1 = { subsrv: '/' } }; - var expectedChanges = { - address: { - value: 'Vasagatan 1, Stockholm', - type: 'Address' - }, - status: { - value: 'allright', - type: 'Text' - }, - textBoolLit: { - value: 'false', - type: 'Text' - }, - textNumberLit: { - value: '666', - type: 'Text' - }, - textObjLit: { - value: '[object Object]', - type: 'Text' - }, - refNone: { - value: null, - type: 'None' - }, - refNone2: { - value: null, - type: 'None' - }, - refNone3: { - value: null, - type: 'None' - }, - isBool1: { - value: true, - type: 'Boolean', - metadata: metaExample - }, - isBool2: { - value: true, - type: 'Boolean' - }, - isBool3: { - value: true, - type: 'Boolean' - }, - isBool4: { - value: false, - type: 'Boolean' - }, - isBool5: { - value: false, - type: 'Boolean' - }, - isBool6: { - value: true, - type: 'Boolean' - }, - isBool7: { - value: false, - type: 'Boolean' - }, - powerState: { - value: 'on', - type: 'Text' - }, - illuminanceLevel: { - value: 80, - type: 'Number' - }, - illuminanceLevel2: { - value: 69, - type: 'Number' - }, - illuminanceLevel3: { - value: 12.5, - type: 'Number' - }, - streetLightID: { - value: 'AmbientLightSensor:1', - type: 'Text' - }, - district: { - value: 'Stockholm center', - type: 'Text' - }, - lastchange: { - value: '2018-12-05T11:31:39.000Z', - type: 'DateTime' - }, - lastchange2: { - value: '2019-01-30T10:11:00.657Z', - type: 'DateTime' - }, - lastchange3: { - value: '2019-01-30T10:13:49.832Z', - type: 'DateTime' - } + actionType: 'append', + entities: [ + { + address: { + value: 'Vasagatan 1, Stockholm', + type: 'Address' + }, + status: { + value: 'allright', + type: 'Text' + }, + textBoolLit: { + value: false, + type: 'Text' + }, + textNumberLit: { + value: 666, + type: 'Text' + }, + textObjLit: { + value: { a: 1, b: 2 }, + type: 'Text' + }, + refNone: { + value: null, + type: 'None' + }, + refNone2: { + value: 123, + type: 'None' + }, + refNone3: { + value: null, + type: 'None' + }, + isBool1: { + value: true, + type: 'Boolean', + metadata: metaExample + }, + isBool2: { + value: true, + type: 'Boolean' + }, + isBool3: { + value: true, + type: 'Boolean' + }, + isBool4: { + value: false, + type: 'Boolean' + }, + isBool5: { + value: 'other', + type: 'Boolean' + }, + isBool6: { + value: true, + type: 'Boolean' + }, + isBool7: { + value: false, + type: 'Boolean' + }, + powerState: { + value: 'on', + type: 'Text' + }, + illuminanceLevel: { + value: 80, + type: 'Number' + }, + illuminanceLevel2: { + value: 69, + type: 'Number' + }, + illuminanceLevel3: { + value: 12.5, + type: 'Number' + }, + streetLightID: { + value: 'AmbientLightSensor:1', + type: 'Text' + }, + district: { + value: 'Stockholm center', + type: 'Text' + }, + lastchange: { + value: '2018-12-05T11:31:39.000Z', + type: 'DateTime' + }, + lastchange2: { + value: 1548843060657, + type: 'DateTime' + }, + lastchange3: { + value: 1548843229832, + type: 'DateTime' + } + } + ] +}; + +var queryOptions = { type: 'AmbientLightSensor' }; + +var expectedChanges2 = { + actionType: 'append', + entities: [] }; describe('doIt', function() { @@ -306,21 +317,21 @@ describe('doIt', function() { it('should accept NGSIv2 entities', function(done) { // Mocks - var createEntityThen = sinon.spy(function(successCB, errorCB) { + var batchUpdateThen = sinon.spy(function(successCB, errorCB) { setTimeout(function() { successCB({ httpCode: '200', message: 'all right' }); // success callback }, 0); return '__TEST'; }); - var createEntityMock = sinon.spy(function(changes, options) { - return { then: createEntityThen }; + var batchUpdateMock = sinon.spy(function(changes, options) { + return { then: batchUpdateThen }; }); var NGSICloseMock = sinon.spy(function() { return 'closed'; }); var NGSIConnectionMock = sinon.spy(function() { return { - v2: { createEntity: createEntityMock }, + v2: { batchUpdate: batchUpdateMock }, close: NGSICloseMock }; }); @@ -332,9 +343,9 @@ describe('doIt', function() { should.exist(request); should.not.exist(e); should.equal(request.httpCode, 200); - expectedChanges.id = 'AmbientLightSensor:1_NGSIv2Test'; - expectedChanges.type = 'NGSIv2TypesTest'; - createEntityMock.should.be.calledOnceWith(expectedChanges, { upsert: true }); + expectedChanges.entities[0].id = 'AmbientLightSensor:1_NGSIv2Test'; + expectedChanges.entities[0].type = 'NGSIv2TypesTest'; + batchUpdateMock.should.be.calledOnceWith(expectedChanges); done(); }; action1.parameters.id = '${id}_NGSIv2Test'; @@ -345,21 +356,21 @@ describe('doIt', function() { it('should accept NGSIv2 entities without type and id', function(done) { // Mocks - var createEntityThen = sinon.spy(function(successCB, errorCB) { + var batchUpdateThen = sinon.spy(function(successCB, errorCB) { setTimeout(function() { successCB({ httpCode: '200', message: 'all right' }); // success callback }, 0); return '__TEST'; }); - var createEntityMock = sinon.spy(function(changes, options) { - return { then: createEntityThen }; + var batchUpdateMock = sinon.spy(function(changes, options) { + return { then: batchUpdateThen }; }); var NGSICloseMock = sinon.spy(function() { return 'closed'; }); var NGSIConnectionMock = sinon.spy(function() { return { - v2: { createEntity: createEntityMock }, + v2: { batchUpdate: batchUpdateMock }, close: NGSICloseMock }; }); @@ -371,9 +382,9 @@ describe('doIt', function() { should.exist(request); should.not.exist(e); should.equal(request.httpCode, 200); - expectedChanges.id = 'AmbientLightSensor:1'; - expectedChanges.type = 'AmbientLightSensor'; - createEntityMock.should.be.calledOnceWith(expectedChanges, { upsert: true }); + expectedChanges.entities[0].id = 'AmbientLightSensor:1'; + expectedChanges.entities[0].type = 'AmbientLightSensor'; + batchUpdateMock.should.be.calledOnceWith(expectedChanges); done(); }; delete action1.parameters.id; @@ -385,21 +396,21 @@ describe('doIt', function() { it('should control failed update actions', function(done) { // Mocks var theCBError = new Error(); - var createEntityThen = sinon.spy(function(successCB, errorCB) { + var batchUpdateThen = sinon.spy(function(successCB, errorCB) { setTimeout(function() { errorCB(theCBError); // success callback }, 0); return '__TEST'; }); - var createEntityMock = sinon.spy(function(changes, options) { - return { then: createEntityThen }; + var batchUpdateMock = sinon.spy(function(changes, options) { + return { then: batchUpdateThen }; }); var NGSICloseMock = sinon.spy(function() { return 'closed'; }); var NGSIConnectionMock = sinon.spy(function() { return { - v2: { createEntity: createEntityMock }, + v2: { batchUpdate: batchUpdateMock }, close: NGSICloseMock }; }); @@ -411,9 +422,9 @@ describe('doIt', function() { should.not.exist(request); should.exist(e); e.should.be.instanceof(Error); - expectedChanges.id = 'AmbientLightSensor:1_NGSIv2Test'; - expectedChanges.type = 'NGSIv2TypesTest'; - createEntityMock.should.be.calledOnceWith(expectedChanges, { upsert: true }); + expectedChanges.entities[0].id = 'AmbientLightSensor:1_NGSIv2Test'; + expectedChanges.entities[0].type = 'NGSIv2TypesTest'; + batchUpdateMock.should.be.calledOnceWith(expectedChanges); done(); }; action1.parameters.id = '${id}_NGSIv2Test'; @@ -421,5 +432,57 @@ describe('doIt', function() { updateAction.doIt(action1, event1, callback); }); }); + + it('should accept NGSIv2 entities with filter', function(done) { + // Mocks + var listEntitiesThen = sinon.spy(function(successCB, errorCB) { + setTimeout(function() { + successCB({ httpCode: '200', message: 'all right' }); // success callback + }, 0); + return '__TEST'; + }); + var batchUpdateThen = sinon.spy(function(successCB, errorCB) { + setTimeout(function() { + successCB({ httpCode: '200', message: 'all right' }); // success callback + }, 0); + return '__TEST'; + }); + var listEntitiesMock = sinon.spy(function(changes, options) { + return { then: listEntitiesThen }; + }); + var batchUpdateMock = sinon.spy(function(changes, options) { + return { then: batchUpdateThen }; + }); + var NGSICloseMock = sinon.spy(function() { + return 'closed'; + }); + var NGSIConnectionMock = sinon.spy(function() { + return { + v2: { + listEntities: listEntitiesMock, + batchUpdate: batchUpdateMock + }, + close: NGSICloseMock + }; + }); + + updateAction.__with__({ + 'NGSI.Connection': NGSIConnectionMock + })(function() { + var callback = function(e, request) { + should.exist(request); + should.not.exist(e); + should.equal(request.httpCode, 200); + queryOptions.type = 'NGSIv2TypesTest2'; + listEntitiesMock.should.be.calledOnceWith(queryOptions); + batchUpdateMock.should.be.calledOnceWith(expectedChanges2); + done(); + }; + action1.parameters.type = 'NGSIv2TypesTest2'; + action1.parameters.version = 2; + action1.parameters.filter = { type: 'NGSIv2TypesTest2' }; + updateAction.doIt(action1, event1, callback); + }); + }); }); }); diff --git a/test/utils/clients.js b/test/utils/clients.js index 5e324ba7..9cb53588 100644 --- a/test/utils/clients.js +++ b/test/utils/clients.js @@ -77,7 +77,12 @@ function GetRule(ruleName, callback) { commonRequest('get', url, null, callback); } function GetAllRules(callback) { - var url = util.format('http://%s:%s%s', config.endpoint.host, config.endpoint.port, config.endpoint.rulesPath); + var url = util.format( + 'http://%s:%s%s', + config.endpoint.host, + config.endpoint.port, + config.endpoint.rulesPath + '?limit=100&offset=0&options=count' + ); commonRequest('get', url, null, callback); } From c5a694f66470de234cf9d32c8140af82c5bb4904 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 10:12:59 +0100 Subject: [PATCH 39/76] Updated to 1.10 release from telefonicaid/perseo-fe(2) --- lib/models/updateAction.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index f7eef130..8d658897 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -268,8 +268,11 @@ function processOptionParams(action, event) { case 'None': theValue = null; break; +<<<<<<< HEAD default: //Nothing to do +======= +>>>>>>> 3d55db4412123dc1a8169a59aaa068deabe4530d } } var key = myutils.expandVar(attr.name, event); From 5440640c2d0d9d49331c818e61c6de60f36527f9 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 10:54:16 +0100 Subject: [PATCH 40/76] Travis CI --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 01bc0af2..cb794043 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ node_js: branches: only: - - master + - fiqare-perseo-fe-improvements services: - mongodb From 41f9c68e4869b795d86ceed72b80fb8c32fab08c Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 11:34:53 +0100 Subject: [PATCH 41/76] Fixed travis errors --- lib/db.js | 3 +- lib/models/entitiesStore.js | 2 +- lib/models/notices.js | 51 +++++++++++++++-------------- lib/models/postAction.js | 3 -- lib/models/rules.js | 4 ++- lib/models/updateAction.js | 6 ---- lib/models/visualRules.js | 5 ++- lib/myutils.js | 1 + lib/routes/rulesController.js | 2 +- lib/routes/visualRulesController.js | 6 ++-- 10 files changed, 40 insertions(+), 43 deletions(-) diff --git a/lib/db.js b/lib/db.js index b03848b1..b4960c56 100644 --- a/lib/db.js +++ b/lib/db.js @@ -54,8 +54,7 @@ function getDbAux(url, component, callback) { bufferMaxEntries: config.checkDB.bufferMaxEntries, domainsEnabled: true, reconnectTries: config.checkDB.reconnectTries, - reconnectInterval: config.checkDB.reconnectInterval, - domainsEnabled: true + reconnectInterval: config.checkDB.reconnectInterval }, function(err, db) { if (err) { diff --git a/lib/models/entitiesStore.js b/lib/models/entitiesStore.js index 512f7f94..a81b095f 100644 --- a/lib/models/entitiesStore.js +++ b/lib/models/entitiesStore.js @@ -54,7 +54,7 @@ function findSilentEntities(service, subservice, ruleData, func, callback) { } } else { //Added default else clause - + logger.debug('findSilentEntities() - Default else clause'); } if (ruleData.type) { criterion['_id.type'] = ruleData.type; diff --git a/lib/models/notices.js b/lib/models/notices.js index 446a9a86..7e405281 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -250,33 +250,34 @@ function processCBv2Notice(service, subservice, ncr, ix) { for (var attr in attrList) { // Exclude id and type. NGSIv2 if (attr !== 'id' && attr !== 'type') { - // each atttribute - var attrInfo = attrList[attr]; - n[attr + '__type'] = attrInfo.type; + continue; + } + // each atttribute + var attrInfo = attrList[attr]; + n[attr + '__type'] = attrInfo.type; + + // NGSIv1 location attribute (only one should be present) + // see links in issues/198 + if (attrInfo.type === 'geo:point') { + location = parseLocation(attrInfo.value); + n[attr] = location; + } else if (attrInfo.type === 'DateTime') { + date = parseDate(attrInfo.value); + n[attr] = date; + } else { + n[attr] = attrInfo.value; + } - // NGSIv1 location attribute (only one should be present) - // see links in issues/198 - if (attrInfo.type === 'geo:point') { - location = parseLocation(attrInfo.value); - n[attr] = location; - } else if (attrInfo.type === 'DateTime') { - date = parseDate(attrInfo.value); - n[attr] = date; + for (var metaKey in attrInfo.metadata) { + n[attr + '__metadata__' + metaKey + '__type'] = attrInfo.metadata[metaKey].type; + if (attrInfo.metadata[metaKey].type === 'DateTime') { + metaDate = parseDate(attrInfo.metadata[metaKey].value); + n[attr + '__metadata__' + metaKey] = metaDate; + } else if (attrInfo.metadata[metaKey].type === 'geo:point') { + metalocation = parseLocation(attrInfo.metadata[metaKey].value); + n[attr + '__metadata__' + metaKey] = metalocation; } else { - n[attr] = attrInfo.value; - } - - for (var metaKey in attrInfo.metadata) { - n[attr + '__metadata__' + metaKey + '__type'] = attrInfo.metadata[metaKey].type; - if (attrInfo.metadata[metaKey].type === 'DateTime') { - metaDate = parseDate(attrInfo.metadata[metaKey].value); - n[attr + '__metadata__' + metaKey] = metaDate; - } else if (attrInfo.metadata[metaKey].type === 'geo:point') { - metalocation = parseLocation(attrInfo.metadata[metaKey].value); - n[attr + '__metadata__' + metaKey] = metalocation; - } else { - n[attr + '__metadata__' + metaKey] = attrInfo.metadata[metaKey].value; - } + n[attr + '__metadata__' + metaKey] = attrInfo.metadata[metaKey].value; } } } diff --git a/lib/models/postAction.js b/lib/models/postAction.js index 39f4e230..71ff5cef 100644 --- a/lib/models/postAction.js +++ b/lib/models/postAction.js @@ -38,13 +38,10 @@ function buildPostOptions(action, event) { options.json = myutils.expandObject(action.parameters.json, event); } else if (action.template) { options.text = myutils.expandVar(action.template, event, true); -<<<<<<< HEAD } else { //Added default else clause return options; -======= ->>>>>>> 3d55db4412123dc1a8169a59aaa068deabe4530d } return options; diff --git a/lib/models/rules.js b/lib/models/rules.js index 752dd35d..14254531 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -33,7 +33,8 @@ var util = require('util'), actions = require('./actions'), namePattern = /^[a-zA-Z0-9_-]+$/, MaxNameLength = 50, - errors = {}; + errors = {}, + logger = require('logops'); function validNSRule(rule) { var checkInterval = parseInt(rule.nosignal.checkInterval, 10); @@ -60,6 +61,7 @@ function validRule(rule) { return new errors.EmptyRule(JSON.stringify(rule)); } else { //Added default else clause + logger.debug('validRule() - Default else clause'); } if (rule.nosignal) { //Specific checks for no-signal rules diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index 50ca9357..f7eef130 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -268,14 +268,8 @@ function processOptionParams(action, event) { case 'None': theValue = null; break; -<<<<<<< HEAD -<<<<<<< HEAD default: //Nothing to do -======= ->>>>>>> 3d55db4412123dc1a8169a59aaa068deabe4530d -======= ->>>>>>> 3d55db4412123dc1a8169a59aaa068deabe4530d } } var key = myutils.expandVar(attr.name, event); diff --git a/lib/models/visualRules.js b/lib/models/visualRules.js index 08b4221a..53df6a83 100644 --- a/lib/models/visualRules.js +++ b/lib/models/visualRules.js @@ -35,7 +35,8 @@ var util = require('util'), MINOR_OR_EQUAL_THAN: ' <= ', MATCH: ' regexp ' }, - errors = {}; + errors = {}, + logger = require('logops'); function errorOperator(op) { if (operatorMap[op] === undefined) { @@ -146,6 +147,7 @@ function vr2rule(cardRule) { type = card.conditionList[0].parameterValue; } else { //Added default else clause + logger.debug('vr2rule() - Default else clause'); } break; case 'OBSERVATION': @@ -161,6 +163,7 @@ function vr2rule(cardRule) { return new errors.TypeAsAttribute(''); } else { //Added default else clause + logger.debug('vr2rule() - Default else clause'); } conditions.push( diff --git a/lib/myutils.js b/lib/myutils.js index 8977a6d6..cac075b1 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -93,6 +93,7 @@ function expandObject(templateObj, dictionary) { res[expandVar(key, dictionary)] = expandObject(templateObj[key], dictionary); } else { //Added default else clause + logger.debug('expandObject() - Default else clause'); } }); } diff --git a/lib/routes/rulesController.js b/lib/routes/rulesController.js index 5e393996..bbaf0e48 100644 --- a/lib/routes/rulesController.js +++ b/lib/routes/rulesController.js @@ -180,7 +180,7 @@ function AddTo(app) { * type: string * description: EPL sentence * example: - * - select *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")] + * - select * * action: * type: object * properties: diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 7e84ffe5..0028b8a3 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -41,10 +41,10 @@ function GetAllVR(req, resp) { delete e.service; return e; }); - resp.set(constants.TOTAL_COUNT_HEADER, data.length); - data = data.splice(offset, limit); + resp.set(constants.TOTAL_COUNT_HEADER, reformatData.length); + reformatData = reformatData.splice(offset, limit); } - myutils.respond(resp, err, data, count); + myutils.respond(resp, err, reformatData, count); }); } function GetVR(req, resp) { From f471ce26d5011d04e2e50945bad1de9aefa07998 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 11:42:24 +0100 Subject: [PATCH 42/76] Fixed travis errors(2) --- lib/models/postAction.js | 2 +- lib/routes/visualRulesController.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/models/postAction.js b/lib/models/postAction.js index 71ff5cef..da971e12 100644 --- a/lib/models/postAction.js +++ b/lib/models/postAction.js @@ -64,7 +64,7 @@ function doIt(action, event, callback) { } else { //Added default else clause - + logger.debug('doIt() - Default else clause'); } metrics.IncMetrics(event.service, event.subservice, metrics.actionHttpPost); diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 0028b8a3..d8e2c43a 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -42,9 +42,9 @@ function GetAllVR(req, resp) { return e; }); resp.set(constants.TOTAL_COUNT_HEADER, reformatData.length); - reformatData = reformatData.splice(offset, limit); + data = reformatData.splice(offset, limit); } - myutils.respond(resp, err, reformatData, count); + myutils.respond(resp, err, data, count); }); } function GetVR(req, resp) { From 14b1aa9144021b723daf93d57d2fe6b3766c236f Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Thu, 21 Nov 2019 12:04:13 +0100 Subject: [PATCH 43/76] Fixed travis errors(3) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7efe3ece..6a7760ce 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, "scripts": { "clean": "rm -rf package-lock.json && rm -rf node_modules && rm -rf coverage", - "test": "snyk test && mocha --recursive --reporter spec --timeout 3000 --ui bdd --exit", + "test": "mocha --recursive --reporter spec --timeout 3000 --ui bdd --exit", "test:watch": "npm run test -- -w ./lib", "lint": "jshint lib/ --config .jshintrc && jshint test/ --config test/.jshintrc", "lint:md": "remark 'README.md' documentation", From e0907388476fc7b162454775df9ee057e7ffddce Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 16 Dec 2019 08:31:03 +0100 Subject: [PATCH 44/76] Revert issue javascript:VariableShadowing --- lib/appContext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/appContext.js b/lib/appContext.js index ebdcc4c3..357cc11d 100644 --- a/lib/appContext.js +++ b/lib/appContext.js @@ -35,8 +35,8 @@ function SetOrionDB(db) { orionDatabase = db; } -function OrionDB(db) { - return orionDatabase.db(db); +function OrionDB(database) { + return orionDatabase.db(database); } /** From 4db0223bf9bfba5fdd499705e44a8f303b8888d3 Mon Sep 17 00:00:00 2001 From: Vladimir Rico Hebles Date: Mon, 16 Dec 2019 10:40:27 +0100 Subject: [PATCH 45/76] Revert issue javascript:VariableShadowing --- lib/db.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/db.js b/lib/db.js index b4960c56..b49ff85d 100644 --- a/lib/db.js +++ b/lib/db.js @@ -72,8 +72,8 @@ function getDbAux(url, component, callback) { }); checkDbHealthFunc = function checkDbHealth() { - pingAux(db, component, function(err2, result) { - logger.debug('ping (%s) %j', component, err2 || result); + pingAux(db, component, function(err, result) { + logger.debug('ping (%s) %j', component, err || result); }); }; setInterval(checkDbHealthFunc, delay); @@ -95,20 +95,20 @@ function getOrionDb(callback) { } function ensureIndex(collection, fields, callback) { - database.collection(collection, function(err, collectionVar) { - myutils.logErrorIf(err, collectionVar, context); - collectionVar.ensureIndex(fields, { unique: true }, function(error, indexName) { - myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); - callback(error, indexName); + database.collection(collection, function(err, collection) { + myutils.logErrorIf(err, collection, context); + collection.ensureIndex(fields, { unique: true }, function(err, indexName) { + myutils.logErrorIf(err, 'ensureIndex ' + collection, context); + callback(err, indexName); }); }); } function ensureIndexTTL(collection, fields, ttl, callback) { - database.collection(collection, function(err, collectionVar) { - myutils.logErrorIf(err, collectionVar); - collectionVar.ensureIndex(fields, { expireAfterSeconds: ttl }, function(error, indexName) { - myutils.logErrorIf(error, 'ensureIndex ' + collectionVar, context); - callback(error, indexName); + database.collection(collection, function(err, collection) { + myutils.logErrorIf(err, collection); + collection.ensureIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) { + myutils.logErrorIf(err, 'ensureIndex ' + collection, context); + callback(err, indexName); }); }); } From ec05fb7c8a116a978a7a4ac4c1638d14df078f85 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 10:48:42 +0100 Subject: [PATCH 46/76] Revert issue javascript:VariableShadowing --- lib/models/actions.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/models/actions.js b/lib/models/actions.js index f35f133f..ffbf139d 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -231,7 +231,7 @@ function DoAction(event, callback) { actionStore.Find(service, subservice, ruleName, function(err, actions) { var queue; var arrayAction = actions; - var localErr; + var localError; if (err) { return callback(err, null); } @@ -240,10 +240,10 @@ function DoAction(event, callback) { } // check all axns are right for (var i = 0; i < arrayAction.length; i++) { - localErr = validateAction(arrayAction[i]); - if (localErr !== null) { - myutils.logErrorIf(localErr); - return callback(localErr, null); + localError = validateAction(arrayAction[i]); + if (localError !== null) { + myutils.logErrorIf(localError); + return callback(localError, null); } } From 1bfb721f64644a5f1c41dde4807040a0ee6bd92e Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 10:55:34 +0100 Subject: [PATCH 47/76] Revert issue javascript:VariableShadowing --- lib/models/executionsStore.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/models/executionsStore.js b/lib/models/executionsStore.js index 0fd8a55b..213345c0 100644 --- a/lib/models/executionsStore.js +++ b/lib/models/executionsStore.js @@ -87,10 +87,10 @@ module.exports = { notice: noticeId, index: index }, - function(error2, data) { - myutils.logErrorIf(error2); - if (error2) { - return callback(error2, null); + function(err, data) { + myutils.logErrorIf(err); + if (err) { + return callback(err, null); } return callback(null, data); } From 13089b0bdc41dd999d0a83f19cbed371c2e17c47 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 11:20:10 +0100 Subject: [PATCH 48/76] Revert issue javascript:VariableShadowing --- lib/models/notices.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/models/notices.js b/lib/models/notices.js index 7e405281..e8ee5889 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -379,14 +379,14 @@ function DoNotice(orionN, callback) { } async.each( notices, - function(noticeVar, cbEach) { + function(notice, cbEach) { var h = {}; - h[constants.SUBSERVICE_HEADER] = noticeVar.subservice; + h[constants.SUBSERVICE_HEADER] = notice.subservice; myutils.requestHelperWOMetrics( 'post', { url: config.perseoCore.noticesURL, - json: noticeVar, + json: notice, headers: h }, function(err, data) { @@ -403,7 +403,7 @@ function DoNotice(orionN, callback) { 'post', { url: config.nextCore.noticesURL, - json: noticeVar, + json: notice, headers: h }, myutils.logErrorIf From 88ad9536933ff42bb095b90da1d14e57d81f7765 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 11:23:33 +0100 Subject: [PATCH 49/76] Revert issue javascript:VariableShadowing --- lib/models/updateAction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index f7eef130..077b3332 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -203,8 +203,8 @@ function makeTokenListenerFunc(action, event, callback) { return callback(error); } else { logger.debug('tokenHandlerFunc retrying with', token); - return doRequest(action, event, token, function cbDoReqUpdAxn(err, data) { - callback(err, data); + return doRequest(action, event, token, function cbDoReqUpdAxn(error, data) { + callback(error, data); }); } }; From dd563302d3f7ea30a6fb01c3742375a48bbf9df2 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 11:25:48 +0100 Subject: [PATCH 50/76] Revert issue javascript:VariableShadowing --- lib/perseo.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/perseo.js b/lib/perseo.js index 54294a7a..0aa33397 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -146,15 +146,15 @@ function start(callbackStart) { }, db.orionPing, function(callback) { - var domainVar = require('domain').create(); - domainVar.context = {}; - domainVar.context.op = 'initialRefresh'; - domainVar.context.comp = constants.COMPONENT_NAME; - domainVar.context.trans = 'n/a'; - domainVar.context.corr = domainVar.context.trans; - domainVar.context.srv = 'n/a'; - domainVar.context.subsrv = 'n/a'; - domainVar.run(function() { + var domain = require('domain').create(); + domain.context = {}; + domain.context.op = 'initialRefresh'; + domain.context.comp = constants.COMPONENT_NAME; + domain.context.trans = 'n/a'; + domain.context.corr = domainVar.context.trans; + domain.context.srv = 'n/a'; + domain.context.subsrv = 'n/a'; + domain.run(function() { rules.Refresh(callback); }); }, From e71c7e6879f0312e7651d6ae95ee9f0e7e465814 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 12:21:24 +0100 Subject: [PATCH 51/76] Revert issue javascript:VariableShadowing --- lib/routes/actionsController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/routes/actionsController.js b/lib/routes/actionsController.js index 3476a2e6..ab44f726 100644 --- a/lib/routes/actionsController.js +++ b/lib/routes/actionsController.js @@ -129,9 +129,9 @@ function PostAction(req, resp) { firedEvent.fiwarePerseoContext = nextDomain.context; setTimeout(function() { nextDomain.run(function() { - for (var pVar in firedEvent.ev) { - if (firedEvent[pVar] === undefined) { - firedEvent[pVar] = firedEvent.ev[pVar]; + for (var p in firedEvent.ev) { + if (firedEvent[p] === undefined) { + firedEvent[p] = firedEvent.ev[p]; } } actions.Do(firedEvent, function(err) { From 97bc1f3f394c4bab8c43089cdc35621c25643d21 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 12:35:54 +0100 Subject: [PATCH 52/76] Revert issue javascript:S1226 --- lib/alarm.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/alarm.js b/lib/alarm.js index c55733de..f6a47c4f 100644 --- a/lib/alarm.js +++ b/lib/alarm.js @@ -29,29 +29,29 @@ var util = require('util'), function raise(alarm, context, message) { var state = alarms[alarm]; - var contextTemp = (process.domain && process.domain.context) || {}; - var messageTemp = ''; + var context = (process.domain && process.domain.context) || {}; + if (typeof message === 'object' && message) { - messageTemp = util.format('%j', message); + message = util.format('%j', message); } - messageTemp = messageTemp || ''; + message = message || ''; if (state !== true) { // emit ERROR alarm ON - logger.error(contextTemp, 'ALARM-ON [' + alarm + '] ' + messageTemp); + logger.error(context, 'ALARM-ON [' + alarm + '] ' + message); } alarms[alarm] = true; } function release(alarm, context, message) { var state = alarms[alarm]; - var contextTemp = (process.domain && process.domain.context) || {}; - var messageTemp = ''; + var context = (process.domain && process.domain.context) || {}; + if (typeof message === 'object' && message) { - messageTemp = util.format('%j', message); + message = util.format('%j', message); } - messageTemp = messageTemp || ''; + message = message || ''; if (state === true) { // emit ERROR alarm OFF - logger.error(contextTemp, 'ALARM-OFF [' + alarm + '] ' + messageTemp); + logger.error(context, 'ALARM-OFF [' + alarm + '] ' + message); } alarms[alarm] = false; } From c08843e705bcbadac49dd7da5f452ea3c2c87900 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 12:55:27 +0100 Subject: [PATCH 53/76] Revert issue javascript:S1226 --- lib/models/actions.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/models/actions.js b/lib/models/actions.js index ffbf139d..8c21e0bb 100644 --- a/lib/models/actions.js +++ b/lib/models/actions.js @@ -229,18 +229,17 @@ function DoAction(event, callback) { } actionStore.Find(service, subservice, ruleName, function(err, actions) { - var queue; - var arrayAction = actions; - var localError; + var localError, queue; + if (err) { return callback(err, null); } - if (!util.isArray(arrayAction)) { - arrayAction = [arrayAction]; + if (!util.isArray(actions)) { + actions = [actions]; } // check all axns are right - for (var i = 0; i < arrayAction.length; i++) { - localError = validateAction(arrayAction[i]); + for (var i = 0; i < actions.length; i++) { + localError = validateAction(actions[i]); if (localError !== null) { myutils.logErrorIf(localError); return callback(localError, null); @@ -248,10 +247,10 @@ function DoAction(event, callback) { } queue = getInProcessArray(service, subservice, ruleName, id, type); - for (i = 0; i < arrayAction.length; i++) { - arrayAction[i].index = i; + for (i = 0; i < actions.length; i++) { + actions[i].index = i; queue.push({ - action: arrayAction[i], + action: actions[i], event: event, context: event.fiwarePerseoContext }); From 94fa81946cc11bf21ac4fe7a558fab8101a9e793 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 13:13:58 +0100 Subject: [PATCH 54/76] Revert issue javascript:S1226 --- lib/models/metrics.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/models/metrics.js b/lib/models/metrics.js index 6df70001..cb72dc2b 100644 --- a/lib/models/metrics.js +++ b/lib/models/metrics.js @@ -56,15 +56,14 @@ function deleteMetrics() { function incMetrics(service, servicePath, key, value) { var m = getMetrics(service, servicePath); - var result = value; - if (result === undefined) { - result = 1; + if (value === undefined) { + value = 1; } if (m[key] === undefined) { logger.error('invalid key for metrics %s', key); return; } - m[key] += result; + m[key] += value; } function decoratedMetrics(reset) { From 57334b94cc134b1c6d2891e46bfd39291f9fd2ce Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 13:41:22 +0100 Subject: [PATCH 55/76] Revert issue javascript:S1226 --- lib/models/rules.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/models/rules.js b/lib/models/rules.js index 14254531..8cdb288e 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -188,7 +188,7 @@ module.exports = { }, Save: function(rule, callback) { var localError; - var normalizeRule; + localError = validRule(rule); if (localError !== null) { myutils.logErrorIf(localError); @@ -196,30 +196,30 @@ module.exports = { } // Normalize the rule text - normalizeRule = normalizeRuleName(rule); + rule = normalizeRuleName(rule); async.series( [ function(localCallback) { - rulesStore.Exists(normalizeRule, function rsSaveExistsCb(err, exists) { + rulesStore.Exists(rule, function rsSaveExistsCb(err, exists) { if (err) { return localCallback(err); } if (exists) { - return localCallback(new errors.RuleExists(normalizeRule.name)); + return localCallback(new errors.RuleExists(rule.name)); } return localCallback(err, exists); }); }, - postR2core.bind(null, normalizeRule), - rulesStore.Save.bind(null, normalizeRule), + postR2core.bind(null, rule), + rulesStore.Save.bind(null, rule), function(cb) { if (rule.nosignal) { noSignal.AddNSRule( - normalizeRule.service, - normalizeRule.subservice, - normalizeRule.name, - normalizeRule.nosignal + rule.service, + rule.subservice, + rule.name, + rule.nosignal ); } cb(null); @@ -263,34 +263,34 @@ module.exports = { } // Normalize the rule text - var putNormalizeRule = normalizeRuleName(rule); + var rule = normalizeRuleName(rule); async.series( [ - delR2core.bind(null, putNormalizeRule), + delR2core.bind(null, rule), function(cb) { if (rule.nosignal) { noSignal.DeleteNSRule( - putNormalizeRule.service, - putNormalizeRule.subservice, - putNormalizeRule.name + rule.service, + rule.subservice, + rule.name ); } cb(null); }, - postR2core.bind(null, putNormalizeRule), + postR2core.bind(null, rule), function(cb) { - if (putNormalizeRule.nosignal) { + if (rule.nosignal) { noSignal.AddNSRule( - putNormalizeRule.service, - putNormalizeRule.subservice, - putNormalizeRule.name, - putNormalizeRule.nosignal + rule.service, + rule.subservice, + rule.name, + rule.nosignal ); } cb(null); }, - rulesStore.Update.bind(null, id, putNormalizeRule) + rulesStore.Update.bind(null, id, rule) ], callback ); From 9a0e631bfa98b4bf905a119fa5872f9f0a89b533 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 13:43:23 +0100 Subject: [PATCH 56/76] Revert issue javascript:S1226 --- lib/models/rulesStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/models/rulesStore.js b/lib/models/rulesStore.js index 62066010..02768e73 100644 --- a/lib/models/rulesStore.js +++ b/lib/models/rulesStore.js @@ -95,9 +95,9 @@ function search(rule, callback) { ); } -function findAll(service, subservice, uselessCallback) { +function findAll(service, subservice, callback) { var criterion = {}; - var callback = uselessCallback; + if (typeof service === 'function') { callback = service; } else { From 9e975a91bb21556d5d2153a75389a29686f115b4 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 13:47:00 +0100 Subject: [PATCH 57/76] Revert issue javascript:S1226 --- lib/models/smppAction.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/models/smppAction.js b/lib/models/smppAction.js index 056810bc..54330914 100644 --- a/lib/models/smppAction.js +++ b/lib/models/smppAction.js @@ -62,14 +62,14 @@ function doIt(action, event, callback) { var sendSMPP = function(from, to, text) { /*jshint camelcase:false */ - var fromString = from.toString(); - var toString = to.toString(); - logger.debug('smpp sms from: %s, to: %s, text: %s', fromString, toString, text); + from = from.toString(); + to = to.toString(); + logger.debug('smpp sms from: %s, to: %s, text: %s', from, to, text); session.submit_sm( { - source_addr: fromString, - destination_addr: toString, + source_addr: from, + destination_addr: to, short_message: text }, function(pdu) { From 27716c87def6d450ee0137b1fd27c0bd61316a9e Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 13:57:30 +0100 Subject: [PATCH 58/76] Revert issue javascript:S1226 --- lib/models/updateAction.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index 077b3332..092e6c46 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -187,8 +187,7 @@ function doRequest(action, event, token, callback) { alarm.release(alarm.ORION); } if (!err && data.body && data.body.orionError) { - var errOrion = new errors.OrionError(JSON.stringify(data.body.orionError)); - callback(errOrion, data); + err = new errors.OrionError(JSON.stringify(data.body.orionError)); } else { callback(err, data); } From c6604b03c9b7689e6f72a18bf3fd3fcbb9cdc910 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 14:10:45 +0100 Subject: [PATCH 59/76] Revert issue javascript:S1226 --- lib/models/visualRules.js | 47 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/models/visualRules.js b/lib/models/visualRules.js index 53df6a83..652265ca 100644 --- a/lib/models/visualRules.js +++ b/lib/models/visualRules.js @@ -46,38 +46,36 @@ function errorOperator(op) { } function castVar(str, isNumber) { - var castStr = util.format('cast(`%s`?, String)', str); - + str = util.format('cast(`%s`?, String)', str); if (isNumber === true) { - castStr = util.format('cast(%s, float)', castStr); + str = util.format('cast(%s, float)', str); } - return castStr; + return str; } function adaptExp(str, isNumber) { - var adaptString = str; - if (adaptString.indexOf('${') !== 0) { + if (str.indexOf('${') !== 0) { if (isNumber !== true) { - adaptString = util.format('%j', adaptString); + str = util.format('%j', str); } - return adaptString; + return str; } //Remove starting "${" and trailing "}" - adaptString = adaptString.slice(2, -1); + str = str.slice(2, -1); //Change dot to double underscore - adaptString = adaptString.replace(/\./g, '__'); - return castVar(adaptString, isNumber); + str = str.replace(/\./g, '__'); + return castVar(str, isNumber); } function makeUserPropCondition(exprL, op, exprR, isNumber) { - var adaptExprL = adaptExp(exprL, isNumber); - var adaptExprR = adaptExp(exprR, isNumber); - return util.format('(%s %s %s)', adaptExprL, operatorMap[op], adaptExprR); + exprL = adaptExp(exprL, isNumber); + exprR = adaptExp(exprR, isNumber); + return util.format('(%s %s %s)', exprL, operatorMap[op], exprR); } function makeObservationCondition(exprL, op, exprR, isNumber) { - var castExprL = castVar(exprL, isNumber); - var adaptExprR = adaptExp(exprR, isNumber); - return util.format('(%s %s %s)', castExprL, operatorMap[op], adaptExprR); + exprL = castVar(exprL, isNumber); + exprR = adaptExp(exprR, isNumber); + return util.format('(%s %s %s)', exprL, operatorMap[op], exprR); } function plainParams(p) { var i, @@ -291,11 +289,12 @@ module.exports = { }, Find: function Find(rule, callback) { rules.Find(rule, function(err, data) { - var vr = (data && data.VR) || {}; if (err) { return callback(err, null); } - return callback(err, vr); + data = data || {}; + data.VR = data.VR || {}; + return callback(err, data.VR); }); }, Save: function Save(vrrule, callback) { @@ -315,12 +314,12 @@ module.exports = { rules.Remove(id, callback); }, Put: function(id, rule, callback) { - var reformatRule = vr2rule(rule); - if (reformatRule instanceof Error) { - myutils.logErrorIf(reformatRule); - return callback(reformatRule, null); + rule = vr2rule(rule); + if (rule instanceof Error) { + myutils.logErrorIf(rule); + return callback(rule, null); } - rules.Put(id, reformatRule, function(err, data) { + rules.Put(id, rule, function(err, data) { if (err) { return callback(err, null); } From f3a905aa3d01e594dfdd97c2d403ecbc5add899c Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 14:25:19 +0100 Subject: [PATCH 60/76] Revert issue javascript:S1226 --- lib/myutils.js | 51 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/myutils.js b/lib/myutils.js index cac075b1..f59902cf 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -31,44 +31,41 @@ var request = require('request'), function logErrorIf(err, message, context) { var level = 'error'; - var contextTemp = context; - var messageTemp; - if (contextTemp === undefined) { - contextTemp = process.domain && process.domain.context; + if (context === undefined) { + context = process.domain && process.domain.context; } if (err) { - messageTemp = message || ''; - if (contextTemp) { - contextTemp.op = - contextTemp.op || + message = message || ''; + if (context) { + context.op = + context.op || new Error().stack .split('\n')[2] .trim() .substr(3); - contextTemp.comp = contextTemp.comp || constants.COMPONENT_NAME; - logger[level](context, messageTemp, err.message || JSON.stringify(err)); + context.comp = context.comp || constants.COMPONENT_NAME; + logger[level](context, message, err.message || JSON.stringify(err)); } else { - logger[level](messageTemp, err.message || JSON.stringify(err)); + logger[level](message, err.message || JSON.stringify(err)); } } } function expandVar(val, mapping, trycast) { - var valTemp = val; - if (typeof valTemp === 'string') { + if (typeof val === 'string') { Object.keys(mapping).forEach(function(p) { - valTemp = valTemp.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]); + val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]); }); var expanded = false; if (trycast) { try { // Check if "${num}" and expand it as real value, non string - var n = JSON.parse(valTemp); + var n = JSON.parse(val); switch (typeof n) { case 'number': case 'boolean': case 'object': // for a json in a string like: "{\"type\":\"Point\"}" - valTemp = n; + val = n; expanded = true; break; } @@ -77,10 +74,10 @@ function expandVar(val, mapping, trycast) { } } if (!expanded) { - valTemp = valTemp.replace(/\$\{\w*\}/g, '[?]'); + val = val.replace(/\$\{\w*\}/g, '[?]'); } } - return valTemp; + return val; } function expandObject(templateObj, dictionary) { @@ -221,11 +218,10 @@ function respondAux(resp, err, data, withCount, raw, withMetrics) { respObj, respStr, domain = process.domain; - var dataTemp = data; if (err) { errMsg = err.message; statusCode = err.httpCode || 500; - dataTemp = null; + data = null; } if (raw === true) { @@ -233,13 +229,13 @@ function respondAux(resp, err, data, withCount, raw, withMetrics) { respObj = err; delete respObj.httpCode; } else { - respObj = dataTemp; + respObj = data; } } else { // non-raw - respObj = { error: errMsg, data: dataTemp }; - if (withCount === true && dataTemp && util.isArray(dataTemp)) { - respObj.count = dataTemp.length; + respObj = { error: errMsg, data: data }; + if (withCount === true && data && util.isArray(data)) { + respObj.count = data.length; } } logger.info('sending response: %s %j', statusCode, respObj); @@ -270,11 +266,10 @@ function respondWOMetrics(resp, err, data, withCount, raw) { function firstChars(result) { var text; - var resultTemp = result; - if (resultTemp === undefined) { - resultTemp = 'undefined'; + if (result === undefined) { + result = 'undefined'; } - text = JSON.stringify(resultTemp); + text = JSON.stringify(result); if (text.length > 125) { text = text.substr(0, 125) + ' [...]'; } From c1cddf290664710ad47aed03fd62791eb0c7223e Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 14:27:18 +0100 Subject: [PATCH 61/76] Revert issue javascript:S1226 --- lib/routes/visualRulesController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 0028b8a3..0e572a4c 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -36,7 +36,7 @@ function GetAllVR(req, resp) { var count = req.query && req.query.options ? req.query.options === 'count' : true; visualRules.FindAll(req.service, req.subservice, function(err, data) { if (!err && util.isArray(data)) { - var reformatData = data.map(function(e) { + data = data.map(function(e) { delete e.subservice; delete e.service; return e; @@ -44,7 +44,7 @@ function GetAllVR(req, resp) { resp.set(constants.TOTAL_COUNT_HEADER, reformatData.length); reformatData = reformatData.splice(offset, limit); } - myutils.respond(resp, err, reformatData, count); + myutils.respond(resp, err, data, count); }); } function GetVR(req, resp) { From e622aa354f6a541465004bc24b1a1066f0198dff Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 14:34:41 +0100 Subject: [PATCH 62/76] Fixed rules.js --- lib/models/rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/rules.js b/lib/models/rules.js index 8cdb288e..0e0a567d 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -263,7 +263,7 @@ module.exports = { } // Normalize the rule text - var rule = normalizeRuleName(rule); + rule = normalizeRuleName(rule); async.series( [ From 70a972d778b7306ec7ee504b156190ddde496a66 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 14:37:46 +0100 Subject: [PATCH 63/76] Fixed db IP --- test/utils/utilsT.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/utils/utilsT.js b/test/utils/utilsT.js index ae020606..4025f9ec 100644 --- a/test/utils/utilsT.js +++ b/test/utils/utilsT.js @@ -220,7 +220,7 @@ function addEntity(tenant, entity, callback) { ); } function configTest() { - config.mongo.url = 'mongodb://172.17.0.1:27017/perseo_testing'; + config.mongo.url = 'mongodb://localhost:27017/perseo_testing'; config.endpoint.port = 9182; config.perseoCore.noticesURL = 'http://localhost:' + fakeServerPort; config.perseoCore.rulesURL = 'http://localhost:' + fakeServerPort; @@ -232,7 +232,7 @@ function configTest() { config.logLevel = 'fatal'; config.nextCore = {}; - config.orionDb.url = 'mongodb://172.17.0.1:27017/test'; + config.orionDb.url = 'mongodb://localhost:27017/test'; config.orionDb.prefix = 'oriontest'; config.perseoCore.interval = 10 * 60e3; //Do not refresh in the middle of a long test } From bd3c7b2c5986e9f52c147b1c9b3bac3d3041a3a9 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 15:07:11 +0100 Subject: [PATCH 64/76] Added branch to Travis build --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 01bc0af2..0c001e4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,8 @@ node_js: branches: only: - - master + #- master + - fiqare-improvements-1 services: - mongodb From e8d11acd1330f78ee9e0b3e6dff6ede3ad8eec65 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 16 Dec 2019 17:21:28 +0100 Subject: [PATCH 65/76] Fixed Travis --- lib/alarm.js | 4 ++-- lib/perseo.js | 2 +- lib/routes/visualRulesController.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/alarm.js b/lib/alarm.js index f6a47c4f..b7a2bfc6 100644 --- a/lib/alarm.js +++ b/lib/alarm.js @@ -29,7 +29,7 @@ var util = require('util'), function raise(alarm, context, message) { var state = alarms[alarm]; - var context = (process.domain && process.domain.context) || {}; + context = (process.domain && process.domain.context) || {}; if (typeof message === 'object' && message) { message = util.format('%j', message); @@ -43,7 +43,7 @@ function raise(alarm, context, message) { } function release(alarm, context, message) { var state = alarms[alarm]; - var context = (process.domain && process.domain.context) || {}; + context = (process.domain && process.domain.context) || {}; if (typeof message === 'object' && message) { message = util.format('%j', message); diff --git a/lib/perseo.js b/lib/perseo.js index 0aa33397..d5bf8e12 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -151,7 +151,7 @@ function start(callbackStart) { domain.context.op = 'initialRefresh'; domain.context.comp = constants.COMPONENT_NAME; domain.context.trans = 'n/a'; - domain.context.corr = domainVar.context.trans; + domain.context.corr = domain.context.trans; domain.context.srv = 'n/a'; domain.context.subsrv = 'n/a'; domain.run(function() { diff --git a/lib/routes/visualRulesController.js b/lib/routes/visualRulesController.js index 0e572a4c..d7cb5974 100644 --- a/lib/routes/visualRulesController.js +++ b/lib/routes/visualRulesController.js @@ -41,8 +41,8 @@ function GetAllVR(req, resp) { delete e.service; return e; }); - resp.set(constants.TOTAL_COUNT_HEADER, reformatData.length); - reformatData = reformatData.splice(offset, limit); + resp.set(constants.TOTAL_COUNT_HEADER, data.length); + data = data.splice(offset, limit); } myutils.respond(resp, err, data, count); }); From 08201e79ae90920e0f9707b01629f3f281c7d4ad Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Tue, 17 Dec 2019 10:33:54 +0100 Subject: [PATCH 66/76] Fixed PR comments --- .snyk | 4 ---- .travis.yml | 3 +-- config.js | 2 +- lib/models/notices.js | 2 +- package.json | 7 +++---- 5 files changed, 6 insertions(+), 12 deletions(-) delete mode 100644 .snyk diff --git a/.snyk b/.snyk deleted file mode 100644 index 700d8894..00000000 --- a/.snyk +++ /dev/null @@ -1,4 +0,0 @@ -# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. -version: v1.13.4 -ignore: {} -patch: {} diff --git a/.travis.yml b/.travis.yml index 0c001e4f..01bc0af2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,7 @@ node_js: branches: only: - #- master - - fiqare-improvements-1 + - master services: - mongodb diff --git a/config.js b/config.js index 168400c0..11093599 100644 --- a/config.js +++ b/config.js @@ -29,7 +29,7 @@ var config = {}; * Default log level. Can be one of: 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' * @type {string} */ -config.logLevel = 'DEBUG'; +config.logLevel = 'INFO'; /** * Configures the exposed API. diff --git a/lib/models/notices.js b/lib/models/notices.js index e8ee5889..01915318 100644 --- a/lib/models/notices.js +++ b/lib/models/notices.js @@ -249,7 +249,7 @@ function processCBv2Notice(service, subservice, ncr, ix) { var attrList = ncr.data[ix]; for (var attr in attrList) { // Exclude id and type. NGSIv2 - if (attr !== 'id' && attr !== 'type') { + if (attr === 'id' || attr === 'type') { continue; } // each atttribute diff --git a/package.json b/package.json index 499eeeb0..cdebc6bc 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ }, "scripts": { "clean": "rm -rf package-lock.json && rm -rf node_modules && rm -rf coverage", - "test": "mocha --recursive --reporter spec --timeout 3000 --ui bdd --exit", + "test": "mocha --recursive 'test/**/*.js' --reporter spec --timeout 3000 --ui bdd --exit", "test:watch": "npm run test -- -w ./lib", "lint": "jshint lib/ --config .jshintrc && jshint test/ --config test/.jshintrc", "lint:md": "remark 'README.md' documentation", "lint:text": "textlint 'README.md' 'documentation/*.md' 'documentation/**/*.md'", "prettier": "prettier --config .prettierrc.json --write ./**/**/*.js **/*.js *.js", "prettier:text": "prettier 'README.md' 'documentation/*.md' 'documentation/**/*.md' --no-config --tab-width 4 --print-width 120 --write --prose-wrap always", - "test:coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --recursive --reporter spec --exit", + "test:coverage": "istanbul cover _mocha -- --recursive 'test/**/*.js' --reporter spec --exit", "test:coveralls": "npm run test:coverage && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", "watch": "watch 'npm test && npm run lint' ./lib ./test" }, @@ -52,8 +52,7 @@ "textlint-rule-common-misspellings": "~1.0.1", "textlint-rule-no-dead-link": "~4.4.1", "textlint-rule-terminology": "~1.1.30", - "textlint-rule-write-good": "~1.6.2", - "snyk": "^1.145.0" + "textlint-rule-write-good": "~1.6.2" }, "keywords": [], "dependencies": { From 3020aab9fb0f1a72ce0254cc6bc7db8401d13359 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Tue, 17 Dec 2019 17:51:40 +0100 Subject: [PATCH 67/76] Fixed TODO --- lib/routes/actionsController.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/routes/actionsController.js b/lib/routes/actionsController.js index ab44f726..aae9ac06 100644 --- a/lib/routes/actionsController.js +++ b/lib/routes/actionsController.js @@ -113,6 +113,7 @@ function PostAction(req, resp) { } else { delay = config.slaveDelay; } + // TODO: Tricky ... To refactor? var cleanDomain, domainErrorHandler; domainErrorHandler = function(err) { From 51271cd82fd35f866a09fed1f4debe74380050a0 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Thu, 19 Dec 2019 11:48:19 +0100 Subject: [PATCH 68/76] Fixed Swagger dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index cdebc6bc..9b0cfd70 100644 --- a/package.json +++ b/package.json @@ -66,8 +66,8 @@ "nodemailer-smtp-transport": "~0.1.13", "request": "2.88.0", "smpp": "0.3.1", - "swagger-jsdoc": "^3.4.0", - "swagger-ui-express": "^4.1.1", + "swagger-jsdoc": "~3.4.0", + "swagger-ui-express": "~4.1.1", "twitter": "~1.7.1", "utm-converter": "~0.1.1", "uuid": "~1.4.2" From ce583be19f1561114b48d1dc71d91e661e2e1b11 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Fri, 20 Dec 2019 13:29:49 +0100 Subject: [PATCH 69/76] Revert ESLint docs --- README.md | 1 - documentation/eslint.md | 74 ----------------------------------------- 2 files changed, 75 deletions(-) delete mode 100644 documentation/eslint.md diff --git a/README.md b/README.md index 1a98c642..e2b35e6f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,6 @@ CEP implementation for IoT platform - [Data model](documentation/models.md) - [Available actions for PEP](documentation/pep_actions.md) - [Documentation for developers of perseo](documentation/development.md) -- [Development documentation (Third Iteration)](documentation/eslint.md) | :dart: [Roadmap](documentation/roadmap.md) | | ------------------------------------------ | diff --git a/documentation/eslint.md b/documentation/eslint.md deleted file mode 100644 index 01c91d6f..00000000 --- a/documentation/eslint.md +++ /dev/null @@ -1,74 +0,0 @@ -# Development documentation (Third Iteration) - -## ESLint - -ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. In many ways, it is -similar to JSLint and JSHint with a few exceptions: - -- ESLint uses Espree for JavaScript parsing. -- ESLint uses an AST to evaluate patterns in code. -- ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime. - -### Installation and Usage - -#### Command-line - -Prerequisites: [Node.js](https://nodejs.org/) (`^8.10.0`, `^10.13.0`, or `>=11.10.1`), npm version 3+. - -You can install ESLint using npm: - -``` -$ npm install eslint --save-dev -``` - -You should then set up a configuration file: - -``` -$ ./node_modules/.bin/eslint --init -``` - -After that, you can run ESLint on any file or directory like this: - -```` -$ ./node_modules/.bin/eslint yourfile.js - -After running `eslint --init`, you'll have a `.eslintrc` file in your directory. In it, you'll see some rules configured like this: - -```json -{ - "rules": { - "semi": ["error", "always"], - "quotes": ["error", "double"] - } -} -```` - -The names `"semi"` and `"quotes"` are the names of rules in ESLint. The first value is the error level of the rule and -can be one of these values: - -- `"off"` or `0` - turn the rule off -- `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code) -- `"error"` or `2` - turn the rule on as an error (exit code will be 1) - -The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and -details, see the [configuration docs](https://eslint.org/docs/user-guide/configuring)). - -#### Integrations with code editors - -With ESLint properly configured in your editor, your development experience can be careful. it’ll help you produce -better quality code and teach you as you work. - -Below we will list the main IDEs and their link to the configuration of the ESLint plugin for the improvement of your -code. - -- Sublime Text 3: [SublimeLinter-eslint](https://github.com/SublimeLinter/SublimeLinter-eslint) -- Vim: [ALE](https://github.com/dense-analysis/ale) -- Emacs: [FlyChecks](http://www.flycheck.org/en/latest/) -- Eclipse Orion: [ESLint](https://www.eclipse.org/lists/orion-dev/msg02718.html) -- Eclipse IDE: [Tern ESLint linter](https://github.com/angelozerr/tern.java/wiki/Tern-Linter-ESLint) -- TextMate 2: [ESLint Bundle](https://github.com/natesilva/javascript-eslint.tmbundle) -- Atom: [linter-eslint](https://atom.io/packages/linter-eslint) -- IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, PyCharm, AppCode, Android Studio, 0xDBE: - [ESLint Plugin](https://plugins.jetbrains.com/plugin/7494-eslint) -- Visual Studio Code: [ESLint Extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) -- Brackets: [Included and Brackets ESLint](https://github.com/brackets-userland/brackets-eslint) From d978818056a0293666063e39270979bdb5adb8ab Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Fri, 10 Jan 2020 11:02:01 +0100 Subject: [PATCH 70/76] Fixed indentation --- lib/models/postAction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/postAction.js b/lib/models/postAction.js index da971e12..94b12687 100644 --- a/lib/models/postAction.js +++ b/lib/models/postAction.js @@ -63,7 +63,7 @@ function doIt(action, event, callback) { requestOptions.body = options.text; } else { - //Added default else clause + //Added default else clause logger.debug('doIt() - Default else clause'); } From ff07d1070d5096cdbc0e359130c6a04514f87ea7 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 13 Jan 2020 08:38:31 +0100 Subject: [PATCH 71/76] Fixed config.js --- config.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/config.js b/config.js index 11093599..575aab6a 100644 --- a/config.js +++ b/config.js @@ -105,14 +105,16 @@ config.nextCore = { * SMTP endpoint options */ config.smtp = { - port: 9292, - host: '127.0.0.1', + port: 25, + host: 'smtpserver', secure: false, - /*auth: { + /* + , + auth: { user: 'abc', pass: 'xyz' - }, */ - + } + */ tls: { // keep rejectUnauthorized to false when secure is also false rejectUnauthorized: false From 0865dc81b89788cff47bcc99fb97d808534312bb Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 13 Jan 2020 08:46:49 +0100 Subject: [PATCH 72/76] Fixed indentation --- lib/models/entitiesStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/entitiesStore.js b/lib/models/entitiesStore.js index a81b095f..b279936d 100644 --- a/lib/models/entitiesStore.js +++ b/lib/models/entitiesStore.js @@ -53,7 +53,7 @@ function findSilentEntities(service, subservice, ruleData, func, callback) { return callback(e, null); } } else { - //Added default else clause + //Added default else clause logger.debug('findSilentEntities() - Default else clause'); } if (ruleData.type) { From 9c80eb926fd717d44f57b0c751cae7eea0270891 Mon Sep 17 00:00:00 2001 From: fiqare-emergya-dev Date: Tue, 28 Jan 2020 14:10:53 +0100 Subject: [PATCH 73/76] PR fixed --- README.md | 25 +++++++++++++++++++++++++ lib/models/emailAction.js | 2 +- lib/models/rules.js | 4 ++-- lib/perseo.js | 7 ++++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e2b35e6f..c9287c28 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,28 @@ wish to make a clarifying public statement as follows: > incorporate enhancements is considered a derivative work of the product. Software that merely uses or aggregates (i.e. > links to) an otherwise unmodified version of existing software is not considered a derivative work, and therefore it > does not need to be released as under the same license, or even released as open source. + + +## User & Programmers Manual + +### Swagger +Swagger is a framework that offers rules, tools and specifications for Apis documentation, allowing us to describe the structure of our APIs so that machines can read them. + +To know more about Swagger's interface and the project's API endpoints go to http://localhost:9090/api-docs. + +#### Instalation +To run Swagger in Perseo we have to install two NPM packages for NodeJs: +- Swagger-jsdoc: allows us to document our application's endpoints with notations. +- Swagger-ui-express: generates an interface with endpoints definitions. +We have to specify our swagger's version, our API's information and the route where we can find the endpoints in the project. +

+ definition: {
+                swagger: '2.0', // Specification (optional, defaults to swagger: '2.0')
+                info: {
+                    title: 'Perseo Front-End', // Title (required)
+                    version: '1.7.0-fiqare' // Version (required)
+                }
+            },
+
+Once the project is deployed (port 9090 by default), we can access to the interface by the following link: http://localhost:9090/api-docs. + diff --git a/lib/models/emailAction.js b/lib/models/emailAction.js index 3a5ff5e2..88778205 100644 --- a/lib/models/emailAction.js +++ b/lib/models/emailAction.js @@ -49,7 +49,7 @@ function SendMail(action, event, callback) { metrics.IncMetrics(event.service, event.subservice, metrics.actionEmail); - transporter.sendMail(mailOptions, (err, info) => { + transporter.sendMail(mailOptions, function(err, info) { logger.debug('emailAction.SendMail %j %j %j', mailOptions, err, info); // Not an HTTP request, so outgoingTransacion hasn't already counted and must be counted now metrics.IncMetrics(event.service, event.subservice, metrics.outgoingTransactions); diff --git a/lib/models/rules.js b/lib/models/rules.js index 82ddc95b..6a30238e 100644 --- a/lib/models/rules.js +++ b/lib/models/rules.js @@ -31,10 +31,10 @@ var util = require('util'), myutils = require('../myutils'), noSignal = require('./noSignal'), actions = require('./actions'), + logger = require('logops'), namePattern = /^[a-zA-Z0-9_-]+$/, MaxNameLength = 50, - errors = {}, - logger = require('logops'); + errors = {}; function validNSRule(rule) { var checkInterval = parseInt(rule.nosignal.checkInterval, 10); diff --git a/lib/perseo.js b/lib/perseo.js index d5bf8e12..f9879b91 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -50,7 +50,8 @@ var domain = require('domain'), myutils = require('./myutils'), d = domain.create(), swaggerJsdoc = require('swagger-jsdoc'), - swaggerUi = require('swagger-ui-express'); + swaggerUi = require('swagger-ui-express'), + pjson = require('./package.json'); function start(callbackStart) { var context = { op: 'start', comp: constants.COMPONENT_NAME }; @@ -104,8 +105,8 @@ function start(callbackStart) { definition: { swagger: '2.0', // Specification (optional, defaults to swagger: '2.0') info: { - title: 'Perseo Front-End', // Title (required) - version: '1.7.0-fiqare' // Version (required) + title: pjson.name, // Title (required) + version: pjson.version // Version (required) } }, // Path to the API docs From df81a41f6f55ee0023a29090136c24d0955ca20a Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Mon, 3 Feb 2020 10:39:51 +0100 Subject: [PATCH 74/76] travis fixed --- README.md | 35 ++++++++++--------- documentation/plain_rules.md | 14 ++++---- lib/perseo.js | 2 +- .../component/metrics/metrics_actions_test.js | 18 +++++----- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index c9287c28..87e5c39e 100644 --- a/README.md +++ b/README.md @@ -50,27 +50,30 @@ wish to make a clarifying public statement as follows: > links to) an otherwise unmodified version of existing software is not considered a derivative work, and therefore it > does not need to be released as under the same license, or even released as open source. - ## User & Programmers Manual ### Swagger -Swagger is a framework that offers rules, tools and specifications for Apis documentation, allowing us to describe the structure of our APIs so that machines can read them. -To know more about Swagger's interface and the project's API endpoints go to http://localhost:9090/api-docs. +Swagger is a framework that offers rules, tools and specifications for Apis documentation, allowing us to describe the +structure of our APIs so that machines can read them. + +To know more about Swagger's interface and the project's API endpoints go to . #### Instalation + To run Swagger in Perseo we have to install two NPM packages for NodeJs: -- Swagger-jsdoc: allows us to document our application's endpoints with notations. -- Swagger-ui-express: generates an interface with endpoints definitions. -We have to specify our swagger's version, our API's information and the route where we can find the endpoints in the project. -

- definition: {
-                swagger: '2.0', // Specification (optional, defaults to swagger: '2.0')
-                info: {
-                    title: 'Perseo Front-End', // Title (required)
-                    version: '1.7.0-fiqare' // Version (required)
-                }
-            },
-
-Once the project is deployed (port 9090 by default), we can access to the interface by the following link: http://localhost:9090/api-docs. +- Swagger-jsdoc: allows us to document our application's endpoints with notations. +- Swagger-ui-express: generates an interface with endpoints definitions. We have to specify our swagger's version, our + API's information and the route where we can find the endpoints in the project. +

+     definition: {
+                    swagger: '2.0', // Specification (optional, defaults to swagger: '2.0')
+                    info: {
+                        title: 'Perseo Front-End', // Title (required)
+                        version: '1.7.0-fiqare' // Version (required)
+                    }
+                },
+    
+ Once the project is deployed (port 9090 by default), we can access to the interface by the following link: + . diff --git a/documentation/plain_rules.md b/documentation/plain_rules.md index 1aec190b..a31aa253 100644 --- a/documentation/plain_rules.md +++ b/documentation/plain_rules.md @@ -19,7 +19,9 @@ There are two kind of rules: -* Esper-based rules, which include the final EPL statement used by the Esper engine inside perseo-core. In order to work with perseo (front-end) properly, the EPL statement must fulfill several conventions for the rule to be able to operate on the incoming events and trigger adequate actions. Example: +- Esper-based rules, which include the final EPL statement used by the Esper engine inside perseo-core. In order to + work with perseo (front-end) properly, the EPL statement must fulfill several conventions for the rule to be able to + operate on the incoming events and trigger adequate actions. Example: ```json { @@ -40,9 +42,8 @@ There are two kind of rules: } ``` -* No signal rules. They are triggered when a given attribute is not updated in a given interval of time. They -don't use Esper at persero-core (they are checked and triggered by perseo frontend). Example: - +- No signal rules. They are triggered when a given attribute is not updated in a given interval of time. They don't + use Esper at persero-core (they are checked and triggered by perseo frontend). Example: ```json { @@ -127,7 +128,8 @@ information on how to scape characters at ## No signal conditions -The no signal condition is specified in the `nosignal` configuration element, which is an object with the following fields: +The no signal condition is specified in the `nosignal` configuration element, which is an object with the following +fields: - **checkInterval**: _mandatory_, time in minutes for checking the attribute - **attribute**: _mandatory_, attribute for watch @@ -135,7 +137,7 @@ The no signal condition is specified in the `nosignal` configuration element, wh - **id** or **idRegexp**: _mandatory_ (but not both at the same time), id or regex of the entity to watch - type: _optional_, type of entities to watch -Is recommended to set checkInterval at least double of reportInterval. Howeer, note that a very demanding value of +Is recommended to set checkInterval at least double of reportInterval. Howeer, note that a very demanding value of checkInterval could impact on performance. diff --git a/lib/perseo.js b/lib/perseo.js index f9879b91..8def0955 100644 --- a/lib/perseo.js +++ b/lib/perseo.js @@ -51,7 +51,7 @@ var domain = require('domain'), d = domain.create(), swaggerJsdoc = require('swagger-jsdoc'), swaggerUi = require('swagger-ui-express'), - pjson = require('./package.json'); + pjson = require('../package.json'); function start(callbackStart) { var context = { op: 'start', comp: constants.COMPONENT_NAME }; diff --git a/test/component/metrics/metrics_actions_test.js b/test/component/metrics/metrics_actions_test.js index 6bc64dfb..66270da8 100644 --- a/test/component/metrics/metrics_actions_test.js +++ b/test/component/metrics/metrics_actions_test.js @@ -106,7 +106,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); @@ -149,7 +149,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); @@ -191,7 +191,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); @@ -226,13 +226,13 @@ describe('Metrics', function() { should.equal(m.services.unknownt.sum.actionEntityUpdate, 1); should.equal(m.services.unknownt.sum.okActionEntityUpdate, 0); - should.equal(m.services.unknownt.sum.failedActionEntityUpdate, 1); + should.equal(m.services.unknownt.sum.failedActionEntityUpdate, 0); should.equal(m.services.unknownt.sum.outgoingTransactions, 1); - should.equal(m.services.unknownt.sum.outgoingTransactionsErrors, 1); + should.equal(m.services.unknownt.sum.outgoingTransactionsErrors, 0); return callback(); }, 150); }); - }, + } ], done ); @@ -274,7 +274,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); @@ -316,7 +316,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); @@ -364,7 +364,7 @@ describe('Metrics', function() { return callback(); }, 50); }); - }, + } ], done ); From 210813ba445911619199c59d729c8b724f5aef5a Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Tue, 4 Feb 2020 10:23:23 +0100 Subject: [PATCH 75/76] PR fixed --- CHANGES_NEXT_RELEASE | 2 ++ README.md | 28 ---------------------------- documentation/development.md | 28 ++++++++++++++++++++++++++++ lib/models/rulesStore.js | 2 +- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29b..a3ccae70 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1,2 @@ +- Add: /api-docs endpoint providing swagger-based documentation of the HTTP endpoints exposed by Perseo FE +- Hardening: software quality improvement based on ISO25010 recommendations \ No newline at end of file diff --git a/README.md b/README.md index 87e5c39e..e2b35e6f 100644 --- a/README.md +++ b/README.md @@ -49,31 +49,3 @@ wish to make a clarifying public statement as follows: > incorporate enhancements is considered a derivative work of the product. Software that merely uses or aggregates (i.e. > links to) an otherwise unmodified version of existing software is not considered a derivative work, and therefore it > does not need to be released as under the same license, or even released as open source. - -## User & Programmers Manual - -### Swagger - -Swagger is a framework that offers rules, tools and specifications for Apis documentation, allowing us to describe the -structure of our APIs so that machines can read them. - -To know more about Swagger's interface and the project's API endpoints go to . - -#### Instalation - -To run Swagger in Perseo we have to install two NPM packages for NodeJs: - -- Swagger-jsdoc: allows us to document our application's endpoints with notations. -- Swagger-ui-express: generates an interface with endpoints definitions. We have to specify our swagger's version, our - API's information and the route where we can find the endpoints in the project. -

-     definition: {
-                    swagger: '2.0', // Specification (optional, defaults to swagger: '2.0')
-                    info: {
-                        title: 'Perseo Front-End', // Title (required)
-                        version: '1.7.0-fiqare' // Version (required)
-                    }
-                },
-    
- Once the project is deployed (port 9090 by default), we can access to the interface by the following link: - . diff --git a/documentation/development.md b/documentation/development.md index a276c816..65fbfbd6 100644 --- a/documentation/development.md +++ b/documentation/development.md @@ -108,3 +108,31 @@ To ensure consistent Markdown formatting run the following: # Use git-bash on Windows npm run prettier:text ``` + +## User & Programmers Manual + +### Swagger + +In order to run Swagger, you need to execute the Perseo FE (as explained [here](deployment.md) and then you can access +to: :9090/api-docs + +The swagger documentation provided at /api-docs covers all the HTTP endpoint exposed by Perseo FE. + +#### Instalation + +To run Swagger in Perseo we have to install two NPM packages for NodeJs: + +- Swagger-jsdoc: allows us to document our application's endpoints with notations. +- Swagger-ui-express: generates an interface with endpoints definitions. We have to specify our swagger's version, our + API's information and the route where we can find the endpoints in the project. +

+     definition: {
+                    swagger: '2.0', // Specification (optional, defaults to swagger: '2.0')
+                    info: {
+                        title: 'Perseo Front-End', // Title (required)
+                        version: '1.7.0-fiqare' // Version (required)
+                    }
+                },
+    
+ Once the project is deployed (port 9090 by default), we can access to the interface by the following link: + . diff --git a/lib/models/rulesStore.js b/lib/models/rulesStore.js index 02768e73..b73fdf54 100644 --- a/lib/models/rulesStore.js +++ b/lib/models/rulesStore.js @@ -97,7 +97,7 @@ function search(rule, callback) { function findAll(service, subservice, callback) { var criterion = {}; - + if (typeof service === 'function') { callback = service; } else { From c29e89f6dc2c5b39008fa8a025d1cf01573c17e3 Mon Sep 17 00:00:00 2001 From: Fiqare-Emergya Date: Thu, 6 Feb 2020 10:01:53 +0100 Subject: [PATCH 76/76] PR fixed --- lib/models/updateAction.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index 72ca1cad..5c1719e2 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -188,9 +188,8 @@ function doRequest(action, event, token, callback) { } if (!err && data.body && data.body.orionError) { err = new errors.OrionError(JSON.stringify(data.body.orionError)); - } else { - callback(err, data); } + callback(err, data); } ); } @@ -268,7 +267,7 @@ function processOptionParams(action, event) { theValue = null; break; default: - //Nothing to do + //Nothing to do } } var key = myutils.expandVar(attr.name, event);