浏览代码

HUE-7079 [editor] Keep the last 50 risk responses and clear the risks on statement change

With this the editor will keep track of the previous 50 risk responses identified by the statement hash, it will also clear the assistant panel suggestions immediately after statement change.
Johan Ahlen 8 年之前
父节点
当前提交
985f816f1d

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

@@ -2142,8 +2142,10 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, get_ord
         var activeLocationsSub = huePubSub.subscribe('editor.active.locations', handleLocationUpdate);
 
         var activeRisksSub = huePubSub.subscribe('editor.active.risks', function (details) {
-          self.activeRisks(details.risks);
-          self.activeEditor(details.editor);
+          if (details.risks !== self.activeRisks()) {
+            self.activeRisks(details.risks);
+            self.activeEditor(details.editor);
+          }
         });
 
         huePubSub.publish('editor.get.active.risks', function (details) {

+ 82 - 31
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1012,9 +1012,59 @@ var EditorViewModel = (function() {
     if (HAS_OPTIMIZER && ! vm.isNotificationManager()) {
       var lastComplexityRequest;
       var lastCheckedComplexityStatement;
+      var knownResponses = [];
 
       self.delayedStatement = ko.pureComputed(self.statement).extend({ rateLimit: { method: "notifyWhenChangesStop", timeout: 2000 } });
 
+      var handleRiskResponse = function(data) {
+        if (data.status == 0) {
+          self.hasSuggestion('');
+          self.complexity(data.query_complexity);
+        } else {
+          self.hasSuggestion('error');
+          self.complexity({'hints': []});
+        }
+        huePubSub.publish('editor.active.risks', {
+          editor: self.ace(),
+          risks: self.complexity() || {}
+        });
+        lastCheckedComplexityStatement = self.statement();
+      };
+
+      var clearActiveRisks = function () {
+        if (self.hasSuggestion() !== null && typeof self.hasSuggestion() !== 'undefined') {
+          self.hasSuggestion(null);
+        }
+
+        if (self.suggestion() !== '') {
+          self.suggestion('');
+        }
+
+        if (self.complexity() !== {}) {
+          self.complexity(undefined);
+          huePubSub.publish('editor.active.risks', {
+            editor: self.ace(),
+            risks: {}
+          });
+        }
+      };
+
+      self.positionStatement.subscribe(function (newStatement) {
+        if (newStatement) {
+          var hash = newStatement.statement.hashCode();
+          var unknownResponse = knownResponses.every(function (knownResponse) {
+            if (knownResponse.hash === hash) {
+              handleRiskResponse(knownResponse.data);
+              return false;
+            }
+            return true;
+          });
+          if (unknownResponse) {
+            clearActiveRisks();
+          }
+        }
+      });
+
       self.checkComplexity = function () {
         if (lastCheckedComplexityStatement === self.statement()) {
           return;
@@ -1023,45 +1073,46 @@ var EditorViewModel = (function() {
         self.getApiHelper().cancelActiveRequest(lastComplexityRequest);
 
         hueAnalytics.log('notebook', 'get_query_risk');
-        self.hasSuggestion(null);
-        self.complexity({});
-        huePubSub.publish('editor.active.risks', {
-          editor: self.ace(),
-          risks: {}
-        });
+        clearActiveRisks();
 
         var changeSubscription = self.statement.subscribe(function () {
           changeSubscription.dispose();
           self.getApiHelper().cancelActiveRequest(lastComplexityRequest);
         });
 
-        lastComplexityRequest = $.ajax({
-          type: 'POST',
-          url: '/notebook/api/optimizer/statement/risk',
-          timeout: 30000, // 30 seconds
-          data: {
-            notebook: ko.mapping.toJSON(notebook.getContext()),
-            snippet: ko.mapping.toJSON(self.getContext())
-          },
-          success: function(data) {
-            if (data.status == 0) {
-              self.hasSuggestion('');
-              self.complexity(data.query_complexity);
-            } else {
-              self.hasSuggestion('error');
-              self.complexity({'hints': []});
-            }
-            huePubSub.publish('editor.active.risks', {
-              editor: self.ace(),
-              risks: self.complexity() || {}
-            });
-            lastCheckedComplexityStatement = self.statement();
-          },
-          always: function(data) {
-            changeSubscription.dispose();
+        var hash = self.statement().hashCode();
+
+        var unknownResponse = knownResponses.every(function (knownResponse) {
+          if (knownResponse.hash === hash) {
+            handleRiskResponse(knownResponse.data);
+            return false;
           }
+          return true;
         });
-
+        if (unknownResponse) {
+          lastComplexityRequest = $.ajax({
+            type: 'POST',
+            url: '/notebook/api/optimizer/statement/risk',
+            timeout: 30000, // 30 seconds
+            data: {
+              notebook: ko.mapping.toJSON(notebook.getContext()),
+              snippet: ko.mapping.toJSON(self.getContext())
+            },
+            success: function (data) {
+              knownResponses.unshift({
+                hash: hash,
+                data: data
+              });
+              if (knownResponses.length > 50) {
+                knownResponses.pop();
+              }
+              handleRiskResponse(data);
+            },
+            always: function(data) {
+              changeSubscription.dispose();
+            }
+          });
+        }
       };
 
       if (self.type() === 'hive' || self.type() === 'impala') {