<commit_before>/**
 * Talk to the data/compute server.
 */

import rx from '/imports/common/rx';
import util from '/imports/common/util';

var UPLOAD_MAX_GIGABYTES = 4,
    UPLOAD_MAX_BYTES = 1024 * 1024 * 1024 * UPLOAD_MAX_GIGABYTES,
    retryLimit = 3,
    jobStatusPollInterval = 1, // second
    job = [];

function log (url, httpCode, norm) {
    console.log('ajax error on url:', url,
        '\n  code:', httpCode,
        '\n  error:', norm.error,
        '\n  stacktrace:', norm.stackTrace);
}

function normalizeErrorResponse (error, url) {
    
    // Standardize the error returned to the caller.
    var norm = {};

    if (error.responseJSON) {
        norm = error.responseJSON;
    } else if (error.responseText) {
        norm.error = error.responseText;
    } else if (error.statusText) {
        norm.error = error.statusText;
    } else {
        norm.error = 'Unknown error';
    }

    log(url, error.status, norm);

    return norm;
}

function getData (url, successFx, errorFx, ok404, parse) {

    // Retrieve view data for the current project.
    // For the case where 404 not found is OK we handle the 404 in the success
    // block as well as the error block. This is because our current data
    // server knows how to handle these and returns the 404 as success.
    // However, in the future we may have a data server we don't have much
    // control over, so we also handle this in the error block.
    function handle404 (url, successFx, errorFx, ok404, parse) {
    
        // Handle the cases where 404 not found is OK.
        
        var id = url.slice(url.lastIndexOf('/') + 1);
    
        if (id === 'layouts.tab') {
        
            // Special handling for 'layouts', whose ID is
            // 'matrixnames' in older maps. Get again with the new ID.
            url = url.slice(0, url.lastIndexOf('/') + 1) + 'matrixnames.tab';
            var i = url.indexOf('Ok404');
            url = url.slice(0, i) + url.slice(i+5);
            getData(url, successFx, errorFx, false, parse);
   
        } else {
            // The success handler of the caller should handle 404s.
            successFx('404');
        }
    }
    
    $.ajax({
        type: 'GET',
        url: url,
        tryCount : 0,
        retryLimit : retryLimit,

        success:  function (result) {
           
            if (result === '404' && ok404) {
                handle404(url, successFx, errorFx, ok404, parse);
                return;
            } else if (parse === 'noParse') {
           
                // Return raw data
                successFx(result);
            } else if (parse === 'tsv') {
           
                // Return tsv-parsed data
                successFx(util.parseTsv(result));
            } else {
           
                // Return json-parsed data
                successFx(JSON.parse(result));
            }
        },
        error: function (errorIn) {
            var msg404 = ' GET ' + url + ' 404 (NOT FOUND) is OK here.',
                error = normalizeErrorResponse(errorIn, url);
           
            // Special handling where the caller has said a 404 is OK.
            if (ok404 && errorIn && 'status' in errorIn &&
                errorIn.status.toString() === '404') {
                console.log(msg404);
                handle404(url, successFx, errorFx, ok404, parse);
           
            // Handle the usual case.
            } else {
                if (errorFx) {
                    errorFx(error);
                }
            }
        }
    });
}

exports.getUserMapAuthorization = function (successFx, errorFx) {

    // Ask the data server if this user is authorized to view the current map.
    var user = Meteor.user(),
        url = HUB_URL + '/mapAuth/mapId/' + ctx.project;
    
    // If there is a signed in user, get the email and roles.
    if (user) { url += 'email/' + user.username + '/role/' +
        rx.get('user.roles').join('+');
    }
    $.ajax({
        type: 'GET',
        url: url,
        tryCount : 0,
        retryLimit : retryLimit,
        success: successFx,
        error: errorFx,
    });
};

exports.getProjectList = function (successFx, errorFx) {

    // Ask the data server if this user is authorized to view the current map.
    // @app.route('/projectList/email/<string:userEmail>/roles/<string:userRoles>',

    var user = Meteor.user(),
        url = HUB_URL + '/mapList';
    
    // If there is a signed in user, get the email and roles.
    if (user) {
        url += '/email/' + user.username;
        var roles = rx.get('user.roles');
        if (roles.length > 0) {
            url += '/role/' + roles.join('+');
        }
    }
    $.ajax({
        type: 'GET',<commit_msg>fix view of own map<commit_after>/**
 * Talk to the data/compute server.
 */

import rx from '/imports/common/rx';
import util from '/imports/common/util';

var UPLOAD_MAX_GIGABYTES = 4,
    UPLOAD_MAX_BYTES = 1024 * 1024 * 1024 * UPLOAD_MAX_GIGABYTES,
    retryLimit = 3,
    jobStatusPollInterval = 1, // second
    job = [];

function log (url, httpCode, norm) {
    console.log('ajax error on url:', url,
        '\n  code:', httpCode,
        '\n  error:', norm.error,
        '\n  stacktrace:', norm.stackTrace);
}

function normalizeErrorResponse (error, url) {
    
    // Standardize the error returned to the caller.
    var norm = {};

    if (error.responseJSON) {
        norm = error.responseJSON;
    } else if (error.responseText) {
        norm.error = error.responseText;
    } else if (error.statusText) {
        norm.error = error.statusText;
    } else {
        norm.error = 'Unknown error';
    }

    log(url, error.status, norm);

    return norm;
}

function getData (url, successFx, errorFx, ok404, parse) {

    // Retrieve view data for the current project.
    // For the case where 404 not found is OK we handle the 404 in the success
    // block as well as the error block. This is because our current data
    // server knows how to handle these and returns the 404 as success.
    // However, in the future we may have a data server we don't have much
    // control over, so we also handle this in the error block.
    function handle404 (url, successFx, errorFx, ok404, parse) {
    
        // Handle the cases where 404 not found is OK.
        
        var id = url.slice(url.lastIndexOf('/') + 1);
    
        if (id === 'layouts.tab') {
        
            // Special handling for 'layouts', whose ID is
            // 'matrixnames' in older maps. Get again with the new ID.
            url = url.slice(0, url.lastIndexOf('/') + 1) + 'matrixnames.tab';
            var i = url.indexOf('Ok404');
            url = url.slice(0, i) + url.slice(i+5);
            getData(url, successFx, errorFx, false, parse);
   
        } else {
            // The success handler of the caller should handle 404s.
            successFx('404');
        }
    }
    
    $.ajax({
        type: 'GET',
        url: url,
        tryCount : 0,
        retryLimit : retryLimit,

        success:  function (result) {
           
            if (result === '404' && ok404) {
                handle404(url, successFx, errorFx, ok404, parse);
                return;
            } else if (parse === 'noParse') {
           
                // Return raw data
                successFx(result);
            } else if (parse === 'tsv') {
           
                // Return tsv-parsed data
                successFx(util.parseTsv(result));
            } else {
           
                // Return json-parsed data
                successFx(JSON.parse(result));
            }
        },
        error: function (errorIn) {
            var msg404 = ' GET ' + url + ' 404 (NOT FOUND) is OK here.',
                error = normalizeErrorResponse(errorIn, url);
           
            // Special handling where the caller has said a 404 is OK.
            if (ok404 && errorIn && 'status' in errorIn &&
                errorIn.status.toString() === '404') {
                console.log(msg404);
                handle404(url, successFx, errorFx, ok404, parse);
           
            // Handle the usual case.
            } else {
                if (errorFx) {
                    errorFx(error);
                }
            }
        }
    });
}

exports.getUserMapAuthorization = function (successFx, errorFx) {

    // Ask the data server if this user is authorized to view the current map.
    var user = Meteor.user(),
        url = HUB_URL + '/mapAuth/mapId/' + ctx.project;
    
    // If there is a signed in user, get the email and roles.
    if (user) {
        url += 'email/' + user.username;
        var roles = rx.get('user.roles');
        if (roles.length > 0) {
            url += '/role/' + roles.join('+');
        }
    }
    $.ajax({
        type: 'GET',
        url: url,
        tryCount : 0,
        retryLimit : retryLimit,
        success: successFx,
        error: errorFx,
    });
};

exports.getProjectList = function (successFx, errorFx) {

    // Ask the data server if this user is authorized to view the current map.
    // @app.route('/projectList/email/<string:userEmail>/roles/<string:userRoles>',

    var user = Meteor.user(),
        url = HUB_URL + '/mapList';
    
    // If there is a signed in user, get the email and roles.
    if (user) {
        url += '/email/' + user.username;
        var roles = rx.get('user.roles');
        if (roles.length > 0) {
            url += '/role/' + roles.join('+');
        }
    }
    $.ajax({
        type: 'GET',
{'fraction_non_alphanumeric': 0.12155506654092005, 'fraction_numerical': 0.01875720423346956, 'mean_word_length': 1.6726407168860262, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2742046', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2804, 'n_words': 878}