spark.vm.js 15 KB

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