spark.vm.js 15 KB

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