UNPKG

openhim-core

Version:

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

183 lines (157 loc) 5.64 kB
var Q, application, co, config, convertToRequiredFormat, domain, fetchData, http, logger, metrics, moment, os, request, statsdServer, statusArray; http = require('http'); config = require('../config/config'); application = config.get('application'); os = require("os"); domain = os.hostname() + '.' + application.name; statsdServer = config.get('statsd'); Q = require("q"); request = require('koa-request'); moment = require("moment"); metrics = require("../metrics"); logger = require("winston"); co = require("co"); statusArray = ['Processing', 'Failed', 'Completed', 'Successful', 'Completed with error(s)']; exports.retrieveTransactionCountPerHour = function*() { var path, raw; path = "/render?target=transformNull(summarize(stats.counters." + domain + ".channels.count,'1hour'))&from=-1days&format=json"; raw = (yield exports.fetchData(path)); return this.body = raw.data.map(function(item) { return { load: item[0], timestamp: moment.unix(item[1]) }; }); }; exports.fetchGlobalStatusMetrics = function*(allowedIds) { var channelId, data, j, k, len, len1, path, renderUrl, result, statusType, total; allowedIds = allowedIds.length > 0 ? allowedIds : (yield metrics.getAllowedChannelIDs(this.authenticated)); data = []; for (j = 0, len = allowedIds.length; j < len; j++) { channelId = allowedIds[j]; total = {}; renderUrl = "/render?target=transformNull(summarize(stats.counters." + domain + ".channels." + channelId; for (k = 0, len1 = statusArray.length; k < len1; k++) { statusType = statusArray[k]; path = renderUrl + ".statuses." + statusType + ".count,'1day'))&format=json"; result = (yield exports.fetchData(path)); if (result && result.data) { total[statusType] = result.data[1][0]; } else { total[statusType] = 0; } } data.push({ _id: { channelID: channelId }, failed: total.Failed, successful: total.Successful, processing: total.Processing, completed: total.Completed, completedWErrors: total['Completed with error(s)'] }); } return this.body = data; }; exports.retrieveAverageLoadTimePerHour = function*(type) { var data, path, raw; path = "/render?target=transformNull(summarize(stats.timers." + domain + ".channels.sum,'1hour','avg'))&from=-1days&format=json"; data = []; raw = (yield exports.fetchData(path)); this.body = exports.convertToRequiredFormat(raw, 'retrieveAverageLoadTimePerHour'); return this.body; }; exports.retrieveChannelMetrics = function*(type, channelId) { var j, len, path, raw, renderUrl, result, statusType, total; renderUrl = "/render?target=transformNull(summarize(stats.counters." + domain + ".channels." + channelId; path = ''; total = {}; if (type === 'status') { for (j = 0, len = statusArray.length; j < len; j++) { statusType = statusArray[j]; path = renderUrl + ".statuses." + statusType + ".count,'1week'))&format=json"; result = (yield exports.fetchData(path)); if (result && result.data) { total[statusType] = (result.data[1] || result.data[0])[0]; } else { total[statusType] = 0; } } this.body = [ { _id: { "channelID": channelId }, failed: total.Failed, successful: total.Successful, processing: total.Processing, completed: total.Completed, completedWErrors: total['Completed with error(s)'] } ]; return this.body; } else { path = renderUrl + ".count,'1day'))&from=-7days&format=json"; path += "&target=transformNull(summarize(stats.timers." + domain + ".channels." + channelId + ".sum,'1day','avg'))"; raw = (yield exports.fetchData(path)); return this.body = exports.convertToRequiredFormat(raw, 'retrieveChannelMetrics'); } }; exports.retrieveSumOfTransactionsPerPeriod = function*(period) { var path; path = "/render?target=integral(stats.counters." + domain + ".channels.count)&from=" + period + "&format=json"; return this.body = (yield fetchData(path)); }; exports.transactionsPerChannelPerHour = function*(period) { var path, raw; path = "/render?target=summarize(stats.counters." + domain + ".channels.count,'1hour')&from=-1days&format=json"; raw = (yield exports.fetchData(path)); return this.body = exports.convertToRequiredFormat(raw, 'transactionsPerChannelPerHour'); }; fetchData = function*(path) { var data, i, item, j, len, options, ref, response; data = {}; options = { url: "http://" + (statsdServer.host + path), json: true }; response = (yield request(options)); ref = response.body; for (i = j = 0, len = ref.length; j < len; i = ++j) { item = ref[i]; if (i === 0) { data.data = item.datapoints; } else { data['data' + i] = item.datapoints; } } return data; }; convertToRequiredFormat = function(raw, requiredFormat) { if (!raw.data) { return []; } switch (requiredFormat) { case "retrieveAverageLoadTimePerHour": return raw.data.map(function(item) { return { avgResp: item[0], timestamp: moment.unix(item[1]) }; }); case "retrieveChannelMetrics": case "transactionsPerChannelPerHour": return raw.data.map(function(item, i) { return { load: item[0], avgResp: raw.data1[i][0], timestamp: moment.unix(item[1]) }; }); } }; exports.fetchData = fetchData; exports.convertToRequiredFormat = convertToRequiredFormat; exports.timer = this.timer; //# sourceMappingURL=statsd.js.map