Browse Source

HUE-8330 [assist] Add polling and cancelling to execution analysis

Johan Ahlen 7 years ago
parent
commit
e21e506535

+ 51 - 5
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -53,6 +53,7 @@ var CancellablePromise = (function () {
 
 
   function CancellablePromise(deferred, request, otherCancellables) {
   function CancellablePromise(deferred, request, otherCancellables) {
     var self = this;
     var self = this;
+    self.cancelCallbacks = [];
     self.deferred = deferred;
     self.deferred = deferred;
     self.request = request;
     self.request = request;
     self.otherCancellables = otherCancellables;
     self.otherCancellables = otherCancellables;
@@ -92,9 +93,23 @@ var CancellablePromise = (function () {
     if (self.otherCancellables) {
     if (self.otherCancellables) {
       self.otherCancellables.forEach(function (cancellable) { if (cancellable.cancel) { cancellable.cancel() } });
       self.otherCancellables.forEach(function (cancellable) { if (cancellable.cancel) { cancellable.cancel() } });
     }
     }
+
+    while (self.cancelCallbacks.length) {
+      self.cancelCallbacks.pop()();
+    }
     return this;
     return this;
   };
   };
 
 
+  CancellablePromise.prototype.onCancel = function (callback) {
+    var self = this;
+    if (self.cancelled) {
+      callback();
+    } else {
+      self.cancelCallbacks.push(callback);
+    }
+    return self;
+  };
+
   CancellablePromise.prototype.then = function () {
   CancellablePromise.prototype.then = function () {
     var self = this;
     var self = this;
     self.deferred.then.apply(self.deferred, arguments);
     self.deferred.then.apply(self.deferred, arguments);
@@ -1956,15 +1971,46 @@ var ApiHelper = (function () {
    * @param {boolean} [options.silenceErrors]
    * @param {boolean} [options.silenceErrors]
    * @param {string} options.computeId
    * @param {string} options.computeId
    * @param {string} options.queryId
    * @param {string} options.queryId
-   * @return {Promise}
+   * @return {CancellablePromise}
    */
    */
   ApiHelper.prototype.fetchQueryExecutionAnalysis = function (options)  {
   ApiHelper.prototype.fetchQueryExecutionAnalysis = function (options)  {
     var self = this;
     var self = this;
     var url = '/metadata/api/workload_analytics/get_impala_query/';
     var url = '/metadata/api/workload_analytics/get_impala_query/';
-    return self.simplePost(url, {
-      'cluster_id': '"' + options.computeId + '"',
-      'query_id': '"' + options.queryId + '"'
-    }, options);
+    var deferred = $.Deferred();
+
+    var tries = 0;
+
+    var cancellablePromises = [];
+
+    var promise = new CancellablePromise(deferred, undefined, cancellablePromises);
+
+    var pollForAnalysis = function () {
+      if (tries === 10) {
+        deferred.reject();
+        return;
+      }
+      tries++;
+      cancellablePromises.pop(); // Remove the last one
+      cancellablePromises.push(deferred, self.simplePost(url, {
+        'cluster_id': '"' + options.computeId + '"',
+        'query_id': '"' + options.queryId + '"'
+      }, options).done(function (response) {
+        if (response && response.data) {
+          deferred.resolve(response.data)
+        } else {
+          var timeout = window.setTimeout(function () {
+            pollForAnalysis();
+          }, 1000 + tries * 500); // TODO: Adjust once fully implemented;
+          promise.onCancel(function () {
+            window.clearTimeout(timeout);
+          })
+        }
+      }).fail(deferred.reject));
+    };
+
+    pollForAnalysis();
+
+    return promise;
   };
   };
 
 
   /**
   /**

+ 7 - 3
desktop/core/src/desktop/templates/assist.mako

@@ -2960,10 +2960,15 @@ from desktop.views import _ko
           }
           }
         });
         });
 
 
+        var lastExecutionAnalysisPromise = undefined;
+
         var clearAnalysisSub = huePubSub.subscribe('assist.clear.execution.analysis', function() {
         var clearAnalysisSub = huePubSub.subscribe('assist.clear.execution.analysis', function() {
           if (!HAS_WORKLOAD_ANALYTICS) {
           if (!HAS_WORKLOAD_ANALYTICS) {
             return;
             return;
           }
           }
+          if (lastExecutionAnalysisPromise) {
+            lastExecutionAnalysisPromise.cancel();
+          }
           self.executionAnalysis(undefined);
           self.executionAnalysis(undefined);
         });
         });
 
 
@@ -2972,13 +2977,12 @@ from desktop.views import _ko
             return;
             return;
           }
           }
           self.loadingExecutionAnalysis(true);
           self.loadingExecutionAnalysis(true);
-          ApiHelper.getInstance().fetchQueryExecutionAnalysis({
+          lastExecutionAnalysisPromise = ApiHelper.getInstance().fetchQueryExecutionAnalysis({
             silenceErrors: true,
             silenceErrors: true,
             computeId: details.computeId,
             computeId: details.computeId,
             queryId: details.queryId
             queryId: details.queryId
           }).done(function (response) {
           }).done(function (response) {
-            if (response && response.data && response.data.query)
-            self.executionAnalysis(response.data.query)
+            self.executionAnalysis(response.query)
           }).always(function () {
           }).always(function () {
             self.loadingExecutionAnalysis(false);
             self.loadingExecutionAnalysis(false);
           });
           });