UNPKG

openhim-core

Version:

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

62 lines (50 loc) 2 kB
var Channel, Q, Transaction, _, authorisation, logger, metrics, moment, mongoose, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; Transaction = require('../model/transactions').Transaction; Channel = require('../model/channels').Channel; moment = require('moment'); logger = require('winston'); mongoose = require('mongoose'); authorisation = require('./authorisation'); Q = require('q'); metrics = require("../metrics"); _ = require('lodash'); exports.getMetrics = function*(groupChannels, timeSeries, channelID) { var channelIDs, channels, endDate, m, query, ref, ref1, startDate; logger.debug("Called getMetrics(" + groupChannels + ", " + timeSeries + ", " + channelID + ")"); channels = (yield authorisation.getUserViewableChannels(this.authenticated)); channelIDs = channels.map(function(c) { return c._id; }); if (typeof channelID === 'string' && !(indexOf.call(channelIDs.map(function(id) { return id.toString(); }), channelID) >= 0)) { this.status = 401; return; } else if (typeof channelID === 'string') { channelIDs = [mongoose.Types.ObjectId(channelID)]; } query = this.request.query; logger.debug("Metrics query object: " + (JSON.stringify(query))); startDate = query.startDate; delete query.startDate; endDate = query.endDate; delete query.endDate; if (Object.keys(query).length === 0) { query = null; } m = (yield metrics.calculateMetrics(new Date(startDate), new Date(endDate), query, channelIDs, timeSeries, groupChannels)); if (((ref = m[0]) != null ? (ref1 = ref._id) != null ? ref1.year : void 0 : void 0) != null) { m = m.map(function(item) { var date; date = _.assign({}, item._id); if (date.month) { date.month = date.month - 1; } item.timestamp = moment.utc(date); return item; }); } return this.body = m; }; //# sourceMappingURL=metrics.js.map