spark.vm.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function s4() {
  17. return Math.floor((1 + Math.random()) * 0x10000)
  18. .toString(16)
  19. .substring(1);
  20. }
  21. function UUID() {
  22. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  23. }
  24. var Result = function (snippet, result) {
  25. var self = this;
  26. self.id = ko.observable(typeof result != "undefined" && result.id != "undefined" && result.id != null ? result.id : UUID());
  27. self.type = ko.observable('table');
  28. self.handle = ko.observable({});
  29. self.meta = ko.observableArray();
  30. self.data = ko.observableArray();
  31. self.clear = function() {
  32. //self.handle = ko.observable({});
  33. self.meta.removeAll();
  34. self.data.removeAll();
  35. };
  36. }
  37. var TYPE_EDITOR_MAP = {
  38. 'hive': 'text/x-hiveql',
  39. 'impala': 'text/x-impalaql',
  40. 'python': 'text/x-python',
  41. 'scala': 'text/x-scala',
  42. 'pig': 'text/x-pig'
  43. }
  44. var Snippet = function (notebook, snippet) {
  45. var self = this;
  46. self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
  47. self.type = ko.observable(typeof snippet.type != "undefined" && snippet.type != null ? snippet.type : 'hive');
  48. self.editorMode = ko.observable(TYPE_EDITOR_MAP[self.type()]);
  49. self.statement = ko.observable('');
  50. self.status = ko.observable('loading');
  51. self.klass = ko.computed(function(){
  52. return 'results ' + self.type();
  53. });
  54. self.result = new Result(snippet, snippet.result);
  55. // init()
  56. // checkStatus()
  57. self.create_session = function() {
  58. $.post("/spark/api/create_session", {
  59. notebook: ko.mapping.toJSON(notebook),
  60. snippet: ko.mapping.toJSON(self)
  61. }, function (data) {
  62. if (data.status == 0) {
  63. notebook.sessions.push(ko.mapping.fromJS(data.session));
  64. self.status('ready');
  65. }
  66. else {
  67. $(document).trigger("error", data.message);
  68. }
  69. }).fail(function (xhr, textStatus, errorThrown) {
  70. $(document).trigger("error", xhr.responseText);
  71. });
  72. };
  73. self.execute = function() {
  74. $(".jHueNotify").hide();
  75. logGA('/execute/' + self.type());
  76. self.result.clear();
  77. self.status('running');
  78. $.post("/spark/api/execute", {
  79. notebook: ko.mapping.toJSON(notebook),
  80. snippet: ko.mapping.toJSON(self)
  81. }, function (data) {
  82. if (data.status == 0) {
  83. $.each(data.handle, function(key, val) {
  84. self.result.handle()[key] = val;
  85. });
  86. self.checkStatus();
  87. }
  88. else {
  89. $(document).trigger("error", data.message);
  90. }
  91. }).fail(function (xhr, textStatus, errorThrown) {
  92. $(document).trigger("error", xhr.responseText);
  93. });
  94. };
  95. self.checkStatus = function() {
  96. $.post("/spark/api/check_status", {
  97. notebook: ko.mapping.toJSON(notebook),
  98. snippet: ko.mapping.toJSON(self)
  99. }, function (data) {
  100. if (data.status == 0) {
  101. self.status(data.query_status.status);
  102. if (self.status() == 'running') {
  103. setTimeout(self.checkStatus, 1000);
  104. } else {
  105. self.fetchResult();
  106. }
  107. }
  108. else {
  109. $(document).trigger("error", data.message);
  110. }
  111. }).fail(function (xhr, textStatus, errorThrown) {
  112. $(document).trigger("error", xhr.responseText);
  113. });
  114. };
  115. self.fetchResult = function() {
  116. $.post("/spark/api/fetch_result", {
  117. notebook: ko.mapping.toJSON(notebook),
  118. snippet: ko.mapping.toJSON(self)
  119. }, function (data) {
  120. if (data.status == 0) {
  121. self.result.meta(data.result.meta);
  122. self.result.data(data.result.data);
  123. // move resultsets to n rows
  124. // check if N rows fetched...
  125. }
  126. else {
  127. $(document).trigger("error", data.message);
  128. }
  129. }).fail(function (xhr, textStatus, errorThrown) {
  130. $(document).trigger("error", xhr.responseText);
  131. });
  132. };
  133. self.fetchResultMetadata = function() {
  134. };
  135. self.cancel = function() {
  136. };
  137. }
  138. var Notebook = function (vm, notebook) {
  139. var self = this;
  140. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : UUID());
  141. self.snippets = ko.observableArray();
  142. self.selectedSnippet = ko.observable('scala');
  143. self.availableSnippets = ko.observableArray(['hive', 'scala', 'sql', 'python', 'pig', 'impala']); // presto, mysql, oracle, sqlite, postgres, phoenix
  144. self.sessions = ko.observableArray(); // {'hive': ..., scala: ...}
  145. self.getSession = function(session_type) {
  146. var _s = null;
  147. $.each(self.sessions(), function (index, s) {
  148. if (s.type() == session_type) {
  149. _s = s;
  150. return false;
  151. }
  152. });
  153. return _s;
  154. };
  155. self.addSnippet = function(snippet) {
  156. var _snippet = new Snippet(self, snippet);
  157. self.snippets.push(_snippet);
  158. if (self.getSession(self.selectedSnippet()) == null) {
  159. _snippet.create_session();
  160. }
  161. }
  162. self.newSnippet = function() {
  163. var snippet = new Snippet(self, {type: self.selectedSnippet()});
  164. self.snippets.push(snippet);
  165. if (self.getSession(self.selectedSnippet()) == null) {
  166. snippet.create_session();
  167. }
  168. }
  169. if (notebook.snippets) {
  170. $.each(notebook.snippets, function(index, snippet) {
  171. self.addSnippet(snippet);
  172. });
  173. }
  174. }
  175. function EditorViewModel(notebooks) {
  176. var self = this;
  177. self.notebooks = ko.observableArray();
  178. self.selectedNotebook = ko.observable();
  179. self.isEditing = ko.observable(false);
  180. self.toggleEditing = function () {
  181. self.isEditing(! self.isEditing());
  182. };
  183. // function bareWidgetBuilder(name, type){
  184. // return new Widget({
  185. // size: 12,
  186. // id: UUID(),
  187. // name: name,
  188. // widgetType: type
  189. // });
  190. // }
  191. //
  192. // self.draggableHive = ko.observable(bareWidgetBuilder("Hive Query", "hive-widget"));
  193. self.init = function() {
  194. $.each(notebooks, function(index, notebook) {
  195. self.loadNotebook(notebook);
  196. if (self.selectedNotebook() == null){
  197. self.selectedNotebook(self.notebooks()[0]);
  198. }
  199. });
  200. }
  201. self.loadNotebook = function(notebook) {
  202. self.notebooks.push(new Notebook(self, notebook));
  203. }
  204. self.newNotebook = function() {
  205. self.notebooks.push(new Notebook(self, {}));
  206. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  207. }
  208. self.save = function() {
  209. };
  210. }
  211. function logGA(page) {
  212. if (typeof trackOnGA == 'function') {
  213. trackOnGA('editor/' + page);
  214. }
  215. }