Przeglądaj źródła

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

Johan Ahlen 6 lat temu
rodzic
commit
01d61c8a53

+ 4 - 2
desktop/core/src/desktop/js/ko/components/assist/ko.assistFunctionsPanel.js

@@ -186,7 +186,7 @@ class AssistFunctionsPanel {
             typesIndex[interpreter.type] = true;
           }
         });
-        this.availableTypes(Object.keys(typesIndex));
+        this.availableTypes(Object.keys(typesIndex).sort());
 
         this.availableTypes().forEach(type => {
           this.initFunctions(type);
@@ -195,7 +195,7 @@ class AssistFunctionsPanel {
         if (lastActiveType && typesIndex[lastActiveType]) {
           this.activeType(lastActiveType);
         } else {
-          this.activeType(this.availableTypes().length ? this.availableTypes[0] : undefined);
+          this.activeType(this.availableTypes().length ? this.availableTypes()[0] : undefined);
         }
       } else {
         this.availableTypes([]);
@@ -249,3 +249,5 @@ class AssistFunctionsPanel {
 }
 
 componentUtils.registerStaticComponent('assist-functions-panel', AssistFunctionsPanel, TEMPLATE);
+
+export default AssistFunctionsPanel;

+ 39 - 6
desktop/core/src/desktop/js/ko/components/assist/ko.assistLangRefPanel.js

@@ -121,8 +121,9 @@ class LanguageReferenceTopic {
 class AssistLangRefPanel {
   constructor(params, element) {
     this.disposals = [];
-    this.availableTypes = ['impala', 'hive'];
-    this.sourceType = ko.observable('hive');
+    this.availableTypes = ko.observableArray();
+    this.sourceType = ko.observable();
+
     this.allTopics = {
       impala: [],
       hive: []
@@ -136,7 +137,7 @@ class AssistLangRefPanel {
     });
 
     const updateType = type => {
-      if (this.availableTypes.indexOf(type) !== -1) {
+      if (this.availableTypes().indexOf(type) !== -1) {
         this.sourceType(type);
       }
     };
@@ -144,14 +145,44 @@ class AssistLangRefPanel {
     const activeSnippetTypeSub = huePubSub.subscribe('active.snippet.type.changed', details => {
       updateType(details.type);
     });
+
+    const configSub = huePubSub.subscribe('cluster.config.set.config', clusterConfig => {
+      const lastActiveType = this.sourceType();
+      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') {
+            typesIndex[interpreter.type] = true;
+          }
+        });
+        this.availableTypes(Object.keys(typesIndex).sort());
+
+        if (lastActiveType && typesIndex[lastActiveType]) {
+          this.sourceType(lastActiveType);
+        } else {
+          this.sourceType(this.availableTypes().length ? this.availableTypes()[0] : undefined);
+        }
+      } else {
+        this.availableTypes([]);
+      }
+    });
+
+    huePubSub.publish('cluster.config.get.config');
+
     this.disposals.push(() => {
+      configSub.remove();
       activeSnippetTypeSub.remove();
     });
 
-    huePubSub.subscribeOnce('set.active.snippet.type', updateType);
-    huePubSub.publish('get.active.snippet.type');
+    huePubSub.publish('get.active.snippet.type', updateType);
 
-    this.topics = ko.pureComputed(() => this.allTopics[this.sourceType()]);
+    this.topics = ko.pureComputed(() => {
+      return this.sourceType() ? this.allTopics[this.sourceType()] : [];
+    });
 
     this.selectedTopic = ko.observable();
 
@@ -328,3 +359,5 @@ componentUtils.registerStaticComponent(
   },
   TEMPLATE
 );
+
+export default AssistLangRefPanel;

+ 65 - 0
desktop/core/src/desktop/js/ko/components/assist/spec/ko.assistFunctionsPanelSpec.js

@@ -0,0 +1,65 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// 'License'); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import huePubSub from 'utils/huePubSub';
+import AssistFunctionsPanel from '../ko.assistFunctionsPanel';
+
+describe('ko.assistFunctionsPanel.js', () => {
+  beforeAll(() => {});
+
+  it('should handle cluster config updates', () => {
+    let clusterConfigGetCalled = false;
+    const configSub = huePubSub.subscribe('cluster.config.get.config', () => {
+      clusterConfigGetCalled = true;
+      huePubSub.publish('cluster.config.set.config', {
+        app_config: {
+          editor: {
+            interpreters: [{ type: 'pig' }, { type: 'pig' }, { type: 'impala' }, { type: 'banana' }]
+          }
+        }
+      });
+    });
+    const subject = new AssistFunctionsPanel();
+
+    expect(clusterConfigGetCalled).toBeTruthy();
+    expect(subject.availableTypes()).toEqual(['impala', 'pig']);
+
+    huePubSub.publish('cluster.config.set.config', {
+      app_config: {
+        editor: {
+          interpreters: [{ type: 'pig' }]
+        }
+      }
+    });
+
+    expect(subject.availableTypes()).toEqual(['pig']);
+    expect(subject.activeType()).toEqual('pig');
+
+    huePubSub.publish('cluster.config.set.config', {
+      app_config: {
+        editor: {
+          interpreters: [{ type: 'banana' }]
+        }
+      }
+    });
+
+    expect(subject.availableTypes()).toEqual([]);
+    expect(subject.activeType()).toBeFalsy();
+
+    configSub.remove();
+    subject.dispose();
+  });
+});

+ 68 - 0
desktop/core/src/desktop/js/ko/components/assist/spec/ko.assistLangRefPanelSpec.js

@@ -0,0 +1,68 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// 'License'); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import huePubSub from 'utils/huePubSub';
+import AssistLangRefPanel from '../ko.assistLangRefPanel';
+
+describe('ko.assistLangRefPanel.js', () => {
+  beforeAll(() => {
+    window.IMPALA_DOC_TOP_LEVEL = [];
+    window.HIVE_DOC_TOP_LEVEL = [];
+  });
+
+  it('should handle cluster config updates', () => {
+    let clusterConfigGetCalled = false;
+    const configSub = huePubSub.subscribe('cluster.config.get.config', () => {
+      clusterConfigGetCalled = true;
+      huePubSub.publish('cluster.config.set.config', {
+        app_config: {
+          editor: {
+            interpreters: [{ type: 'hive' }, { type: 'impala' }, { type: 'banana' }]
+          }
+        }
+      });
+    });
+    const subject = new AssistLangRefPanel();
+
+    expect(clusterConfigGetCalled).toBeTruthy();
+    expect(subject.availableTypes()).toEqual(['hive', 'impala']);
+
+    huePubSub.publish('cluster.config.set.config', {
+      app_config: {
+        editor: {
+          interpreters: [{ type: 'impala' }]
+        }
+      }
+    });
+
+    expect(subject.availableTypes()).toEqual(['impala']);
+    expect(subject.sourceType()).toEqual('impala');
+
+    huePubSub.publish('cluster.config.set.config', {
+      app_config: {
+        editor: {
+          interpreters: [{ type: 'banana' }]
+        }
+      }
+    });
+
+    expect(subject.availableTypes()).toEqual([]);
+    expect(subject.sourceType()).toBeFalsy();
+
+    configSub.remove();
+    subject.dispose();
+  });
+});