spark.vm.js 17 KB

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