<commit_before>const P = require('bluebird')
const _ = require('lodash')
const logger = require('winston')
const graphData = require('../../graph/data')
const { firebase } = require('../../firebase')
const helpers = require('./helpers')

module.exports = function processUserStats({ data, resolve, reject }) {
  const { userId } = data

  return P.try(() => {
    const g = graphData.getGraph()

    const connected = g.successors(userId) || []
    const own = g.node(userId)

    if (!own) {
      return resolve()
    }

    const total = connected.length

    let maxDistance = 0
    _.each(connected, (id) => {
      const other = g.node(id)

      const distance = helpers.getDistance(own, other)
      maxDistance = distance > maxDistance ? distance : maxDistance
    })

    const score = Math.floor(maxDistance) + (total * 5)

    return {
      total,
      maxDistance,
      score,
    }
  })
  .then(processed => {
    logger.info(processed)
    logger.info(`Processed stats for user ${userId}`)
    return firebase.database().ref('userStats').child(userId).set(processed)
  })
  .then(resolve)
  .catch(err => {
    logger.error(err)
    reject(err)
  })
}<commit_msg>Continue this stupid logging nonsense<commit_after>const P = require('bluebird')
const _ = require('lodash')
const logger = require('winston')
const graphData = require('../../graph/data')
const { firebase } = require('../../firebase')
const helpers = require('./helpers')

module.exports = function processUserStats({ data, resolve, reject }) {
  const { userId } = data

  return P.resolve().then(() => {
    try {
      const g = graphData.getGraph()

      const connected = g.successors(userId) || []
      const own = g.node(userId)

      if (!own) {
        return resolve()
      }

      const total = connected.length

      let maxDistance = 0
      _.each(connected, (id) => {
        const other = g.node(id)

        const distance = helpers.getDistance(own, other)
        maxDistance = distance > maxDistance ? distance : maxDistance
      })

      const score = Math.floor(maxDistance) + (total * 5)

      return {
        total,
        maxDistance,
        score,
      }
    } catch (err) {
      console.log('🐷', err)
      throw err
    }
  })
  .then(processed => {
    logger.info('🍕', processed)
    logger.info(`Processed stats for user ${userId}`)
    return firebase.database().ref('userStats').child(userId).set(processed)
  })
  .then(resolve)
  .catch(err => {
    logger.error(err)
    reject(err)
  })
}
{'fraction_non_alphanumeric': 0.15158730158730158, 'fraction_numerical': 0.0015873015873015873, 'mean_word_length': 2.7019089574155655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5889134', 'n_tokens_mistral': 782, 'n_tokens_neox': 760, 'n_words': 178}