<commit_before>angular.module('app').controller('collectionQueryCtrl', [
  '$scope',
  '$timeout',
  function($scope, $timeout) {
    if (!$scope.collection) throw new Error('collection is required for collection query directive');

    $scope.loading = false;
    $scope.queryTime = null;

    $scope.codeEditorOptions = {};

    $scope.form = {
      searchQuery: '{}'
    };

    $scope.search = function search() {
      $scope.loading = true;
      $scope.error = null;

      var searchQuery;

      try {
        searchQuery = $scope.$eval($scope.form.searchQuery);
      } catch (err) {
        $scope.error = err && err.message ? err.message : err;
        $scope.loading = false;
        return;
      }

      var startTime = performance.now();

      $scope.collection.find(searchQuery, function(err, results) {
        var endTime = performance.now();

        $timeout(function() {
          if (err) $scope.error = err;

          $scope.queryTime = (endTime - startTime).toFixed(5);

          $scope.loading = false;

          $scope.results = results;
        }, 500);
      });
    };

    $scope.search();

    // function convertToKeyValuPairs(obj) {
    //   var kvp = [];
    //
    //   for (var key in obj) {
    //     kvp.push({
    //       key: key,
    //       value: obj[key]
    //     });
    //   }
    //
    //   return kvp;
    // }
  }
]);<commit_msg>allow find, update, remove and aggregate mongo queries<commit_after>angular.module('app').controller('collectionQueryCtrl', [
  '$scope',
  '$timeout',
  function($scope, $timeout) {
    if (!$scope.collection) throw new Error('collection is required for collection query directive');

    $scope.loading = false;
    $scope.queryTime = null;

    // const FIND_QUERY_CHECK_REGEX = /^find.*$/; //checks if it's an find query
    const FIND_QUERY_FULL_REGEX = /^(?:find)([^]+)/; //extracts the query when it's a find query

    // const UPDATE_QUERY_CHECK_REGEX = /^update.*$/;
    const UPDATE_QUERY_FULL_REGEX = /^(?:update)([^]+)/;

    // const REMOVE_QUERY_CHECK_REGEX = /^remove.*$/;
    const REMOVE_QUERY_FULL_REGEX = /^(?:remove)([^]+)/;

    // const AGGREGATE_QUERY_CHECK_REGEX = /^aggregate.*$/;
    const AGGREGATE_QUERY_FULL_REGEX = /^(?:aggregate)([^]+)/;

    $scope.codeEditorOptions = {};

    $scope.form = {
      searchQuery: 'find({\n\n})'
    };

    $scope.search = function search() {
      $scope.loading = true;
      $scope.error = null;

      var queryFn = getQueryTypeFn($scope.form.searchQuery);

      if (!queryFn) {
        $scope.error = 'Sorry, that is not a valid mongo query type';
        $scope.loading = false;
        return;
      }

      var searchQuery = convertSearchQueryToJsObject($scope.form.searchQuery);

      if (!searchQuery) {
        $scope.error = 'Sorry, that is not a valid mongo query';
        $scope.loading = false;
        return;
      }

      var startTime = performance.now();

      queryFn(searchQuery, function(err, results) {
        var endTime = performance.now();

        $timeout(function() {
          if (err) $scope.error = err;

          $scope.queryTime = (endTime - startTime).toFixed(5);

          $scope.loading = false;

          $scope.results = results;
        });
      });
    };

    $scope.search();

    function convertSearchQueryToJsObject(query) {

      var match;

      if (matchesRegex(query, FIND_QUERY_FULL_REGEX)) {
        match = getRegexMatch(query, FIND_QUERY_FULL_REGEX);
      } else if (matchesRegex(query, UPDATE_QUERY_FULL_REGEX)) {
        match = getRegexMatch(query, UPDATE_QUERY_FULL_REGEX);
      } else if (matchesRegex(query, REMOVE_QUERY_FULL_REGEX)) {
        match = getRegexMatch(query, REMOVE_QUERY_FULL_REGEX);
      } else if (matchesRegex(query, AGGREGATE_QUERY_FULL_REGEX)) {
        match = getRegexMatch(query, AGGREGATE_QUERY_FULL_REGEX);
      } else {
        match = null;
      }

      if (!match) return null;

      try {
        match = $scope.$eval(match);
      } catch (err) {
        $scope.error = err && err.message ? err.message : err;
        $scope.loading = false;
      }
      return match;
    }

    function getQueryTypeFn(query) {
      if (matchesRegex(query, FIND_QUERY_FULL_REGEX)) {
        return findQuery;
      } else if (matchesRegex(query, UPDATE_QUERY_FULL_REGEX)) {
        return updateQuery;
      } else if (matchesRegex(query, REMOVE_QUERY_FULL_REGEX)) {
        return removeQuery;
      } else if (matchesRegex(query, AGGREGATE_QUERY_FULL_REGEX)) {
        return aggregateQuery;
      } else {
        return null;
      }
    }

    function matchesRegex(query, regex) {
      var match = query.match(regex);
      return match && match.length ? true : false;
    }

    function getRegexMatch(query, regex) {
      var matches = query.match(regex);
      return matches && matches.length > 1 ? matches[1] : null;
    }

    function findQuery(query, next) {
      $scope.collection.find(query, next);
    }

    function updateQuery(query, next) {
      $scope.collection.update(query, next);
    }

    function removeQuery(query, next) {
      $scope.collection.remove(query, next);
    }

    function aggregateQuery(aggregate, next) {
      $scope.collection.aggregate(aggregate, next);
    }
  }
]);
{'fraction_non_alphanumeric': 0.13893100833965125, 'fraction_numerical': 0.001326762699014405, 'mean_word_length': 2.4243997404282935, 'pattern_counts': {'":': 0, '<': 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': '6344928', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1628, 'n_words': 340}