<commit_before>/*
   * Copyright 2016 NIIT Ltd, Wipro Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   * Contributors:
   *
   * 1. <NAME>
   * 2. <NAME>
   * 3. <NAME>
   * 4. <NAME>
   * 5. <NAME>
   * 6. <NAME>
*/
   
var hotChocolate = angular.module('hotChocolate');
hotChocolate.controller('queryController', function($scope, $http, $rootScope, GraphService, executeQueryService, $uibModal, $compile, $cookies, $window, $timeout) {
  if(!$cookies.get('userName')){
    $window.location.href = '/';
  }
  else{
    $rootScope.container = angular.element(document).find('#tableDiv');
    $scope.items = [{
                    label: 'Measures',
                    list: []
                  },
                  {
                    label: 'Columns',
                    list: []
                  }, {
                    label: 'Rows',
                    list: []
                  }, {
                    label: 'Filters',
                    list: []
                  }];
  $scope.deleteItem = function(childIndex, parentIndex) {
    $scope.items[parentIndex].list.splice(childIndex, 1);
  };

  $scope.logout = function(){
    $cookies.put('userName', undefined);
    $window.location.href = '/logout';
  };

  $scope.getExecuteQueryData = function() {
    executeQueryService.removeGrid($rootScope.container);
    if($scope.items[0].list.length == 0 && $scope.items[1].list.length == 0) {
      $scope.mdxInputErrorMessage = "Atleast one of Measures and Columns need to be filled.";
      $scope.isMdxInputError = true;
      $timeout(function() {
        $scope.isMdxInputError = false;
      }, 3000);
    }
    else if ($scope.items[2].list.length == 0) {
      $scope.mdxInputErrorMessage = "Rows should not be empty.";
      $scope.isMdxInputError = true;
      $timeout(function() {
        $scope.isMdxInputError = false;
      }, 3000);
    }
    else {
      var parameters = {
        connId : $rootScope.connId,
        dataSource: $rootScope.DataSourceName,
        catalog: $rootScope.CatalogName,
        statement: $scope.buildQuery()
      };
      $scope.loadingQuery = true;
      executeQueryService.render($rootScope.container, parameters).then(function(data) {
        $timeout(function() {
          $scope.loadingQuery = false;
        }, 100);
        $scope.graphArray = data;
      });
    } // end else
  }; // end function

  $scope.buildQuery = function () {
    var measures = $scope.items[0].list,
        measureArr = [];
    for(var i=0; i < measures.length; i++) {
      measureArr.push(measures[i].unique_name);
    }
    var measureSet = "{" + measureArr.join() + "}";

    var columns = $scope.items[1].list;
        columnSet = $scope.buildSubQuery(columns);
        columnSubQuery = columns.length > 0 ? (measures.length > 0 ? "(" + columnSet + " * " + measureSet + ")" : columnSet) : measureSet;

    var rows = $scope.items[2].list;
        rowSet = $scope.buildSubQuery(rows);

    var filters = $scope.items[3].list,
        filterArr = [];
    for(var j=0; j < filters.length; j++) {
      filterArr.push(filters[j].unique_name);
    }
    var filterSet = "{" + filterArr.join() + "}";
    var filterSubQuery = filters.length > 0 ? " where " + filterSet : "";
    var nonEmpty = $scope.isQueryNonEmpty ? "non empty" : "";
    $scope.mdxQuery = "select " + nonEmpty + " (" + columnSubQuery + ") on columns, " + nonEmpty + " (" + rowSet + ") on rows" + " from ["+ $rootScope.CubeName +"]" + filterSubQuery ;
    return $scope.mdxQuery;
  };

  $scope.buildSubQuery = function (itemArr) {
    var columnResult = $scope.groupBy(itemArr, function(item){
            return [item.hierName];
        });
        columnArr = [];
    for(var j=0; j < columnResult.length; j++) {
      var subColumnQuery = "";
      var subColumnArr = [];
      for(var k=0; k < columnResult[j].length; k++) {
        subColumnArr.push(columnResult[j][k].isMember === "yes" ? columnResult[j][k].unique_name : columnResult[j][k].unique_name + ".members");
      }
      if (subColumnArr.length > 1) {
        subColumnQuery = "Hierarchize ({" + subColumnArr.join() + "})";
      } else {
        subColumnQuery = subColumnArr.join();
      }
      columnArr.push(subColumnQuery);
    }
    return "{" + columnArr.join("*") + "}";
  };

  $scope.groupBy = function ( array , f ) {
    var groups = {};
    array.forEach( function( o )
    {
      var group = JSON.stringify( f(o) );
      groups[group] = groups[group] || [];
      groups[group].push( o );
    });
    return Object.keys(groups).map( function( group )
    {
      return groups[group];
    });
  };

  $scope.sortList = function(event, ui, listIdx) {
    var itemArr = $scope.items[listIdx].list,
        currItem = itemArr.pop();
    delete currItem.children;
    var isValidationError = false;
    if(listIdx !== 0 && currItem.hierName === "Measures") {
      isValidationError = true;
    }
    for(var h=1; h < 4; h++) {
      if(h !== listIdx) {
        for(var g=0; g < $scope.items[h].list.length; g++) {
          if($scope.items[h].list[g].hierName === currItem.hierName) {
            isValidationError = true;
            break;
          }
        }
      }
    }
    if(!isValidationError && itemArr.indexOf(currItem) == -1) {
      itemArr.push(currItem);
      for(var i=0; i < itemArr.length-1; i++) {
        if(itemArr[i].hierName == currItem.hierName) {
          if(itemArr[i].levelIdx > currItem.levelIdx) {
            itemArr.splice(i, 0, currItem);
          }
          else {<commit_msg>fixed validation bug<commit_after>/*
   * Copyright 2016 NIIT Ltd, Wipro Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   * Contributors:
   *
   * 1. <NAME>
   * 2. <NAME>
   * 3. <NAME>
   * 4. <NAME>
   * 5. <NAME>
   * 6. <NAME>
*/

var hotChocolate = angular.module('hotChocolate');
hotChocolate.controller('queryController', function($scope, $http, $rootScope, GraphService, executeQueryService, $uibModal, $compile, $cookies, $window, $timeout) {
  if(!$cookies.get('userName')){
    $window.location.href = '/';
  }
  else{
    $rootScope.container = angular.element(document).find('#tableDiv');
    $scope.items = [{
                    label: 'Measures',
                    list: []
                  },
                  {
                    label: 'Columns',
                    list: []
                  }, {
                    label: 'Rows',
                    list: []
                  }, {
                    label: 'Filters',
                    list: []
                  }];
  $scope.deleteItem = function(childIndex, parentIndex) {
    $scope.items[parentIndex].list.splice(childIndex, 1);
  };

  $scope.logout = function(){
    $cookies.put('userName', undefined);
    $window.location.href = '/logout';
  };

  $scope.getExecuteQueryData = function() {
    executeQueryService.removeGrid($rootScope.container);
    if($scope.items[0].list.length == 0 && $scope.items[1].list.length == 0) {
      $scope.mdxInputErrorMessage = "Atleast one of Measures and Columns need to be filled.";
      $scope.isMdxInputError = true;
      $timeout(function() {
        $scope.isMdxInputError = false;
      }, 3000);
    }
    else if ($scope.items[2].list.length == 0) {
      $scope.mdxInputErrorMessage = "Rows should not be empty.";
      $scope.isMdxInputError = true;
      $timeout(function() {
        $scope.isMdxInputError = false;
      }, 3000);
    }
    else {
      var parameters = {
        connId : $rootScope.connId,
        dataSource: $rootScope.DataSourceName,
        catalog: $rootScope.CatalogName,
        statement: $scope.buildQuery()
      };
      $scope.loadingQuery = true;
      executeQueryService.render($rootScope.container, parameters).then(function(data) {
        $timeout(function() {
          $scope.loadingQuery = false;
        }, 100);
        $scope.graphArray = data;
      });
    } // end else
  }; // end function

  $scope.buildQuery = function () {
    var measures = $scope.items[0].list,
        measureArr = [];
    for(var i=0; i < measures.length; i++) {
      measureArr.push(measures[i].unique_name);
    }
    var measureSet = "{" + measureArr.join() + "}";

    var columns = $scope.items[1].list;
        columnSet = $scope.buildSubQuery(columns);
        columnSubQuery = columns.length > 0 ? (measures.length > 0 ? "(" + columnSet + " * " + measureSet + ")" : columnSet) : measureSet;

    var rows = $scope.items[2].list;
        rowSet = $scope.buildSubQuery(rows);

    var filters = $scope.items[3].list,
        filterArr = [];
    for(var j=0; j < filters.length; j++) {
      filterArr.push(filters[j].unique_name);
    }
    var filterSet = "{" + filterArr.join() + "}";
    var filterSubQuery = filters.length > 0 ? " where " + filterSet : "";
    var nonEmpty = $scope.isQueryNonEmpty ? "non empty" : "";
    $scope.mdxQuery = "select " + nonEmpty + " (" + columnSubQuery + ") on columns, " + nonEmpty + " (" + rowSet + ") on rows" + " from ["+ $rootScope.CubeName +"]" + filterSubQuery ;
    return $scope.mdxQuery;
  };

  $scope.buildSubQuery = function (itemArr) {
    var columnResult = $scope.groupBy(itemArr, function(item){
            return [item.hierName];
        });
        columnArr = [];
    for(var j=0; j < columnResult.length; j++) {
      var subColumnQuery = "";
      var subColumnArr = [];
      for(var k=0; k < columnResult[j].length; k++) {
        subColumnArr.push(columnResult[j][k].isMember === "yes" ? columnResult[j][k].unique_name : columnResult[j][k].unique_name + ".members");
      }
      if (subColumnArr.length > 1) {
        subColumnQuery = "Hierarchize ({" + subColumnArr.join() + "})";
      } else {
        subColumnQuery = subColumnArr.join();
      }
      columnArr.push(subColumnQuery);
    }
    return "{" + columnArr.join("*") + "}";
  };

  $scope.groupBy = function ( array , f ) {
    var groups = {};
    array.forEach( function( o )
    {
      var group = JSON.stringify( f(o) );
      groups[group] = groups[group] || [];
      groups[group].push( o );
    });
    return Object.keys(groups).map( function( group )
    {
      return groups[group];
    });
  };

  $scope.sortList = function(event, ui, listIdx) {
    var itemArr = $scope.items[listIdx].list,
        currItem = itemArr.pop();
    delete currItem.children;
    var isValidationError = false;
    if(listIdx !== 0 && currItem.hierName === "Measures" || listIdx == 0 && currItem.hierName !== "Measures") {
      isValidationError = true;
    }
    for(var h=1; h < 4; h++) {
      if(h !== listIdx) {
        for(var g=0; g < $scope.items[h].list.length; g++) {
          if($scope.items[h].list[g].hierName === currItem.hierName) {
            isValidationError = true;
            break;
          }
        }
      }
    }
    if(!isValidationError && itemArr.indexOf(currItem) == -1) {
      itemArr.push(currItem);
      for(var i=0; i < itemArr.length-1; i++) {
        if(itemArr[i].hierName == currItem.hierName) {
          if(itemArr[i].levelIdx > currItem.levelIdx) {
            itemArr.splice(i, 0, currItem);
          }
          else {
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.008629191321499015, 'mean_word_length': 2.3203274215552523, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5270412', 'n_tokens_mistral': 3884, 'n_tokens_neox': 3779, 'n_words': 800}