Browse Source

[notebook] Create a hook to display results coming from API without async execution

Romain Rigaux 10 years ago
parent
commit
e4aca666b0

+ 3 - 1
desktop/libs/notebook/src/notebook/connectors/base.py

@@ -125,6 +125,8 @@ class Api(object):
   def close_session(self, session):
     pass
 
-
   def fetch_result(self, notebook, snippet, rows, start_over):
     pass
+
+  def get_log(self, notebook, snippet, startFrom=None, size=None):
+    return 'No logs'

+ 25 - 20
desktop/libs/notebook/src/notebook/connectors/jdbc.py

@@ -20,8 +20,9 @@ import re
 
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.i18n import force_unicode
+from django.utils.translation import ugettext as _
 
-from notebook.connectors.base import Api, QueryError, QueryExpired
+from notebook.connectors.base import Api, QueryError
 
 import jaydebeapi
 
@@ -36,7 +37,7 @@ def query_error_handler(func):
     except Exception, e:
       message = force_unicode(str(e))
       if 'Class com.mysql.jdbc.Driver not found' in message:
-        raise QueryExpired(_('%s: did you export CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar?') % message)
+        raise QueryError(_('%s: did you export CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar?') % message)
       else:
         raise QueryError(message)
   return decorator
@@ -47,6 +48,7 @@ class JDBCApi(Api):
   # TODO
   # async with queuing system
   # impersonation / prompting for username/password
+  @query_error_handler
   def execute(self, notebook, snippet):
     user = 'root'
     password = 'root'
@@ -67,28 +69,32 @@ class JDBCApi(Api):
     curs = db.cursor()
     curs.execute(snippet['statement'])
 
-    print curs.description
+    data = curs.fetchmany(100)
+    description = curs.description
+    
+    curs.close()
+    db.close()
     
     return {
-      'result': curs.fetchmany(100)
+      'sync': True,
+      'result': {
+        'has_more': False,
+        'data': list(data),
+        'meta': [{
+          'name': column[0],
+          'type': 'TODO',
+          'comment': ''
+        } for column in description],
+        'type': 'table'
+      }
     }
 
   @query_error_handler
   def check_status(self, notebook, snippet):
-    return {'status': 'running'}
+    return {'status': 'available'}
 
   def _fetch_result(self, cursor):
-
-    return {
-        'has_more': results.has_more,
-        'data': list(results.rows()),
-        'meta': [{
-          'name': column.name,
-          'type': column.type,
-          'comment': column.comment
-        } for column in results.data_table.cols()],
-        'type': 'table'
-    }
+    return {}
 
   @query_error_handler
   def fetch_result_metadata(self):
@@ -98,13 +104,12 @@ class JDBCApi(Api):
   def cancel(self, notebook, snippet):
     return {'status': 0}
 
-  @query_error_handler
-  def get_log(self, notebook, snippet, startFrom=None, size=None):
-    return 'No logs'
-
   def download(self, notebook, snippet, format):
     raise PopupException('Downloading is not supported yet')
 
+  def _get_jobs(self, logs):
+    return []
+
   def _progress(self, snippet, logs):
     if snippet['type'] == 'hive':
       match = re.search('Total jobs = (\d+)', logs, re.MULTILINE)

+ 40 - 30
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -405,8 +405,14 @@ var Snippet = function (vm, notebook, snippet) {
         self.result.clear();
         self.result.handle(data.handle);
         self.result.hasResultset(data.handle.has_result_set);
-        self.checkStatus();
-      } else {console.log('aaa');
+        if (data.handle.sync) {
+          self.loadData(data.handle, 100);
+          self.status('success');
+          self.progress(100);
+        } else {
+          self.checkStatus();
+        }
+      } else {
         self._ajaxError(data, self.execute);
       }
     }).fail(function (xhr, textStatus, errorThrown) {
@@ -431,34 +437,7 @@ var Snippet = function (vm, notebook, snippet) {
       startOver: startOver
     }, function (data) {
       if (data.status == 0) {
-        rows -= data.result.data.length;
-
-        var _initialIndex = self.result.data().length;
-        var _tempData = [];
-        $.each(data.result.data, function (index, row) {
-          row.unshift(_initialIndex + index);
-          self.result.data.push(row);
-          _tempData.push(row);
-        });
-
-        self.result.images(typeof data.result.images != "undefined" && data.result.images != null ? data.result.images : []);
-
-        $(document).trigger("renderData", {data: _tempData, snippet: self, initial: _initialIndex == 0});
-
-        if (!self.result.fetchedOnce()) {
-          data.result.meta.unshift({type: "INT_TYPE", name: "", comment: null});
-          self.result.meta(data.result.meta);
-          self.result.type(data.result.type);
-          self.result.fetchedOnce(true);
-        }
-
-        if (data.result.has_more && rows > 0) {
-          setTimeout(function () {
-            self.fetchResultData(rows, false);
-          }, 500);
-        } else if (notebook.snippets()[notebook.snippets().length - 1] == self) {
-          notebook.newSnippet();
-        }
+        self.loadData(data, rows);
       } else {
         self._ajaxError(data);
         $(document).trigger("renderDataError", {snippet: self});
@@ -468,6 +447,37 @@ var Snippet = function (vm, notebook, snippet) {
     });
   };
 
+  self.loadData = function (data, rows) {
+    rows -= data.result.data.length;
+
+    var _initialIndex = self.result.data().length;
+    var _tempData = [];
+    $.each(data.result.data, function (index, row) {
+      row.unshift(_initialIndex + index);
+      self.result.data.push(row);
+      _tempData.push(row);
+    });
+
+    self.result.images(typeof data.result.images != "undefined" && data.result.images != null ? data.result.images : []);
+
+    $(document).trigger("renderData", {data: _tempData, snippet: self, initial: _initialIndex == 0});
+
+    if (! self.result.fetchedOnce()) {
+      data.result.meta.unshift({type: "INT_TYPE", name: "", comment: null});
+      self.result.meta(data.result.meta);
+      self.result.type(data.result.type);
+      self.result.fetchedOnce(true);
+    }
+
+    if (data.result.has_more && rows > 0) {
+      setTimeout(function () {
+        self.fetchResultData(rows, false);
+      }, 500);
+    } else if (notebook.snippets()[notebook.snippets().length - 1] == self) {
+      notebook.newSnippet();
+    }
+  };
+  
   self.fetchResultMetadata = function () {
     $.post("/notebook/api/fetch_result_metadata", {
       notebook: ko.mapping.toJSON(notebook.getContext()),