Selaa lähdekoodia

HUE-7820 [core] Add ability to set comments through the sqlMetadata object

Johan Ahlen 7 vuotta sitten
vanhempi
commit
ca38d90

+ 89 - 34
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -74,8 +74,9 @@ var ApiHelper = (function () {
   var NAV_URLS = {
     ADD_TAGS: '/metadata/api/navigator/add_tags',
     DELETE_TAGS: '/metadata/api/navigator/delete_tags',
+    FIND_ENTITY: '/metadata/api/navigator/find_entity',
     LIST_TAGS: '/metadata/api/navigator/list_tags',
-    FIND_ENTITY: '/metadata/api/navigator/find_entity'
+    UPDATE_PROPERTIES: '/metadata/api/navigator/update_properties',
   };
 
   var NAV_OPT_URLS = {
@@ -318,7 +319,7 @@ var ApiHelper = (function () {
    */
   ApiHelper.prototype.simplePost = function (url, data, options) {
     var self = this;
-    $.post(url, data, function (data) {
+    return $.post(url, data, function (data) {
       if (self.successResponseIsError(data)) {
         self.assistErrorCallback(options)(data);
       } else if (typeof options.successCallback !== 'undefined') {
@@ -1587,6 +1588,7 @@ var ApiHelper = (function () {
    * @param {string} options.sourceType
    * @param {boolean} [options.silenceErrors]
    * @param {boolean} [options.cachedOnly] - Default false
+   * @param {boolean} [options.refreshCache] - Default false
    *
    * @param {string[]} [options.path] - The path to fetch
    *
@@ -1601,6 +1603,7 @@ var ApiHelper = (function () {
       sourceType: options.sourceType,
       silenceErrors: options.silenceErrors,
       cachedOnly: options.cachedOnly,
+      refreshCache: options.refreshCache,
       successCallback: promise.resolve,
       errorCallback: self.assistErrorCallback({
         errorCallback: promise.reject,
@@ -1613,6 +1616,47 @@ var ApiHelper = (function () {
   };
 
 
+  ApiHelper.prototype.updateSourceMetadata = function (options) {
+    var self = this;
+    var url;
+    var data = {
+      source_type: options.sourceType
+    };
+    if (options.path.length === 1) {
+      url = '/metastore/databases/' + options.path[1] + '/alter';
+      data.properties = ko.mapping.toJSON(options.properties);
+    } else if (options.path.length === 2) {
+      url = '/metastore/table/' + options.path[0] + '/' + options.path[1] + '/alter';
+      if (options.properties) {
+        if (options.properties.comment) {
+          data.comment = options.properties.comment;
+        }
+        if (options.properties.name) {
+          data.new_table_name = options.properties.name;
+        }
+      }
+    } else if (options.path > 2) {
+      url = '/metastore/table/' + options.path[0] + '/' + options.path[1] + '/alter_column';
+      data.column = options.path.slice(2).join('.');
+      if (options.properties) {
+        if (options.properties.comment) {
+          data.comment = options.properties.comment;
+        }
+        if (options.properties.name) {
+          data.new_column_name = options.properties.name;
+        }
+        if (options.properties.type) {
+          data.new_column_type = options.properties.name;
+        }
+        if (options.properties.partitions) {
+          data.partition_spec = ko.mapping.toJSON(options.properties.partitions);
+        }
+      }
+    }
+    return self.simplePost(url, data, options);
+  };
+
+
   /**
    * Fetches a navigator entity for the given source and path
    *
@@ -1652,6 +1696,47 @@ var ApiHelper = (function () {
     return promise;
   };
 
+  ApiHelper.prototype.updateNavigatorMetadata = function (options) {
+    var self = this;
+    return self.simplePost(NAV_URLS.UPDATE_PROPERTIES, {
+      id: ko.mapping.toJSON(options.identity),
+      properties: ko.mapping.toJSON(options.properties)
+    }, options)
+  };
+
+
+  ApiHelper.prototype.addNavTags = function (entityId, tags) {
+    return $.post(NAV_URLS.ADD_TAGS, {
+      id: ko.mapping.toJSON(entityId),
+      tags: ko.mapping.toJSON(tags)
+    });
+  };
+
+  ApiHelper.prototype.deleteNavTags = function (entityId, tags) {
+    return $.post(NAV_URLS.DELETE_TAGS, {
+      id: ko.mapping.toJSON(entityId),
+      tags: ko.mapping.toJSON(tags)
+    });
+  };
+
+  /**
+   * Lists all available navigator tags
+   *
+   * @param {Object} options
+   * @param {Function} options.successCallback
+   * @param {Function} [options.errorCallback]
+   * @param {boolean} [options.silenceErrors]
+   */
+  ApiHelper.prototype.listNavTags = function (options) {
+    var self = this;
+    fetchAssistData.bind(self)($.extend({ sourceType: 'nav' }, options, {
+      url: NAV_URLS.LIST_TAGS,
+      errorCallback: self.assistErrorCallback(options),
+      noCache: true
+    }));
+  };
+
+
   /**
    * @param {Object} options
    * @param {string} options.sourceType
@@ -1833,37 +1918,6 @@ var ApiHelper = (function () {
     return $.post(FETCH_CONFIG, data);
   };
 
-  ApiHelper.prototype.addNavTags = function (entityId, tags) {
-    return $.post(NAV_URLS.ADD_TAGS, {
-      id: ko.mapping.toJSON(entityId),
-      tags: ko.mapping.toJSON(tags)
-    });
-  };
-
-  ApiHelper.prototype.deleteNavTags = function (entityId, tags) {
-    return $.post(NAV_URLS.DELETE_TAGS, {
-      id: ko.mapping.toJSON(entityId),
-      tags: ko.mapping.toJSON(tags)
-    });
-  };
-
-  /**
-   * Lists all available navigator tags
-   *
-   * @param {Object} options
-   * @param {Function} options.successCallback
-   * @param {Function} [options.errorCallback]
-   * @param {boolean} [options.silenceErrors]
-   */
-  ApiHelper.prototype.listNavTags = function (options) {
-    var self = this;
-    fetchAssistData.bind(self)($.extend({ sourceType: 'nav' }, options, {
-      url: NAV_URLS.LIST_TAGS,
-      errorCallback: self.assistErrorCallback(options),
-      noCache: true
-    }));
-  };
-
   ApiHelper.prototype.createNavOptDbTablesJson = function (options) {
     var self = this;
     var tables = [];
@@ -2184,6 +2238,7 @@ var ApiHelper = (function () {
    * @param {string} options.sourceType
    * @param {string} options.url
    * @param {boolean} [options.noCache]
+   * @param {boolean} [options.refreshCache] - Default false
    * @param {Function} options.cacheCondition - Determines whether it should be cached or not
    * @param {Function} options.successCallback
    * @param {Function} options.errorCallback
@@ -2200,7 +2255,7 @@ var ApiHelper = (function () {
       return
     }
 
-    if (!options.noCache) {
+    if (!options.noCache && !options.refreshCache) {
       var cachedData = $.totalStorage(self.getAssistCacheIdentifier(options)) || {};
       if (typeof cachedData[options.url] !== "undefined" && ! self.hasExpired(cachedData[options.url].timestamp, options.cacheType || 'default')) {
         options.successCallback(cachedData[options.url].data);

+ 57 - 27
desktop/core/src/desktop/static/desktop/js/sqlMetadata.js

@@ -22,46 +22,75 @@ var SqlMetadata = (function () {
     self.hasErrors = false;
 
     self.sourceType = options.sourceType;
-    self.path = options.path;
+    self.path = typeof options.path === 'string' ? options.path.split('.') : options.path;
 
     self.sourceMeta = undefined;
     self.navigatorMeta = undefined;
-    self.commentObservable = undefined;
 
-    self.lastNavigatorPromise = undefined;
     self.lastSourcePromise = undefined;
+    self.lastNavigatorPromise = undefined;
 
     self.silenceErrors = options.silenceErrors;
     self.cachedOnly = options.cachedOnly;
   }
 
-  var refreshCommentObservable = function (sqlMeta) {
-    if (sqlMeta.commentObservable) {
-      if (HAS_NAVIGATOR) {
-        sqlMeta.getNavigatorMeta().done(function () {
-          if (sqlMeta.navigatorMeta && sqlMeta.navigatorMeta.entity) {
-            sqlMeta.commentObservable(sqlMeta.navigatorMeta.entity.description || sqlMeta.navigatorMeta.entity.originalDescription);
-          } else {
-            sqlMeta.getSourceMeta().done(function () {
-              sqlMeta.commentObservable(sqlMeta.sourceMeta ? (sqlMeta.sourceMeta.comment || '') : '');
-            });
-          }
-        })
-      } else {
-        sqlMeta.getSourceMeta().done(function () {
-          sqlMeta.commentObservable(sqlMeta.sourceMeta ? (sqlMeta.sourceMeta.comment || '') : '');
-        });
-      }
+  SqlMetadata.prototype.getComment = function () {
+    var self = this;
+    var deferred = $.Deferred();
+
+    var resolveWithSourceMeta = function () {
+      self.getSourceMeta().done(function () {
+        deferred.resolve(self.sourceMeta && self.sourceMeta.comment || '');
+      });
+    };
+
+    if (HAS_NAVIGATOR) {
+      self.getNavigatorMeta().done(function () {
+        if (self.navigatorMeta && self.navigatorMeta.entity) {
+          deferred.resolve(self.navigatorMeta.entity.description || self.navigatorMeta.entity.originalDescription || '');
+        } else {
+          resolveWithSourceMeta();
+        }
+      }).fail(resolveWithSourceMeta)
+    } else {
+      resolveWithSourceMeta();
     }
+
+    return deferred.promise();
   };
 
-  SqlMetadata.prototype.getCommentObservable = function () {
+  SqlMetadata.prototype.setComment = function (comment) {
     var self = this;
-    if (!self.commentObservable) {
-      self.commentObservable = ko.observable();
-      refreshCommentObservable(self);
+    var deferred = $.Deferred();
+
+    if (HAS_NAVIGATOR) {
+      self.getNavigatorMeta().done(function () {
+        if (self.navigatorMeta && self.navigatorMeta.entity) {
+          ApiHelper.getInstance().updateNavigatorMetadata({
+            identity: self.navigatorMeta.entity.identity,
+            properties: {
+              description: comment
+            }
+          }).done(function () {
+            self.loadNavigatorMeta();
+            self.getComment().done(deferred.resolve);
+          }).fail(deferred.reject);
+        }
+      }).fail(deferred.reject);
+    } else {
+      ApiHelper.getInstance().updateSourceMetadata({
+        sourceType: self.sourceType,
+        path: self.path,
+        properties: {
+          comment: comment
+        }
+      }).done(function () {
+        self.loadSourceMeta(true);
+        self.getComment().done(deferred.resolve);
+      }).fail(deferred.reject);
     }
-    return self.commentObservable;
+
+    return deferred.promise();
   };
 
   SqlMetadata.prototype.getSourceMeta = function () {
@@ -109,14 +138,15 @@ var SqlMetadata = (function () {
     return self.sourceMeta && self.sourceMeta.type === 'array';
   };
 
-  SqlMetadata.prototype.loadSourceMeta = function () {
+  SqlMetadata.prototype.loadSourceMeta = function (refreshCache) {
     var self = this;
     self.lastSourcePromise = $.Deferred();
     ApiHelper.getInstance().fetchSourceMetadata({
       sourceType: self.sourceType,
       path: self.path,
       silenceErrors: self.silenceErrors,
-      cachedOnly: self.cachedOnly
+      cachedOnly: self.cachedOnly,
+      refreshCache: refreshCache
     }).done(function (data) {
       self.sourceMeta = data;
       self.loaded = true;