spark.vm.js 15 KB

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