Browse Source

HUE-8289 [assist] Limit facets to existing column types and show counts in the filter autocomplete

Johan Ahlen 7 years ago
parent
commit
7f93bff4c5

+ 29 - 0
desktop/core/src/desktop/static/desktop/js/assist/assistDbEntry.js

@@ -147,6 +147,35 @@ var AssistDbEntry = (function () {
     }
   };
 
+  AssistDbEntry.prototype.knownFacetValues = function () {
+    var self = this;
+    var types = {};
+    if (self.parent === null) { // Only find facets on the DB level
+      self.entries().forEach(function (tableEntry) {
+        if (self.assistDbSource.sourceType !== 'solr') {
+          if (tableEntry.catalogEntry.isTable()) {
+            types.table =  types.table ? types.table + 1 : 1;
+          } else if (tableEntry.catalogEntry.isView()) {
+            types.view =  types.view ? types.view + 1 : 1;
+          }
+        }
+        if (tableEntry.open()) {
+          tableEntry.entries().forEach(function (colEntry) {
+            if (!types[colEntry.catalogEntry.getType()]) {
+              types[colEntry.catalogEntry.getType()] = 1;
+            } else {
+              types[colEntry.catalogEntry.getType()]++;
+            }
+          })
+        }
+      });
+    }
+    if (Object.keys(types).length) {
+      return { type: types }
+    }
+    return {};
+  };
+
   AssistDbEntry.prototype.getDatabaseName = function () {
     return findNameInHierarchy(this, function (entry) { return entry.catalogEntry.isDatabase() });
   };

+ 1 - 1
desktop/core/src/desktop/templates/assist.mako

@@ -870,7 +870,7 @@ from desktop.views import _ko
           params: {
             querySpec: filter.querySpec,
             facets: ['type'],
-            knownFacetValues: sourceType === 'solr' ? SOLR_ASSIST_KNOWN_FACET_VALUES : SQL_ASSIST_KNOWN_FACET_VALUES,
+            knownFacetValues: knownFacetValues.bind($data),
             autocompleteFromEntries: autocompleteFromEntries
           }
         } --><!-- /ko -->

+ 2 - 8
desktop/core/src/desktop/templates/ko_components/ko_inline_autocomplete.mako

@@ -62,12 +62,6 @@ from desktop.views import _ko
       var getSortedFacets = function (facetIndex) {
         var result = Object.keys(facetIndex);
         result.sort(function (a, b) {
-          if (facetIndex[a] > facetIndex[b]) {
-            return -1;
-          }
-          if (facetIndex[b] > facetIndex[a]) {
-            return 1;
-          }
           return a.localeCompare(b);
         });
         return result;
@@ -280,7 +274,7 @@ from desktop.views import _ko
         var querySpec = { query: self.searchInput() };
 
         if (self.lastParseResult.facets) {
-          var knownFacetValues = ko.unwrap(self.knownFacetValues);
+          var knownFacetValues = typeof self.knownFacetValues === 'function' ? self.knownFacetValues() : self.knownFacetValues;
           var cleanFacets = {};
           Object.keys(self.lastParseResult.facets).forEach(function (facet) {
             if (!knownFacetValues[facet]) {
@@ -380,7 +374,7 @@ from desktop.views import _ko
         }
 
         if (self.lastParseResult.suggestFacetValues) {
-          var facetValues = ko.unwrap(self.knownFacetValues);
+          var facetValues = typeof self.knownFacetValues === 'function' ? self.knownFacetValues() : self.knownFacetValues;
           if (facetValues && facetValues[self.lastParseResult.suggestFacetValues.toLowerCase()]) {
             var matchedFacets = facetValues[self.lastParseResult.suggestFacetValues.toLowerCase()];
             getSortedFacets(matchedFacets).forEach(function (value) {