Bläddra i källkod

HUE-8916 [assist] Make the functions assist panel depend on cluster config

Johan Ahlen 6 år sedan
förälder
incheckning
ac18163f41

+ 6 - 2
desktop/core/src/desktop/js/apps/notebook/editorViewModel.js

@@ -306,9 +306,13 @@ class EditorViewModel {
 
     huePubSub.subscribe(
       'get.active.snippet.type',
-      () => {
+      callback => {
         withActiveSnippet(activeSnippet => {
-          huePubSub.publish('set.active.snippet.type', activeSnippet.type());
+          if (callback) {
+            callback(activeSnippet.type());
+          } else {
+            huePubSub.publish('set.active.snippet.type', activeSnippet.type());
+          }
         });
       },
       self.huePubSubId

+ 6 - 2
desktop/core/src/desktop/js/apps/notebook2/editorViewModel.js

@@ -207,9 +207,13 @@ class EditorViewModel {
 
     huePubSub.subscribe(
       'get.active.snippet.type',
-      () => {
+      callback => {
         withActiveSnippet(activeSnippet => {
-          huePubSub.publish('set.active.snippet.type', activeSnippet.type());
+          if (callback) {
+            callback(activeSnippet.type());
+          } else {
+            huePubSub.publish('set.active.snippet.type', activeSnippet.type());
+          }
         });
       },
       self.huePubSubId

+ 47 - 21
desktop/core/src/desktop/js/ko/components/assist/ko.assistFunctionsPanel.js

@@ -84,17 +84,12 @@ class AssistFunctionsPanel {
     this.disposals = [];
 
     this.activeType = ko.observable();
-    this.availableTypes = ko.observableArray(
-      window.IS_EMBEDDED ? ['Impala'] : ['Hive', 'Impala', 'Pig']
-    );
+    this.availableTypes = ko.observableArray();
+
     this.query = ko.observable().extend({ rateLimit: 400 });
     this.selectedFunction = ko.observable();
 
-    this.availableTypes().forEach(type => {
-      this.initFunctions(type);
-    });
-
-    const selectedFunctionPerType = { Hive: null, Impala: null, Pig: null };
+    const selectedFunctionPerType = {};
     this.selectedFunction.subscribe(newFunction => {
       if (newFunction) {
         selectedFunctionPerType[this.activeType()] = newFunction;
@@ -150,18 +145,13 @@ class AssistFunctionsPanel {
     });
 
     this.activeType.subscribe(newType => {
-      this.selectedFunction(selectedFunctionPerType[newType]);
-      this.activeCategories(this.categories[newType]);
-      apiHelper.setInTotalStorage('assist', 'function.panel.active.type', newType);
+      if (newType) {
+        this.selectedFunction(selectedFunctionPerType[newType]);
+        this.activeCategories(this.categories[newType]);
+        apiHelper.setInTotalStorage('assist', 'function.panel.active.type', newType);
+      }
     });
 
-    const lastActiveType = apiHelper.getFromTotalStorage(
-      'assist',
-      'function.panel.active.type',
-      this.availableTypes()[0]
-    );
-    this.activeType(lastActiveType);
-
     const updateType = type => {
       this.availableTypes().every(availableType => {
         if (availableType.toLowerCase() === type) {
@@ -178,12 +168,48 @@ class AssistFunctionsPanel {
       updateType(details.type);
     });
 
+    const configSub = huePubSub.subscribe('cluster.config.set.config', clusterConfig => {
+      const lastActiveType =
+        this.activeType() || apiHelper.getFromTotalStorage('assist', 'function.panel.active.type');
+      if (
+        clusterConfig.app_config &&
+        clusterConfig.app_config.editor &&
+        clusterConfig.app_config.editor.interpreters
+      ) {
+        const typesIndex = {};
+        clusterConfig.app_config.editor.interpreters.forEach(interpreter => {
+          if (
+            interpreter.type === 'hive' ||
+            interpreter.type === 'impala' ||
+            interpreter.type === 'pig'
+          ) {
+            typesIndex[interpreter.type] = true;
+          }
+        });
+        this.availableTypes(Object.keys(typesIndex));
+
+        this.availableTypes().forEach(type => {
+          this.initFunctions(type);
+        });
+
+        if (lastActiveType && typesIndex[lastActiveType]) {
+          this.activeType(lastActiveType);
+        } else {
+          this.activeType(this.availableTypes().length ? this.availableTypes[0] : undefined);
+        }
+      } else {
+        this.availableTypes([]);
+      }
+    });
+
+    huePubSub.publish('cluster.config.get.config');
+
     this.disposals.push(() => {
       activeSnippetTypeSub.remove();
+      configSub.remove();
     });
 
-    huePubSub.subscribeOnce('set.active.snippet.type', updateType);
-    huePubSub.publish('get.active.snippet.type');
+    huePubSub.publish('get.active.snippet.type', updateType);
   }
 
   dispose() {
@@ -195,7 +221,7 @@ class AssistFunctionsPanel {
   initFunctions(dialect) {
     this.categories[dialect] = [];
     const functions =
-      dialect === 'Pig'
+      dialect === 'pig'
         ? PigFunctions.CATEGORIZED_FUNCTIONS
         : SqlFunctions.CATEGORIZED_FUNCTIONS[dialect.toLowerCase()];