spark.vm.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 != null ? result.id : UUID());
  19. self.type = ko.observable(typeof 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. self.logs = ko.observable('');
  24. if (typeof result.handle != "undefined" && result.handle != null) {
  25. $.each(result.handle, function(key, val) {
  26. self.handle()[key] = val;
  27. });
  28. }
  29. self.clear = function() {
  30. $.each(self.handle, function(key, val) {
  31. delete self.handle()[key];
  32. });
  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.name = ko.observable(typeof snippet.name != "undefined" && snippet.name != null ? snippet.name : '');
  48. self.type = ko.observable(typeof snippet.type != "undefined" && snippet.type != null ? snippet.type : "hive");
  49. self.editorMode = ko.observable(TYPE_EDITOR_MAP[self.type()]);
  50. self.statement_raw = ko.observable(typeof snippet.statement_raw != "undefined" && snippet.statement_raw != null ? snippet.statement_raw : '');
  51. self.status = ko.observable(typeof snippet.status != "undefined" && snippet.status != null ? snippet.status : 'loading');
  52. self.variables = ko.observableArray([]);
  53. self.variableNames = ko.computed(function() {
  54. var matches = [];
  55. var myRegexp = /(?:[^\\]\$)([^\d'" ]\w*)/g;
  56. var match = myRegexp.exec(self.statement_raw());
  57. while (match != null) {
  58. matches.push(match[1]);
  59. match = myRegexp.exec(self.statement());
  60. }
  61. return matches;
  62. });
  63. self.variableNames.subscribe(function(newVal){
  64. var toDelete = [];
  65. var toAdd = [];
  66. $.each(newVal, function(key, name) {
  67. var match = ko.utils.arrayFirst(self.variables(), function(_var) {
  68. return _var.name() == name;
  69. });
  70. if (! match) {
  71. toAdd.push(name);
  72. }
  73. });
  74. $.each(self.variables(), function(key, _var) {
  75. var match = ko.utils.arrayFirst(newVal, function(name) {
  76. return _var.name() == name;
  77. });
  78. if (! match) {
  79. toDelete.push(_var);
  80. }
  81. });
  82. $.each(toDelete, function(index, item) {
  83. self.variables.remove(item);
  84. });
  85. $.each(toAdd, function(index, item) {
  86. self.variables.push(ko.mapping.fromJS({'name': item, 'value': ''}));
  87. });
  88. self.variables.sort(function(left, right) {
  89. var leftIndex = newVal.indexOf(left.name());
  90. var rightIndex = newVal.indexOf(right.name());
  91. return leftIndex == rightIndex ? 0 : (leftIndex < rightIndex ? -1 : 1);
  92. });
  93. });
  94. self.statement = ko.computed(function() {
  95. var statement = self.statement_raw();
  96. $.each(self.variables(), function(index, variable) {
  97. statement = statement.replace(RegExp("([^\\\\])\\$" + variable.name(), "g"), "$1" + variable.value());
  98. });
  99. return statement;
  100. });
  101. self.result = new Result(snippet, snippet.result);
  102. self.showLogs = ko.observable(typeof snippet.showLogs != "undefined" && snippet.showLogs != null ? snippet.showLogs : false);
  103. self.size = ko.observable(typeof snippet.size != "undefined" && snippet.size != null ? snippet.size : 12).extend({ numeric: 0 });
  104. self.offset = ko.observable(typeof snippet.offset != "undefined" && snippet.offset != null ? snippet.offset : 0).extend({ numeric: 0 });
  105. self.isLoading = ko.computed(function(){
  106. return self.status() == "loading";
  107. });
  108. self.klass = ko.computed(function () {
  109. return "snippet card card-widget";
  110. });
  111. self.editorKlass = ko.computed(function(){
  112. return "editor span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  113. });
  114. self.resultsKlass = ko.computed(function(){
  115. return "results " + self.type();
  116. });
  117. self.expand = function () {
  118. self.size(self.size() + 1);
  119. $("#snippet_" + self.id()).trigger("resize");
  120. }
  121. self.compress = function () {
  122. self.size(self.size() - 1);
  123. $("#snippet_" + self.id()).trigger("resize");
  124. }
  125. self.moveLeft = function () {
  126. self.offset(self.offset() - 1);
  127. }
  128. self.moveRight = function () {
  129. self.offset(self.offset() + 1);
  130. }
  131. self.remove = function (notebook, snippet) {
  132. notebook.snippets.remove(snippet);
  133. }
  134. self.checkStatusTimeout = null;
  135. self.init = function() {
  136. if (self.status() == 'running') {
  137. self.checkStatus();
  138. }
  139. };
  140. self.create_session = function() {
  141. $.post("/spark/api/create_session", {
  142. notebook: ko.mapping.toJSON(notebook),
  143. snippet: ko.mapping.toJSON(self)
  144. }, function (data) {
  145. if (data.status == 0) {
  146. notebook.addSession(ko.mapping.fromJS(data.session));
  147. self.status('ready');
  148. }
  149. else {
  150. $(document).trigger("error", data.message);
  151. }
  152. }).fail(function (xhr, textStatus, errorThrown) {
  153. $(document).trigger("error", xhr.responseText);
  154. });
  155. };
  156. self.execute = function() {
  157. $(".jHueNotify").hide();
  158. logGA('/execute/' + self.type());
  159. self.result.clear();
  160. self.status('running');
  161. $.post("/spark/api/execute", {
  162. notebook: ko.mapping.toJSON(notebook),
  163. snippet: ko.mapping.toJSON(self)
  164. }, function (data) {
  165. if (data.status == 0) {
  166. $.each(data.handle, function(key, val) {
  167. self.result.handle()[key] = val;
  168. });
  169. self.checkStatus();
  170. }
  171. else if (data.status == -2) {
  172. self.create_session();
  173. } else {
  174. $(document).trigger("error", data.message);
  175. }
  176. }).fail(function (xhr, textStatus, errorThrown) {
  177. $(document).trigger("error", xhr.responseText);
  178. });
  179. };
  180. self.checkStatus = function() {
  181. $.post("/spark/api/check_status", {
  182. notebook: ko.mapping.toJSON(notebook),
  183. snippet: ko.mapping.toJSON(self)
  184. }, function (data) {
  185. if (data.status == 0) {
  186. self.status(data.query_status.status);
  187. if (self.status() == 'running') {
  188. self.checkStatusTimeout = setTimeout(self.checkStatus, 1000);
  189. }
  190. else if (self.status() == 'available') {
  191. self.fetchResult(100);
  192. }
  193. } else if (data.status == -2) {
  194. self.create_session();
  195. } else {
  196. $(document).trigger("error", data.message);
  197. }
  198. }).fail(function (xhr, textStatus, errorThrown) {
  199. $(document).trigger("error", xhr.responseText);
  200. });
  201. };
  202. self.fetchResult = function(rows) {
  203. $.post("/spark/api/fetch_result", {
  204. notebook: ko.mapping.toJSON(notebook),
  205. snippet: ko.mapping.toJSON(self),
  206. rows: rows
  207. }, function (data) {
  208. if (data.status == 0) {
  209. rows -= data.result.data.length;
  210. self.result.meta(data.result.meta);
  211. $.each(data.result.data, function(index, row) {
  212. self.result.data.push(row);
  213. });
  214. if (data.result.hasMore && rows > 0) {
  215. setTimeout(function(){ self.fetchResult(rows); }, 500);
  216. }
  217. } else if (data.status == -2) {
  218. self.create_session();
  219. } else {
  220. $(document).trigger("error", data.message);
  221. }
  222. }).fail(function (xhr, textStatus, errorThrown) {
  223. $(document).trigger("error", xhr.responseText);
  224. });
  225. };
  226. self.fetchResultMetadata = function() {
  227. };
  228. self.cancel = function() {
  229. if (self.checkStatusTimeout != null) {
  230. clearTimeout(self.checkStatusTimeout);
  231. self.checkStatusTimeout = null;
  232. }
  233. $.post("/spark/api/cancel_statement", {
  234. notebook: ko.mapping.toJSON(notebook),
  235. snippet: ko.mapping.toJSON(self)
  236. }, function (data) {
  237. if (data.status == 0) {
  238. self.status('canceled');
  239. } else {
  240. $(document).trigger("error", data.message);
  241. }
  242. }).fail(function (xhr, textStatus, errorThrown) {
  243. $(document).trigger("error", xhr.responseText);
  244. });
  245. };
  246. self.get_log = function() {
  247. $.post("/spark/api/get_log", {
  248. notebook: ko.mapping.toJSON(notebook),
  249. snippet: ko.mapping.toJSON(self)
  250. }, function (data) {
  251. if (data.status == 0) {
  252. self.result.logs(data.log); // todo smarter?
  253. } else {
  254. $(document).trigger("error", data.message);
  255. }
  256. }).fail(function (xhr, textStatus, errorThrown) {
  257. $(document).trigger("error", xhr.responseText);
  258. });
  259. };
  260. }
  261. var Notebook = function (vm, notebook) {
  262. var self = this;
  263. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
  264. self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
  265. self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
  266. self.snippets = ko.observableArray();
  267. self.selectedSnippet = ko.observable('scala');
  268. self.availableSnippets = ko.observableArray(['impala', 'hive', 'scala', 'spark sql', 'python', 'text', 'pig']); // presto, mysql, oracle, sqlite, postgres, phoenix
  269. self.sessions = ko.mapping.fromJS(typeof notebook.sessions != "undefined" && notebook.sessions != null ? notebook.sessions : []);
  270. self.getSession = function(session_type) {
  271. var _s = null;
  272. $.each(self.sessions(), function (index, s) {
  273. if (s.type() == session_type) {
  274. _s = s;
  275. return false;
  276. }
  277. });
  278. return _s;
  279. };
  280. self.addSession = function(session) {
  281. var toRemove = []
  282. $.each(self.sessions(), function (index, s) {
  283. if (s.type() == session.type()) {
  284. toRemove.push(s);
  285. }
  286. });
  287. $.each(toRemove, function (index, s) {
  288. self.sessions.remove(s);
  289. });
  290. self.sessions.push(session);
  291. };
  292. self.addSnippet = function(snippet) {
  293. var _snippet = new Snippet(self, snippet);
  294. self.snippets.push(_snippet);
  295. if (self.getSession(_snippet.type()) == null) {
  296. _snippet.create_session();
  297. }
  298. _snippet.init();
  299. };
  300. self.newSnippet = function() {
  301. var snippet = new Snippet(self, {type: self.selectedSnippet(), result: {}});
  302. self.snippets.push(snippet);
  303. if (self.getSession(self.selectedSnippet()) == null) {
  304. snippet.create_session();
  305. }
  306. };
  307. if (notebook.snippets) {
  308. $.each(notebook.snippets, function(index, snippet) {
  309. self.addSnippet(snippet);
  310. });
  311. }
  312. self.save = function () {
  313. $.post("/spark/api/notebook/save", {
  314. "notebook": ko.mapping.toJSON(self)
  315. }, function (data) {
  316. if (data.status == 0) {
  317. self.id(data.id);
  318. $(document).trigger("info", data.message);
  319. if (window.location.search.indexOf("notebook") == -1) {
  320. window.location.hash = '#notebook=' + data.id;
  321. }
  322. }
  323. else {
  324. $(document).trigger("error", data.message);
  325. }
  326. }).fail(function (xhr, textStatus, errorThrown) {
  327. $(document).trigger("error", xhr.responseText);
  328. });
  329. };
  330. }
  331. function EditorViewModel(notebooks) {
  332. var self = this;
  333. self.notebooks = ko.observableArray();
  334. self.selectedNotebook = ko.observable();
  335. self.isEditing = ko.observable(true);
  336. self.isEditing.subscribe(function(newVal){
  337. $(document).trigger("editingToggled");
  338. });
  339. self.toggleEditing = function () {
  340. self.isEditing(! self.isEditing());
  341. };
  342. self.assistContent = ko.observable();
  343. self.init = function() {
  344. $.each(notebooks, function(index, notebook) {
  345. self.loadNotebook(notebook);
  346. if (self.selectedNotebook() == null){
  347. self.selectedNotebook(self.notebooks()[0]);
  348. }
  349. });
  350. };
  351. self.loadNotebook = function(notebook) {
  352. self.notebooks.push(new Notebook(self, notebook));
  353. };
  354. self.newNotebook = function() {
  355. self.notebooks.push(new Notebook(self, {}));
  356. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  357. };
  358. self.saveNotebook = function() {
  359. self.selectedNotebook().save();
  360. };
  361. }
  362. function logGA(page) {
  363. if (typeof trackOnGA == 'function') {
  364. trackOnGA('editor/' + page);
  365. }
  366. }