UNPKG

openhim-core

Version:

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

194 lines (172 loc) 5.7 kB
var SDC, addRouteEvents, application, config, domain, enableTSNormalization, events, formatTS, getTsDiff, logger, messageStore, minEvPeriod, moment, orchestrationTsBufferMillis, os, ref, ref1, ref2, sdc, statsdServer, storeEvents; moment = require('moment'); logger = require('winston'); events = require('../model/events'); messageStore = require('../middleware/messageStore'); config = require("../config/config"); config.visualizer = config.get('visualizer'); statsdServer = config.get('statsd'); application = config.get('application'); SDC = require('statsd-client'); os = require('os'); domain = (os.hostname()) + "." + application.name + ".appMetrics"; sdc = new SDC(statsdServer); minEvPeriod = (ref = config.visualizer.minimumEventPeriodMillis) != null ? ref : 100; enableTSNormalization = (ref1 = config.visualizer.enableTSNormalization) != null ? ref1 : false; if (enableTSNormalization === true) { orchestrationTsBufferMillis = (ref2 = config.visualizer.orchestrationTsBufferMillis) != null ? ref2 : 100; } else { orchestrationTsBufferMillis = 0; } formatTS = function(ts) { return moment(new Date(ts)).valueOf(); }; getTsDiff = function(ctxStartTS, obj) { var earliestTS, i, len, record, ts, tsDiff; earliestTS = 0; for (i = 0, len = obj.length; i < len; i++) { record = obj[i]; ts = formatTS(record.request.timestamp); if (earliestTS < ts) { earliestTS = ts; } } tsDiff = ctxStartTS - earliestTS; tsDiff += orchestrationTsBufferMillis; return tsDiff; }; addRouteEvents = function(ctx, dst, route, prefix, tsDiff) { var endTS, ref3, ref4, ref5, routeStatus, startTS; if (((route != null ? (ref3 = route.request) != null ? ref3.timestamp : void 0 : void 0) != null) && ((route != null ? (ref4 = route.response) != null ? ref4.timestamp : void 0 : void 0) != null)) { startTS = formatTS(route.request.timestamp); endTS = formatTS(route.response.timestamp); if (enableTSNormalization === true) { startTS = startTS + tsDiff; endTS = endTS + tsDiff; } if (startTS > endTS) { startTS = endTS; } if (endTS - startTS < minEvPeriod) { endTS = startTS + minEvPeriod; } dst.push({ channelID: ctx.authorisedChannel._id, transactionID: ctx.transactionId, visualizerTimestamp: startTS, route: prefix, event: 'start', name: route.name }); routeStatus = 'success'; if ((500 <= (ref5 = route.response.status) && ref5 <= 599)) { routeStatus = 'error'; } return dst.push({ channelID: ctx.authorisedChannel._id, transactionID: ctx.transactionId, visualizerTimestamp: endTS, route: prefix, event: 'end', name: route.name, status: route.response.status, visualizerStatus: routeStatus }); } }; storeEvents = function(ctx, done) { var endTS, event, i, j, k, l, len, len1, len2, len3, now, orch, ref3, ref4, ref5, ref6, ref7, route, startTS, status, trxEvents, tsDiff; logger.info("Storing events for transaction: " + ctx.transactionId); trxEvents = []; startTS = formatTS(ctx.requestTimestamp); endTS = formatTS(ctx.response.timestamp); if (startTS > endTS) { startTS = endTS; } if (endTS - startTS < minEvPeriod) { endTS = startTS + minEvPeriod; } trxEvents.push({ channelID: ctx.authorisedChannel._id, transactionID: ctx.transactionId, visualizerTimestamp: startTS, route: 'primary', event: 'start', name: ctx.authorisedChannel.name }); if (ctx.routes) { tsDiff = getTsDiff(startTS, ctx.routes); ref3 = ctx.routes; for (i = 0, len = ref3.length; i < len; i++) { route = ref3[i]; addRouteEvents(ctx, trxEvents, route, 'route', tsDiff); if (route.orchestrations) { tsDiff = getTsDiff(startTS, route.orchestrations); ref4 = route.orchestrations; for (j = 0, len1 = ref4.length; j < len1; j++) { orch = ref4[j]; addRouteEvents(ctx, trxEvents, orch, 'orchestration', tsDiff); } } } } if (((ref5 = ctx.mediatorResponse) != null ? ref5.orchestrations : void 0) != null) { tsDiff = getTsDiff(startTS, ctx.mediatorResponse.orchestrations); ref6 = ctx.mediatorResponse.orchestrations; for (k = 0, len2 = ref6.length; k < len2; k++) { orch = ref6[k]; addRouteEvents(ctx, trxEvents, orch, 'orchestration', tsDiff); } } status = 'success'; if ((500 <= (ref7 = ctx.response.status) && ref7 <= 599)) { status = 'error'; } trxEvents.push({ channelID: ctx.authorisedChannel._id, transactionID: ctx.transactionId, visualizerTimestamp: endTS + orchestrationTsBufferMillis, route: 'primary', event: 'end', name: ctx.authorisedChannel.name, status: ctx.response.status, visualizerStatus: status }); now = new Date; for (l = 0, len3 = trxEvents.length; l < len3; l++) { event = trxEvents[l]; event.created = now; } return events.Event.collection.ensureIndex({ created: 1 }, { expireAfterSeconds: 3600 }, function() { return events.Event.collection.insert(trxEvents, function(err) { if (err) { return done(err); } else { return done(); } }); }); }; exports.koaMiddleware = function*(next) { var ctx, startTime; (yield next); if (statsdServer.enabled) { startTime = new Date(); } ctx = this; (function(ctx) { var f; f = function() { return storeEvents(ctx, function() {}); }; return setTimeout(f, 0); })(ctx); if (statsdServer.enabled) { return sdc.timing(domain + ".eventsMiddleware", startTime); } }; //# sourceMappingURL=events.js.map