Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prettier code formatting #314

Merged
merged 13 commits into from
Feb 1, 2019
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"singleQuote": true,
"parser": "flow",
"printWidth": 120,
"trailingComma": "none",
"tabWidth": 4
}
50 changes: 22 additions & 28 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ var config = {};
*/
config.logLevel = 'INFO';


/**
* Configures the exposed API.
*/
config.endpoint = {
host: 'localhost',
port: 9090,
rulesPath : '/rules',
actionsPath : '/actions/do',
noticesPath : '/notices',
vrPath : '/m2m/vrules',
checkPath : '/check',
versionPath : '/version',
rulesPath: '/rules',
actionsPath: '/actions/do',
noticesPath: '/notices',
vrPath: '/m2m/vrules',
checkPath: '/check',
versionPath: '/version',
logPath: '/admin/log',
metricsPath: '/admin/metrics'
};
Expand All @@ -58,38 +57,36 @@ config.isMaster = true;
*/
config.slaveDelay = 500;


/**
* DB Configuration.
*/
config.mongo = {
// The URI to use for the database connection. It supports replica set URIs.
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
// I.e.: 'mongodb://user:pass@host1:27017,host2:27018,host3:27019/cep?replicaSet=myrep'
url : 'mongodb://localhost:27017/cep'
url: 'mongodb://localhost:27017/cep'
};

/**
* OrionDB Configuration.
*/
config.orionDb = {
url : 'mongodb://localhost:27017/orion',
collection : 'entities',
prefix : 'orion',
url: 'mongodb://localhost:27017/orion',
collection: 'entities',
prefix: 'orion',
batchSize: 500
};


/**
* EPL core options
*
* interval is the time in milliseconds between refreshing rules
* at core. Each <<interval>> ms, the rules are sent to core.
*/
config.perseoCore = {
rulesURL : 'http://localhost:8080/perseo-core/rules',
noticesURL : 'http://localhost:8080/perseo-core/events',
interval: 60e3*5
rulesURL: 'http://localhost:8080/perseo-core/rules',
noticesURL: 'http://localhost:8080/perseo-core/events',
interval: 60e3 * 5
};
/**
* NEXT EPL core options (with HA)
Expand All @@ -104,35 +101,32 @@ config.nextCore = {
*/
};


/**
* SMTP endpoint options
*/
config.smtp = {
port: 25,
host: 'smtpserver',
secure: false
secure: false,
/*
,
auth: {
user: 'abc',
pass: 'xyz'
}
*/
,
tls: {
// keep rejectUnauthorized to false when secure is also false
rejectUnauthorized: false
// keep rejectUnauthorized to false when secure is also false
rejectUnauthorized: false
}

};

/**
* SMS endpoint options
*/
config.sms = {
URL : 'http://sms-endpoint/smsoutbound',
API_KEY : '',
URL: 'http://sms-endpoint/smsoutbound',
API_KEY: '',
API_SECRET: '',
from: 'tel:22012;phone-context=+34'
};
Expand All @@ -153,7 +147,7 @@ config.smpp = {
* Orion (Context Broker) endpoint options
*/
config.orion = {
URL : 'http://orion-endpoint:1026/NGSI10/updateContext'
URL: 'http://orion-endpoint:1026/NGSI10/updateContext'
};

/**
Expand All @@ -171,7 +165,7 @@ config.authentication = {
* @type {{}}
*/
config.collections = {
rules : 'rules',
rules: 'rules',
executions: 'executions'
};

Expand All @@ -190,8 +184,8 @@ config.executionsTTL = 1 * 24 * 60 * 60;
*
* @type {{}}
*/
config.DEFAULT_SUBSERVICE= '/';
config.DEFAULT_SERVICE= 'unknownt';
config.DEFAULT_SUBSERVICE = '/';
config.DEFAULT_SERVICE = 'unknownt';

/**
* CheckDB configuration
Expand Down
20 changes: 10 additions & 10 deletions ghpages/javascripts/scale.fix.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var metas = document.getElementsByTagName('meta');
var i;
if (navigator.userAgent.match(/iPhone/i)) {
for (i=0; i<metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
for (i = 0; i < metas.length; i++) {
if (metas[i].name == 'viewport') {
metas[i].content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
}
}
}
document.addEventListener("gesturestart", gestureStart, false);
document.addEventListener('gesturestart', gestureStart, false);
}
function gestureStart() {
for (i=0; i<metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6";
for (i = 0; i < metas.length; i++) {
if (metas[i].name == 'viewport') {
metas[i].content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}
}
}
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/perseo');
module.exports = require('./lib/perseo');
1 change: 0 additions & 1 deletion lib/alarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var util = require('util'),
logger = require('logops'),
alarms = {};


function raise(alarm, context, message) {
var state = alarms[alarm];
context = (process.domain && process.domain.context) || {};
Expand Down
5 changes: 1 addition & 4 deletions lib/appContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
*/
'use strict';

var
database,
orionDatabase;
var database, orionDatabase;

function SetDB(db) {
database = db;
Expand Down Expand Up @@ -67,4 +65,3 @@ module.exports.SetOrionDB = SetOrionDB;
* @return {Object} db connection
*/
module.exports.OrionDb = OrionDB;

1 change: 0 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ module.exports = {
REALIP_HEADER: 'X-Real-IP',
COMPONENT_NAME: 'perseo-fe'
};

90 changes: 50 additions & 40 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ var async = require('async'),
database,
orionDb,
delay = config.checkDB.delay,
context = {comp: constants.COMPONENT_NAME, op: 'checkDB'};
context = { comp: constants.COMPONENT_NAME, op: 'checkDB' };

function pingAux(db, component, callback) {
db.command({ping: 1}, function(err, result) {
db.command({ ping: 1 }, function(err, result) {
if (err) {
alarm.raise(component, context, err.message);
}
else {
} else {
alarm.release(component, context, result);
}
return callback(err, result);
Expand All @@ -49,39 +48,43 @@ function pingAux(db, component, callback) {
function getDbAux(url, component, callback) {
var client = require('mongodb').MongoClient,
checkDbHealthFunc;
client.connect(url, {
db: {
bufferMaxEntries: config.checkDB.bufferMaxEntries,
domainsEnabled: true
client.connect(
url,
{
db: {
bufferMaxEntries: config.checkDB.bufferMaxEntries,
domainsEnabled: true
},
server: {
reconnectTries: config.checkDB.reconnectTries,
reconnectInterval: config.checkDB.reconnectInterval,
domainsEnabled: true
}
},
server: {
reconnectTries: config.checkDB.reconnectTries,
reconnectInterval: config.checkDB.reconnectInterval,
domainsEnabled: true
}
}, function(err, db) {
if (err) {
myutils.logErrorIf(err, 'connect', context);
return callback(err, null);
}
function(err, db) {
if (err) {
myutils.logErrorIf(err, 'connect', context);
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(err, result) {
logger.debug('ping (%s) %j', component, err || 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);
});
};
setInterval(checkDbHealthFunc, delay);
return callback(null, db);
});

checkDbHealthFunc = function checkDbHealth() {
pingAux(db, component, function(err, result) {
logger.debug('ping (%s) %j', component, err || result);
});
};
setInterval(checkDbHealthFunc, delay);
return callback(null, db);
}
);
}
function getDb(callback) {
getDbAux(config.mongo.url, alarm.DATABASE, function cb(err, db) {
Expand All @@ -99,7 +102,7 @@ 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) {
collection.ensureIndex(fields, { unique: true }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
Expand All @@ -108,18 +111,25 @@ function ensureIndex(collection, fields, callback) {
function ensureIndexTTL(collection, fields, ttl, callback) {
database.collection(collection, function(err, collection) {
myutils.logErrorIf(err, collection);
collection.ensureIndex(fields, {expireAfterSeconds: ttl}, function(err, indexName) {
collection.ensureIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
});
}

function setUp(cbSU) {
async.parallel([
ensureIndex.bind(null, rulesCollection, {name: 1, subservice: 1, service: 1}),
ensureIndexTTL.bind(null, executionsCollection, {lastTime: 1}, config.executionsTTL)
], cbSU);
async.parallel(
[
ensureIndex.bind(null, rulesCollection, {
name: 1,
subservice: 1,
service: 1
}),
ensureIndexTTL.bind(null, executionsCollection, { lastTime: 1 }, config.executionsTTL)
],
cbSU
);
}

function closeAux(db, callback) {
Expand Down
6 changes: 2 additions & 4 deletions lib/middleware/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var domain = require('domain'),
* @return {Function} Express middleWare.
*/
function requestDomain() {

return function requestDomain(req, res, next) {
var reqDomain = domain.create();
reqDomain.add(req);
Expand All @@ -57,7 +56,8 @@ function requestDomain() {
cleanDomain = function() {
var responseTime = Date.now() - reqDomain.start;
logger.debug('response-time: ' + responseTime + ' statusCode: ' + res.statusCode);
if (req.url.indexOf(config.endpoint.actionsPath) !== 0 &&
if (
req.url.indexOf(config.endpoint.actionsPath) !== 0 &&
req.url.indexOf(config.endpoint.checkPath) !== 0 &&
req.url.indexOf(config.endpoint.versionPath) !== 0 &&
req.url.indexOf(config.endpoint.logPath) !== 0 &&
Expand Down Expand Up @@ -114,9 +114,7 @@ function requestDomain() {
res.once('finish', cleanDomain);
reqDomain.on('error', domainErrorHandler);
reqDomain.run(requestHandler);

};

}
/**
* Express middleWare that creates a domain per request
Expand Down
1 change: 0 additions & 1 deletion lib/middleware/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ exports.middleware = function() {
}
};
};

11 changes: 8 additions & 3 deletions lib/middleware/logRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ var logger = require('logops');
*/
exports.middleware = function() {
return function logRequest(req, res, next) {
logger.info('incoming request %j', {method: req.method, url: req.url, headers: req.headers, body: req.body,
subservice: req.subservice, service: req.service});
logger.info('incoming request %j', {
method: req.method,
url: req.url,
headers: req.headers,
body: req.body,
subservice: req.subservice,
service: req.service
});
next();
};
};

Loading