<commit_before>package org.ojacquemart.eurobets.firebase.management.table

import org.ojacquemart.eurobets.lang.loggerFor
import kotlin.comparisons.compareBy
import kotlin.comparisons.thenBy

class GlobalLeaguesTableCalculator {

    private val log = loggerFor<GlobalLeaguesTableCalculator>()

    fun getRows(nbMatches: Int, leaguesTables: List<LeagueTable>): List<LeagueStatRow> {
        log.info("Compute global leagues tables at match #$nbMatches")

        val leaguesStatRows = leaguesTables
                .filter { it.table.nbRows > 1 }
                .map { leagueTable ->
                    val league = leagueTable.league
                    val table = leagueTable.table
                    val players = table.nbRows
                    val tableReduced = reduce(table)

                    val pointsAvg = tableReduced.points.toFloat().div(players)
                    val leagueBets = tableReduced.bets

                    LeagueStatRow(name = league.name, slug = league.slug,
                            players = table.nbRows,
                            points = pointsAvg,
                            assiduity = leagueBets.toFloat().div(nbMatches * players),
                            perfects = tableReduced.perfects.toFloat().div(leagueBets),
                            goods = tableReduced.goods.toFloat().div(leagueBets),
                            bads = tableReduced.bads.toFloat().div(leagueBets))
                }

        val positions = leaguesStatRows
                .groupBy { it.points }
                .toList()
                .map { pair -> pair.first }
                .sortedDescending()

        val comparator = compareBy<LeagueStatRow> { it.position }
                .thenBy { it.perfects }.thenBy { it.goods }
                .thenBy { it.assiduity }

        return leaguesStatRows
                .map { leagueStatRow ->
                    val position = positions.indexOf(leagueStatRow.points) + 1

                    leagueStatRow.copy(position = position)
                }.sortedWith(comparator)
    }

    fun reduce(leagueTable: Table): TableRow {
        return leagueTable.table.reduce { current, next ->
            current.copy(bets = current.bets + next.bets,
                    points = current.points + next.points,
                    perfects = current.perfects + next.perfects,<commit_msg>polish: sort leagues<commit_after>package org.ojacquemart.eurobets.firebase.management.table

import org.ojacquemart.eurobets.lang.loggerFor
import kotlin.comparisons.compareBy
import kotlin.comparisons.thenBy

class GlobalLeaguesTableCalculator {

    private val log = loggerFor<GlobalLeaguesTableCalculator>()

    fun getRows(nbMatches: Int, leaguesTables: List<LeagueTable>): List<LeagueStatRow> {
        log.info("Compute global leagues tables at match #$nbMatches")

        val leaguesStatRows = leaguesTables
                .filter { it.table.nbRows > 1 }
                .map { leagueTable ->
                    val league = leagueTable.league
                    val table = leagueTable.table
                    val players = table.nbRows
                    val tableReduced = reduce(table)

                    val pointsAvg = tableReduced.points.toFloat().div(players)
                    val leagueBets = tableReduced.bets

                    LeagueStatRow(name = league.name, slug = league.slug,
                            players = table.nbRows,
                            points = pointsAvg,
                            assiduity = leagueBets.toFloat().div(nbMatches * players),
                            perfects = tableReduced.perfects.toFloat().div(leagueBets),
                            goods = tableReduced.goods.toFloat().div(leagueBets),
                            bads = tableReduced.bads.toFloat().div(leagueBets))
                }

        val positions = leaguesStatRows
                .groupBy { it.points }
                .toList()
                .map { pair -> pair.first }
                .sortedDescending()

        val comparator = compareBy<LeagueStatRow> { it.position }
                .thenBy { -it.perfects }.thenBy { -it.goods }
                .thenBy { -it.assiduity }

        return leaguesStatRows
                .map { leagueStatRow ->
                    val position = positions.indexOf(leagueStatRow.points) + 1

                    leagueStatRow.copy(position = position)
                }.sortedWith(comparator)
    }

    fun reduce(leagueTable: Table): TableRow {
        return leagueTable.table.reduce { current, next ->
            current.copy(bets = current.bets + next.bets,
                    points = current.points + next.points,
                    perfects = current.perfects + next.perfects,
{'fraction_non_alphanumeric': 0.08561788274989456, 'fraction_numerical': 0.0008435259384226065, 'mean_word_length': 1.6467633928571428, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '3115442', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1209, 'n_words': 237}