Bläddra i källkod

HUE-9148 [assist] Switch to using the config API in the DB assist panel

Johan Ahlen 5 år sedan
förälder
incheckning
a7afe21a5f

+ 136 - 122
desktop/core/src/desktop/js/ko/components/assist/ko.assistDbPanel.js

@@ -23,6 +23,7 @@ import componentUtils from 'ko/components/componentUtils';
 import dataCatalog from 'catalog/dataCatalog';
 import huePubSub from 'utils/huePubSub';
 import I18n from 'utils/i18n';
+import { CONFIG_REFRESHED_EVENT, GET_KNOWN_CONFIG_EVENT } from '../../../utils/hueConfig';
 
 const ASSIST_TABLE_TEMPLATES = `
   <script type="text/html" id="assist-no-database-entries">
@@ -581,71 +582,40 @@ class AssistDbPanel {
     const self = this;
     self.options = options;
     self.i18n = options.i18n;
-    self.initialized = false;
-    self.initalizing = false;
 
     self.isStreams = options.isStreams;
     self.isSolr = options.isSolr;
     self.nonSqlType = self.isSolr || self.isStreams;
 
-    if (typeof options.sourceTypes === 'undefined') {
-      options.sourceTypes = [];
-
-      if (self.isSolr) {
-        options.sourceTypes = [{ type: 'solr', name: 'solr' }];
-      } else if (self.isStreams) {
-        options.sourceTypes = [{ type: 'kafka', name: 'kafka' }];
-      } else {
-        window.ASSIST_SQL_INTERPRETERS.forEach(interpreter => {
-          options.sourceTypes.push(interpreter);
-        });
-      }
-    }
-
     self.sources = ko.observableArray();
     self.sourceIndex = {};
     self.selectedSource = ko.observable(null);
 
-    options.sourceTypes.forEach(sourceType => {
-      self.sourceIndex[sourceType.type] = new AssistDbSource({
-        i18n: self.i18n,
-        type: sourceType.type,
-        name: sourceType.name,
-        nonSqlType: sourceType.type === 'solr' || sourceType.type === 'kafka',
-        navigationSettings: options.navigationSettings
-      });
-      self.sources.push(self.sourceIndex[sourceType.type]);
-    });
-
-    if (self.sources().length === 1) {
-      self.selectedSource(self.sources()[0]);
-    }
-
-    if (self.sourceIndex['solr']) {
-      huePubSub.subscribe('assist.collections.refresh', () => {
-        const solrSource = self.sourceIndex['solr'];
-        const doRefresh = () => {
-          if (solrSource.selectedNamespace()) {
-            const assistDbNamespace = solrSource.selectedNamespace();
-            dataCatalog
-              .getEntry({
-                sourceType: 'solr',
-                namespace: assistDbNamespace.namespace,
-                compute: assistDbNamespace.compute(),
-                path: []
-              })
-              .done(entry => {
-                entry.clearCache({ cascade: true });
-              });
+    self.breadcrumb = ko.pureComputed(() => {
+      if (self.isStreams && self.selectedSource()) {
+        return self.selectedSource().name;
+      }
+      if (!self.isSolr && self.selectedSource()) {
+        if (self.selectedSource().selectedNamespace()) {
+          if (
+            self
+              .selectedSource()
+              .selectedNamespace()
+              .selectedDatabase()
+          ) {
+            return self
+              .selectedSource()
+              .selectedNamespace()
+              .selectedDatabase().catalogEntry.name;
+          }
+          if (window.HAS_MULTI_CLUSTER) {
+            return self.selectedSource().selectedNamespace().name;
           }
-        };
-        if (!solrSource.hasNamespaces()) {
-          solrSource.loadNamespaces(true).done(doRefresh);
-        } else {
-          doRefresh();
         }
-      });
-    }
+        return self.selectedSource().name;
+      }
+      return null;
+    });
 
     huePubSub.subscribe('assist.db.highlight', catalogEntry => {
       huePubSub.publish('left.assist.show');
@@ -672,7 +642,31 @@ class AssistDbPanel {
       }, 0);
     });
 
-    if (!self.isSolr && !self.isStreams) {
+    if (self.isSolr) {
+      huePubSub.subscribe('assist.collections.refresh', () => {
+        const solrSource = self.sourceIndex['solr'];
+        const doRefresh = () => {
+          if (solrSource.selectedNamespace()) {
+            const assistDbNamespace = solrSource.selectedNamespace();
+            dataCatalog
+              .getEntry({
+                sourceType: 'solr',
+                namespace: assistDbNamespace.namespace,
+                compute: assistDbNamespace.compute(),
+                path: []
+              })
+              .done(entry => {
+                entry.clearCache({ cascade: true });
+              });
+          }
+        };
+        if (!solrSource.hasNamespaces()) {
+          solrSource.loadNamespaces(true).done(doRefresh);
+        } else {
+          doRefresh();
+        }
+      });
+    } else if (!self.isSolr && !self.isStreams) {
       huePubSub.subscribe('assist.set.database', databaseDef => {
         if (!databaseDef.source || !self.sourceIndex[databaseDef.source]) {
           return;
@@ -747,12 +741,6 @@ class AssistDbPanel {
         }
       });
 
-      huePubSub.publish('assist.db.panel.ready');
-
-      huePubSub.subscribe('assist.is.db.panel.ready', () => {
-        huePubSub.publish('assist.db.panel.ready');
-      });
-
       self.selectedSource.subscribe(newSource => {
         if (newSource) {
           if (newSource.namespaces().length === 0) {
@@ -765,51 +753,13 @@ class AssistDbPanel {
       });
     }
 
-    if (self.isSolr || self.isStreams) {
-      if (self.sources().length === 1) {
-        self.selectedSource(self.sources()[0]);
-        self
-          .selectedSource()
-          .loadNamespaces()
-          .done(() => {
-            const solrNamespace = self.selectedSource().selectedNamespace();
-            if (solrNamespace) {
-              solrNamespace.initDatabases();
-              solrNamespace.whenLoaded(() => {
-                solrNamespace.setDatabase('default');
-              });
-            }
-          });
-      }
-    }
+    self.init(options.navigationSettings, options.activeSourceType).then(() => {
+      huePubSub.publish('assist.db.panel.ready');
 
-    self.breadcrumb = ko.pureComputed(() => {
-      if (self.isStreams && self.selectedSource()) {
-        return self.selectedSource().name;
-      }
-      if (!self.isSolr && self.selectedSource()) {
-        if (self.selectedSource().selectedNamespace()) {
-          if (
-            self
-              .selectedSource()
-              .selectedNamespace()
-              .selectedDatabase()
-          ) {
-            return self
-              .selectedSource()
-              .selectedNamespace()
-              .selectedDatabase().catalogEntry.name;
-          }
-          if (window.HAS_MULTI_CLUSTER) {
-            return self.selectedSource().selectedNamespace().name;
-          }
-        }
-        return self.selectedSource().name;
-      }
-      return null;
+      huePubSub.subscribe('assist.is.db.panel.ready', () => {
+        huePubSub.publish('assist.db.panel.ready');
+      });
     });
-
-    self.init();
   }
 
   setDatabaseWhenLoaded(namespace, databaseName) {
@@ -873,25 +823,89 @@ class AssistDbPanel {
     }
   }
 
-  init() {
-    if (this.initialized) {
-      return;
-    }
-    if (this.isSolr) {
-      this.selectedSource(this.sourceIndex['solr']);
-    } else if (this.isStreams) {
-      this.selectedSource(this.sourceIndex['kafka']);
-    } else {
-      const storageSourceType = apiHelper.getFromTotalStorage('assist', 'lastSelectedSource');
-      if (!this.selectedSource()) {
-        if (this.options.activeSourceType) {
-          this.selectedSource(this.sourceIndex[this.options.activeSourceType]);
-        } else if (storageSourceType && this.sourceIndex[storageSourceType]) {
-          this.selectedSource(this.sourceIndex[storageSourceType]);
-        }
+  setSingleSource(type, navigationSettings, nonSqlType) {
+    this.sourceIndex = {};
+    const source = new AssistDbSource({
+      i18n: self.i18n,
+      type: type,
+      name: type,
+      nonSqlType: nonSqlType,
+      navigationSettings: navigationSettings
+    });
+    this.sourceIndex[type] = source;
+    this.sources([source]);
+    this.selectedSource(source);
+
+    source.loadNamespaces().done(() => {
+      const namespace = self.selectedSource().selectedNamespace();
+      if (namespace) {
+        namespace.initDatabases();
+        namespace.whenLoaded(() => {
+          namespace.setDatabase('default');
+        });
       }
-    }
-    this.initialized = true;
+    });
+  }
+
+  async init(navigationSettings, activeSourceType) {
+    return new Promise(resolve => {
+      if (self.isSolr) {
+        this.setSingleSource('solr', navigationSettings, true);
+        resolve();
+        return;
+      }
+
+      if (self.isStreams) {
+        this.setSingleSource('kafka', navigationSettings, true);
+        resolve();
+        return;
+      }
+
+      const updateFromConfig = config => {
+        const sources = [];
+        if (
+          config &&
+          config.app_config &&
+          config.app_config.editor &&
+          config.app_config.editor.interpreters
+        ) {
+          const interpreters = config.app_config.editor.interpreters;
+          interpreters.forEach(interpreter => {
+            if (interpreter.is_sql) {
+              const source =
+                this.sourceIndex[interpreter.type] ||
+                new AssistDbSource({
+                  i18n: self.i18n,
+                  type: interpreter.type,
+                  name: interpreter.name,
+                  nonSqlType: false,
+                  navigationSettings: navigationSettings
+                });
+              sources.push(source);
+            }
+          });
+        }
+        this.sourceIndex = {};
+        sources.forEach(source => {
+          this.sourceIndex[source.sourceType] = source;
+        });
+
+        if (sources.indexOf(this.selectedSource()) === -1) {
+          const storageSourceType =
+            activeSourceType || apiHelper.getFromTotalStorage('assist', 'lastSelectedSource');
+          this.selectedSource(
+            this.sourceIndex[storageSourceType] || (sources.length ? sources[0] : null)
+          );
+        }
+        this.sources(sources);
+      };
+
+      huePubSub.subscribe(CONFIG_REFRESHED_EVENT, updateFromConfig);
+      huePubSub.publish(GET_KNOWN_CONFIG_EVENT, config => {
+        updateFromConfig(config);
+        resolve();
+      });
+    });
   }
 }
 

+ 0 - 11
desktop/core/src/desktop/templates/global_js_constants.mako

@@ -662,17 +662,6 @@
   window.USE_NEW_SIDE_PANELS = '${ USE_NEW_SIDE_PANELS.get() }' === 'True'
   window.USER_HOME_DIR = '${ home_dir }';
 
-  // TODO: Refactor assist to fetch from config.
-  window.ASSIST_SQL_INTERPRETERS = [];
-  % for interpreter in get_ordered_interpreters(request.user):
-    % if interpreter["is_sql"]:
-      ASSIST_SQL_INTERPRETERS.push({
-        type: '${ interpreter["type"] }',
-        name: '${ interpreter["name"] }'
-      });
-    % endif
-  % endfor
-
   var userGroups = [];
   % for group in user.groups.all():
     userGroups.push('${ group }');