spark.vm.js 15 KB

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