<commit_before>'use strict';
/*
 * Copyright 2014 Next Century Corporation
 * 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.
 *
 */
angular.module('neonDemo.directives')
.directive('databaseConfig', ['datasets', 'layouts', 'ConnectionService', 'DatasetService', function(datasets, layouts, connectionService, datasetService) {
    return {
        templateUrl: 'partials/directives/databaseConfig.html',
        restrict: 'E',
        scope: {
            storeSelect: '=',
            hostName: '=',
            gridsterConfigs: "="
        },
        link: function($scope, el) {
            el.addClass('databaseConfig');

            $scope.showDbTable = false;
            $scope.selectedDB = null;
            $scope.selectedTable = null;
            $scope.databases = [];
            $scope.dbTables = [];
            $scope.tableFields = [];
            $scope.tableFieldMappings = {};
            $scope.isConnected = false;
            $scope.clearPopover = '';
            $scope.activeServer = "Choose dataset";
            $scope.servers = datasets;

            $scope.fields = [
                {
                    label: "Latitude",
                    name: "latitude",
                    selected: ""
                },
                {
                    label: "Longitude",
                    name: "longitude",
                    selected: ""
                },
                {
                    label: "Date (and Time)",
                    name: "date",
                    selected: ""
                }
            ];

            $scope.datastoreSelect = $scope.storeSelect || 'mongo';
            $scope.hostnameInput = $scope.hostName || 'localhost';
            $scope.tableNameToFieldNames = {};
            $scope.layoutName = "";

            $scope.initialize = function() {
                $scope.messenger = new neon.eventing.Messenger();
            };

            $scope.connectToDataServer = function() {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "dataset-selector",
                    elementType: "button",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", $scope.datastoreSelect]
                });

                $scope.showDbTable = true;

                // Clear the active dataset while creating a custom connection so the visualizations cannot query.
                datasetService.setActiveDataset({});

                // Connect to the datastore.
                connectionService.createActiveConnection($scope.datastoreSelect, $scope.hostnameInput);

                // Clear the table names to force re-selection by the user.
                $scope.databases = [];
                $scope.dbTables = [];
                $scope.selectedDB = null;
                $scope.selectedTable = null;

                // Flag that we're connected for the front-end controls enable/disable code.
                $scope.isConnected = true;

                // Pull in the databse names.
                var connection = connectionService.getActiveConnection();
                if(connection) {
                    connection.getDatabaseNames(function(results) {
                        $scope.$apply(function() {
                            populateDatabaseDropdown(results);
                        });
                    });
                }
            };

            $scope.connectToPreset = function(server) {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "dataset-menu",
                    elementType: "button",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", server.name, "connect"]
                });

                // Change name of active connection.
                $scope.activeServer = server.name;

                // Set datastore connection details and connect to the datastore.
                $scope.datastoreSelect = server.datastore;
                $scope.hostnameInput = server.hostname;
                $scope.connectToDataServer();

                datasetService.setActiveDataset(server);

                $scope.selectedDB = server.databases[0].name;
                $scope.selectedTable = server.databases[0].tables[0].name;
                $scope.tableFields = server.databases[0].tables[0].fields;
                $scope.tableFieldMappings = server.databases[0].tables[0].mappings;
                $scope.updateLayout();

                var databaseNames = [];
                for(var i = 0; i < server.databases.length; ++i) {
                    databaseNames.push(server.databases[i].name);
                }

                // Wait to publish the dataset change until we've updated the field names.
                $scope.updateDatabases(databaseNames, $scope.publishDatasetChanged);
            };

            var populateDatabaseDropdown = function(dbs) {
                $scope.databases = dbs;
            };

            $scope.connectToDatabase = function(updateFieldsCallback) {
                var connection = connectionService.getActiveConnection();
                if(connection) {
                    connection.getTableNames($scope.selectedDB, function(tableNames) {
                        $scope.$apply(function() {
                            populateTableDropdown(tableNames);
                        });
                    });
                    $scope.updateFieldsForTables(updateFieldsCallback);
                }
            };

            $scope.selectDatabase = function(updateCallback) {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "database-selector",
                    elementType: "combobox",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", $scope.selectedDB, "database"]
                });

                if($scope.selectedDB) {
                    $scope.updateDatabases([$scope.selectedDB]);
                } else {
                    $scope.dbTables = [];
                }
            };

            $scope.updateDatabases = function(databaseNames, updateCallback) {
                var databaseName = databaseNames.shift();

                var connection = connectionService.getActiveConnection();
                if(connection) {
                    if(!datasetService.getDatabaseWithName(databaseName)) {
                        datasetService.dataset.databases.push({
                            name: databaseName,
                            tables: []
                        });
                    }
                    connection.getTableNamesAndFieldNames(databaseName, function(tableNamesAndFieldNames) {
                        $scope.$apply(function() {
                            var tableNames = Object.keys(tableNamesAndFieldNames);
                            populateTableDropdown(tableNames);

                            for(var i = 0; i < tableNames.length; ++i) {
                                var tableName = tableNames[i];

                                // Update the fields for this table if it exists in the active dataset.
                                datasetService.updateFields(databaseName, tableName, tableNamesAndFieldNames[tableName]);

                                // Store fields for each table locally because the dataset service ignores tables not included in the dataset.
                                // TODO Determine how to handle fields from tables in the database that are not included in the dataset.  This may
                                //      be solved once we update the custom connection interface to support multi-table datasets and field mappings.
                                $scope.tableNameToFieldNames[tableName] = tableNamesAndFieldNames[tableName];

                                if(databaseName === $scope.selectedDB && tableName === $scope.selectedTable) {<commit_msg>Changed selectTable() to attempt autofilling. Also has a temporary fix to prevent failure on selecting a custom database.<commit_after>'use strict';
/*
 * Copyright 2014 Next Century Corporation
 * 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.
 *
 */
angular.module('neonDemo.directives')
.directive('databaseConfig', ['datasets', 'layouts', 'ConnectionService', 'DatasetService', function(datasets, layouts, connectionService, datasetService) {
    return {
        templateUrl: 'partials/directives/databaseConfig.html',
        restrict: 'E',
        scope: {
            storeSelect: '=',
            hostName: '=',
            gridsterConfigs: "="
        },
        link: function($scope, el) {
            el.addClass('databaseConfig');

            $scope.showDbTable = false;
            $scope.selectedDB = null;
            $scope.selectedTable = null;
            $scope.databases = [];
            $scope.dbTables = [];
            $scope.tableFields = [];
            $scope.tableFieldMappings = {};
            $scope.isConnected = false;
            $scope.clearPopover = '';
            $scope.activeServer = "Choose dataset";
            $scope.servers = datasets;

            $scope.fields = [
                {
                    label: "Latitude",
                    name: "latitude",
                    selected: ""
                },
                {
                    label: "Longitude",
                    name: "longitude",
                    selected: ""
                },
                {
                    label: "Date (and Time)",
                    name: "date",
                    selected: ""
                }
            ];

            $scope.datastoreSelect = $scope.storeSelect || 'mongo';
            $scope.hostnameInput = $scope.hostName || 'localhost';
            $scope.tableNameToFieldNames = {};
            $scope.layoutName = "";

            $scope.initialize = function() {
                $scope.messenger = new neon.eventing.Messenger();
            };

            $scope.connectToDataServer = function() {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "dataset-selector",
                    elementType: "button",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", $scope.datastoreSelect]
                });

                $scope.showDbTable = true;

                // Clear the active dataset while creating a custom connection so the visualizations cannot query.
                datasetService.setActiveDataset({});

                // Connect to the datastore.
                connectionService.createActiveConnection($scope.datastoreSelect, $scope.hostnameInput);

                // Clear the table names to force re-selection by the user.
                $scope.databases = [];
                $scope.dbTables = [];
                $scope.selectedDB = null;
                $scope.selectedTable = null;

                // Flag that we're connected for the front-end controls enable/disable code.
                $scope.isConnected = true;

                // Pull in the databse names.
                var connection = connectionService.getActiveConnection();
                if(connection) {
                    connection.getDatabaseNames(function(results) {
                        $scope.$apply(function() {
                            populateDatabaseDropdown(results);
                        });
                    });
                }
            };

            $scope.connectToPreset = function(server) {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "dataset-menu",
                    elementType: "button",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", server.name, "connect"]
                });

                // Change name of active connection.
                $scope.activeServer = server.name;

                // Set datastore connection details and connect to the datastore.
                $scope.datastoreSelect = server.datastore;
                $scope.hostnameInput = server.hostname;
                $scope.connectToDataServer();

                datasetService.setActiveDataset(server);

                $scope.selectedDB = server.databases[0].name;
                $scope.selectedTable = server.databases[0].tables[0].name;
                $scope.tableFields = server.databases[0].tables[0].fields;
                $scope.tableFieldMappings = server.databases[0].tables[0].mappings;
                $scope.updateLayout();

                var databaseNames = [];
                for(var i = 0; i < server.databases.length; ++i) {
                    databaseNames.push(server.databases[i].name);
                }

                // Wait to publish the dataset change until we've updated the field names.
                $scope.updateDatabases(databaseNames, $scope.publishDatasetChanged);
            };

            var populateDatabaseDropdown = function(dbs) {
                $scope.databases = dbs;
            };

            $scope.connectToDatabase = function(updateFieldsCallback) {
                var connection = connectionService.getActiveConnection();
                if(connection) {
                    connection.getTableNames($scope.selectedDB, function(tableNames) {
                        $scope.$apply(function() {
                            populateTableDropdown(tableNames);
                        });
                    });
                    $scope.updateFieldsForTables(updateFieldsCallback);
                }
            };

            $scope.selectDatabase = function(updateCallback) {
                XDATA.userALE.log({
                    activity: "select",
                    action: "click",
                    elementId: "database-selector",
                    elementType: "combobox",
                    elementGroup: "top",
                    source: "user",
                    tags: ["dataset", $scope.selectedDB, "database"]
                });

                if($scope.selectedDB) {
                    $scope.updateDatabases([$scope.selectedDB]);
                } else {
                    $scope.dbTables = [];
                }
            };

            $scope.updateDatabases = function(databaseNames, updateCallback) {
                var databaseName = databaseNames.shift();

                var connection = connectionService.getActiveConnection();
                if(connection) {
                    // This is a temporary solution.
                    if(!datasetService.getDatabaseWithName(databaseName)) {
                        datasetService.dataset.databases.push({
                            name: databaseName,
                            tables: []
                        });
                    }
                    
                    connection.getTableNamesAndFieldNames(databaseName, function(tableNamesAndFieldNames) {
                        $scope.$apply(function() {
                            var tableNames = Object.keys(tableNamesAndFieldNames);
                            populateTableDropdown(tableNames);

                            for(var i = 0; i < tableNames.length; ++i) {
                                var tableName = tableNames[i];

                                // Update the fields for this table if it exists in the active dataset.
                                datasetService.updateFields(databaseName, tableName, tableNamesAndFieldNames[tableName]);

                                // Store fields for each table locally because the dataset service ignores tables not included in the dataset.
                                // TODO Determine how to handle fields from tables in the database that are not included in the dataset.  This may
                                //      be solved once we update the custom connection interface to support multi-table datasets and field mappings.
                                $scope.tableNameToFieldNames[tableName] = tableNamesAndFieldNames[tableName];

                                if(databaseName === $scope.selectedDB && tableName === $scope.selectedTable) {
{'fraction_non_alphanumeric': 0.09414106939704209, 'fraction_numerical': 0.0019340159271899887, 'mean_word_length': 1.405719759168035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5610407', 'n_tokens_mistral': 4096, 'n_tokens_neox': 3793, 'n_words': 936}