<commit_before>var admin = (function () {

var exports = {};

function failed_listing_users(xhr, error) {
    util.destroy_loading_indicator($('#subs_page_loading_indicator'));
    ui.report_error("Error listing users or bots", xhr, $("#administration-status"));
}

function failed_listing_streams(xhr, error) {
    ui.report_error("Error listing streams", xhr, $("#administration-status"));
}

function populate_users (realm_people_data) {
    var users_table = $("#admin_users_table");
    var deactivated_users_table = $("#admin_deactivated_users_table");
    var bots_table = $("#admin_bots_table");
    // Clear table rows, but not the table headers
    users_table.find("tr.user_row").remove();
    deactivated_users_table.find("tr.user_row").remove();
    bots_table.find("tr.user_row").remove();

    var active_users = [];
    var deactivated_users = [];
    var bots = [];
    _.each(realm_people_data.members, function (user) {
        if (user.is_bot) {
            bots.push(user);
        } else if (user.is_active) {
            active_users.push(user);
        } else {
            deactivated_users.push(user);
        }
    });

    active_users = _.sortBy(active_users, 'full_name');
    deactivated_users = _.sortBy(deactivated_users, 'full_name');
    bots = _.sortBy(bots, 'full_name');

    _.each(bots, function (user) {
        bots_table.append(templates.render("admin_user_list", {user: user}));
    });
    _.each(active_users, function (user) {
        users_table.append(templates.render("admin_user_list", {user: user}));
    });
    _.each(deactivated_users, function (user) {
        deactivated_users_table.append(templates.render("admin_user_list", {user: user}));
    });
    util.destroy_loading_indicator($('#admin_page_users_loading_indicator'));
    util.destroy_loading_indicator($('#admin_page_bots_loading_indicator'));
    util.destroy_loading_indicator($('#admin_page_deactivated_users_loading_indicator'));
}

function populate_streams (streams_data) {
    var streams_table = $("#admin_streams_table");
    _.each(streams_data.streams, function (stream) {
        streams_table.append(templates.render("admin_streams_list", {stream: stream}));
    });
    util.destroy_loading_indicator($('#admin_page_streams_loading_indicator'));
}

exports.setup_page = function () {
    // create loading indicators
    util.make_loading_indicator($('#admin_page_users_loading_indicator'));
    util.make_loading_indicator($('#admin_page_bots_loading_indicator'));
    util.make_loading_indicator($('#admin_page_streams_loading_indicator'));
    util.make_loading_indicator($('#admin_page_deactivated_users_loading_indicator'));

    // Populate users and bots tables
    $.ajax({
        type:     'GET',
        url:      '/json/users',
        dataType: 'json',
        timeout:  10*1000,
        success: populate_users,<commit_msg>Avoid duplicate streams in the Administration page.<commit_after>var admin = (function () {

var exports = {};

function failed_listing_users(xhr, error) {
    util.destroy_loading_indicator($('#subs_page_loading_indicator'));
    ui.report_error("Error listing users or bots", xhr, $("#administration-status"));
}

function failed_listing_streams(xhr, error) {
    ui.report_error("Error listing streams", xhr, $("#administration-status"));
}

function populate_users (realm_people_data) {
    var users_table = $("#admin_users_table");
    var deactivated_users_table = $("#admin_deactivated_users_table");
    var bots_table = $("#admin_bots_table");
    // Clear table rows, but not the table headers
    users_table.find("tr.user_row").remove();
    deactivated_users_table.find("tr.user_row").remove();
    bots_table.find("tr.user_row").remove();

    var active_users = [];
    var deactivated_users = [];
    var bots = [];
    _.each(realm_people_data.members, function (user) {
        if (user.is_bot) {
            bots.push(user);
        } else if (user.is_active) {
            active_users.push(user);
        } else {
            deactivated_users.push(user);
        }
    });

    active_users = _.sortBy(active_users, 'full_name');
    deactivated_users = _.sortBy(deactivated_users, 'full_name');
    bots = _.sortBy(bots, 'full_name');

    _.each(bots, function (user) {
        bots_table.append(templates.render("admin_user_list", {user: user}));
    });
    _.each(active_users, function (user) {
        users_table.append(templates.render("admin_user_list", {user: user}));
    });
    _.each(deactivated_users, function (user) {
        deactivated_users_table.append(templates.render("admin_user_list", {user: user}));
    });
    util.destroy_loading_indicator($('#admin_page_users_loading_indicator'));
    util.destroy_loading_indicator($('#admin_page_bots_loading_indicator'));
    util.destroy_loading_indicator($('#admin_page_deactivated_users_loading_indicator'));
}

function populate_streams (streams_data) {
    var streams_table = $("#admin_streams_table").expectOne();
    streams_table.find("tr.stream_row").remove();
    _.each(streams_data.streams, function (stream) {
        streams_table.append(templates.render("admin_streams_list", {stream: stream}));
    });
    util.destroy_loading_indicator($('#admin_page_streams_loading_indicator'));
}

exports.setup_page = function () {
    // create loading indicators
    util.make_loading_indicator($('#admin_page_users_loading_indicator'));
    util.make_loading_indicator($('#admin_page_bots_loading_indicator'));
    util.make_loading_indicator($('#admin_page_streams_loading_indicator'));
    util.make_loading_indicator($('#admin_page_deactivated_users_loading_indicator'));

    // Populate users and bots tables
    $.ajax({
        type:     'GET',
        url:      '/json/users',
        dataType: 'json',
        timeout:  10*1000,
        success: populate_users,
{'fraction_non_alphanumeric': 0.14143835616438355, 'fraction_numerical': 0.002054794520547945, 'mean_word_length': 4.257425742574258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1732756', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1809, 'n_words': 272}