<commit_before>import $ from 'jquery';

import {
  assign,
} from './utils';

var dataInterface;
(function(){
  var $ajax = (o)=> {
    return $.ajax(assign({}, {dataType: 'json', method: 'GET'}, o));
  };
  const assetMapping = {
    'a': 'assets',
    'c': 'collections',
    'p': 'permissions',
  };

  var rootUrl = (function(){
    try {
      return document.head.querySelector('meta[name=kpi-root-url]').content.replace(/\/$/, '');
    } catch (e) {
      console.error('no kpi-root-url meta tag set. defaulting to ""');
      return '';
    }
  })();
  this.rootUrl = rootUrl;

  assign(this, {
    selfProfile: ()=> $ajax({ url: `${rootUrl}/me/` }),
    serverEnvironment: ()=> $ajax({ url: `${rootUrl}/environment/` }),
    queryUserExistence: (username)=> {
      var d = new $.Deferred();
      $ajax({ url: `${rootUrl}/users/${username}/` })
        .done(()=>{ d.resolve(username, true); })
        .fail(()=>{ d.reject(username, false); });
      return d.promise();
    },
    logout: ()=> {
      var d = new $.Deferred();
      $ajax({ url: `${rootUrl}/api-auth/logout/` }).done(d.resolve).fail(function (/*resp, etype, emessage*/) {
        // logout request wasn't successful, but may have logged the user out
        // querying '/me/' can confirm if we have logged out.
        dataInterface.selfProfile().done(function(data){
          if (data.message === 'user is not logged in') {
            d.resolve(data);
          } else {
            d.fail(data);
          }
        }).fail(d.fail);
      });
      return d.promise();
    },
    patchProfile (data) {
      return $ajax({
        url: `${rootUrl}/me/`,
        method: 'PATCH',
        data: data
      });
    },
    listBlocks () {
      return $ajax({
        url: `${rootUrl}/assets/?q=asset_type:block`
      });
    },
    listTemplates () {
      return $ajax({
        url: `${rootUrl}/assets/?q=asset_type:template`
      });
    },
    listSurveys() {
      return $ajax({
        url: `${rootUrl}/assets/`,
        data: {
          q: 'asset_type:survey'
        },
        method: 'GET'
      });
    },
    listCollections () {
      return $.getJSON(`${rootUrl}/collections/?all_public=true`);
    },
    listAllAssets () {
      var d = new $.Deferred();
      $.when($.getJSON(`${rootUrl}/assets/?parent=`), $.getJSON(`${rootUrl}/collections/?parent=`)).done(function(assetR, collectionR){
        var assets = assetR[0],
            collections = collectionR[0];
        var r = {
          results: [],
        };
        var pushItem = function (item){
          r.results.push(item);
        };
        assets.results.forEach(pushItem);
        collections.results.forEach(pushItem);
        var sortAtt = 'date_modified';
        r.results.sort(function(a, b){
          var ad = a[sortAtt], bd = b[sortAtt];
          return (ad === bd) ? 0 : ((ad > bd) ? -1 : 1);
        });
        d.resolve(r);
      }).fail(d.fail);
      return d.promise();
    },
    createAssetSnapshot (data) {
      return $ajax({
        url: `${rootUrl}/asset_snapshots/`,
        method: 'POST',
        data: data
      });
    },
    getReportData (data) {
      let identifierString;
      if (data.identifiers) {
        identifierString = `?names=${data.identifiers.join(',')}`
      }
      if (data.group_by != '')
        identifierString += `&split_by=${data.group_by}`

      return $ajax({
        url: `${rootUrl}/reports/${data.uid}/${identifierString}`,
      });
    },
    createTemporaryAssetSnapshot ({source}) {
      return $ajax({
        url: `${rootUrl}/asset_snapshots/`,
        method: 'POST',
        data: {
          source: source
        }
      });
    },
    cloneAsset ({uid, name, version_id}) {
      let data = {
        clone_from: uid,
      };
      if (name) {
        data.name = name;
      }
      if (version_id) {
        data.clone_from_version_id = version_id;
      }
      return $ajax({
        method: 'POST',<commit_msg>add asset_type option to cloneAsset<commit_after>import $ from 'jquery';

import {
  assign,
} from './utils';

var dataInterface;
(function(){
  var $ajax = (o)=> {
    return $.ajax(assign({}, {dataType: 'json', method: 'GET'}, o));
  };
  const assetMapping = {
    'a': 'assets',
    'c': 'collections',
    'p': 'permissions',
  };

  var rootUrl = (function(){
    try {
      return document.head.querySelector('meta[name=kpi-root-url]').content.replace(/\/$/, '');
    } catch (e) {
      console.error('no kpi-root-url meta tag set. defaulting to ""');
      return '';
    }
  })();
  this.rootUrl = rootUrl;

  assign(this, {
    selfProfile: ()=> $ajax({ url: `${rootUrl}/me/` }),
    serverEnvironment: ()=> $ajax({ url: `${rootUrl}/environment/` }),
    queryUserExistence: (username)=> {
      var d = new $.Deferred();
      $ajax({ url: `${rootUrl}/users/${username}/` })
        .done(()=>{ d.resolve(username, true); })
        .fail(()=>{ d.reject(username, false); });
      return d.promise();
    },
    logout: ()=> {
      var d = new $.Deferred();
      $ajax({ url: `${rootUrl}/api-auth/logout/` }).done(d.resolve).fail(function (/*resp, etype, emessage*/) {
        // logout request wasn't successful, but may have logged the user out
        // querying '/me/' can confirm if we have logged out.
        dataInterface.selfProfile().done(function(data){
          if (data.message === 'user is not logged in') {
            d.resolve(data);
          } else {
            d.fail(data);
          }
        }).fail(d.fail);
      });
      return d.promise();
    },
    patchProfile (data) {
      return $ajax({
        url: `${rootUrl}/me/`,
        method: 'PATCH',
        data: data
      });
    },
    listBlocks () {
      return $ajax({
        url: `${rootUrl}/assets/?q=asset_type:block`
      });
    },
    listTemplates () {
      return $ajax({
        url: `${rootUrl}/assets/?q=asset_type:template`
      });
    },
    listSurveys() {
      return $ajax({
        url: `${rootUrl}/assets/`,
        data: {
          q: 'asset_type:survey'
        },
        method: 'GET'
      });
    },
    listCollections () {
      return $.getJSON(`${rootUrl}/collections/?all_public=true`);
    },
    listAllAssets () {
      var d = new $.Deferred();
      $.when($.getJSON(`${rootUrl}/assets/?parent=`), $.getJSON(`${rootUrl}/collections/?parent=`)).done(function(assetR, collectionR){
        var assets = assetR[0],
            collections = collectionR[0];
        var r = {
          results: [],
        };
        var pushItem = function (item){
          r.results.push(item);
        };
        assets.results.forEach(pushItem);
        collections.results.forEach(pushItem);
        var sortAtt = 'date_modified';
        r.results.sort(function(a, b){
          var ad = a[sortAtt], bd = b[sortAtt];
          return (ad === bd) ? 0 : ((ad > bd) ? -1 : 1);
        });
        d.resolve(r);
      }).fail(d.fail);
      return d.promise();
    },
    createAssetSnapshot (data) {
      return $ajax({
        url: `${rootUrl}/asset_snapshots/`,
        method: 'POST',
        data: data
      });
    },
    getReportData (data) {
      let identifierString;
      if (data.identifiers) {
        identifierString = `?names=${data.identifiers.join(',')}`
      }
      if (data.group_by != '')
        identifierString += `&split_by=${data.group_by}`

      return $ajax({
        url: `${rootUrl}/reports/${data.uid}/${identifierString}`,
      });
    },
    createTemporaryAssetSnapshot ({source}) {
      return $ajax({
        url: `${rootUrl}/asset_snapshots/`,
        method: 'POST',
        data: {
          source: source
        }
      });
    },
    cloneAsset ({uid, name, version_id, new_asset_type}) {
      let data = {
        clone_from: uid,
      };
      if (name) { data.name = name; }
      if (version_id) { data.clone_from_version_id = version_id; }
      if (new_asset_type) { data.asset_type = new_asset_type; }
      return $ajax({
        method: 'POST',
{'fraction_non_alphanumeric': 0.1865, 'fraction_numerical': 0.00125, 'mean_word_length': 2.0773076923076923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4409396', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2561, 'n_words': 514}