Pārlūkot izejas kodu

HUE-8330 [frontend] Add a ContextCatalog and fetch contexts

For now just the autocomplete endpoint is implemented
Johan Ahlen 7 gadi atpakaļ
vecāks
revīzija
2d12134ba7

+ 1 - 1
apps/metastore/src/metastore/static/metastore/js/metastore.ko.js

@@ -68,7 +68,7 @@ var MetastoreViewModel = (function () {
 
     self.database = ko.observable(null);
 
-    contextHelper.getSourceContexts().done(function (sourceContexts) {
+    ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: self.sourceType() }).done(function (sourceContexts) {
       // TODO: Context selection
       self.sourceContexts(sourceContexts);
       self.activeSourceContext(sourceContexts[0]);

+ 1 - 1
apps/pig/src/pig/templates/app.mako

@@ -1020,7 +1020,7 @@ ${ commonshare() | n,unicode }
       var apiHelper = ApiHelper.getInstance({
         user: '${ user }'
       });
-      contextHelper.getSourceContexts().done(function (sourceContexts) {
+      ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'hive' }).done(function (sourceContexts) {
         // TODO: Context selection
         DataCatalog.getChildren({ sourceContext: sourceContexts[0], sourceType: 'hive', path: ['default'], silenceErrors: true }).done(function (childEntries) {
           availableTables = $.map(childEntries, function (entry) { return entry.name }).join(' ');

+ 8 - 1
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1273,6 +1273,7 @@ var ApiHelper = (function () {
   /**
    * @param {Object} options
    * @param {string} options.sourceType
+   * @param {string} options.sourceContext
    * @param {boolean} [options.silenceErrors]
    *
    * @param {string[]} [options.path] - The path to fetch
@@ -1295,7 +1296,7 @@ var ApiHelper = (function () {
           type: sourceType,
           source: isQuery ? 'query' : 'data',
         }),
-        cluster: ko.mapping.toJSON(window.context || '')
+        cluster: options.sourceContext.id
       },
       timeout: options.timeout
     }).success(function (data) {
@@ -1946,6 +1947,12 @@ var ApiHelper = (function () {
     return new CancellablePromise(deferred, request);
   };
 
+  ApiHelper.prototype.fetchSourceContexts = function (options) {
+    var self = this;
+    var url = '/desktop/api2/context/' + options.app + '/' + options.sourceType;
+    return self.simpleGet(url, undefined, options);
+  };
+
   ApiHelper.prototype.getClusterConfig = function (data) {
     return $.post(FETCH_CONFIG, data);
   };

+ 92 - 23
desktop/core/src/desktop/static/desktop/js/contextHelper.js

@@ -14,36 +14,105 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-var SourceContext = (function () {
-  function SourceContext(name) {
-    var self = this;
-    self.name = name;
-  }
+/**
+ * @typedef {Object} SourceContext
+ * @property {string} id
+ * @property {string} name
+ */
 
-  return SourceContext;
-})();
+var ContextCatalog = (function () {
 
-var contextHelper = (function () {
+  var CONTEXT_CATALOG_VERSION = 1;
 
-  function ContextHelper () {
-    var self = this;
-    self.sourceContexts = [];
+  var ContextCatalog = (function () {
+    function ContextCatalog() {
+      var self = this;
+      self.entries = {};
 
-    if (window.IS_EMBEDDED && window.embeddedSourceContext) {
-      self.sourceContexts.push(new SourceContext(window.embeddedSourceContext))
-    } else {
-      self.sourceContexts.push(new SourceContext('defaultNamespace')) // TODO: Drop when we fetch from backend
+      // TODO: Add caching
     }
-  }
 
-  ContextHelper.prototype.getSourceContexts = function () {
-    var self = this;
-    var deferred = $.Deferred();
+    ContextCatalog.prototype.getContextCatalogEntry = function (app) {
+      var self = this;
+      if (!self.entries[app]) {
+        self.entries[app] = new ContextCatalogEntry(app);
+      }
+      return self.entries[app];
+    };
 
-    deferred.resolve(self.sourceContexts);
+    return ContextCatalog;
+  })();
 
-    return deferred.promise();
-  };
 
-  return new ContextHelper(); // Singleton
+  var ContextCatalogEntry = (function () {
+    var ContextCatalogEntry = function (app) {
+      var self = this;
+      self.app = app;
+      self.reset();
+    };
+
+    ContextCatalogEntry.prototype.reset = function () {
+      var self = this;
+      self.sourceContexts = {}; // TODO: Cache this
+      self.sourceContextsPromises = {};
+    };
+
+    /**
+     *
+     * @param {Object} options
+     * @param {string} options.sourceType
+     * @param {boolean} [options.silenceErrors]
+     * @return {Promise}
+     */
+    ContextCatalogEntry.prototype.getSourceContexts = function (options) {
+      var self = this;
+
+      if (self.sourceContextsPromises[options.sourceType]) {
+        return self.sourceContextsPromises[options.sourceType];
+      }
+
+      var deferred = $.Deferred();
+      self.sourceContextsPromises[options.sourceType] = deferred.promise();
+
+      ApiHelper.getInstance().fetchSourceContexts(options).done(function (sourceContexts) {
+        if (sourceContexts[self.app] && sourceContexts[self.app][options.sourceType]) {
+          var context = sourceContexts[self.app][options.sourceType];
+          // TODO: For now we only care about namespaces.
+          if (context.namespaces) {
+            self.sourceContexts[self.sourceType] = context.namespaces;
+            deferred.resolve(self.sourceContexts[self.sourceType])
+            // TODO: save
+          } else {
+            deferred.reject();
+          }
+        } else {
+          deferred.reject();
+        }
+      });
+
+      return deferred.promise({ name: 'foo' });
+    };
+
+    return ContextCatalogEntry;
+  })();
+
+  return (function () {
+    var contextCatalog = new ContextCatalog();
+
+    return {
+      BROWSER_APP: 'browser',
+      EDITOR_APP: 'editor',
+
+      /**
+       * @param {Object} options
+       * @param {string} options.app
+       * @param {string} options.sourceType
+       * @param {boolean} [options.silenceErrors]
+       * @return {Promise}
+       */
+      getSourceContexts: function (options) {
+        return contextCatalog.getContextCatalogEntry(options.app).getSourceContexts(options);
+      }
+    }
+  })();
 })();

+ 6 - 5
desktop/core/src/desktop/static/desktop/js/dataCatalog.js

@@ -66,6 +66,7 @@ var DataCatalog = (function () {
   var fetchAndSave = function (apiHelperFunction, attributeName, entry, apiOptions) {
     return ApiHelper.getInstance()[apiHelperFunction]({
       sourceType: entry.dataCatalog.sourceType,
+      sourceContext: entry.sourceContext,
       path: entry.path, // Set for DataCatalogEntry
       paths: entry.paths, // Set for MultiTableEntry
       silenceErrors: apiOptions && apiOptions.silenceErrors,
@@ -137,7 +138,7 @@ var DataCatalog = (function () {
         return deferred.reject().promise();
       }
 
-      var keyPrefix = sourceContext.name;
+      var keyPrefix = sourceContext.id;
       if (rootPath.length) {
         keyPrefix += '_' +  rootPath.join('.');
       }
@@ -177,7 +178,7 @@ var DataCatalog = (function () {
       }
       var deferred = $.Deferred();
 
-      var identifier = dataCatalogEntry.sourceContext.name;
+      var identifier = dataCatalogEntry.sourceContext.id;
       if (dataCatalogEntry.path.length) {
         identifier += '_' + dataCatalogEntry.path.join('.');
       }
@@ -341,7 +342,7 @@ var DataCatalog = (function () {
     DataCatalog.prototype.getKnownEntry = function (options) {
       var self = this;
       var identifier = typeof options.path === 'string' ? options.path : options.path.join('.');
-      identifier = options.sourceContext.name + (identifier ? '_' + identifier : '');
+      identifier = options.sourceContext.id + (identifier ? '_' + identifier : '');
       return self.entries[identifier];
     };
 
@@ -356,7 +357,7 @@ var DataCatalog = (function () {
     DataCatalog.prototype.getEntry = function (options) {
       var self = this;
       var identifier = typeof options.path === 'string' ? options.path : options.path.join('.');
-      identifier = options.sourceContext.name + (identifier ? '_' + identifier : '');
+      identifier = options.sourceContext.id + (identifier ? '_' + identifier : '');
       if (self.entries[identifier]) {
         return self.entries[identifier];
       }
@@ -425,7 +426,7 @@ var DataCatalog = (function () {
       });
       var uniquePaths = Object.keys(pathSet);
       uniquePaths.sort();
-      return sourceContext.name + '_' + uniquePaths.join(',');
+      return sourceContext.id + '_' + uniquePaths.join(',');
     };
 
     /**

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/jquery.hiveautocomplete.js

@@ -53,7 +53,7 @@
     if (self.options.activeSourceContext) {
       self.activeSourceContextDeferred.resolve(self.options.activeSourceContext);
     } else {
-      contextHelper.getSourceContexts().done(function (sourceContexts) {
+      ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'hive' }).done(function (sourceContexts) {
         // TODO: Context selection in caller
         self.activeSourceContextDeferred.resolve(sourceContexts[0]);
       })

+ 2 - 1
desktop/core/src/desktop/templates/assist.mako

@@ -1126,7 +1126,8 @@ from desktop.views import _ko
           }
         };
 
-        contextHelper.getSourceContexts().done(function (sourceContexts) {
+        // TODO: Create AssistDbContext objects and replace AssistDbSource below
+        ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'hive' }).done(function (sourceContexts) {
           // TODO: Context selection
           self.activeSourceContext(sourceContexts[0]);
 

+ 1 - 1
desktop/core/src/desktop/templates/ko_components/ko_context_popover.mako

@@ -1469,7 +1469,7 @@ from metadata.conf import has_navigator
         }
 
         if (self.isCatalogEntry) {
-          contextHelper.getSourceContexts().done(function (sourceContexts) {
+          ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: sourceType }).done(function (sourceContexts) {
             // TODO: Context selection for global search results?
             DataCatalog.getEntry({ sourceType: sourceType, sourceContext: sourceContexts[0], path: path, definition: { type: params.data.type.toLowerCase() }}).done(function (catalogEntry) {
               catalogEntry.navigatorMeta = params.data;

+ 1 - 1
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -558,7 +558,7 @@ var Collection = function (vm, collection) {
   self.suggest = ko.mapping.fromJS(collection.suggest);
   self.activeSourceContext = ko.observable();
 
-  contextHelper.getSourceContexts().done(function (sourceContexts) {
+  ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: collection.engine || 'solr' }).done(function (sourceContexts) {
     // TODO: Context selection
     self.activeSourceContext(sourceContexts[0]);
   });

+ 2 - 1
desktop/libs/indexer/src/indexer/templates/importer.mako

@@ -2050,7 +2050,8 @@ ${ assist.assistPanel() }
 
       self.activeSourceContext = ko.observable();
 
-      contextHelper.getSourceContexts().done(function (sourceContexts) {
+      // TODO: sourceType?
+      ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'hive' }).done(function (sourceContexts) {
         // TODO: Context selection for create wizard
         self.activeSourceContext(sourceContexts[0]);
       });

+ 2 - 1
desktop/libs/indexer/src/indexer/templates/indexes.mako

@@ -649,7 +649,8 @@ ${ assist.assistPanel() }
 
       self.activeSourceContext = ko.observable();
 
-      contextHelper.getSourceContexts().done(function (sourceContexts) {
+
+      ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'solr' }).done(function (sourceContexts) {
         // TODO: Context selection
         self.activeSourceContext(sourceContexts[0]);
       });

+ 1 - 1
desktop/libs/indexer/src/indexer/templates/topics.mako

@@ -624,7 +624,7 @@ ${ assist.assistPanel() }
 
       self.activeSourceContext = ko.observable();
 
-      contextHelper.getSourceContexts().done(function (sourceContexts) {
+      ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: 'solr' }).done(function (sourceContexts) {
         // TODO: Context selection
         self.activeSourceContext(sourceContexts[0]);
       });

+ 1 - 1
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -377,7 +377,7 @@ var EditorViewModel = (function() {
 
         var sourceContextDeferred = $.Deferred();
         if (!self.sourceContext()) {
-          contextHelper.getSourceContexts().done(function (sourceContexts) {
+          ContextCatalog.getSourceContexts({ app: ContextCatalog.BROWSER_APP, sourceType: self.type() }).done(function (sourceContexts) {
             // TODO: Context selection for the notebook
             self.sourceContext(sourceContexts[0]);
             sourceContextDeferred.resolve();