spark.vm.js 16 KB

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