<commit_before>const Dash = require('rethinkdbdash')
const r = new Dash()
const roles = require('../roles')
const genlog = require('./generic_logger')

module.exports = {
  awardPoints: (user, type) => {
    return new Promise((resolve, reject) => {
      let now = new Date()
      let today = new Date(now.getFullYear(), now.getUTCMonth(), now.getUTCDate()).getTime()
      let consecutive
      let streak
      r.db('DFB').table('analytics').get(user).then(o => {
        if (o === null) return r.db('DFB').table('analytics').insert({
          id: user,
          consecutive: [],
          streak: 0
        })
        consecutive = o.consecutive !== undefined ? o.consecutive : [today.toString()]
        streak = o.streak !== undefined ? o.streak : 0
        if (consecutive.indexOf(today.toString()) === -1) {
          let last = new Date(parseInt(consecutive[consecutive.length -1]))
          let difference = today - last
          if (difference === 86400000) { // 1 day difference
            consecutive.push(today.toString())
            if (consecutive.length - streak === 1) { // streak counter is 1 day behind
              streak++
            }
          } else {
            consecutive = [today.toString()]
          }
        }
        r.db('DFB').table('analytics').get(user).update({
          [type]: { [today]: r.row(type)(today.toString()).default(0).add(1) },
          consecutive: consecutive,
          streak: streak
        }).run().then(resolve).catch(reject)
      })
    })
  },
  getPoints: (user) => {
    return new Promise((resolve, reject) => {
      r.db('DFB').table('analytics').get(user).run().then(resolve).catch(reject)
    })
  },
  roleUsers: (guild, bot) => {
    r.db("DFB").table("analytics").run().then((results) => {
      results.forEach((row) => {
        let totalDays = Object.keys(row.messages).length
        let consecutiveDays = row.streak
        let member = guild.members.find(member => member.id === row.id)<commit_msg>set streak for legacy-ish stats<commit_after>const Dash = require('rethinkdbdash')
const r = new Dash()
const roles = require('../roles')
const genlog = require('./generic_logger')

module.exports = {
  awardPoints: (user, type) => {
    return new Promise((resolve, reject) => {
      let now = new Date()
      let today = new Date(now.getFullYear(), now.getUTCMonth(), now.getUTCDate()).getTime()
      let consecutive
      let streak
      r.db('DFB').table('analytics').get(user).then(o => {
        if (o === null) return r.db('DFB').table('analytics').insert({
          id: user,
          consecutive: [],
          streak: 0
        })
        consecutive = o.consecutive !== undefined ? o.consecutive : [today.toString()]
        streak = o.streak !== undefined ? o.streak : 0
        if (consecutive.indexOf(today.toString()) === -1) {
          let last = new Date(parseInt(consecutive[consecutive.length -1]))
          let difference = today - last
          if (difference === 86400000) { // 1 day difference
            consecutive.push(today.toString())
            if (consecutive.length > 1 && streak === 0) streak = consecutive.length // backwards compatibility
            if (consecutive.length - streak === 1) { // streak counter is 1 day behind
              streak++
            }
          } else {
            consecutive = [today.toString()]
          }
        }
        r.db('DFB').table('analytics').get(user).update({
          [type]: { [today]: r.row(type)(today.toString()).default(0).add(1) },
          consecutive: consecutive,
          streak: streak
        }).run().then(resolve).catch(reject)
      })
    })
  },
  getPoints: (user) => {
    return new Promise((resolve, reject) => {
      r.db('DFB').table('analytics').get(user).run().then(resolve).catch(reject)
    })
  },
  roleUsers: (guild, bot) => {
    r.db("DFB").table("analytics").run().then((results) => {
      results.forEach((row) => {
        let totalDays = Object.keys(row.messages).length
        let consecutiveDays = row.streak
        let member = guild.members.find(member => member.id === row.id)
{'fraction_non_alphanumeric': 0.17003654080389768, 'fraction_numerical': 0.00876979293544458, 'mean_word_length': 2.3711001642036127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '1822642', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1227, 'n_words': 254}