فهرست منبع

HUE-5469 [editor] Check Hive query complexity in the background

Johan Ahlen 9 سال پیش
والد
کامیت
b780ba15ea

+ 59 - 27
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -378,7 +378,7 @@ var EditorViewModel = (function() {
         query: self.queriesFilter(),
         include_trashed: false
       });
-    }
+    };
 
     var lastQueriesPage = 1;
     self.currentQueryTab.subscribe(function (newValue) {
@@ -443,7 +443,7 @@ var EditorViewModel = (function() {
           self._ajaxError(data);
         }
       });
-    }
+    };
     self.associatedDocumentLoading = ko.observable(true);
     self.associatedDocument = ko.observable();
     self.associatedDocumentUuid = ko.observable(typeof snippet.associatedDocumentUuid != "undefined" && snippet.associatedDocumentUuid != null ? snippet.associatedDocumentUuid : null);
@@ -668,14 +668,6 @@ var EditorViewModel = (function() {
 
     self.is_redacted = ko.observable(typeof snippet.is_redacted != "undefined" && snippet.is_redacted != null ? snippet.is_redacted : false);
 
-    self.complexity = ko.observable();
-    self.hasComplexity = ko.computed(function () {
-      return self.complexity();
-    });
-
-    self.suggestion = ko.observable(typeof snippet.complexity != "undefined" && snippet.complexity != null ? snippet.complexity : '');
-    self.hasSuggestion = ko.observable(false);
-
     self.chartType = ko.observable(typeof snippet.chartType != "undefined" && snippet.chartType != null ? snippet.chartType : ko.HUE_CHARTS.TYPES.BARCHART);
     self.chartType.subscribe(prepopulateChart);
     self.chartSorting = ko.observable(typeof snippet.chartSorting != "undefined" && snippet.chartSorting != null ? snippet.chartSorting : "none");
@@ -783,6 +775,62 @@ var EditorViewModel = (function() {
       };
     };
 
+    self.complexity = ko.observable();
+    self.hasComplexity = ko.computed(function () {
+      return self.complexity();
+    });
+
+    self.suggestion = ko.observable(typeof snippet.complexity != "undefined" && snippet.complexity != null ? snippet.complexity : '');
+    self.hasSuggestion = ko.observable(false);
+
+    if (HAS_OPTIMIZER) {
+      var lastRequest;
+      var lastCheckedStatement;
+
+      var checkComplexity = function () {
+        if (lastCheckedStatement === self.statement_raw()) {
+          return;
+        }
+
+        if (lastRequest && lastRequest.readyState < 4) {
+          lastRequest.abort();
+        }
+        self.complexity(null);
+
+        logGA('get_query_risk');
+        lastRequest = $.ajax({
+          type: 'POST',
+          url: '/notebook/api/optimizer/statement/risk',
+          timeout: 10000, // 10 seconds
+          data: {
+            notebook: ko.mapping.toJSON(notebook.getContext()),
+            snippet: ko.mapping.toJSON(self.getContext())
+          },
+          success: function(data) {
+            if (data.status == 0) {
+              self.complexity(ko.mapping.fromJS(data.query_complexity));
+            } else {
+              // TODO: Silence errors
+              $(document).trigger('error', data.message);
+            }
+            lastCheckedStatement = self.statement_raw();
+          }
+        });
+      };
+
+      var changeThrottle = -1;
+
+      self.statement_raw.subscribe(function () {
+        if (self.type() === 'hive') {
+          window.clearTimeout(changeThrottle);
+          changeThrottle = window.setTimeout(checkComplexity, 2000);
+        }
+      });
+      if (self.statement_raw()) {
+        changeThrottle = window.setTimeout(checkComplexity, 2000);
+      }
+    }
+
     self._ajaxError = function (data, callback) {
       if (data.status == -2) { // Session expired
         var existingSession = notebook.getSession(self.type());
@@ -1056,7 +1104,7 @@ var EditorViewModel = (function() {
           self._ajaxError(data);
         }
       });
-    }
+    };
 
     self.queryCompatibility = function () {
       logGA('compatibility');
@@ -1376,22 +1424,6 @@ var EditorViewModel = (function() {
       });
     };
 
-    self.getComplexity = function () {
-      logGA('get_query_risk');
-      self.complexity(null);
-
-      $.post("/notebook/api/optimizer/statement/risk", {
-        notebook: ko.mapping.toJSON(notebook.getContext()),
-        snippet: ko.mapping.toJSON(self.getContext())
-      }, function(data) {
-        if (data.status == 0) {
-          self.complexity(ko.mapping.fromJS(data.query_complexity));
-        } else {
-          $(document).trigger("error", data.message);
-        }
-      });
-    };
-
     self.loadQueryHistory = function (n) {
       logGA('load_query_history');
 

+ 1 - 6
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -2203,18 +2203,13 @@ ${ hueIcons.symbols() }
             <i class="fa fa-fw fa-eraser"></i> ${_('Clear')}
           </a>
         </li>
-        <!-- ko if: $root.isOptimizerEnabled -->
+        <!-- ko if: HAS_OPTIMIZER -->
         <li class="divider"></li>
         <li>
           <a href="javascript:void(0)" data-bind="click: queryCompatibility" title="${ _('Get Impala compatibility hints') }">
             <i class="fa fa-fw fa-random"></i> ${_('Check Impala compatibility')}
           </a>
         </li>
-        <li>
-          <a href="javascript:void(0)" data-bind="click: getComplexity" title="${ _('Get recommendations on query risks and optimizations') }">
-            <i class="fa fa-fw fa-check"></i> ${_('Check complexity')}
-          </a>
-        </li>
         <li>
           <a href="javascript:void(0)" data-bind="click: getSimilarQueries" title="${ _('Expand query with similar queries suggestions') }">
             <i class="fa fa-fw fa-comments"></i> ${_('Show similarities')}