Răsfoiți Sursa

HUE-8758 [connectors] Track connector changes instead of dialect changes

Johan Ahlen 5 ani în urmă
părinte
comite
f689920fcb

+ 2 - 5
desktop/core/src/desktop/js/apps/notebook/app.js

@@ -27,7 +27,7 @@ import hueUtils from 'utils/hueUtils';
 import I18n from 'utils/i18n';
 import sqlWorkerHandler from 'sql/sqlWorkerHandler';
 import { initNotebook2 } from 'apps/notebook2/app';
-import { ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT } from 'apps/notebook2/events';
+import { ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT } from 'apps/notebook2/events';
 
 if (window.ENABLE_NOTEBOOK_2) {
   initNotebook2();
@@ -926,10 +926,7 @@ if (window.ENABLE_NOTEBOOK_2) {
           if (app === 'editor') {
             huePubSub.publish('redraw.fixed.headers');
             huePubSub.publish('hue.scrollleft.show');
-            huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-              dialect: viewModel.editorType(),
-              isSqlDialect: viewModel.getSnippetViewSettings(viewModel.editorType()).sqlDialect
-            });
+            huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, viewModel.activeConnector());
           }
         },
         HUE_PUB_SUB_EDITOR_ID

+ 68 - 50
desktop/core/src/desktop/js/apps/notebook/editorViewModel.js

@@ -26,9 +26,10 @@ import hueUtils from 'utils/hueUtils';
 import Notebook from 'apps/notebook/notebook';
 import Snippet from 'apps/notebook/snippet';
 import {
-  ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-  GET_ACTIVE_SNIPPET_DIALECT_EVENT
+  ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+  GET_ACTIVE_SNIPPET_CONNECTOR_EVENT
 } from 'apps/notebook2/events';
+import { findConnector } from 'utils/hueConfig';
 
 class EditorViewModel {
   constructor(editor_id, notebooks, options, CoordinatorEditorViewModel, RunningCoordinatorModel) {
@@ -47,13 +48,30 @@ class EditorViewModel {
     self.isMobile = ko.observable(options.mobile);
     self.isNotificationManager = ko.observable(options.is_notification_manager || false);
     self.editorType = ko.observable(options.editor_type);
+    self.activeConnector = ko.observable();
+
+    const updateConnector = type => {
+      if (type) {
+        findConnector(connector => connector.type === type).then(connector => {
+          self.activeConnector(connector);
+        });
+      }
+    };
+
+    updateConnector(self.editorType());
+
     self.editorType.subscribe(newVal => {
-      self.editorMode(newVal != 'notebook');
+      if (!this.activeConnector() || this.activeConnector().type !== newVal) {
+        updateConnector(newVal);
+      }
+
+      self.editorMode(newVal !== 'notebook');
       hueUtils.changeURLParameter('type', newVal);
       if (self.editorMode()) {
         self.selectedNotebook().fetchHistory(); // Js error if notebook did not have snippets
       }
     });
+
     self.preEditorTogglingSnippet = ko.observable();
     self.toggleEditorMode = function() {
       const notebook = self.selectedNotebook();
@@ -303,10 +321,10 @@ class EditorViewModel {
     });
 
     huePubSub.subscribe(
-      GET_ACTIVE_SNIPPET_DIALECT_EVENT,
+      GET_ACTIVE_SNIPPET_CONNECTOR_EVENT,
       callback => {
         withActiveSnippet(activeSnippet => {
-          callback(activeSnippet.type()); // Dialect = type in editor v1
+          callback(activeSnippet.connector());
         });
       },
       self.huePubSubId
@@ -566,10 +584,7 @@ class EditorViewModel {
               if (self.editorMode()) {
                 self.editorType(data.document.type.substring('query-'.length));
                 if (!self.isNotificationManager()) {
-                  huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-                    dialect: self.editorType(),
-                    isSqlDialect: self.getSnippetViewSettings(self.editorType()).sqlDialect
-                  });
+                  huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, self.activeConnector());
                 }
                 self.changeURL(
                   self.URLS.editor + '?editor=' + data.document.id + '&type=' + self.editorType()
@@ -593,51 +608,54 @@ class EditorViewModel {
     };
 
     self.newNotebook = function(editorType, callback, queryTab) {
-      if (!self.isNotificationManager()) {
-        huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-          dialect: editorType,
-          isSqlDialect: editorType ? self.getSnippetViewSettings(editorType).sqlDialect : undefined
-        });
-      }
-      $.post(
-        '/notebook/api/create_notebook',
-        {
-          type: editorType || options.editor_type,
-          directory_uuid: window.location.getParameter('directory_uuid'),
-          gist: self.isNotificationManager() ? undefined : window.location.getParameter('gist')
-        },
-        data => {
-          self.loadNotebook(data.notebook);
-          if (self.editorMode() && !self.isNotificationManager()) {
-            const snippet =
-              self.selectedNotebook().snippets().length == 0
-                ? self.selectedNotebook().newSnippet(self.editorType())
-                : self.selectedNotebook().snippets()[0];
-            if (
-              queryTab &&
-              ['queryHistory', 'savedQueries', 'queryBuilderTab'].indexOf(queryTab) > -1
-            ) {
-              snippet.currentQueryTab(queryTab);
-            }
-            huePubSub.publish('detach.scrolls', self.selectedNotebook().snippets()[0]);
-            if (window.location.getParameter('type') === '') {
-              hueUtils.changeURLParameter('type', self.editorType());
+      const type = editorType || options.editor_type;
+
+      const create = () => {
+        $.post(
+          '/notebook/api/create_notebook',
+          {
+            type: type,
+            directory_uuid: window.location.getParameter('directory_uuid'),
+            gist: self.isNotificationManager() ? undefined : window.location.getParameter('gist')
+          },
+          data => {
+            self.loadNotebook(data.notebook);
+            if (self.editorMode() && !self.isNotificationManager()) {
+              const snippet =
+                self.selectedNotebook().snippets().length == 0
+                  ? self.selectedNotebook().newSnippet(self.editorType())
+                  : self.selectedNotebook().snippets()[0];
+              if (
+                queryTab &&
+                ['queryHistory', 'savedQueries', 'queryBuilderTab'].indexOf(queryTab) > -1
+              ) {
+                snippet.currentQueryTab(queryTab);
+              }
+              huePubSub.publish('detach.scrolls', self.selectedNotebook().snippets()[0]);
+              if (window.location.getParameter('type') === '') {
+                hueUtils.changeURLParameter('type', self.editorType());
+              }
+              if (!self.isNotificationManager()) {
+                huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, self.activeConnector());
+              }
             }
-            if (!self.isNotificationManager()) {
-              huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-                dialect: editorType,
-                isSqlDialect: editorType
-                  ? self.getSnippetViewSettings(editorType).sqlDialect
-                  : undefined
-              });
+
+            if (callback) {
+              callback();
             }
           }
+        );
+      };
 
-          if (callback) {
-            callback();
-          }
-        }
-      );
+      if (!self.isNotificationManager()) {
+        findConnector(connector => connector.type === type).then(connector => {
+          self.activeConnector(connector);
+          huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, self.activeConnector());
+          create();
+        });
+      } else {
+        create();
+      }
     };
 
     self.saveNotebook = function() {

+ 28 - 14
desktop/core/src/desktop/js/apps/notebook/snippet.js

@@ -30,7 +30,8 @@ import Session from 'apps/notebook/session';
 import sqlStatementsParser from 'parse/sqlStatementsParser';
 import { SHOW_EVENT as SHOW_GIST_MODAL_EVENT } from 'ko/components/ko.shareGistModal';
 import { cancelActiveRequest } from 'api/apiUtils';
-import { ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT } from 'apps/notebook2/events';
+import { ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT } from 'apps/notebook2/events';
+import { findConnector } from 'utils/hueConfig';
 
 const NOTEBOOK_MAPPING = {
   ignore: [
@@ -176,16 +177,36 @@ class Snippet {
     self.type = ko.observable(
       typeof snippet.type != 'undefined' && snippet.type != null ? snippet.type : 'hive'
     );
-    self.type.subscribe(newVal => {
+
+    self.connector = ko.observable();
+
+    const updateConnector = type => {
+      if (type) {
+        findConnector(connector => connector.type === type).then(self.connector);
+      }
+    };
+
+    updateConnector(self.type());
+
+    self.type.subscribe(type => {
+      if (!self.connector() || self.connector().type !== type) {
+        updateConnector(type);
+      }
       self.status('ready');
     });
 
+    self.isSqlDialect = ko.pureComputed(() => {
+      return vm.getSnippetViewSettings(self.type()).sqlDialect;
+    });
+
     self.connector = ko.pureComputed(() => {
       // To support optimizer changes in editor v2
-      if (self.type() === 'hive' || self.type() === 'impala') {
-        return { optimizer: 'api', type: self.type() };
-      }
-      return {};
+      return {
+        optimizer: self.type() === 'hive' || self.type() === 'impala' ? 'api' : 'off',
+        type: self.type(),
+        dialect: self.type(),
+        is_sql: self.isSqlDialect()
+      };
     });
 
     self.isBatchable = ko.computed(() => {
@@ -236,10 +257,7 @@ class Snippet {
 
     self.inFocus.subscribe(newValue => {
       if (newValue) {
-        huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-          dialect: self.type(),
-          isSqlDialect: self.isSqlDialect()
-        });
+        huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, self.connector());
       }
     });
 
@@ -253,10 +271,6 @@ class Snippet {
 
     self.showExecutionAnalysis = ko.observable(false);
 
-    self.isSqlDialect = ko.pureComputed(() => {
-      return vm.getSnippetViewSettings(self.type()).sqlDialect;
-    });
-
     self.getPlaceHolder = function() {
       return vm.getSnippetViewSettings(self.type()).placeHolder;
     };

+ 7 - 13
desktop/core/src/desktop/js/apps/notebook2/editorViewModel.js

@@ -25,8 +25,8 @@ import hueUtils from 'utils/hueUtils';
 
 import Notebook from 'apps/notebook2/notebook';
 import {
-  ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-  GET_ACTIVE_SNIPPET_DIALECT_EVENT
+  ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+  GET_ACTIVE_SNIPPET_CONNECTOR_EVENT
 } from 'apps/notebook2/events';
 import { CONFIG_REFRESHED_EVENT, GET_KNOWN_CONFIG_EVENT, findConnector } from 'utils/hueConfig';
 
@@ -158,10 +158,10 @@ class EditorViewModel {
     });
 
     huePubSub.subscribe(
-      GET_ACTIVE_SNIPPET_DIALECT_EVENT,
+      GET_ACTIVE_SNIPPET_CONNECTOR_EVENT,
       callback => {
         this.withActiveSnippet(activeSnippet => {
-          callback(activeSnippet.dialect());
+          callback(activeSnippet.connector());
         });
       },
       this.huePubSubId
@@ -313,10 +313,7 @@ class EditorViewModel {
     if (!connector) {
       console.warn('No connector found for type ' + editorType);
     } else {
-      huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-        dialect: connector.dialect,
-        isSqlDialect: connector.is_sql
-      });
+      huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, connector);
     }
 
     return new Promise((resolve, reject) => {
@@ -383,12 +380,9 @@ class EditorViewModel {
     }
   }
 
-  notifyDialectChange(dialect, isSqlDialect) {
+  notifyDialectChange(dialect) {
     if (dialect && this.lastNotifiedDialect !== dialect) {
-      huePubSub.publish(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, {
-        dialect: dialect,
-        isSqlDialect: isSqlDialect
-      });
+      huePubSub.publish(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, this.activeConnector());
       this.lastNotifiedDialect = dialect;
     }
   }

+ 2 - 2
desktop/core/src/desktop/js/apps/notebook2/events.js

@@ -1,5 +1,5 @@
-export const ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT = 'active.snippet.dialect.changed';
-export const GET_ACTIVE_SNIPPET_DIALECT_EVENT = 'get.active.snippet.dialect';
+export const ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT = 'active.snippet.connector.changed';
+export const GET_ACTIVE_SNIPPET_CONNECTOR_EVENT = 'get.active.snippet.connector';
 export const REDRAW_CHART_EVENT = 'result.chart.redraw';
 export const HIDE_FIXED_HEADERS_EVENT = 'result.grid.hide.fixed.headers';
 export const REDRAW_FIXED_HEADERS_EVENT = 'result.grid.redraw.fixed.headers';

+ 3 - 3
desktop/core/src/desktop/js/ko/components/assist/ko.assistEditorContextPanel.js

@@ -24,7 +24,7 @@ import componentUtils from 'ko/components/componentUtils';
 import dataCatalog from 'catalog/dataCatalog';
 import huePubSub from 'utils/huePubSub';
 import I18n from 'utils/i18n';
-import { ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT } from 'apps/notebook2/events';
+import { ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT } from 'apps/notebook2/events';
 
 const TEMPLATE =
   ASSIST_TABLE_TEMPLATES +
@@ -210,8 +210,8 @@ class AssistEditorContextPanel {
         (this.sourceType() === 'impala' || this.sourceType() === 'hive')
     );
 
-    const typeSub = huePubSub.subscribe(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, details => {
-      this.sourceType(details.dialect);
+    const typeSub = huePubSub.subscribe(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, connector => {
+      this.sourceType(connector.dialect);
     });
 
     this.disposals.push(() => {

+ 8 - 6
desktop/core/src/desktop/js/ko/components/assist/ko.assistFunctionsPanel.js

@@ -23,8 +23,8 @@ import { PigFunctions, SqlFunctions } from 'sql/sqlFunctions';
 import I18n from 'utils/i18n';
 import { CONFIG_REFRESHED_EVENT, filterConnectors } from 'utils/hueConfig';
 import {
-  ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-  GET_ACTIVE_SNIPPET_DIALECT_EVENT
+  ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+  GET_ACTIVE_SNIPPET_CONNECTOR_EVENT
 } from 'apps/notebook2/events';
 
 // prettier-ignore
@@ -173,9 +173,9 @@ class AssistFunctionsPanel {
     };
 
     const activeSnippetDialectSub = huePubSub.subscribe(
-      ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-      details => {
-        updateDialect(details.dialect);
+      ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+      connector => {
+        updateDialect(connector.dialect);
       }
     );
 
@@ -212,7 +212,9 @@ class AssistFunctionsPanel {
           this.availableDialects().length ? this.availableDialects()[0] : undefined
         );
       }
-      huePubSub.publish(GET_ACTIVE_SNIPPET_DIALECT_EVENT, updateDialect);
+      huePubSub.publish(GET_ACTIVE_SNIPPET_CONNECTOR_EVENT, connector => {
+        updateDialect(connector.dialect);
+      });
     };
 
     configUpdated();

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

@@ -23,8 +23,8 @@ import I18n from 'utils/i18n';
 import { GET_KNOWN_CONFIG_EVENT, CONFIG_REFRESHED_EVENT, filterConnectors } from 'utils/hueConfig';
 import { simpleGet } from 'api/apiUtils';
 import {
-  ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-  GET_ACTIVE_SNIPPET_DIALECT_EVENT
+  ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+  GET_ACTIVE_SNIPPET_CONNECTOR_EVENT
 } from 'apps/notebook2/events';
 
 // prettier-ignore
@@ -150,9 +150,9 @@ class AssistLangRefPanel {
     };
 
     const activeSnippetDialectSub = huePubSub.subscribe(
-      ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT,
-      details => {
-        updateDialect(details.dialect);
+      ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+      connector => {
+        updateDialect(connector.dialect);
       }
     );
 
@@ -185,7 +185,9 @@ class AssistLangRefPanel {
       activeSnippetDialectSub.remove();
     });
 
-    huePubSub.publish(GET_ACTIVE_SNIPPET_DIALECT_EVENT, updateDialect);
+    huePubSub.publish(GET_ACTIVE_SNIPPET_CONNECTOR_EVENT, connector => {
+      updateDialect(connector.dialect);
+    });
 
     this.topics = ko.pureComputed(() => {
       return this.activeDialect() ? this.allTopics[this.activeDialect()] : [];

+ 10 - 7
desktop/core/src/desktop/js/ko/components/assist/ko.rightAssistPanel.js

@@ -20,7 +20,7 @@ import apiHelper from 'api/apiHelper';
 import componentUtils from 'ko/components/componentUtils';
 import huePubSub from 'utils/huePubSub';
 import I18n from 'utils/i18n';
-import { ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT } from 'apps/notebook2/events';
+import { ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT } from 'apps/notebook2/events';
 
 const EDITOR_ASSISTANT_TAB = 'editorAssistant';
 const DASHBOARD_ASSISTANT_TAB = 'dashboardAssistant';
@@ -56,7 +56,7 @@ const TEMPLATE = `
   <!-- ko if: visible -->
   <div class="right-assist-contents">
     <!-- ko if: editorAssistantTabAvailable-->
-    <div data-bind="component: { name: 'assist-editor-context-panel', params: { activeTab: activeTab, sourceType: sourceType } }, visible: activeTab() === 'editorAssistant'"></div>
+    <div data-bind="component: { name: 'assist-editor-context-panel', params: { activeTab: activeTab, sourceType: dialect } }, visible: activeTab() === 'editorAssistant'"></div>
     <!-- /ko -->
 
     <!-- ko if: functionsTabAvailable -->
@@ -82,7 +82,7 @@ class RightAssistPanel {
 
     this.activeTab = ko.observable();
     this.visible = params.visible;
-    this.sourceType = ko.observable();
+    this.dialect = ko.observable();
 
     this.editorAssistantTabAvailable = ko.observable(false);
     this.dashboardAssistantTabAvailable = ko.observable(false);
@@ -142,7 +142,7 @@ class RightAssistPanel {
     };
 
     const updateContentsForType = (type, isSqlDialect) => {
-      this.sourceType(type);
+      this.dialect(type);
 
       // TODO: Get these dynamically from langref and functions modules when moved to webpack
       this.functionsTabAvailable(type === 'hive' || type === 'impala' || type === 'pig');
@@ -165,9 +165,12 @@ class RightAssistPanel {
       updateTabs();
     };
 
-    const snippetTypeSub = huePubSub.subscribe(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, details => {
-      updateContentsForType(details.dialect, details.isSqlDialect);
-    });
+    const snippetTypeSub = huePubSub.subscribe(
+      ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
+      connector => {
+        updateContentsForType(connector.dialect, connector.is_sql);
+      }
+    );
     this.disposals.push(snippetTypeSub.remove.bind(snippetTypeSub));
 
     const onAppChange = appName => {

+ 3 - 3
desktop/core/src/desktop/js/sidePanelViewModel.js

@@ -20,7 +20,7 @@ import * as ko from 'knockout';
 import apiHelper from 'api/apiHelper';
 import hueAnalytics from 'utils/hueAnalytics';
 import huePubSub from 'utils/huePubSub';
-import { ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT } from 'apps/notebook2/events';
+import { ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT } from 'apps/notebook2/events';
 
 class SidePanelViewModel {
   constructor() {
@@ -71,8 +71,8 @@ class SidePanelViewModel {
     huePubSub.subscribe('set.current.app.name', onAppChange);
     huePubSub.publish('get.current.app.name', onAppChange);
 
-    huePubSub.subscribe(ACTIVE_SNIPPET_DIALECT_CHANGED_EVENT, details => {
-      self.rightAssistAvailable(details.isSqlDialect || details.dialect === 'pig');
+    huePubSub.subscribe(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, connector => {
+      self.rightAssistAvailable(connector.is_sql || connector.dialect === 'pig');
     });
 
     self.activeAppViewModel = ko.observable();