<commit_before>'use strict';

var queries = angular.module('cliffhanger.queries', ['ngRoute']);

queries.controller('QueryWizardCtrl', function ($scope, $uibModalInstance, $log, datasets, queryService) {
    $scope.query = {}; //container for query
    $scope.tableResult = {};
    $scope.step = 1; //which step in the modal is on
    $scope.maxSteps = 5; //number of steps in modal
    $scope.datasets = datasets;
    $scope.tags = []; //TODO load based upon datasets


    $scope.addJoinColumn = false; //whether to include joined column in select
    $scope.selectedDatasets = []; //datasets selected to be joined
    $scope.selectedTags = []; //selected join tags
    $scope.selectedColumns = []; //select columns for query

    //method responsible for handling changes due to checkboxes
    //d -> item selected/deselected
    //selections -> array to add/remove item
    $scope.change = function (d, selections) {
        if (d.selected) {
            selections.push(d);
        } else {
            for (var i = 0; i < selections.length; i++) {
                $log.debug(selections[i]);
                if (selections[i].name == d.name) {
                    selections.splice(i, 1);
                }
            }
        }
        $log.debug(selections);
    }

    //method responsible for handling changes due to checkboxes
    //d -> item selected/deselected
    //selections -> array to add/remove item
    $scope.changeColumns = function (column, selections, dataset) {
        if (column.selected) {
            column.db_table_name = dataset.db_table_name;
            selections.push(column);
        } else {
            for (var i = 0; i < selections.length; i++) {
                $log.debug(selections[i]);
                if (selections[i].name == column.name) {
                    selections.splice(i, 1);
                }
            }
        }
        $log.debug(selections);
    }

    //load the joinable tags only in the selected datasets
    $scope.loadTags = function () {
        for (var i = 0; i < $scope.selectedDatasets[0].tags.length; i++) {
            var tagA = $scope.selectedDatasets[0].tags[i];
            for (var j = 0; j < $scope.selectedDatasets[1].tags.length; j++) {
                var tagB = $scope.selectedDatasets[1].tags[j];
                //if tags match, aren't EMPTY and are not a duplicate
                if (tagA.name == tagB.name && tagA.name != '<EMPTY>' && $scope.tags.indexOf(tagA) == -1) {
                    $scope.tags.push(tagA);
                    break;
                }
            }
        }
        $log.debug($scope.tags);
    }


    //advance the modal to the next step
    $scope.next = function () {
        $scope.step++;
        if ($scope.step == 2) {
            $scope.loadTags();
        } else if ($scope.step == 4) $scope.buildQuery();
        else if ($scope.step == 5) $scope.runQuery($scope.query);
    };

    //go back a step in the modal
    $scope.previous = function () {
        $scope.step--;
        if ($scope.step < $scope.maxSteps) $scope.progressType = null;
    };

    //dismiss the modal
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    //complete the modal
    $scope.submit = function () {};

    //add where and limit clause to SQL query string
    $scope.addToQuery = function () {
        $scope.query = $scope.query.replace(";", "");
        if ($scope.statement.where != "" && $scope.statement.limit != "") {
            $scope.statement.text = "\nWHERE " + $scope.statement.where + "\n" + "LIMIT " + $scope.statement.limit + ";";
        } else if ($scope.statement.where != "" && $scope.statement.limit == "") {
            $scope.statement.text = "\nWHERE " + $scope.statement.where + ";";
        } else if ($scope.statement.where == "" && $scope.statement.limit != "") {
            $scope.statement.text = "\nLIMIT " + $scope.statement.limit + ";";
        } else if ($scope.statement.where != "" && $scope.statement.limit != "") {
            $scope.statement.text = ";";
        }
        $log.debug($scope.statement);
    };

    //build a new query given the user's choices
    $scope.buildQuery = function () {
        //query input packaged
        var queryInput = {
            datasets: $scope.selectedDatasets,
            joinTag: $scope.selectedTags,
            addJoinColumn: $scope.addJoinColumn,
            columns: $scope.selectedColumns
        }

        queryService.buildQuery(queryInput)
            .then(
                function (response) {
                    //success callback
                    if (response.status == 'Success') {
                        $scope.query = response.data;
                        $scope.progressType = 'success';
                    } else {
                        $scope.progressType = 'danger';
                        $scope.buildQueryError = true;
                        $log.error(response.data);

                    }

                }, //failure to connect<commit_msg>minor edits<commit_after>'use strict';

var queries = angular.module('cliffhanger.queries', ['ngRoute']);

queries.controller('QueryWizardCtrl', function ($scope, $uibModalInstance, $log, datasets, queryService) {
    $scope.query = {}; //container for query
    $scope.tableResult = {};
    $scope.step = 1; //which step in the modal is on
    $scope.maxSteps = 5; //number of steps in modal
    $scope.datasets = datasets;
    $scope.tags = []; //TODO load based upon datasets


    $scope.addJoinColumn = false; //whether to include joined column in select
    $scope.selectedDatasets = []; //datasets selected to be joined
    $scope.selectedTags = []; //selected join tags
    $scope.selectedColumns = []; //select columns for query

    //method responsible for handling changes due to checkboxes
    //d -> item selected/deselected
    //selections -> array to add/remove item
    $scope.change = function (d, selections) {
        if (d.selected) {
            selections.push(d);
        } else {
            for (var i = 0; i < selections.length; i++) {
                $log.debug(selections[i]);
                if (selections[i].name == d.name) {
                    selections.splice(i, 1);
                }
            }
        }
        $log.debug(selections);
    }

    //method responsible for handling changes due to checkboxes
    //d -> item selected/deselected
    //selections -> array to add/remove item
    $scope.changeColumns = function (column, selections, dataset) {
        if (column.selected) {
            column.db_table_name = dataset.db_table_name;
            selections.push(column);
        } else {
            for (var i = 0; i < selections.length; i++) {
                $log.debug(selections[i]);
                if (selections[i].name == column.name) {
                    selections.splice(i, 1);
                }
            }
        }
        $log.debug(selections);
    }

    //load the joinable tags only in the selected datasets
    $scope.loadTags = function () {
        for (var i = 0; i < $scope.selectedDatasets[0].tags.length; i++) {
            var tagA = $scope.selectedDatasets[0].tags[i];
            for (var j = 0; j < $scope.selectedDatasets[1].tags.length; j++) {
                var tagB = $scope.selectedDatasets[1].tags[j];
                //if tags match, aren't EMPTY and are not a duplicate
                if (tagA.name == tagB.name && tagA.name != '<EMPTY>' && $scope.tags.indexOf(tagA) == -1) {
                    $scope.tags.push(tagA);
                    break;
                }
            }
        }
        $log.debug($scope.tags);
    }


    //advance the modal to the next step
    $scope.next = function () {
        $scope.step++;
        if ($scope.step == 2) {
            $scope.loadTags();
        } else if ($scope.step == 4) $scope.buildQuery();
        else if ($scope.step == 5) $scope.runQuery($scope.query);
    };

    //go back a step in the modal
    $scope.previous = function () {
        $scope.step--;
        if ($scope.step < $scope.maxSteps) $scope.progressType = null;
    };

    //dismiss the modal
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    //complete the modal
    $scope.submit = function () {};

    //add where and limit clause to SQL query string
    $scope.addToQuery = function () {
        if ($scope.statement.where != "" && $scope.statement.limit != "") {
            $scope.query = $scope.query.replace(";", "");
            $scope.statement.text = "\nWHERE " + $scope.statement.where + "\n" + "LIMIT " + $scope.statement.limit + ";";
        } else if ($scope.statement.where != "" && $scope.statement.limit == "") {
            $scope.query = $scope.query.replace(";", "");
            $scope.statement.text = "\nWHERE " + $scope.statement.where + ";";
        } else if ($scope.statement.where == "" && $scope.statement.limit != "") {
            $scope.query = $scope.query.replace(";", "");
            $scope.statement.text = "\nLIMIT " + $scope.statement.limit + ";";
        } else if ($scope.statement.where == "" && $scope.statement.limit == "") {
            $scope.statement.text = ";";
        }
        $log.debug($scope.statement);
    };

    //build a new query given the user's choices
    $scope.buildQuery = function () {
        //query input packaged
        var queryInput = {
            datasets: $scope.selectedDatasets,
            joinTag: $scope.selectedTags,
            addJoinColumn: $scope.addJoinColumn,
            columns: $scope.selectedColumns
        }

        queryService.buildQuery(queryInput)
            .then(
                function (response) {
                    //success callback
                    if (response.status == 'Success') {
                        $scope.query = response.data;
                        $scope.progressType = 'success';
                    } else {
                        $scope.progressType = 'danger';
                        $scope.buildQueryError = true;
                        $log.error(response.data);

                    }

                }, //failure to connect
{'fraction_non_alphanumeric': 0.1437869822485207, 'fraction_numerical': 0.003155818540433925, 'mean_word_length': 1.7783561643835617, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '234775', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2788, 'n_words': 670}