<commit_before>/* eslint-disable import/imports-first */

// FIXME: replace globals with central (non-global) config
global.__SERVER__ = true

const Promise = require('bluebird')

const updateProjectCycleStats = require('src/server/actions/updateProjectStats')
const {findPlayers, getPlayerById} = require('src/server/db/player')
const {findChapters} = require('src/server/db/chapter')
const {getCyclesForChapter} = require('src/server/db/cycle')
const {getProjectsForChapterInCycle} = require('src/server/db/project')
const {COMPLETE} = require('src/common/models/cycle')
const {finish} = require('./util')

const LOG_PREFIX = '[runStats]'

// FIXME: hardcoded, yuck
const PRO_PLAYER_RATING = 1300
const PRO_PLAYER_IDS = {
  '070b3063-0ff7-40c6-b3d1-321fa49b6c94': 'bluemihai',
  '75dbe257-a701-4725-ba74-4341376f540d': 'jrob8577',
  'dcf14075-6fbe-44ab-89bf-cba2511f0278': 'deadlyicon',
  '3760fbe8-2c2e-46d9-bca7-a9610dc0d417': 'prattsj',
  'ed958f6f-1870-4ba9-8de9-e1092c9fa758': 'deonna',
}

run()
  .then(() => finish())
  .catch(err => finish(err))

async function run() {
  const errors = []

  const players = await findPlayers()
  await Promise.each(players, player => {
    return clearPlayerStats(player)
  })

  const proPlayerStats = {elo: {rating: PRO_PLAYER_RATING}}
  const proPlayers = players.filter(player => PRO_PLAYER_IDS[player.id])
  await Promise.each(proPlayers, proPlayer => {
    return setPlayerStats(proPlayer, proPlayerStats)
  })

  const chapters = await findChapters()
  await Promise.each(chapters, chapter => {
    return updateChapterStats(chapter).catch(err => {
      errors.push(err)
    })
  })

  if (errors.length) {
    console.error(LOG_PREFIX, 'Errors:')
    errors.forEach(err => console.error('\n', err))
    throw new Error('Stats computation failed')
  }

  const playersFinal = await findPlayers()

  // log final/current ratings
  playersFinal
    .filter(player => player.stats && player.stats.elo)
    .map(player => ({
      id: player.id,
      elo: ((player.stats || {}).elo || {}).rating || null
    }))
    .sort((a, b) => a.elo - b.elo)
    .forEach(player => console.log(player.id.slice(0, 8), player.elo))
}<commit_msg>adding tanner to runStats script; setting each pro players elo individually<commit_after>/* eslint-disable import/imports-first */

// FIXME: replace globals with central (non-global) config
global.__SERVER__ = true

const Promise = require('bluebird')

const updateProjectCycleStats = require('src/server/actions/updateProjectStats')
const {findPlayers, getPlayerById} = require('src/server/db/player')
const {findChapters} = require('src/server/db/chapter')
const {getCyclesForChapter} = require('src/server/db/cycle')
const {getProjectsForChapterInCycle} = require('src/server/db/project')
const {COMPLETE} = require('src/common/models/cycle')
const {finish} = require('./util')

const LOG_PREFIX = '[runStats]'

// FIXME: hardcoded, yuck
const PRO_PLAYERS = {
  '070b3063-0ff7-40c6-b3d1-321fa49b6c94': {handle: 'bluemihai', initialEloRating: 1300},
  '75dbe257-a701-4725-ba74-4341376f540d': {handle: 'jrob8577', initialEloRating: 1300},
  'dcf14075-6fbe-44ab-89bf-cba2511f0278': {handle: 'deadlyicon', initialEloRating: 1300},
  '3760fbe8-2c2e-46d9-bca7-a9610dc0d417': {handle: 'prattsj', initialEloRating: 1300},
  'ed958f6f-1870-4ba9-8de9-e1092c9fa758': {handle: 'deonna', initialEloRating: 1300},
  'f490c8ee-e609-4774-bcf5-9ed7f938676d': {handle: 'tannerwelsh', initialEloRating: 1150},
}

run()
  .then(() => finish())
  .catch(err => finish(err))

async function run() {
  const errors = []

  const players = await findPlayers()
  await Promise.each(players, player => {
    return clearPlayerStats(player)
  })

  const proPlayers = players.filter(player => PRO_PLAYERS[player.id])
  await Promise.each(proPlayers, proPlayer => {
    return setPlayerStats(proPlayer, {elo: {rating: PRO_PLAYERS[proPlayer.id].initialEloRating}})
  })

  const chapters = await findChapters()
  await Promise.each(chapters, chapter => {
    return updateChapterStats(chapter).catch(err => {
      errors.push(err)
    })
  })

  if (errors.length) {
    console.error(LOG_PREFIX, 'Errors:')
    errors.forEach(err => console.error('\n', err))
    throw new Error('Stats computation failed')
  }

  const playersFinal = await findPlayers()

  // log final/current ratings
  playersFinal
    .filter(player => player.stats && player.stats.elo)
    .map(player => ({
      id: player.id,
      elo: ((player.stats || {}).elo || {}).rating || null
    }))
    .sort((a, b) => a.elo - b.elo)
    .forEach(player => console.log(player.id.slice(0, 8), player.elo))
}
{'fraction_non_alphanumeric': 0.15691776386790415, 'fraction_numerical': 0.057414202460608674, 'mean_word_length': 4.4970344009489915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3175651', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1624, 'n_words': 306}