UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

284 lines (261 loc) 9.37 kB
var calculateEarliestRouteDiff, config, createChannelEndEvent, createChannelStartEvent, createOrchestrationEvents, createPrimaryRouteEvents, createRouteEvents, createSecondaryRouteEvents, determineStatusType, enableTSNormalization, events, logger, messageStore, moment, normalizationBuffer, ref, saveEvents, timestampAsMillis; moment = require('moment'); logger = require('winston'); events = require('../model/events'); messageStore = require('../middleware/messageStore'); config = require('../config/config'); config.events = config.get('events'); if (!config.events) { config.events = config.get('visualizer'); config.events.normalizationBuffer = config.events.orchestrationTsBufferMillis; } enableTSNormalization = (ref = config.events.enableTSNormalization) != null ? ref : false; if (enableTSNormalization === true) { normalizationBuffer = 100; } else { normalizationBuffer = 0; } timestampAsMillis = function(ts) { return moment(new Date(ts)).valueOf(); }; calculateEarliestRouteDiff = function(baseTS, routes) { var earliestTS, i, len, route, ts, tsDiff; earliestTS = 0; for (i = 0, len = routes.length; i < len; i++) { route = routes[i]; ts = timestampAsMillis(route.request.timestamp); if (earliestTS < ts) { earliestTS = ts; } } tsDiff = baseTS - earliestTS; tsDiff += normalizationBuffer; return tsDiff; }; determineStatusType = function(statusCode) { var status; status = 'success'; if ((500 <= statusCode && statusCode <= 599)) { status = 'error'; } return status; }; exports.saveEvents = saveEvents = function(trxEvents, callback) { var event, i, len, now; now = new Date; for (i = 0, len = trxEvents.length; i < len; i++) { event = trxEvents[i]; event.created = now; } return events.Event.collection.ensureIndex({ created: 1 }, { expireAfterSeconds: 3600 }, function() { return events.Event.collection.insert(trxEvents, function(err) { if (err) { return callback(err); } else { return callback(); } }); }); }; createRouteEvents = function(dst, transactionId, channel, route, type, tsAdjustment, autoRetryAttempt) { var endTS, ref1, ref2, startTS; if (((route != null ? (ref1 = route.request) != null ? ref1.timestamp : void 0 : void 0) != null) && ((route != null ? (ref2 = route.response) != null ? ref2.timestamp : void 0 : void 0) != null)) { startTS = timestampAsMillis(route.request.timestamp); endTS = timestampAsMillis(route.response.timestamp); if (enableTSNormalization === true) { startTS = startTS + tsAdjustment; endTS = endTS + tsAdjustment; } if (startTS > endTS) { startTS = endTS; } dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: startTS, type: type, event: 'start', name: route.name, mediator: route.mediatorURN, autoRetryAttempt: autoRetryAttempt }); return dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: endTS, type: type, event: 'end', name: route.name, mediator: route.mediatorURN, status: route.response.status, statusType: determineStatusType(route.response.status), autoRetryAttempt: autoRetryAttempt }); } }; createChannelStartEvent = function(dst, transactionId, requestTimestamp, channel, autoRetryAttempt) { return dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: timestampAsMillis(requestTimestamp), type: 'channel', event: 'start', name: channel.name, autoRetryAttempt: autoRetryAttempt }); }; createChannelEndEvent = function(dst, transactionId, requestTimestamp, channel, response, autoRetryAttempt) { var endTS, startTS; startTS = timestampAsMillis(requestTimestamp); endTS = timestampAsMillis(response.timestamp); if (endTS < startTS) { endTS = startTS; } return dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: endTS + normalizationBuffer, type: 'channel', event: 'end', name: channel.name, status: response.status, statusType: determineStatusType(response.status), autoRetryAttempt: autoRetryAttempt }); }; createPrimaryRouteEvents = function(dst, transactionId, requestTimestamp, channel, routeName, mediatorURN, response, autoRetryAttempt) { var endTS, startTS; startTS = timestampAsMillis(requestTimestamp); dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: startTS, type: 'primary', event: 'start', name: routeName, mediator: mediatorURN, autoRetryAttempt: autoRetryAttempt }); endTS = timestampAsMillis(response.timestamp); if (endTS < startTS) { endTS = startTS; } return dst.push({ channelID: channel._id, transactionID: transactionId, normalizedTimestamp: endTS + normalizationBuffer, type: 'primary', event: 'end', name: routeName, status: response.status, statusType: determineStatusType(response.status), mediator: mediatorURN, autoRetryAttempt: autoRetryAttempt }); }; createOrchestrationEvents = function(dst, transactionId, requestTimestamp, channel, orchestrations) { var i, len, orch, results, startTS, tsDiff; if (requestTimestamp) { startTS = timestampAsMillis(requestTimestamp); tsDiff = calculateEarliestRouteDiff(startTS, orchestrations); } results = []; for (i = 0, len = orchestrations.length; i < len; i++) { orch = orchestrations[i]; results.push(createRouteEvents(dst, transactionId, channel, orch, 'orchestration', tsDiff)); } return results; }; exports.createSecondaryRouteEvents = createSecondaryRouteEvents = function(dst, transactionId, requestTimestamp, channel, routes) { var i, len, orch, results, route, startTS, tsDiff; startTS = timestampAsMillis(requestTimestamp); tsDiff = calculateEarliestRouteDiff(startTS, routes); results = []; for (i = 0, len = routes.length; i < len; i++) { route = routes[i]; createRouteEvents(dst, transactionId, channel, route, 'route', tsDiff); if (route.orchestrations) { tsDiff = calculateEarliestRouteDiff(startTS, route.orchestrations); results.push((function() { var j, len1, ref1, results1; ref1 = route.orchestrations; results1 = []; for (j = 0, len1 = ref1.length; j < len1; j++) { orch = ref1[j]; results1.push(createRouteEvents(dst, transactionId, channel, orch, 'orchestration', tsDiff)); } return results1; })()); } else { results.push(void 0); } } return results; }; exports.createTransactionEvents = function(dst, transaction, channel) { var getPrimaryRouteName, ref1, timestamp; getPrimaryRouteName = function() { var i, len, r, ref1; ref1 = channel.routes; for (i = 0, len = ref1.length; i < len; i++) { r = ref1[i]; if (r.primary) { return r.name; } } return null; }; timestamp = ((ref1 = transaction.request) != null ? ref1.timestamp : void 0) ? transaction.request.timestamp : new Date(); if (transaction.request && transaction.response) { createPrimaryRouteEvents(dst, transaction._id, timestamp, channel, getPrimaryRouteName(), null, transaction.response); } if (transaction.orchestrations) { createOrchestrationEvents(dst, transaction._id, timestamp, channel, transaction.orchestrations); } if (transaction.routes) { return createSecondaryRouteEvents(dst, transaction._id, timestamp, channel, transaction.routes); } }; exports.koaMiddleware = function*(next) { var ctx, runAsync; ctx = this; runAsync = function(method) { return (function(ctx) { var f; f = function() { return method(ctx, function(err) { if (err) { return logger.err(err); } }); }; return setTimeout(f, 0); })(ctx); }; runAsync(function(ctx, done) { var trxEvents; logger.debug("Storing channel start event for transaction: " + ctx.transactionId); trxEvents = []; createChannelStartEvent(trxEvents, ctx.transactionId, ctx.requestTimestamp, ctx.authorisedChannel, ctx.currentAttempt); return saveEvents(trxEvents, done); }); (yield next); return runAsync(function(ctx, done) { var mediatorURN, orchestrations, ref1, ref2, trxEvents; logger.debug("Storing channel end and primary routes events for transaction: " + ctx.transactionId); trxEvents = []; mediatorURN = (ref1 = ctx.mediatorResponse) != null ? ref1['x-mediator-urn'] : void 0; orchestrations = (ref2 = ctx.mediatorResponse) != null ? ref2.orchestrations : void 0; createPrimaryRouteEvents(trxEvents, ctx.transactionId, ctx.requestTimestamp, ctx.authorisedChannel, ctx.primaryRoute.name, mediatorURN, ctx.response, ctx.currentAttempt); if (orchestrations) { createOrchestrationEvents(trxEvents, ctx.transactionId, ctx.requestTimestamp, ctx.authorisedChannel, orchestrations, ctx.currentAttempt); } createChannelEndEvent(trxEvents, ctx.transactionId, ctx.requestTimestamp, ctx.authorisedChannel, ctx.response, ctx.currentAttempt); return saveEvents(trxEvents, done); }); }; //# sourceMappingURL=events.js.map