<commit_before>import {getCycleById, getLatestCycleForChapter} from 'src/server/db/cycle'
import {GOAL_SELECTION} from 'src/common/models/cycle'
import {getPlayersInPool, findPoolsByCycleId} from 'src/server/db/pool'
import {connect} from 'src/db'

const r = connect()

export default async function getCycleVotingResults(chapterId, cycleId) {
  const cycle = cycleId ?
    await getCycleById(cycleId, {mergeChapter: true}) :
    await getLatestCycleForChapter(chapterId, {mergeChapter: true})

  const poolsExpr = findPoolsByCycleId(cycle.id)
  const pools = await poolsExpr
    .merge(_mergeCandidateGoals)
    .merge(_mergeUsers)
    .merge(_mergeVoterPlayerIds)
    .merge(_mergeVotingIsStillOpen(cycle))

  return {
    id: 'cycleVotingResults',
    cycle,
    pools,
  }
}

function _mergeCandidateGoals(pool) {
  const candidateGoals = _validVotesForPool(pool)
    .group(vote => vote('goals').pluck('url', 'title'), {multi: true})
    .ungroup()
    .map(doc => ({
      goal: doc('group'),
      playerGoalRanks: doc('reduction').map(vote => ({
        playerId: vote('playerId'),
        goalRank: vote('goals')('url').offsetsOf(doc('group')('url')).nth(0)
      }))
    }))
    .orderBy(r.desc(row => row('playerGoalRanks').count()))
  return {candidateGoals}
}

function _validVotesForPool(pool) {
  return r.table('votes')
    .getAll(pool('id'), {index: 'poolId'})
    .hasFields('goals')
}

function _mergeUsers(pool) {
  return {
    users: getPlayersInPool(pool('id')).coerceTo('array')
  }
}

function _mergeVoterPlayerIds(pool) {
  return {
    voterPlayerIds: pool('candidateGoals').concatMap(goal => goal('playerGoalRanks')('playerId')).distinct()
  }
}

function _mergeVotingIsStillOpen(cycle) {
  return {
    votingIsStillOpen: cycle.state === GOAL_SELECTION
  }
}
<commit_msg>removing unused id attribute from getCycleVotingResults<commit_after>import {getCycleById, getLatestCycleForChapter} from 'src/server/db/cycle'
import {GOAL_SELECTION} from 'src/common/models/cycle'
import {getPlayersInPool, findPoolsByCycleId} from 'src/server/db/pool'
import {connect} from 'src/db'

const r = connect()

export default async function getCycleVotingResults(chapterId, cycleId) {
  const cycle = cycleId ?
    await getCycleById(cycleId, {mergeChapter: true}) :
    await getLatestCycleForChapter(chapterId, {mergeChapter: true})

  const poolsExpr = findPoolsByCycleId(cycle.id)
  const pools = await poolsExpr
    .merge(_mergeCandidateGoals)
    .merge(_mergeUsers)
    .merge(_mergeVoterPlayerIds)
    .merge(_mergeVotingIsStillOpen(cycle))

  return {
    cycle,
    pools,
  }
}

function _mergeCandidateGoals(pool) {
  const candidateGoals = _validVotesForPool(pool)
    .group(vote => vote('goals').pluck('url', 'title'), {multi: true})
    .ungroup()
    .map(doc => ({
      goal: doc('group'),
      playerGoalRanks: doc('reduction').map(vote => ({
        playerId: vote('playerId'),
        goalRank: vote('goals')('url').offsetsOf(doc('group')('url')).nth(0)
      }))
    }))
    .orderBy(r.desc(row => row('playerGoalRanks').count()))
  return {candidateGoals}
}

function _validVotesForPool(pool) {
  return r.table('votes')
    .getAll(pool('id'), {index: 'poolId'})
    .hasFields('goals')
}

function _mergeUsers(pool) {
  return {
    users: getPlayersInPool(pool('id')).coerceTo('array')
  }
}

function _mergeVoterPlayerIds(pool) {
  return {
    voterPlayerIds: pool('candidateGoals').concatMap(goal => goal('playerGoalRanks')('playerId')).distinct()
  }
}

function _mergeVotingIsStillOpen(cycle) {
  return {
    votingIsStillOpen: cycle.state === GOAL_SELECTION
  }
}

{'fraction_non_alphanumeric': 0.14831211953514112, 'fraction_numerical': 0.0005534034311012728, 'mean_word_length': 4.66614420062696, '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': '4540778', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1251, 'n_words': 209}