<commit_before>module.exports = function (apiKey) {
  return function (metrics) {
    var Closeio = require('close.io')
    var closeio = new Closeio(apiKey)
    var closeioMetrics = new CloseioMetrics(closeio, metrics)

    var createdLastWeek = 'date_created < "1 week ago"'
    var createdTwoWeeksAgo =
      'date_created < "2 weeks ago" and date_created >= "1 week ago"'
    var activeStatus = 'status_type: active'

    closeioMetrics.query('closeio leads created last week',
      '/lead/',
      createdLastWeek,
      'total_results'
    )

    closeioMetrics.query('closeio leads created 2 weeks ago',
      '/lead/',
      createdTwoWeeksAgo,
      'total_results'
    )

    closeioMetrics.query('closeio opportunities created last week',
      '/opportunity/',
      createdLastWeek,
      'total_results'
    )

    closeioMetrics.query('closeio opportunities created 2 weeks ago',
      '/opportunity/',
      createdTwoWeeksAgo,
      'total_results'
    )

    closeioMetrics.query('closeio active opportunities total value',
      '/opportunity/',
      activeStatus,
      'total_value_annualized'
    )

    closeioMetrics.query('closeio active opportunities expected value',
      '/opportunity/',
      activeStatus,
      'expected_value_annualized'
    )

    closeioMetrics.setValueOfWonOpportunitiesThisMonthByUser()
  }
}

function CloseioMetrics(closeio, metrics) {
  this.closeio = closeio
  this.metrics = metrics
}

CloseioMetrics.prototype.query = function (key, path, query, prop) {
  var metrics = this.metrics

  this.closeio._get(path, { _fields: '', query: query })
    .then(function (results) {
      metrics.set(key, results[prop])
    })
}

CloseioMetrics.prototype.setValueOfWonOpportunitiesThisMonthByUser = function () {
  var date = new Date()
  date.setDate(1)
  var since = date.toISOString().split('T')[0]
  var users = {}

  aggregateWonOpportunityValues(this.closeio, 0)
  this.metrics.set('closeio won opportunities value this month by user', users)

  function aggregateWonOpportunityValues(closeio, lastCount) {
    closeio
      ._get('/opportunity/', {
        _fields: 'user_id,user_name,value',
        _skip: lastCount,
        query: 'status_type: won and date >= ' + since
      })
      .then(function (results) {
        lastCount += results.data.length

        results.data.forEach(function (data) {
          var user = users[data.user_id] = users[data.user_id] || {
            name: data.user_name,
            value: 0
          }

          user.value += data.value
        })

        if (results.total_results > lastCount) {
          aggregateWonOpportunityValues(closeio, lastCount)
        }
      })
  }
}<commit_msg>Use a closure and do away with the constructor function.<commit_after>module.exports = function (apiKey) {
  var Closeio = require('close.io')
  var closeio = new Closeio(apiKey)

  return function (metrics) {
    var createdLastWeek = 'date_created < "1 week ago"'
    var createdTwoWeeksAgo =
      'date_created < "2 weeks ago" and date_created >= "1 week ago"'
    var activeStatus = 'status_type: active'

    query('closeio leads created last week',
      '/lead/',
      createdLastWeek,
      'total_results'
    )

    query('closeio leads created 2 weeks ago',
      '/lead/',
      createdTwoWeeksAgo,
      'total_results'
    )

    query('closeio opportunities created last week',
      '/opportunity/',
      createdLastWeek,
      'total_results'
    )

    query('closeio opportunities created 2 weeks ago',
      '/opportunity/',
      createdTwoWeeksAgo,
      'total_results'
    )

    query('closeio active opportunities total value',
      '/opportunity/',
      activeStatus,
      'total_value_annualized'
    )

    query('closeio active opportunities expected value',
      '/opportunity/',
      activeStatus,
      'expected_value_annualized'
    )

    setValueOfWonOpportunitiesThisMonthByUser()
  }

  function query(key, path, query, prop) {
    closeio._get(path, { _fields: '', query: query })
      .then(function (results) {
        metrics.set(key, results[prop])
      })
  }

  function setValueOfWonOpportunitiesThisMonthByUser() {
    var date = new Date()
    date.setDate(1)
    var since = date.toISOString().split('T')[0]
    var users = {}

    aggregateWonOpportunityValues(0)
    metrics.set('closeio won opportunities value this month by user', users)

    function aggregateWonOpportunityValues(lastCount) {
      closeio
        ._get('/opportunity/', {
          _fields: 'user_id,user_name,value',
          _skip: lastCount,
          query: 'status_type: won and date >= ' + since
        })
        .then(function (results) {
          lastCount += results.data.length

          results.data.forEach(function (data) {
            var user = users[data.user_id] = users[data.user_id] || {
              name: data.user_name,
              value: 0
            }

            user.value += data.value
          })

          if (results.total_results > lastCount) {
            aggregateWonOpportunityValues(lastCount)
          }
        })
    }
  }
}
{'fraction_non_alphanumeric': 0.10919427563222897, 'fraction_numerical': 0.0035287198588512055, 'mean_word_length': 2.6390870185449358, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2506583', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1507, 'n_words': 336}