浏览代码

HUE-3756 [editor] Better update of the available queries in the History refresh

If we have a lot of available ones, the number of calls is huge.

How about refactoring it to be a clean 2 loops:

    one for the 'running' ones that checks every 30s
    one for the 'available' ones that checks every 5min

and we start both at the initial notebook page load.
Romain Rigaux 10 年之前
父节点
当前提交
ce66f01
共有 1 个文件被更改,包括 34 次插入32 次删除
  1. 34 32
      desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

+ 34 - 32
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1393,7 +1393,7 @@
       });
     };
 
-    self.fetchHistory = function () {
+    self.fetchHistory = function (callback) {
       self.loadingHistory(true);
       $.get("/notebook/api/get_history", {
         doc_type: self.selectedSnippet(),
@@ -1418,38 +1418,40 @@
         self.history(parsedHistory);
       }).always(function(){
         self.loadingHistory(false);
+        if (callback) {
+          callback();
+        }
       });
     };
 
-    self.updateHistoryRunning = false;
-    self.updateHistory = function () {
-      if (!self.updateHistoryRunning) {
-        var items = $.grep(self.history(), function (item) {
-          return item.status() == 'available' || item.status() == 'running' || item.status() == 'starting';
-        });
+    self.updateHistory = function (statuses, interval) {
+      var items = $.grep(self.history(), function (item) {
+        return statuses.indexOf(item.status()) != -1;
+      });
 
-        function updateHistoryCall(item) {
-          $.post("/notebook/api/check_status", {
-            notebook: ko.mapping.toJSON({id: item.uuid()}),
-          }, function (data) {
-            var status = data.status == -3 ? 'expired' : (data.status == 0 ? data.query_status.status : null);
-            if (status && item.status() != status) {
-              item.status(status);
-            }
-          }).always(function () {
-            if (items.length > 0) {
-              window.setTimeout(function () {
-                updateHistoryCall(items.pop());
-              }, 1000);
-            }
-            self.updateHistoryRunning = (items.length > 0);
-          });
-        }
+      function updateHistoryCall(item) {
+        $.post("/notebook/api/check_status", {
+          notebook: ko.mapping.toJSON({id: item.uuid()}),
+        }, function (data) {
+          var status = data.status == -3 ? 'expired' : (data.status == 0 ? data.query_status.status : null);
+          if (status && item.status() != status) {
+            item.status(status);
+          }
+        }).always(function () {
+          if (items.length > 0) {
+            window.setTimeout(function () {
+              updateHistoryCall(items.pop());
+            }, 1000);
+          } else {
+            window.setTimeout(function() { self.updateHistory(statuses, interval); }, interval);
+          }
+        });
+      }
 
-        if (items.length > 0) {
-          self.updateHistoryRunning = true;
-          updateHistoryCall(items.pop());
-        }
+     if (items.length > 0) {
+        updateHistoryCall(items.pop());
+      } else {
+        window.setTimeout(function() { self.updateHistory(statuses, interval); }, interval);
       }
     };
 
@@ -1545,9 +1547,10 @@
         self.addSnippet(snippet);
       });
       if (vm.editorMode && self.history().length == 0) {
-        self.fetchHistory();
-        window.clearInterval(vm.updateHistoryInterval);
-        vm.updateHistoryInterval = window.setInterval(self.updateHistory, 20000);
+        self.fetchHistory(function() {
+          self.updateHistory(['starting', 'running'], 20000);
+          self.updateHistory(['available'], 60000 * 5);
+        });
       }
     }
   };
@@ -1650,7 +1653,6 @@
     self.availableSnippets = ko.mapping.fromJS(options.languages);
 
     self.editorMode = options.mode == 'editor';
-    self.updateHistoryInterval = null;
 
     self.getSnippetViewSettings = function (snippetType) {
       if (options.snippetViewSettings[snippetType]) {