Browse Source

HUE-5430 [assist] Add popular sorting of tables

Johan Ahlen 9 years ago
parent
commit
78161bf

+ 31 - 6
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1408,6 +1408,24 @@ var ApiHelper = (function () {
     return ko.mapping.toJSON(dbTables);
   };
 
+  /**
+   * Fetches the top tables for the given database
+   *
+   * @param {Object} options
+   * @param {string} options.sourceType
+   * @param {Function} options.successCallback
+   * @param {Function} [options.errorCallback]
+   * @param {boolean} [options.silenceErrors]
+   *
+   * @param {Object[]} options.database
+   */
+  ApiHelper.prototype.fetchNavOptTopTables = function (options) {
+    var self = this;
+    self.fetchNavCached('/metadata/api/optimizer/top_tables', options, function (data) {
+      return data.status === 0 && data.top_tables && data.top_tables.length > 0;
+    });
+  };
+
   /**
    * Fetches the top columns for the given tables
    *
@@ -1500,9 +1518,18 @@ var ApiHelper = (function () {
   ApiHelper.prototype.fetchNavCached = function (url, options, cacheCondition) {
     var self = this;
 
-    var dbTablesJson = self.createNavDbTablesJson(options);
-
-    var hash = dbTablesJson.hashCode();
+    var data, hash;
+    if (options.tables) {
+      data = {
+        dbTables: self.createNavDbTablesJson(options)
+      };
+      hash = data.dbTables.hashCode();
+    } else if (options.database) {
+      data = {
+        database: options.database
+      };
+      hash = data.database;
+    }
 
     var fetchFunction = function (storeInCache) {
       if (options.timeout === 0) {
@@ -1513,9 +1540,7 @@ var ApiHelper = (function () {
       $.ajax({
         type: 'post',
         url: url,
-        data: {
-          dbTables: dbTablesJson
-        },
+        data: data,
         timeout: options.timeout
       })
       .done(function (data) {

+ 42 - 2
desktop/core/src/desktop/static/desktop/js/assist/assistDbEntry.js

@@ -75,8 +75,48 @@ var AssistDbEntry = (function () {
       return self.entries().length > 0;
     });
 
+    var deferredSort = false;
+
+    self.assistDbSource.selectedDatabase.subscribe(function (newValue) {
+      if (newValue === self && deferredSort) {
+        self.applySort(self.assistDbSource.activeSort(), self.entries);
+      }
+    });
+
+    self.applySort = function (sortName, entries) {
+      if (sortName === 'popular' && self.definition.isDatabase) {
+        if (self.assistDbSource.selectedDatabase() === self) {
+          self.assistDbSource.apiHelper.fetchNavOptTopTables({
+            sourceType: self.assistDbSource.sourceType,
+            database: self.definition.name,
+            silenceErrors: true,
+            successCallback: function (data) {
+              var popularityIndex = {};
+              data.top_tables.forEach(function (topTable) {
+                popularityIndex[topTable.name] = topTable.popularity;
+              });
+              self.entries().forEach(function (entry) {
+                if (popularityIndex[entry.definition.name]) {
+                  entry.definition.popularity = popularityIndex[entry.definition.name];
+                }
+                entries.sort(self.sortFunctions.popular);
+              });
+            },
+            errorCallback: function (data) {
+              entries.sort(self.sortFunctions.creation);
+            }
+          });
+          deferredSort = false;
+        } else {
+          deferredSort = true;
+        }
+      } else {
+        entries.sort(self.sortFunctions[sortName]);
+      }
+    };
+
     self.assistDbSource.activeSort.subscribe(function (newSort) {
-      self.entries.sort(self.sortFunctions[newSort]);
+      self.applySort(newSort, self.entries);
     });
 
     self.filteredEntries = ko.pureComputed(function () {
@@ -354,7 +394,7 @@ var AssistDbEntry = (function () {
         self.entries(newEntries);
         self.entries()[0].open(true);
       } else {
-        newEntries.sort(self.sortFunctions[self.assistDbSource.activeSort()]);
+        self.applySort(self.assistDbSource.activeSort(), newEntries);
         self.entries(newEntries);
       }
       if (typeof callback === 'function') {

+ 18 - 1
desktop/core/src/desktop/static/desktop/js/assist/assistDbSource.js

@@ -25,6 +25,18 @@ var AssistDbSource = (function () {
         return sortFunctions.alpha(a, b);
       }
       return a.definition.index - b.definition.index;
+    },
+    popular: function (a, b) {
+      if (a.definition.popularity && !b.definition.popularity) {
+        return 1;
+      }
+      if (b.definition.popularity && !a.definition.popularity) {
+        return -1;
+      }
+      if (a.definition.popularity && b.definition.popularity) {
+        return a.definition.popularity - b.definition.popularity
+      }
+      return sortFunctions.creation(a, b);
     }
   };
 
@@ -58,7 +70,12 @@ var AssistDbSource = (function () {
     self.activeSort = ko.observable('alpha');
 
     self.activeSort.subscribe(function (newSort) {
-      self.databases.sort(sortFunctions[newSort]);
+      if (newSort === 'popular') {
+        // TODO: Sort popular databases
+        self.databases.sort(sortFunctions.alpha)
+      } else {
+        self.databases.sort(sortFunctions[newSort]);
+      }
     });
 
     self.filter = {

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

@@ -1026,8 +1026,11 @@ from notebook.conf import ENABLE_QUERY_BUILDER
       <!-- ko if: $parent.activeSort -->
       <a class="inactive-action" data-toggle="dropdown" href="javascript:void(0)"><i class="pointer fa fa-sort" title="${_('Sort')}"></i></a>
       <ul class="dropdown-menu hue-inner-drop-down" style="top: initial; left: inherit; position: fixed; z-index:10000;">
-        <li><a href="javascript:void(0)" data-bind="click: function () { $parent.activeSort('creation'); }"><i class="fa fa-fw" data-bind="css: { 'fa-check': $parent.activeSort() === 'creation' }"></i> ${ _('Column order') }</a></li>
         <li><a href="javascript:void(0)" data-bind="click: function () { $parent.activeSort('alpha'); }"><i class="fa fa-fw" data-bind="css: { 'fa-check': $parent.activeSort() === 'alpha' }"></i> ${ _('Alphabetical') }</a></li>
+        <li><a href="javascript:void(0)" data-bind="click: function () { $parent.activeSort('creation'); }"><i class="fa fa-fw" data-bind="css: { 'fa-check': $parent.activeSort() === 'creation' }"></i> ${ _('Column order') }</a></li>
+        <!-- ko if: HAS_OPTIMIZER -->
+        <li><a href="javascript:void(0)" data-bind="click: function () { $parent.activeSort('popular'); }"><i class="fa fa-fw" data-bind="css: { 'fa-check': $parent.activeSort() === 'popular' }"></i> ${ _('Popularity') }</a></li>
+        <!-- /ko -->
       </ul>
       <!-- /ko -->
       <a class="inactive-action" href="javascript:void(0)" data-bind="click: toggleSearch, css: { 'blue' : isSearchVisible }"><i class="pointer fa fa-filter" title="${_('Filter')}"></i></a>