Răsfoiți Sursa

HUE-8330 [assist] Make sure namespaces are refreshed throughout on assist refresh

Johan Ahlen 7 ani în urmă
părinte
comite
c4070720bb

+ 62 - 41
apps/metastore/src/metastore/static/metastore/js/metastore.model.js

@@ -51,63 +51,84 @@ var MetastoreSource = (function () {
       });
     });
 
-    huePubSub.subscribe('data.catalog.entry.refreshed', function (details) {
-      var refreshedEntry = details.entry;
-
-      if (refreshedEntry.getSourceType() !== self.type) {
-        return;
-      }
-
+    var getCurrentState = function () {
+      var result = {
+        namespaceId: null,
+        database: null,
+        table: null
+      };
       var prevNamespaceId = null;
       var prevDbName = null;
       var prevTableName = null;
       if (self.namespace()) {
-        prevNamespaceId = self.namespace().id;
+        result.namespaceId = self.namespace().id;
         if (self.namespace().database()) {
-          prevDbName = self.namespace().database().catalogEntry.name;
+          result.database = self.namespace().database().catalogEntry.name;
           if (self.namespace().database().table()) {
-            prevTableName = self.namespace().database().table().catalogEntry.name;
+            result.table = self.namespace().database().table().catalogEntry.name;
           }
-
         }
       }
+      return result;
+    };
 
-      var setPrevious = function () {
-        if (prevNamespaceId) {
-          self.setNamespaceById(prevNamespaceId).done(function () {
-            if (prevDbName) {
-              self.namespace().setDatabaseByName(prevDbName, function () {
-                if (self.namespace().database() && prevTableName) {
-                  self.namespace().database().setTableByName(prevTableName);
-                }
-              });
-            }
-          });
-        }
-      };
-
-      var completeRefresh = function () {
-        self.reloading(true);
-        if (self.namespace() && self.namespace().database() && self.namespace().database().table()) {
-          self.namespace().database().table(null);
-        }
-        if (self.namespace() && self.namespace().database()) {
-          self.namespace().database(null);
-        }
-        if (self.namespace()) {
-          self.namespace(null);
-        }
-        self.loadNamespaces().done(setPrevious).always(function () {
-          self.reloading(false);
+    var setState = function (state) {
+      if (state.namespaceId) {
+        self.setNamespaceById(state.namespaceId).done(function () {
+          if (state.database) {
+            self.namespace().setDatabaseByName(state.database, function () {
+              if (self.namespace().database() && state.table) {
+                self.namespace().database().setTableByName(state.table);
+              }
+            });
+          }
         });
-      };
+      }
+    };
+
+    var completeRefresh = function (previousState) {
+      self.reloading(true);
+      if (self.namespace() && self.namespace().database() && self.namespace().database().table()) {
+        self.namespace().database().table(null);
+      }
+      if (self.namespace() && self.namespace().database()) {
+        self.namespace().database(null);
+      }
+      if (self.namespace()) {
+        self.namespace(null);
+      }
+      self.loadNamespaces().done(function () {
+        setState(previousState);
+      }).always(function () {
+        self.reloading(false);
+      });
+    };
+
+    huePubSub.subscribe('context.catalog.namespaces.refreshed', function (sourceType) {
+      if (self.type !== sourceType) {
+        return;
+      }
+      var previousState = getCurrentState();
+      completeRefresh(previousState);
+    });
+
+    huePubSub.subscribe('data.catalog.entry.refreshed', function (details) {
+      var refreshedEntry = details.entry;
+
+      if (refreshedEntry.getSourceType() !== self.type) {
+        return;
+      }
+
+      var previousState = getCurrentState();
 
       if (refreshedEntry.isSource()) {
-        completeRefresh();
+        completeRefresh(previousState);
       } else if (refreshedEntry.isDatabase() && self.namespace()) {
         self.namespace().databases().some(function (database) {
           if (database.catalogEntry === refreshedEntry) {
-            database.load(setPrevious, self.optimizerEnabled(), self.navigatorEnabled());
+            database.load(function () {
+              setState(previousState);
+            }, self.optimizerEnabled(), self.navigatorEnabled());
             return true;
           }
         })

+ 34 - 26
desktop/core/src/desktop/static/desktop/js/assist/assistDbSource.js

@@ -74,6 +74,39 @@ var AssistDbSource = (function () {
     self.hasNamespaces = ko.pureComputed(function () {
       return self.namespaces().length > 0;
     });
+
+    huePubSub.subscribe('context.catalog.namespaces.refreshed', function (sourceType) {
+      if (self.sourceType !== sourceType) {
+        return;
+      }
+
+      self.loading(true);
+      ContextCatalog.getNamespaces({ sourceType: self.sourceType }).done(function (context) {
+        var newNamespaces = [];
+        var existingNamespaceIndex = {};
+        self.namespaceRefreshEnabled(context.dynamic);
+        self.namespaces().forEach(function (assistNamespace) {
+          existingNamespaceIndex[assistNamespace.namespace.id] = assistNamespace;
+        });
+        context.namespaces.forEach(function(newNamespace) {
+          if (existingNamespaceIndex[newNamespace.id]) {
+            existingNamespaceIndex[newNamespace.id].namespace = newNamespace;
+            existingNamespaceIndex[newNamespace.id].name = newNamespace.name;
+            newNamespaces.push(existingNamespaceIndex[newNamespace.id]);
+          } else {
+            newNamespaces.push(new AssistDbNamespace({
+              sourceType: self.sourceType,
+              namespace: newNamespace,
+              i18n: self.i18n,
+              navigationSettings: self.navigationSettings
+            }));
+          }
+        });
+        self.namespaces(newNamespaces);
+      }).always(function () {
+        self.loading(false);
+      })
+    });
   }
 
   AssistDbSource.prototype.whenLoaded = function (callback) {
@@ -123,32 +156,7 @@ var AssistDbSource = (function () {
 
   AssistDbSource.prototype.triggerRefresh = function (data, event) {
     var self = this;
-    self.loading(true);
-    ContextCatalog.getNamespaces({ sourceType: self.sourceType, clearCache: true }).done(function (context) {
-      var newNamespaces = [];
-      var existingNamespaceIndex = {};
-      self.namespaceRefreshEnabled(context.dynamic);
-      self.namespaces().forEach(function (assistNamespace) {
-        existingNamespaceIndex[assistNamespace.namespace.id] = assistNamespace;
-      });
-      context.namespaces.forEach(function(newNamespace) {
-        if (existingNamespaceIndex[newNamespace.id]) {
-          existingNamespaceIndex[newNamespace.id].namespace = newNamespace;
-          existingNamespaceIndex[newNamespace.id].name = newNamespace.name;
-          newNamespaces.push(existingNamespaceIndex[newNamespace.id]);
-        } else {
-          newNamespaces.push(new AssistDbNamespace({
-            sourceType: self.sourceType,
-            namespace: newNamespace,
-            i18n: self.i18n,
-            navigationSettings: self.navigationSettings
-          }));
-        }
-      });
-      self.namespaces(newNamespaces);
-    }).always(function () {
-      self.loading(false);
-    })
+    ContextCatalog.getNamespaces({ sourceType: self.sourceType, clearCache: true });
   };
 
   return AssistDbSource;

+ 11 - 6
desktop/core/src/desktop/static/desktop/js/contextCatalog.js

@@ -81,6 +81,7 @@ var ContextCatalog = (function () {
     ContextCatalog.prototype.getNamespaces = function (options) {
       var self = this;
 
+      var notifyForRefresh = self.namespacePromises[options.sourceType] && options.clearCache;
       if (options.clearCache) {
         self.namespacePromises[options.sourceType] = undefined;
         self.namespaces[options.sourceType] = undefined;
@@ -103,11 +104,15 @@ var ContextCatalog = (function () {
         ApiHelper.getInstance().fetchContextNamespaces(options).done(function (namespaces) {
           if (namespaces[options.sourceType]) {
             var namespaces = namespaces[options.sourceType];
-            var dynamic = namespaces.hasMultiCluster;
+            var dynamic = true; //namespaces.hasMultiCluster;
             if (namespaces) {
-              self.namespaces[self.sourceType] = { namespaces: namespaces, dynamic: dynamic };
-              deferred.resolve(self.namespaces[self.sourceType]);
-              self.saveLater(NAMESPACES_CONTEXT_TYPE, options.sourceType, self.namespaces[self.sourceType]);
+              self.namespaces[options.sourceType] = { namespaces: namespaces, dynamic: dynamic };
+              deferred.resolve(self.namespaces[options.sourceType]);
+              if (notifyForRefresh) {
+                huePubSub.publish('context.catalog.namespaces.refreshed', options.sourceType);
+              }
+
+              self.saveLater(NAMESPACES_CONTEXT_TYPE, options.sourceType, self.namespaces[options.sourceType]);
             } else {
               deferred.reject();
             }
@@ -154,8 +159,8 @@ var ContextCatalog = (function () {
         if (computes[options.sourceType]) {
           var computes = computes[options.sourceType];
           if (computes) {
-            self.computes[self.sourceType] = computes;
-            deferred.resolve(self.computes[self.sourceType])
+            self.computes[options.sourceType] = computes;
+            deferred.resolve(self.computes[options.sourceType])
             // TODO: save
           } else {
             deferred.reject();

+ 57 - 19
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -356,6 +356,7 @@ var EditorViewModel = (function() {
       return ApiHelper.getInstance(vm);
     };
 
+    self.namespaceRefreshEnabled = ko.observable(false);
     self.availableNamespaces = ko.observableArray();
     self.namespace = ko.observable();
     self.availableComputes = ko.observableArray();
@@ -373,16 +374,22 @@ var EditorViewModel = (function() {
       }
     });
 
-    var namespacesPromise = ContextCatalog.getNamespaces({ sourceType: self.type() }).done(function (context) {
-      self.availableNamespaces(context.namespaces);
-      if (!snippet.namespace || !context.namespaces.some(function (namespace) {
-        if (namespace.id === snippet.namespace.id) {
-          self.namespace(namespace);
-          return true;
-        }})) {
-        self.namespace(context.namespaces[0]);
-      }
-    });
+    var namespacesPromise;
+    self.reloadNamespaces = function () {
+      namespacesPromise = ContextCatalog.getNamespaces({ sourceType: self.type() }).done(function (context) {
+        self.namespaceRefreshEnabled(context.dynamic);
+        self.availableNamespaces(context.namespaces);
+        if (!snippet.namespace || !context.namespaces.some(function (namespace) {
+          if (namespace.id === snippet.namespace.id) {
+            self.namespace(namespace);
+            return true;
+          }})) {
+          self.namespace(context.namespaces[0]);
+        }
+      });
+    };
+
+    self.reloadNamespaces();
 
     self.database = ko.observable();
     var previousDatabase = null;
@@ -2938,20 +2945,43 @@ var EditorViewModel = (function() {
 
     huePubSub.subscribeOnce('assist.db.panel.ready', function () {
       if (self.type().indexOf('query') === 0) {
-        if (self.snippets().length === 1) {
+
+        var whenDatabaseAvailable = function (snippet) {
           huePubSub.publish('assist.set.database', {
-            source: self.snippets()[0].type(),
-            namespace: self.snippets()[0].namespace(),
-            name: self.snippets()[0].database()
+            source: snippet.type(),
+            namespace: snippet.namespace(),
+            name: snippet.database()
           });
+        };
+
+        var whenNamespaceAvailable = function (snippet) {
+          if (snippet.database()) {
+            whenDatabaseAvailable(snippet);
+          } else {
+            var databaseSub = snippet.database.subscribe(function () {
+              databaseSub.dispose();
+              whenDatabaseAvailable(snippet);
+            })
+          }
+        };
+
+        var whenSnippetAvailable = function (snippet) {
+          if (snippet.namespace()) {
+            whenNamespaceAvailable(snippet);
+          } else {
+            var namespaceSub = snippet.namespace.subscribe(function () {
+              namespaceSub.dispose();
+              whenNamespaceAvailable(snippet);
+            })
+          }
+        };
+
+        if (self.snippets().length === 1) {
+          whenSnippetAvailable(self.snippets()[0]);
         } else {
           var snippetsSub = self.snippets.subscribe(function (snippets) {
             if (snippets.length === 1) {
-              huePubSub.publish('assist.set.database', {
-                source: self.snippets()[0].type(),
-                namespace: self.snippets()[0].namespace(),
-                name: self.snippets()[0].database()
-              });
+              whenSnippetAvailable(snippets[0])
             }
             snippetsSub.dispose();
           })
@@ -3219,6 +3249,14 @@ var EditorViewModel = (function() {
       })
     }, self.huePubSubId);
 
+    huePubSub.subscribe('context.catalog.namespaces.refreshed', function (sourceType) {
+      self.selectedNotebook().snippets().forEach(function (snippet) {
+        if (snippet.type() === sourceType) {
+          snippet.reloadNamespaces();
+        }
+      })
+    });
+
     huePubSub.subscribe('data.catalog.entry.refreshed', function (details) {
       var notebook = self.selectedNotebook();
       if (details.entry.isSource() && notebook) {

+ 2 - 2
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -941,11 +941,11 @@ ${ sqlSyntaxDropdown.sqlSyntaxDropdown() }
     <!-- ko template: { name: 'longer-operation${ suffix }' } --><!-- /ko -->
     <span class="execution-timer" data-bind="visible: type() != 'text' && status() != 'ready' && status() != 'loading', text: result.executionTime().toHHMMSS()" title="${ _('Execution time') }"></span>
 
-    <!-- ko if: availableNamespaces().length > 1 -->
+    <!-- ko if: availableNamespaces().length > 1 || namespaceRefreshEnabled() -->
     <span class="editor-header-title">${ _('Namespace') }</span>
     <div data-bind="component: { name: 'hue-drop-down', params: { value: namespace, entries: availableNamespaces, labelAttribute: 'name', searchable: true, linkTitle: '${ _ko('Active namespace') }' } }" style="display: inline-block"></div>
     <!-- /ko -->
-    <!-- ko if: availableComputes().length > 1 -->
+    <!-- ko if: availableComputes().length > 1 || namespaceRefreshEnabled() -->
     <span class="editor-header-title">${ _('Compute') }</span>
     <div data-bind="component: { name: 'hue-drop-down', params: { value: compute, entries: availableComputes, labelAttribute: 'name', searchable: true, linkTitle: '${ _ko('Active compute') }' } }" style="display: inline-block"></div>
     <!-- /ko -->