Skip to content

Commit

Permalink
Synced changes from the walkner-wmes project.
Browse files Browse the repository at this point in the history
  • Loading branch information
morkai committed Apr 19, 2015
1 parent a37eb01 commit dd9341b
Show file tree
Hide file tree
Showing 30 changed files with 326 additions and 376 deletions.
30 changes: 27 additions & 3 deletions backend/extensions.js
Expand Up @@ -6,6 +6,16 @@

var util = require('util');

Object.defineProperty(Object.prototype, 'toJSON', {
configurable: false,
enumerable: false,
writable: true,
value: function()
{
return this;
}
});

Object.defineProperty(Error.prototype, 'toJSON', {
configurable: false,
enumerable: false,
Expand All @@ -17,11 +27,14 @@ Object.defineProperty(Error.prototype, 'toJSON', {
message: error.message,
stack: error.stack
};
var keys = Object.keys(error);

Object.keys(error).forEach(function(property)
for (var i = 0; i < keys.length; ++i)
{
result[property] = error[property];
});
var key = keys[i];

result[key] = error[key];
}

return result;
}
Expand All @@ -31,3 +44,14 @@ console.inspect = function(value, depth, colors)
{
console.log(util.inspect(value, {depth: depth || null, colors: colors !== false}));
};

console.bench = function(label, context, func)
{
var time = process.hrtime();
var result = func.call(context);
var diff = process.hrtime(time);

console.log('[bench] %s %d ms', label, (diff[0] * 1e9 + diff[1]) / 1e6);

return result;
};
147 changes: 147 additions & 0 deletions backend/helpers.js
@@ -0,0 +1,147 @@
// Copyright (c) 2014, Łukasz Walukiewicz <lukasz@walukiewicz.eu>. Some Rights Reserved.
// Licensed under CC BY-NC-SA 4.0 <http://creativecommons.org/licenses/by-nc-sa/4.0/>.
// Part of the walkner-tp project <http://lukasz.walukiewicz.eu/p/walkner-tp>

'use strict';

exports.formatDate = function(date)
{
if (!date)
{
return '';
}

if (!(date instanceof Date))
{
date = new Date(date);
}

var result = date.getFullYear() + '-';
var month = date.getMonth() + 1;
var day = date.getDate();

if (month < 10)
{
result += '0' + month;
}
else
{
result += month;
}

if (day < 10)
{
result += '-0' + day;
}
else
{
result += '-' + day;
}

return result;
};

exports.formatTime = function(date)
{
if (!date)
{
return '';
}

if (!(date instanceof Date))
{
date = new Date(date);
}

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var result = hours < 10 ? ('0' + hours) : hours;

if (minutes < 10)
{
result += ':0' + minutes;
}
else
{
result += ':' + minutes;
}

if (seconds < 10)
{
result += ':0' + seconds;
}
else
{
result += ':' + seconds;
}

return result;
};

exports.formatDateTime = function(date)
{
if (!date)
{
return '';
}

if (!(date instanceof Date))
{
date = new Date(date);
}

var result = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

if (month < 10)
{
result += '-0' + month;
}
else
{
result += '-' + month;
}

if (day < 10)
{
result += '-0' + day;
}
else
{
result += '-' + day;
}

if (hours < 10)
{
result += ' 0' + hours;
}
else
{
result += ' ' + hours;
}

if (minutes < 10)
{
result += ':0' + minutes;
}
else
{
result += ':' + minutes;
}

if (seconds < 10)
{
result += ':0' + seconds;
}
else
{
result += ':' + seconds;
}

return result;
};
14 changes: 11 additions & 3 deletions backend/main.js
Expand Up @@ -8,9 +8,10 @@ var startTime = Date.now();

require('./extensions');

var lodash = require('lodash');
var _ = require('lodash');
var moment = require('moment');
var main = require('h5.main');
var blocked = process.env.NODE_ENV === 'production' ? function() {} : require('blocked');
var config = require(process.argv[2]);

moment.locale('pl');
Expand Down Expand Up @@ -50,7 +51,7 @@ var modules = (config.modules || []).map(function(module)
});

var app = {
options: lodash.merge({}, config, {
options: _.merge({}, config, {
id: config.id,
startTime: startTime,
env: process.env.NODE_ENV,
Expand All @@ -59,4 +60,11 @@ var app = {
})
};

main(app, modules);
_.merge(app, require('./helpers'));

blocked(function(ms)
{
app.debug("Event loop blocked for %sms :(", ms);
});

main(app, modules);
6 changes: 3 additions & 3 deletions backend/modules/createDictionaryModule.js
Expand Up @@ -4,7 +4,7 @@

'use strict';

var lodash = require('lodash');
var _ = require('lodash');

module.exports = function createDictionaryModule(modelName, setUpRoutes, customSetUp)
{
Expand Down Expand Up @@ -48,7 +48,7 @@ module.exports = function createDictionaryModule(modelName, setUpRoutes, customS

app.broker.subscribe(Model.TOPIC_PREFIX + '.deleted', function(message)
{
module.models = lodash.filter(module.models, function(model)
module.models = _.filter(module.models, function(model)
{
return model._id !== message.model._id;
});
Expand Down Expand Up @@ -77,7 +77,7 @@ module.exports = function createDictionaryModule(modelName, setUpRoutes, customS
module.models = models;
module.modelsById = {};

lodash.forEach(models, function(model)
_.forEach(models, function(model)
{
module.modelsById[model._id] = model;
});
Expand Down
27 changes: 16 additions & 11 deletions backend/modules/events/index.js
Expand Up @@ -4,8 +4,7 @@

'use strict';

var lodash = require('lodash');
var mongoose = require('mongoose');
var _ = require('lodash');
var setUpEventsRoutes = require('./routes');

exports.DEFAULT_CONFIG = {
Expand All @@ -15,6 +14,7 @@ exports.DEFAULT_CONFIG = {
collection: function(app) { return app.mongodb.db.collection('events'); },
insertDelay: 1000,
topics: ['events.**'],
blacklist: [],
print: []
};

Expand Down Expand Up @@ -81,7 +81,7 @@ exports.start = function startEventsModule(app, module)
}
else
{
types.forEach(function(type)
_.forEach(types, function(type)
{
module.types[type] = 1;
});
Expand All @@ -98,25 +98,25 @@ exports.start = function startEventsModule(app, module)
{
var queueInfoEvent = queueEvent.bind(null, 'info');

module.config.topics.forEach(function(topic)
_.forEach(module.config.topics, function(topic)
{
app.broker.subscribe(topic, queueInfoEvent);
});
}
else
{
lodash.each(module.config.topics, function(topics, severity)
_.forEach(module.config.topics, function(topics, severity)
{
var queueCustomSeverityEvent = queueEvent.bind(null, severity);

topics.forEach(function(topic)
_.forEach(topics, function(topic)
{
app.broker.subscribe(topic, queueCustomSeverityEvent);
});
});
}

module.config.print.forEach(function(topic)
_.forEach(module.config.print, function(topic)
{
app.broker.subscribe(topic, printMessage);
});
Expand All @@ -134,12 +134,17 @@ exports.start = function startEventsModule(app, module)
return fetchAllTypes();
}

if (module.config.blacklist.indexOf(topic) !== -1)
{
return;
}

var user = null;

if (lodash.isObject(data.user))
if (_.isObject(data.user))
{
user = {
_id: new mongoose.Types.ObjectId(data.user._id),
_id: String(data.user._id),
name: data.user.lastName && data.user.firstName
? (data.user.lastName + ' ' + data.user.firstName)
: data.user.login,
Expand All @@ -148,7 +153,7 @@ exports.start = function startEventsModule(app, module)
};
}

if (!lodash.isObject(data))
if (!_.isObject(data))
{
data = {};
}
Expand All @@ -159,7 +164,7 @@ exports.start = function startEventsModule(app, module)

var type = topic.replace(/^events\./, '');

if (lodash.isString(data.severity))
if (_.isString(data.severity))
{
severity = data.severity;

Expand Down

0 comments on commit dd9341b

Please sign in to comment.