Browse Source

HUE-5444 [assist] Only fetch popular tables for open databases

Johan Ahlen 9 years ago
parent
commit
7518481877

+ 0 - 2
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -16,7 +16,6 @@
 
 
 var ApiHelper = (function () {
 var ApiHelper = (function () {
 
 
-
   var AUTOCOMPLETE_API_PREFIX = "/notebook/api/autocomplete/";
   var AUTOCOMPLETE_API_PREFIX = "/notebook/api/autocomplete/";
   var SAMPLE_API_PREFIX = "/notebook/api/sample/";
   var SAMPLE_API_PREFIX = "/notebook/api/sample/";
   var DOCUMENTS_API = "/desktop/api2/doc/";
   var DOCUMENTS_API = "/desktop/api2/doc/";
@@ -1711,7 +1710,6 @@ var ApiHelper = (function () {
     var cachedData = $.totalStorage("hue.assist." + self.getTotalStorageUserPrefix(options.sourceType)) || {};
     var cachedData = $.totalStorage("hue.assist." + self.getTotalStorageUserPrefix(options.sourceType)) || {};
     var cachedId = options.hash ? options.url + options.hash : options.url;
     var cachedId = options.hash ? options.url + options.hash : options.url;
 
 
-
     if (typeof cachedData[cachedId] == "undefined" || self.hasExpired(cachedData[cachedId].timestamp, options.cacheType || 'default')) {
     if (typeof cachedData[cachedId] == "undefined" || self.hasExpired(cachedData[cachedId].timestamp, options.cacheType || 'default')) {
       if (typeof options.editor !== 'undefined' && options.editor !== null) {
       if (typeof options.editor !== 'undefined' && options.editor !== null) {
         options.editor.showSpinner();
         options.editor.showSpinner();

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

@@ -53,6 +53,7 @@ var AssistDbEntry = (function () {
     self.highlightParent = ko.observable(false);
     self.highlightParent = ko.observable(false);
     self.activeSort = self.assistDbSource.activeSort;
     self.activeSort = self.assistDbSource.activeSort;
     self.popularityIndex = {};
     self.popularityIndex = {};
+    self.popularityIndexSet = false;
 
 
     self.expandable = typeof definition.type === "undefined" || /table|view|struct|array|map/i.test(definition.type);
     self.expandable = typeof definition.type === "undefined" || /table|view|struct|array|map/i.test(definition.type);
 
 
@@ -182,6 +183,7 @@ var AssistDbEntry = (function () {
 
 
   AssistDbEntry.prototype.setPopularityIndex = function (popularityIndex) {
   AssistDbEntry.prototype.setPopularityIndex = function (popularityIndex) {
     var self = this;
     var self = this;
+    self.popularityIndexSet = true;
     self.popularityIndex = popularityIndex;
     self.popularityIndex = popularityIndex;
   };
   };
 
 

+ 28 - 17
desktop/core/src/desktop/static/desktop/js/assist/assistDbSource.js

@@ -117,6 +117,34 @@ var AssistDbSource = (function () {
 
 
     self.selectedDatabase = ko.observable();
     self.selectedDatabase = ko.observable();
 
 
+    self.selectedDatabase.subscribe(function () {
+      var db = self.selectedDatabase();
+      if (HAS_OPTIMIZER && db && !db.popularityIndexSet) {
+        self.apiHelper.fetchNavOptTopTables({
+          sourceType: self.sourceType,
+          database: db.definition.name,
+          silenceErrors: true,
+          successCallback: function (data) {
+            var popularityIndex = {};
+            data.top_tables.forEach(function (topTable) {
+              popularityIndex[topTable.name] = topTable.popularity;
+            });
+            db.setPopularityIndex(popularityIndex);
+            if (self.activeSort() === 'popular') {
+              if (db.loading()) {
+                var subscription = db.loading.subscribe(function () {
+                  db.entries.sort(sortFunctions.popular);
+                  subscription.remove();
+                });
+              } else {
+                db.entries.sort(sortFunctions.popular);
+              }
+            }
+          }
+        });
+      }
+    });
+
     self.reloading = ko.observable(false);
     self.reloading = ko.observable(false);
 
 
     self.loadingTables = ko.pureComputed(function() {
     self.loadingTables = ko.pureComputed(function() {
@@ -178,23 +206,6 @@ var AssistDbSource = (function () {
         return database;
         return database;
       });
       });
 
 
-      if (HAS_OPTIMIZER && dbs.length > 0) {
-        dbs.forEach(function (db) {
-          self.apiHelper.fetchNavOptTopTables({
-            sourceType: self.sourceType,
-            database: db.definition.name,
-            silenceErrors: true,
-            successCallback: function (data) {
-              var popularityIndex = {};
-              data.top_tables.forEach(function (topTable) {
-                popularityIndex[topTable.name] = topTable.popularity;
-              });
-              db.setPopularityIndex(popularityIndex);
-            }
-          });
-        });
-      }
-
       dbs.sort(sortFunctions[self.activeSort()]);
       dbs.sort(sortFunctions[self.activeSort()]);
       self.databases(dbs);
       self.databases(dbs);
       self.reloading(false);
       self.reloading(false);