<commit_before>"""
Leaderboard API endpoints - get/sort/filter the leaderboard
"""
import operator

import flask
import sqlalchemy

from .. import model, util

from . import util as api_util
from .blueprint import web_api

_COUNT_KEY = 'count'
_LOADERBOARD_ALIAS = 'full_leaderboard'


def _count_leaderboard_query(where_clause):
    """
    Considering the desired clause, return the number of distinct users in leaderboard
    :param where_clause: Clause to filter by
    :return: Number of distinct users
    """
    full_leaderboard = model.ranked_bots_users.select().where(where_clause).reduce_columns().alias(_LOADERBOARD_ALIAS)
    return sqlalchemy.sql.select([
        sqlalchemy.sql.func.count(sqlalchemy.distinct(full_leaderboard.c.user_id))
    ]).select_from(full_leaderboard).where(full_leaderboard.c.num_games > 0)


@web_api.route("/leaderboard")
@util.cross_origin(methods=["GET"])
def leaderboard():
    result = []
    offset, limit = api_util.get_offset_limit()

    where_clause, order_clause, manual_sort = api_util.get_sort_filter({
        "user_id": model.ranked_bots_users.c.user_id,
        "username": model.ranked_bots_users.c.username,
        "level": model.ranked_bots_users.c.player_level,
        "organization_id": model.ranked_bots_users.c.organization_id,
        "organization_name": model.ranked_bots_users.c.organization_name,
        "country_code": model.ranked_bots_users.c.country_code,
        "country_subdivision_code": model.ranked_bots_users.c.country_subdivision_code,<commit_msg>Equating logic in count<commit_after>"""
Leaderboard API endpoints - get/sort/filter the leaderboard
"""
import operator

import flask
import sqlalchemy

from .. import model, util

from . import util as api_util
from .blueprint import web_api

_COUNT_KEY = 'count'
_LEADERBOARD_ALIAS = 'full_leaderboard'


def _count_leaderboard_query(where_clause):
    """
    Considering the desired clause, return the number of distinct users in leaderboard
    :param where_clause: Clause to filter by
    :return: Number of distinct users
    """
    full_leaderboard = model.ranked_bots_users.select().where(where_clause).reduce_columns().alias(_LEADERBOARD_ALIAS)
    return sqlalchemy.sql.select([
        sqlalchemy.sql.func.count(sqlalchemy.distinct(full_leaderboard.c.user_id))
    ]).select_from(full_leaderboard)


@web_api.route("/leaderboard")
@util.cross_origin(methods=["GET"])
def leaderboard():
    result = []
    offset, limit = api_util.get_offset_limit()

    where_clause, order_clause, manual_sort = api_util.get_sort_filter({
        "user_id": model.ranked_bots_users.c.user_id,
        "username": model.ranked_bots_users.c.username,
        "level": model.ranked_bots_users.c.player_level,
        "organization_id": model.ranked_bots_users.c.organization_id,
        "organization_name": model.ranked_bots_users.c.organization_name,
        "country_code": model.ranked_bots_users.c.country_code,
        "country_subdivision_code": model.ranked_bots_users.c.country_subdivision_code,
{'fraction_non_alphanumeric': 0.10231023102310231, 'fraction_numerical': 0.00033003300330033004, 'mean_word_length': 4.6654205607476635, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 4, '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': '7542509', 'n_tokens_mistral': 1039, 'n_tokens_neox': 989, 'n_words': 177}