<commit_before>// Licensed to Cloudera, Inc. under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  Cloudera, Inc. licenses this file
// to you 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.

var AssistDbEntry = (function () {
  /**
   * @param {Object} definition
   * @param {string} definition.type
   * @param {string} definition.name
   * @param {boolean} [definition.isColumn]
   * @param {boolean} [definition.isTable]
   * @param {boolean} [definition.isView]
   * @param {boolean} [definition.isDatabase]
   * @param {boolean} [definition.isMapValue]
   * @param {boolean} [definition.isArray]
   * @param {AssistDbEntry} parent
   * @param {AssistDbSource} assistDbSource
   * @param {Object} filter
   * @param {function} filter.query (observable)
   * @param {function} filter.showViews (observable)
   * @param {function} filter.showTables (observable)
   * @param {Object} i18n
   * @param {string} i18n.errorLoadingTablePreview
   * @param {Object} navigationSettings
   * @constructor
   */
  function AssistDbEntry (definition, parent, assistDbSource, filter, i18n, navigationSettings, sortFunctions) {
    var self = this;
    self.i18n = i18n;
    self.definition = definition;
    self.sortFunctions = sortFunctions;

    self.assistDbSource = assistDbSource;
    self.parent = parent;
    self.filter = filter;
    self.isSearchVisible = assistDbSource.isSearchVisible;
    self.editingSearch = ko.observable(false);
    self.sourceType = self.assistDbSource.sourceType;
    self.invalidateOnRefresh =  self.assistDbSource.invalidateOnRefresh;
    self.highlight = ko.observable(false);
    self.activeSort = self.assistDbSource.activeSort;
    self.popularity = ko.observable(0);

    self.expandable = typeof definition.type === "undefined" || /table|view|struct|array|map/i.test(definition.type);

    self.loaded = false;
    self.loading = ko.observable(false);
    self.open = ko.observable(false);
    self.entries = ko.observableArray([]);
    self.statsVisible = ko.observable(false);

    self.hasErrors = ko.observable(false);

    self.navigationSettings = navigationSettings;

    self.open.subscribe(function(newValue) {
      if (newValue && self.entries().length == 0) {
        self.loadEntries();
      }
    });

    self.hasEntries = ko.pureComputed(function() {
      return self.entries().length > 0;
    });

    self.assistDbSource.activeSort.subscribe(function (newSort) {
      self.entries.sort(self.sortFunctions[newSort]);
    });

    self.filteredEntries = ko.pureComputed(function () {
      if (self.filter == null || (self.filter.showTables && self.filter.showTables() && self.filter.showViews() && !self.filter.showActive() && self.filter.query().length === 0)) {
        return self.entries();
      }
      var result = [];
      $.each(self.entries(), function (index, entry) {
        if (self.filter.showActive()) {
          var found = false;
          for (var i = 0; i < self.filter.activeEditorTables().length; i++) {
            var activeTable = self.filter.activeEditorTables()[i];
            if (activeTable.db && self.definition.name !== activeTable.db) {
              continue;
            }
            if (activeTable.table === entry.definition.name) {
              found = true;
              break;
            }
          }
          if (!found) {
            return;
          }
        }
        if ((entry.definition.isTable && !self.filter.showTables()) || (entry.definition.isView && !self.filter.showViews()) ) {
          return;
        }
        if (entry.definition.name.toLowerCase().indexOf(self.filter.query().toLowerCase()) > -1) {
          result.push(entry);
        }
      });
      return result;
    });

    self.tableName = null;
    self.columnName = null;
    self.type = null;
    self.databaseName = self.getHierarchy()[0];
    if (self.definition.isTable || self.definition.isView) {
      self.tableName = self.definition.name;
      self.columnName = null;
      self.type = self.definition.type;
    } else if (self.definition.isColumn) {
      self.tableName = parent.definition.name;
      self.columnName = self.definition.name;
    }

    self.editorText = ko.pureComputed(function () {
      if (self.definition.isTable || self.definition.isView) {
        return self.getTableName();
      }
      if (self.definition.isColumn) {
        return self.getColumnName() + ', ';
      }
      return self.getComplexName() + ', ';
    });
  }

  var findNameInHierarchy = function (entry, searchCondition) {
    var sourceType = entry.sourceType;
    while (entry && !searchCondition(entry)) {
      entry = entry.parent;
    }
    if (entry) {
      return SqlUtils.backTickIfNeeded(sourceType, entry.definition.name);
    }
  };

  AssistDbEntry.prototype.getDatabaseName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isDatabase });
  };

  AssistDbEntry.prototype.getTableName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isTable || entry.definition.isView });
  };

  AssistDbEntry.prototype.getColumnName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isColumn });
  };

  AssistDbEntry.prototype.getComplexName = function () {
    var entry = self;
    var sourceType = self.sourceType;<commit_msg>HUE-6086 [assist] Fix js error when opening a complex type<commit_after>// Licensed to Cloudera, Inc. under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  Cloudera, Inc. licenses this file
// to you 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.

var AssistDbEntry = (function () {
  /**
   * @param {Object} definition
   * @param {string} definition.type
   * @param {string} definition.name
   * @param {boolean} [definition.isColumn]
   * @param {boolean} [definition.isTable]
   * @param {boolean} [definition.isView]
   * @param {boolean} [definition.isDatabase]
   * @param {boolean} [definition.isMapValue]
   * @param {boolean} [definition.isArray]
   * @param {AssistDbEntry} parent
   * @param {AssistDbSource} assistDbSource
   * @param {Object} filter
   * @param {function} filter.query (observable)
   * @param {function} filter.showViews (observable)
   * @param {function} filter.showTables (observable)
   * @param {Object} i18n
   * @param {string} i18n.errorLoadingTablePreview
   * @param {Object} navigationSettings
   * @constructor
   */
  function AssistDbEntry (definition, parent, assistDbSource, filter, i18n, navigationSettings, sortFunctions) {
    var self = this;
    self.i18n = i18n;
    self.definition = definition;
    self.sortFunctions = sortFunctions;

    self.assistDbSource = assistDbSource;
    self.parent = parent;
    self.filter = filter;
    self.isSearchVisible = assistDbSource.isSearchVisible;
    self.editingSearch = ko.observable(false);
    self.sourceType = self.assistDbSource.sourceType;
    self.invalidateOnRefresh =  self.assistDbSource.invalidateOnRefresh;
    self.highlight = ko.observable(false);
    self.activeSort = self.assistDbSource.activeSort;
    self.popularity = ko.observable(0);

    self.expandable = typeof definition.type === "undefined" || /table|view|struct|array|map/i.test(definition.type);

    self.loaded = false;
    self.loading = ko.observable(false);
    self.open = ko.observable(false);
    self.entries = ko.observableArray([]);
    self.statsVisible = ko.observable(false);

    self.hasErrors = ko.observable(false);

    self.navigationSettings = navigationSettings;

    self.open.subscribe(function(newValue) {
      if (newValue && self.entries().length == 0) {
        self.loadEntries();
      }
    });

    self.hasEntries = ko.pureComputed(function() {
      return self.entries().length > 0;
    });

    self.assistDbSource.activeSort.subscribe(function (newSort) {
      self.entries.sort(self.sortFunctions[newSort]);
    });

    self.filteredEntries = ko.pureComputed(function () {
      if (self.filter == null || (self.filter.showTables && self.filter.showTables() && self.filter.showViews() && !self.filter.showActive() && self.filter.query().length === 0)) {
        return self.entries();
      }
      var result = [];
      $.each(self.entries(), function (index, entry) {
        if (self.filter.showActive()) {
          var found = false;
          for (var i = 0; i < self.filter.activeEditorTables().length; i++) {
            var activeTable = self.filter.activeEditorTables()[i];
            if (activeTable.db && self.definition.name !== activeTable.db) {
              continue;
            }
            if (activeTable.table === entry.definition.name) {
              found = true;
              break;
            }
          }
          if (!found) {
            return;
          }
        }
        if ((entry.definition.isTable && !self.filter.showTables()) || (entry.definition.isView && !self.filter.showViews()) ) {
          return;
        }
        if (entry.definition.name.toLowerCase().indexOf(self.filter.query().toLowerCase()) > -1) {
          result.push(entry);
        }
      });
      return result;
    });

    self.tableName = null;
    self.columnName = null;
    self.type = null;
    self.databaseName = self.getHierarchy()[0];
    if (self.definition.isTable || self.definition.isView) {
      self.tableName = self.definition.name;
      self.columnName = null;
      self.type = self.definition.type;
    } else if (self.definition.isColumn) {
      self.tableName = parent.definition.name;
      self.columnName = self.definition.name;
    }

    self.editorText = ko.pureComputed(function () {
      if (self.definition.isTable || self.definition.isView) {
        return self.getTableName();
      }
      if (self.definition.isColumn) {
        return self.getColumnName() + ', ';
      }
      return self.getComplexName() + ', ';
    });
  }

  var findNameInHierarchy = function (entry, searchCondition) {
    var sourceType = entry.sourceType;
    while (entry && !searchCondition(entry)) {
      entry = entry.parent;
    }
    if (entry) {
      return SqlUtils.backTickIfNeeded(sourceType, entry.definition.name);
    }
  };

  AssistDbEntry.prototype.getDatabaseName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isDatabase });
  };

  AssistDbEntry.prototype.getTableName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isTable || entry.definition.isView });
  };

  AssistDbEntry.prototype.getColumnName = function () {
    return findNameInHierarchy(this, function (entry) { return entry.definition.isColumn });
  };

  AssistDbEntry.prototype.getComplexName = function () {
    var entry = this;
    var sourceType = entry.sourceType;
{'fraction_non_alphanumeric': 0.12716811207471648, 'fraction_numerical': 0.003835890593729153, 'mean_word_length': 2.9989996665555183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5711867', 'n_tokens_mistral': 3507, 'n_tokens_neox': 3449, 'n_words': 788}