# All files / src/api metrics.coffee

35.48% Statements 11/31
16.67% Branches 1/6
0% Functions 0/5
34.48% Lines 10/29

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    431x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

     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'

    # all in one getMetrics generator function for metrics API
    exports.getMetrics = (groupChannels, timeSeries, channelID) ->
      logger.debug "Called getMetrics(#{groupChannels}, #{timeSeries}, #{channelID})"
      channels = yield authorisation.getUserViewableChannels this.authenticated
      channelIDs = channels.map (c) -> return c._id
      if typeof channelID is 'string' and not (channelID in (channelIDs.map (id) -> id.toString()) )
        this.status = 401
        return
      else if typeof channelID is '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 is 0
        query = null

      m = yield metrics.calculateMetrics new Date(startDate), new Date(endDate), query, channelIDs, timeSeries, groupChannels

      if m[0]?._id?.year? # if there are time components
        m = m.map (item) ->
          date = _.assign {}, item._id
          # adapt for moment (month starting at 0)
          if date.month then date.month = date.month - 1
          item.timestamp = moment.utc(date)
          return item

      this.body = m