瀏覽代碼

HUE-7820 [core] Add Navigator metadata to the sqlMetadata object

Johan Ahlen 8 年之前
父節點
當前提交
70a6ff038b

+ 41 - 65
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1592,7 +1592,7 @@ var ApiHelper = (function () {
    *
    * @return {Deferred} Promise
    */
-  ApiHelper.prototype.fetchSqlMetadata = function (options) {
+  ApiHelper.prototype.fetchSourceMetadata = function (options) {
     var self = this;
     var promise = $.Deferred();
 
@@ -1612,6 +1612,46 @@ var ApiHelper = (function () {
     return promise;
   };
 
+
+  /**
+   * Fetches a navigator entity for the given source and path
+   *
+   * @param {Object} options
+   * @param {boolean} [options.silenceErrors]
+   * @param {boolean} [options.noCache]
+   *
+   * @param {boolean} [options.isView] - Default false
+   * @param {string[]} options.path
+   */
+  ApiHelper.prototype.fetchNavigatorMetadata = function (options) {
+    var self = this;
+    var promise = $.Deferred();
+    var url = NAV_URLS.FIND_ENTITY;
+
+    if (options.path.length === 1) {
+      url += '?type=database&name=' + options.path[0];
+    } else if (options.path.length === 2) {
+      url +=  (options.isView ? '?type=view' : '?type=table') + '&database=' + options.path[0] + '&name=' + options.path[1];
+    } else if (options.path.length === 3) {
+      url +=  '?type=field&database=' + options.path[0] + '&table=' + options.path[1] + '&name=' + options.path[2];
+    }
+
+    fetchAssistData.bind(self)({
+      url: url,
+      sourceType: 'nav',
+      noCache: options.noCache,
+      silenceErrors: options.silenceErrors,
+      successCallback: promise.resolve,
+      errorCallback: self.assistErrorCallback({
+        errorCallback: promise.reject,
+        silenceErrors: options.silenceErrors
+      }),
+      cacheCondition: genericCacheCondition
+    });
+
+    return promise;
+  };
+
   /**
    * @param {Object} options
    * @param {string} options.sourceType
@@ -1638,27 +1678,6 @@ var ApiHelper = (function () {
     });
   };
 
-
-  /**
-   * @param {Object} options
-   * @param {string} options.sourceType
-   * @param {Function} options.successCallback
-   * @param {Function} [options.errorCallback]
-   * @param {boolean} [options.silenceErrors]
-   *
-   * @param {string[]} options.hierarchy
-   */
-  // TODO: Drop and use fetchSqlMetadata instead
-  ApiHelper.prototype.fetchPanelData = function (options) {
-    var self = this;
-    fetchAssistData.bind(self)($.extend({}, options, {
-      url: AUTOCOMPLETE_API_PREFIX + options.hierarchy.join("/"),
-      errorCallback: self.assistErrorCallback(options),
-      cacheCondition: genericCacheCondition
-    }));
-  };
-
-
   /**
    * @param {Object} options
    * @param {string} options.sourceType
@@ -1814,49 +1833,6 @@ var ApiHelper = (function () {
     return $.post(FETCH_CONFIG, data);
   };
 
-  /**
-   * Fetches a navigator entity for the given identifierChain
-   *
-   * @param {Object} options
-   * @param {string} options.sourceType
-   * @param {Function} options.successCallback
-   * @param {Function} [options.errorCallback]
-   * @param {boolean} [options.silenceErrors]
-   *
-   * @param {boolean} [options.isView] - Default false
-   * @param {Object[]} options.identifierChain
-   * @param {string} options.identifierChain.name
-   * @param {string} [options.defaultDatabase]
-   */
-  ApiHelper.prototype.fetchNavEntity = function (options) {
-    var self = this;
-
-    var clonedIdentifierChain = options.identifierChain.concat();
-
-    var dpPromise = self.containsDatabase(options.sourceType, clonedIdentifierChain[0].name);
-
-    dpPromise.done(function (firstIsDatabase) {
-      var database = options.defaultDatabase && !firstIsDatabase ? options.defaultDatabase : clonedIdentifierChain.shift().name;
-      var url = NAV_URLS.FIND_ENTITY + '?type=database&name=' + database;
-
-      var isView = !!options.isView;
-
-      if (clonedIdentifierChain.length > 0) {
-        var table = clonedIdentifierChain.shift().name;
-        url = NAV_URLS.FIND_ENTITY + (isView ? '?type=view' : '?type=table') + '&database=' + database + '&name=' + table;
-        if (clonedIdentifierChain.length > 0) {
-          url = NAV_URLS.FIND_ENTITY + '?type=field&database=' + database + '&table=' + table + '&name=' + clonedIdentifierChain.shift().name;
-        }
-      }
-
-      fetchAssistData.bind(self)($.extend({ sourceType: 'nav' }, options, {
-        url: url,
-        errorCallback: self.assistErrorCallback(options),
-        noCache: true
-      }));
-    });
-  };
-
   ApiHelper.prototype.addNavTags = function (entityId, tags) {
     return $.post(NAV_URLS.ADD_TAGS, {
       id: ko.mapping.toJSON(entityId),

+ 17 - 17
desktop/core/src/desktop/static/desktop/js/assist/assistDbEntry.js

@@ -325,8 +325,8 @@ var AssistDbEntry = (function () {
 
       var newEntries = [];
       var index = 0;
-      if (typeof sqlMeta.meta.tables_meta !== "undefined") {
-        newEntries = $.map(sqlMeta.meta.tables_meta, function(table) {
+      if (typeof sqlMeta.sourceMeta.tables_meta !== "undefined") {
+        newEntries = $.map(sqlMeta.sourceMeta.tables_meta, function(table) {
           table.index = index++;
           table.title = table.name + (table.comment ? ' - ' + table.comment : '');
           table.displayName = table.name;
@@ -334,8 +334,8 @@ var AssistDbEntry = (function () {
           table.isView = /view/i.test(table.type);
           return self.createEntry(table);
         });
-      } else if (typeof sqlMeta.meta.extended_columns !== "undefined" && sqlMeta.meta.extended_columns !== null) {
-        newEntries = $.map(sqlMeta.meta.extended_columns, function (columnDef) {
+      } else if (typeof sqlMeta.sourceMeta.extended_columns !== "undefined" && sqlMeta.sourceMeta.extended_columns !== null) {
+        newEntries = $.map(sqlMeta.sourceMeta.extended_columns, function (columnDef) {
           var displayName = columnDef.name;
           if (typeof columnDef.type !== "undefined" && columnDef.type !== null) {
             displayName += ' (' + columnDef.type + ')'
@@ -355,8 +355,8 @@ var AssistDbEntry = (function () {
           columnDef.type = shortType;
           return self.createEntry(columnDef);
         });
-      } else if (typeof sqlMeta.meta.columns !== "undefined" && sqlMeta.meta.columns !== null) {
-        newEntries = $.map(sqlMeta.meta.columns, function(columnName) {
+      } else if (typeof sqlMeta.sourceMeta.columns !== "undefined" && sqlMeta.sourceMeta.columns !== null) {
+        newEntries = $.map(sqlMeta.sourceMeta.columns, function(columnName) {
           return self.createEntry({
             name: columnName,
             index: index++,
@@ -370,23 +370,23 @@ var AssistDbEntry = (function () {
           self.createEntry({
             name: "key",
             index: index++,
-            displayName: "key (" + sqlMeta.meta.key.type + ")",
-            title: "key (" + sqlMeta.meta.key.type + ")",
-            type: sqlMeta.meta.key.type,
+            displayName: "key (" + sqlMeta.sourceMeta.key.type + ")",
+            title: "key (" + sqlMeta.sourceMeta.key.type + ")",
+            type: sqlMeta.sourceMeta.key.type,
             isComplex: true
           }),
           self.createEntry({
             name: "value",
             index: index++,
-            displayName: "value (" + sqlMeta.meta.value.type + ")",
-            title: "value (" + sqlMeta.meta.value.type + ")",
+            displayName: "value (" + sqlMeta.sourceMeta.value.type + ")",
+            title: "value (" + sqlMeta.sourceMeta.value.type + ")",
             isMapValue: true,
-            type: sqlMeta.meta.value.type,
+            type: sqlMeta.sourceMeta.value.type,
             isComplex: true
           })
         ];
       } else if (sqlMeta.isStruct()) {
-        newEntries = $.map(sqlMeta.meta.fields, function(field) {
+        newEntries = $.map(sqlMeta.sourceMeta.fields, function(field) {
           return self.createEntry({
             name: field.name,
             index: index++,
@@ -401,10 +401,10 @@ var AssistDbEntry = (function () {
           self.createEntry({
             name: "item",
             index: index++,
-            displayName: "item (" + sqlMeta.meta.item.type + ")",
-            title: "item (" + sqlMeta.meta.item.type + ")",
+            displayName: "item (" + sqlMeta.sourceMeta.item.type + ")",
+            title: "item (" + sqlMeta.sourceMeta.item.type + ")",
             isArray: true,
-            type: sqlMeta.meta.item.type,
+            type: sqlMeta.sourceMeta.item.type,
             isComplex: true
           })
         ];
@@ -461,7 +461,7 @@ var AssistDbEntry = (function () {
       })
     }
 
-    self.metadata.load(self.navigationSettings.rightAssist || !!silenceErrors, false).done(successCallback).fail(errorCallback);
+    self.metadata.loadSourceMeta(self.navigationSettings.rightAssist || !!silenceErrors, false).done(successCallback).fail(errorCallback);
   };
 
   /**

+ 46 - 9
desktop/core/src/desktop/static/desktop/js/sqlMetadata.js

@@ -24,35 +24,55 @@ var SqlMetadata = (function () {
     self.sourceType = options.sourceType;
     self.path = options.path;
 
-    self.meta;
+    self.sourceMeta;
+    self.navigatorMeta;
   }
 
+  SqlMetadata.prototype.isDatabase = function () {
+    var self = this;
+    return self.path.length === 1;
+  };
+
+  SqlMetadata.prototype.isTable = function () {
+    var self = this;
+    return self.sourceMeta && typeof self.sourceMeta.columns !== 'undefined' && !self.sourceMeta.is_view;
+  };
+
+  SqlMetadata.prototype.isView = function () {
+    var self = this;
+    return self.sourceMeta && typeof self.sourceMeta.columns !== 'undefined' && self.sourceMeta.is_view;
+  };
+
+  SqlMetadata.prototype.isField = function () {
+    var self = this;
+    return self.path.length > 2
+  };
+
   SqlMetadata.prototype.isMap = function () {
     var self = this;
-    return self.meta && self.meta.type === 'map';
+    return self.sourceMeta && self.sourceMeta.type === 'map';
   };
 
   SqlMetadata.prototype.isStruct = function () {
     var self = this;
-    return self.meta && self.meta.type === 'struct';
+    return self.sourceMeta && self.sourceMeta.type === 'struct';
   };
 
   SqlMetadata.prototype.isArray = function () {
     var self = this;
-    return self.meta && self.meta.type === 'array';
+    return self.sourceMeta && self.sourceMeta.type === 'array';
   };
 
-  SqlMetadata.prototype.load = function (silenceErrors, cachedOnly) {
+  SqlMetadata.prototype.loadSourceMeta = function (silenceErrors, cachedOnly) {
     var self = this;
     var promise = $.Deferred();
-    ApiHelper.getInstance().fetchSqlMetadata({
+    ApiHelper.getInstance().fetchSourceMetadata({
       sourceType: self.sourceType,
       path: self.path,
       silenceErrors: silenceErrors,
       cachedOnly: cachedOnly
-    })
-    .done(function (data) {
-      self.meta = data;
+    }).done(function (data) {
+      self.sourceMeta = data;
       self.loaded = true;
       promise.resolve(self);
     }).fail(function (message) {
@@ -62,6 +82,23 @@ var SqlMetadata = (function () {
 
     return promise;
   };
+  
+  SqlMetadata.prototype.loadNavigatorMeta = function (silenceErrors) {
+    var self = this;
+    var promise = $.Deferred();
+    if (HAS_NAVIGATOR) {
+      ApiHelper.getInstance().fetchNavigatorMetadata({
+        path: self.path,
+        silenceErrors: silenceErrors,
+      }).done(function (data) {
+        self.navigatorMeta = data;
+        promise.resolve(self);
+      }).fail(promise.reject);
+    } else {
+      promise.resolve();
+    }
+    return promise;
+  };
 
   return SqlMetadata;
 })();

+ 3 - 1
desktop/core/src/desktop/templates/global_js_constants.mako

@@ -23,7 +23,7 @@
 
   from beeswax.conf import LIST_PARTITIONS_LIMIT
   from indexer.conf import ENABLE_NEW_INDEXER
-  from metadata.conf import has_optimizer, OPTIMIZER
+  from metadata.conf import has_navigator, has_optimizer, OPTIMIZER
 %>
 
 (function () {
@@ -44,6 +44,8 @@
 
   window.ENABLE_SQL_SYNTAX_CHECK = '${ conf.ENABLE_SQL_SYNTAX_CHECK.get() }' === 'True';
 
+  window.HAS_NAVIGATOR = '${ has_navigator(request.user) }' === 'True';
+
   window.HAS_OPTIMIZER = '${ has_optimizer() }' === 'True';
 
   window.HUE_CONTAINER = '${ IS_EMBEDDED.get() }' === 'True' ? '.hue-embedded-container' : 'body';

+ 13 - 12
desktop/core/src/desktop/templates/ko_components/ko_nav_tags.mako

@@ -82,19 +82,20 @@ from django.utils.translation import ugettext as _
 
         var fetchNavEntity = function () {
           var fetchDeferral = $.Deferred();
-          apiHelper.fetchNavEntity({
-            sourceType: ko.unwrap(params.sourceType),
-            identifierChain: identifierChain,
-            isView: typeof params.fetchedData !== 'undefined' && typeof params.fetchedData() !== 'undefined' && params.fetchedData().is_view,
-            defaultDatabase: ko.unwrap(params.defaultDatabase),
-            silenceErrors: true,
-            noCache: true,
-            successCallback: function (data) {
-              fetchDeferral.resolve(data.entity);
-            },
-            errorCallback: function (error) {
-              fetchDeferral.reject()
+          var path = $.map(identifierChain, function (identifier) { return identifier.name });
+
+          apiHelper.containsDatabase(params.sourceType, path[0]).done(function (firstIsDatabase) {
+            if (!firstIsDatabase) {
+              path.unshift(ko.unwrap(params.defaultDatabase));
             }
+            apiHelper.fetchNavigatorMetadata({
+              path: path,
+              isView: typeof params.fetchedData !== 'undefined' && typeof params.fetchedData() !== 'undefined' && params.fetchedData().is_view,
+              silenceErrors: true,
+              noCache: true
+            }).done(function (data) {
+              fetchDeferral.resolve(data.entity);
+            }).fail(fetchDeferral.reject);
           });
           return fetchDeferral;
         };