spark.vm.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. var Result = function (snippet, result) {
  17. var self = this;
  18. self.id = ko.observable(typeof result.id != "undefined" && result.id != "undefined" && result.id != null ? result.id : UUID());
  19. self.type = ko.observable(typeof result.type != "undefined" && result.type != "undefined" && result.type != null ? result.type : 'table');
  20. self.handle = ko.observable({});
  21. self.meta = ko.mapping.fromJS(typeof result.meta != "undefined" && result.meta != null ? result.meta : []);
  22. self.data = ko.mapping.fromJS(typeof result.data != "undefined" && result.data != null ? result.data : []);
  23. if (typeof result.handle != "undefined" && result.handle != null) {
  24. $.each(result.handle, function(key, val) {
  25. self.handle()[key] = val;
  26. });
  27. }
  28. self.clear = function() {
  29. $.each(self.handle, function(key, val) {
  30. delete self.handle()[key];
  31. });
  32. self.meta.removeAll();
  33. self.data.removeAll();
  34. };
  35. }
  36. var TYPE_EDITOR_MAP = {
  37. 'hive': 'text/x-hiveql',
  38. 'impala': 'text/x-impalaql',
  39. 'python': 'text/x-python',
  40. 'scala': 'text/x-scala',
  41. 'pig': 'text/x-pig'
  42. }
  43. var Snippet = function (notebook, snippet) {
  44. var self = this;
  45. self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
  46. self.type = ko.observable(typeof snippet.type != "undefined" && snippet.type != null ? snippet.type : "hive");
  47. self.editorMode = ko.observable(TYPE_EDITOR_MAP[self.type()]);
  48. self.statement = ko.observable(typeof snippet.statement != "undefined" && snippet.statement != null ? snippet.statement : '');
  49. self.status = ko.observable(typeof snippet.status != "undefined" && snippet.status != null ? snippet.status : 'loading');
  50. self.variables = ko.computed(function() {
  51. return self.statement().match(/\$[^\d'"](\w*)/g);
  52. });
  53. self.result = new Result(snippet, snippet.result);
  54. self.size = ko.observable(typeof snippet.size != "undefined" && snippet.size != null ? snippet.size : 12).extend({ numeric: 0 });
  55. self.offset = ko.observable(typeof snippet.offset != "undefined" && snippet.offset != null ? snippet.offset : 0).extend({ numeric: 0 });
  56. self.isLoading = ko.computed(function(){
  57. return self.status() == "loading";
  58. });
  59. self.klass = ko.computed(function () {
  60. return "snippet card card-widget";
  61. });
  62. self.editorKlass = ko.computed(function(){
  63. return "editor span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  64. });
  65. self.resultsKlass = ko.computed(function(){
  66. return "results " + self.type();
  67. });
  68. self.expand = function () {
  69. self.size(self.size() + 1);
  70. $("#snippet_" + self.id()).trigger("resize");
  71. }
  72. self.compress = function () {
  73. self.size(self.size() - 1);
  74. $("#snippet_" + self.id()).trigger("resize");
  75. }
  76. self.moveLeft = function () {
  77. self.offset(self.offset() - 1);
  78. }
  79. self.moveRight = function () {
  80. self.offset(self.offset() + 1);
  81. }
  82. self.remove = function (notebook, snippet) {
  83. notebook.snippets.remove(snippet);
  84. }
  85. // init()
  86. // checkStatus()
  87. self.create_session = function() {
  88. $.post("/spark/api/create_session", {
  89. notebook: ko.mapping.toJSON(notebook),
  90. snippet: ko.mapping.toJSON(self)
  91. }, function (data) {
  92. if (data.status == 0) {
  93. notebook.addSession(ko.mapping.fromJS(data.session));
  94. self.status('ready');
  95. }
  96. else {
  97. $(document).trigger("error", data.message);
  98. }
  99. }).fail(function (xhr, textStatus, errorThrown) {
  100. $(document).trigger("error", xhr.responseText);
  101. });
  102. };
  103. self.execute = function() {
  104. $(".jHueNotify").hide();
  105. logGA('/execute/' + self.type());
  106. self.result.clear();
  107. self.status('running');
  108. $.post("/spark/api/execute", {
  109. notebook: ko.mapping.toJSON(notebook),
  110. snippet: ko.mapping.toJSON(self)
  111. }, function (data) {
  112. if (data.status == 0) {
  113. $.each(data.handle, function(key, val) {
  114. self.result.handle()[key] = val;
  115. });
  116. self.checkStatus();
  117. }
  118. else if (data.status == -2) {
  119. self.create_session();
  120. } else {
  121. $(document).trigger("error", data.message);
  122. }
  123. }).fail(function (xhr, textStatus, errorThrown) {
  124. $(document).trigger("error", xhr.responseText);
  125. });
  126. };
  127. self.checkStatus = function() {
  128. $.post("/spark/api/check_status", {
  129. notebook: ko.mapping.toJSON(notebook),
  130. snippet: ko.mapping.toJSON(self)
  131. }, function (data) {
  132. if (data.status == 0) {
  133. self.status(data.query_status.status);
  134. if (self.status() == 'running') {
  135. setTimeout(self.checkStatus, 1000);
  136. } else {
  137. self.fetchResult();
  138. }
  139. } else if (data.status == -2) {
  140. self.create_session();
  141. } else {
  142. $(document).trigger("error", data.message);
  143. }
  144. }).fail(function (xhr, textStatus, errorThrown) {
  145. $(document).trigger("error", xhr.responseText);
  146. });
  147. };
  148. self.fetchResult = function() {
  149. $.post("/spark/api/fetch_result", {
  150. notebook: ko.mapping.toJSON(notebook),
  151. snippet: ko.mapping.toJSON(self)
  152. }, function (data) {
  153. if (data.status == 0) {
  154. self.result.meta(data.result.meta);
  155. self.result.data(data.result.data);
  156. // move resultsets to n rows
  157. // check if N rows fetched...
  158. } else if (data.status == -2) {
  159. self.create_session();
  160. } else {
  161. $(document).trigger("error", data.message);
  162. }
  163. }).fail(function (xhr, textStatus, errorThrown) {
  164. $(document).trigger("error", xhr.responseText);
  165. });
  166. };
  167. self.fetchResultMetadata = function() {
  168. };
  169. self.cancel = function() {
  170. };
  171. }
  172. var Notebook = function (vm, notebook) {
  173. var self = this;
  174. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
  175. self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
  176. self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
  177. self.snippets = ko.observableArray();
  178. self.selectedSnippet = ko.observable('scala');
  179. self.availableSnippets = ko.observableArray(['hive', 'scala', 'sql', 'python', 'text', 'pig', 'impala']); // presto, mysql, oracle, sqlite, postgres, phoenix
  180. self.sessions = ko.mapping.fromJS(typeof notebook.sessions != "undefined" && notebook.sessions != null ? notebook.sessions : []);
  181. self.getSession = function(session_type) {
  182. var _s = null;
  183. $.each(self.sessions(), function (index, s) {
  184. if (s.type() == session_type) {
  185. _s = s;
  186. return false;
  187. }
  188. });
  189. return _s;
  190. };
  191. self.addSession = function(session) {
  192. var toRemove = []
  193. $.each(self.sessions(), function (index, s) {
  194. if (s.type() == session.type()) {
  195. toRemove.push(s);
  196. }
  197. });
  198. $.each(toRemove, function (index, s) {
  199. self.sessions.remove(s);
  200. });
  201. self.sessions.push(session);
  202. };
  203. self.addSnippet = function(snippet) {
  204. var _snippet = new Snippet(self, snippet);
  205. self.snippets.push(_snippet);
  206. if (self.getSession(_snippet.type()) == null) {
  207. _snippet.create_session();
  208. }
  209. };
  210. self.newSnippet = function() {
  211. var snippet = new Snippet(self, {type: self.selectedSnippet()});
  212. self.snippets.push(snippet);
  213. if (self.getSession(self.selectedSnippet()) == null) {
  214. snippet.create_session();
  215. }
  216. };
  217. if (notebook.snippets) {
  218. $.each(notebook.snippets, function(index, snippet) {
  219. self.addSnippet(snippet);
  220. });
  221. }
  222. self.save = function () {
  223. $.post("/spark/api/notebook/save", {
  224. "notebook": ko.mapping.toJSON(self)
  225. }, function (data) {
  226. if (data.status == 0) {
  227. self.id(data.id);
  228. $(document).trigger("info", data.message);
  229. if (window.location.search.indexOf("notebook") == -1) {
  230. window.location.hash = '#notebook=' + data.id;
  231. }
  232. }
  233. else {
  234. $(document).trigger("error", data.message);
  235. }
  236. }).fail(function (xhr, textStatus, errorThrown) {
  237. $(document).trigger("error", xhr.responseText);
  238. });
  239. };
  240. }
  241. function EditorViewModel(notebooks) {
  242. var self = this;
  243. self.notebooks = ko.observableArray();
  244. self.selectedNotebook = ko.observable();
  245. self.isEditing = ko.observable(true);
  246. self.isEditing.subscribe(function(newVal){
  247. $(document).trigger("editingToggled");
  248. });
  249. self.toggleEditing = function () {
  250. self.isEditing(! self.isEditing());
  251. };
  252. self.assistContent = ko.observable();
  253. self.init = function() {
  254. $.each(notebooks, function(index, notebook) {
  255. self.loadNotebook(notebook);
  256. if (self.selectedNotebook() == null){
  257. self.selectedNotebook(self.notebooks()[0]);
  258. }
  259. });
  260. };
  261. self.loadNotebook = function(notebook) {
  262. self.notebooks.push(new Notebook(self, notebook));
  263. };
  264. self.newNotebook = function() {
  265. self.notebooks.push(new Notebook(self, {}));
  266. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  267. };
  268. self.saveNotebook = function() {
  269. self.selectedNotebook().save();
  270. };
  271. }
  272. function logGA(page) {
  273. if (typeof trackOnGA == 'function') {
  274. trackOnGA('editor/' + page);
  275. }
  276. }