UNPKG

openhim-core

Version:

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

100 lines (80 loc) 2.95 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMetrics = getMetrics; var _winston = require('winston'); var _winston2 = _interopRequireDefault(_winston); var _mongoose = require('mongoose'); var _mongoose2 = _interopRequireDefault(_mongoose); var _authorisation = require('./authorisation'); var authorisation = _interopRequireWildcard(_authorisation); var _metrics = require('../metrics'); var metrics = _interopRequireWildcard(_metrics); var _moment = require('moment'); var _moment2 = _interopRequireDefault(_moment); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // all in one getMetrics generator function for metrics API async function getMetrics(ctx, groupChannels, timeSeries, channelID) { _winston2.default.debug(`Called getMetrics(${groupChannels}, ${timeSeries}, ${channelID})`); const channels = await authorisation.getUserViewableChannels(ctx.authenticated); let channelIDs = channels.map(c => c._id); if (typeof channelID === 'string') { if (channelIDs.map(id => id.toString()).includes(channelID)) { channelIDs = [_mongoose2.default.Types.ObjectId(channelID)]; } else { ctx.status = 401; return; } } const query = ctx.request.query; if (!query.startDate || !query.endDate) { ctx.status = 400; ctx.body = 'Both start and end date are required'; return; } const filters = { startDate: new Date(query.startDate), endDate: new Date(query.endDate), timeSeries, channels: channelIDs }; const results = await metrics.calculateMetrics(filters, groupChannels); ctx.body = results.map(convertMetric); } /** * Convert metrics to the format expected to be returned by the API to prevent * breakage. */ function convertMetric(metric) { const timestamp = (0, _moment2.default)(metric.startTime); return { total: metric.requests, avgResp: calculateAverage(metric.responseTime, metric.requests), minResp: metric.minResponseTime, maxResp: metric.maxResponseTime, failed: metric.failed, successful: metric.successful, processing: metric.processing, completed: metric.completed, completedWErrors: metric.completedWithErrors, timestamp: metric.startTime, _id: { channelID: metric.channelID, minute: timestamp.minute(), hour: timestamp.hour(), day: timestamp.day(), week: timestamp.week(), month: timestamp.month(), year: timestamp.year() } }; } function calculateAverage(total, count) { if (count === 0) { return 0; } return total / count; } //# sourceMappingURL=metrics.js.map