spark.vm.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 Snippet = function (notebook, snippet) {
  38. var self = this;
  39. self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
  40. self.type = ko.observable(typeof snippet.type != "undefined" && snippet.type != null ? snippet.type : 'hive');
  41. self.statement = ko.observable('');
  42. self.status = ko.observable('loading');
  43. self.klass = ko.computed(function(){
  44. return 'results ' + self.type();
  45. });
  46. self.result = new Result(snippet, snippet.result);
  47. // init()
  48. // checkStatus()
  49. self.create_session = function() {
  50. $.post("/spark/api/create_session", {
  51. notebook: ko.mapping.toJSON(notebook),
  52. snippet: ko.mapping.toJSON(self)
  53. }, function (data) {
  54. if (data.status == 0) {
  55. notebook.sessions.push(ko.mapping.fromJS(data.session));
  56. self.status('ready');
  57. }
  58. else {
  59. $(document).trigger("error", data.message);
  60. }
  61. }).fail(function (xhr, textStatus, errorThrown) {
  62. $(document).trigger("error", xhr.responseText);
  63. });
  64. };
  65. self.execute = function() {
  66. $(".jHueNotify").hide();
  67. logGA('/execute/' + self.type());
  68. self.result.clear();
  69. self.status('running');
  70. $.post("/spark/api/execute", {
  71. notebook: ko.mapping.toJSON(notebook),
  72. snippet: ko.mapping.toJSON(self)
  73. }, function (data) {
  74. if (data.status == 0) {
  75. $.each(data.handle, function(key, val) {
  76. self.result.handle()[key] = val;
  77. });
  78. self.checkStatus();
  79. }
  80. else {
  81. $(document).trigger("error", data.message);
  82. }
  83. }).fail(function (xhr, textStatus, errorThrown) {
  84. $(document).trigger("error", xhr.responseText);
  85. });
  86. };
  87. self.checkStatus = function() {
  88. $.post("/spark/api/check_status", {
  89. notebook: ko.mapping.toJSON(notebook),
  90. snippet: ko.mapping.toJSON(self)
  91. }, function (data) {
  92. if (data.status == 0) {
  93. self.status(data.query_status.status);
  94. if (self.status() == 'running') {
  95. setTimeout(self.checkStatus, 1000);
  96. } else {
  97. self.fetchResult();
  98. }
  99. }
  100. else {
  101. $(document).trigger("error", data.message);
  102. }
  103. }).fail(function (xhr, textStatus, errorThrown) {
  104. $(document).trigger("error", xhr.responseText);
  105. });
  106. };
  107. self.fetchResult = function() {
  108. $.post("/spark/api/fetch_result", {
  109. notebook: ko.mapping.toJSON(notebook),
  110. snippet: ko.mapping.toJSON(self)
  111. }, function (data) {
  112. if (data.status == 0) {
  113. self.result.meta(data.result.meta);
  114. self.result.data(data.result.data);
  115. // move resultsets to n rows
  116. // check if N rows fetched...
  117. }
  118. else {
  119. $(document).trigger("error", data.message);
  120. }
  121. }).fail(function (xhr, textStatus, errorThrown) {
  122. $(document).trigger("error", xhr.responseText);
  123. });
  124. };
  125. self.fetchResultMetadata = function() {
  126. };
  127. self.cancel = function() {
  128. };
  129. }
  130. var Notebook = function (vm, notebook) {
  131. var self = this;
  132. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : UUID());
  133. self.snippets = ko.observableArray();
  134. self.selectedSnippet = ko.observable('scala');
  135. self.availableSnippets = ko.observableArray(['hive', 'scala', 'sql', 'python', 'pig', 'impala']); // presto, mysql, oracle, sqlite, postgres, phoenix
  136. self.sessions = ko.observableArray(); // {'hive': ..., scala: ...}
  137. self.getSession = function(session_type) {
  138. var _s = null;
  139. $.each(self.sessions(), function (index, s) {
  140. if (s.type() == session_type) {
  141. _s = s;
  142. return false;
  143. }
  144. });
  145. return _s;
  146. };
  147. self.addSnippet = function(snippet) {
  148. var _snippet = new Snippet(self, snippet);
  149. self.snippets.push(_snippet);
  150. if (self.getSession(self.selectedSnippet()) == null) {
  151. _snippet.create_session();
  152. }
  153. }
  154. self.newSnippet = function() {
  155. var snippet = new Snippet(self, {type: self.selectedSnippet()});
  156. self.snippets.push(snippet);
  157. if (self.getSession(self.selectedSnippet()) == null) {
  158. snippet.create_session();
  159. }
  160. }
  161. if (notebook.snippets) {
  162. $.each(notebook.snippets, function(index, snippet) {
  163. self.addSnippet(snippet);
  164. });
  165. }
  166. }
  167. function EditorViewModel(notebooks) {
  168. var self = this;
  169. self.notebooks = ko.observableArray();
  170. self.selectedNotebook = ko.observable();
  171. self.isEditing = ko.observable(false);
  172. self.toggleEditing = function () {
  173. self.isEditing(! self.isEditing());
  174. };
  175. // function bareWidgetBuilder(name, type){
  176. // return new Widget({
  177. // size: 12,
  178. // id: UUID(),
  179. // name: name,
  180. // widgetType: type
  181. // });
  182. // }
  183. //
  184. // self.draggableHive = ko.observable(bareWidgetBuilder("Hive Query", "hive-widget"));
  185. self.init = function() {
  186. $.each(notebooks, function(index, notebook) {
  187. self.loadNotebook(notebook);
  188. if (self.selectedNotebook() == null){
  189. self.selectedNotebook(self.notebooks()[0]);
  190. }
  191. });
  192. }
  193. self.loadNotebook = function(notebook) {
  194. self.notebooks.push(new Notebook(self, notebook));
  195. }
  196. self.newNotebook = function() {
  197. self.notebooks.push(new Notebook(self, {}));
  198. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  199. }
  200. self.save = function() {
  201. };
  202. }
  203. function logGA(page) {
  204. if (typeof trackOnGA == 'function') {
  205. trackOnGA('editor/' + page);
  206. }
  207. }