spark.vm.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.init = function() {
  164. if (self.status() == 'running') {
  165. self.checkStatus();
  166. }
  167. };
  168. self.create_session = function() {
  169. $.post("/spark/api/create_session", {
  170. notebook: ko.mapping.toJSON(notebook),
  171. snippet: ko.mapping.toJSON(self)
  172. }, function (data) {
  173. if (data.status == 0) {
  174. notebook.addSession(ko.mapping.fromJS(data.session));
  175. self.status('ready');
  176. }
  177. else {
  178. $(document).trigger("error", data.message);
  179. }
  180. }).fail(function (xhr, textStatus, errorThrown) {
  181. $(document).trigger("error", xhr.responseText);
  182. });
  183. };
  184. self.execute = function() {
  185. $(document).trigger("executeStarted", self);
  186. $(".jHueNotify").hide();
  187. logGA('/execute/' + self.type());
  188. self.result.clear();
  189. self.status('running');
  190. $.post("/spark/api/execute", {
  191. notebook: ko.mapping.toJSON(notebook),
  192. snippet: ko.mapping.toJSON(self)
  193. }, function (data) {
  194. if (data.status == 0) {
  195. $.each(data.handle, function(key, val) {
  196. self.result.handle()[key] = val;
  197. });
  198. self.checkStatus();
  199. }
  200. else if (data.status == -2) {
  201. self.create_session();
  202. } else {
  203. $(document).trigger("error", data.message);
  204. }
  205. }).fail(function (xhr, textStatus, errorThrown) {
  206. $(document).trigger("error", xhr.responseText);
  207. });
  208. };
  209. self.checkStatus = function() {
  210. $.post("/spark/api/check_status", {
  211. notebook: ko.mapping.toJSON(notebook),
  212. snippet: ko.mapping.toJSON(self)
  213. }, function (data) {
  214. if (data.status == 0) {
  215. self.status(data.query_status.status);
  216. if (self.status() == 'running') {
  217. self.checkStatusTimeout = setTimeout(self.checkStatus, 1000);
  218. }
  219. else if (self.status() == 'available') {
  220. self.fetchResult(100);
  221. }
  222. } else if (data.status == -2) {
  223. self.create_session();
  224. } else {
  225. $(document).trigger("error", data.message);
  226. }
  227. }).fail(function (xhr, textStatus, errorThrown) {
  228. $(document).trigger("error", xhr.responseText);
  229. });
  230. };
  231. self.fetchResult = function(rows) {
  232. $.post("/spark/api/fetch_result", {
  233. notebook: ko.mapping.toJSON(notebook),
  234. snippet: ko.mapping.toJSON(self),
  235. rows: rows
  236. }, function (data) {
  237. if (data.status == 0) {
  238. rows -= data.result.data.length;
  239. data.result.meta.unshift({ type:"INT_TYPE", name:"", comment:null});
  240. self.result.meta(data.result.meta);
  241. $.each(data.result.data, function(index, row) {
  242. row.unshift(index);
  243. self.result.data.push(row);
  244. });
  245. if (data.result.hasMore && rows > 0) {
  246. setTimeout(function(){ self.fetchResult(rows); }, 500);
  247. }
  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.fetchResultMetadata = function() {
  258. };
  259. self.cancel = function() {
  260. if (self.checkStatusTimeout != null) {
  261. clearTimeout(self.checkStatusTimeout);
  262. self.checkStatusTimeout = null;
  263. }
  264. $.post("/spark/api/cancel_statement", {
  265. notebook: ko.mapping.toJSON(notebook),
  266. snippet: ko.mapping.toJSON(self)
  267. }, function (data) {
  268. if (data.status == 0) {
  269. self.status('canceled');
  270. } else {
  271. $(document).trigger("error", data.message);
  272. }
  273. }).fail(function (xhr, textStatus, errorThrown) {
  274. $(document).trigger("error", xhr.responseText);
  275. });
  276. };
  277. self.get_log = function() {
  278. $.post("/spark/api/get_log", {
  279. notebook: ko.mapping.toJSON(notebook),
  280. snippet: ko.mapping.toJSON(self)
  281. }, function (data) {
  282. if (data.status == 0) {
  283. self.result.logs(data.log); // todo smarter?
  284. } else {
  285. $(document).trigger("error", data.message);
  286. }
  287. }).fail(function (xhr, textStatus, errorThrown) {
  288. $(document).trigger("error", xhr.responseText);
  289. });
  290. };
  291. }
  292. var Notebook = function (vm, notebook) {
  293. var self = this;
  294. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
  295. self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
  296. self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
  297. self.snippets = ko.observableArray();
  298. self.selectedSnippet = ko.observable('scala');
  299. self.availableSnippets = ko.observableArray(['impala', 'hive', 'scala', 'spark sql', 'python', 'text', 'pig']); // presto, mysql, oracle, sqlite, postgres, phoenix
  300. self.sessions = ko.mapping.fromJS(typeof notebook.sessions != "undefined" && notebook.sessions != null ? notebook.sessions : []);
  301. self.getSession = function(session_type) {
  302. var _s = null;
  303. $.each(self.sessions(), function (index, s) {
  304. if (s.type() == session_type) {
  305. _s = s;
  306. return false;
  307. }
  308. });
  309. return _s;
  310. };
  311. self.addSession = function(session) {
  312. var toRemove = []
  313. $.each(self.sessions(), function (index, s) {
  314. if (s.type() == session.type()) {
  315. toRemove.push(s);
  316. }
  317. });
  318. $.each(toRemove, function (index, s) {
  319. self.sessions.remove(s);
  320. });
  321. self.sessions.push(session);
  322. };
  323. self.addSnippet = function(snippet) {
  324. var _snippet = new Snippet(self, snippet);
  325. self.snippets.push(_snippet);
  326. if (self.getSession(_snippet.type()) == null) {
  327. _snippet.create_session();
  328. }
  329. _snippet.init();
  330. };
  331. self.newSnippet = function() {
  332. var snippet = new Snippet(self, {type: self.selectedSnippet(), result: {}});
  333. self.snippets.push(snippet);
  334. if (self.getSession(self.selectedSnippet()) == null) {
  335. snippet.create_session();
  336. }
  337. };
  338. if (notebook.snippets) {
  339. $.each(notebook.snippets, function(index, snippet) {
  340. self.addSnippet(snippet);
  341. });
  342. }
  343. self.save = function () {
  344. $.post("/spark/api/notebook/save", {
  345. "notebook": ko.mapping.toJSON(self)
  346. }, function (data) {
  347. if (data.status == 0) {
  348. self.id(data.id);
  349. $(document).trigger("info", data.message);
  350. if (window.location.search.indexOf("notebook") == -1) {
  351. window.location.hash = '#notebook=' + data.id;
  352. }
  353. }
  354. else {
  355. $(document).trigger("error", data.message);
  356. }
  357. }).fail(function (xhr, textStatus, errorThrown) {
  358. $(document).trigger("error", xhr.responseText);
  359. });
  360. };
  361. }
  362. function EditorViewModel(notebooks) {
  363. var self = this;
  364. self.notebooks = ko.observableArray();
  365. self.selectedNotebook = ko.observable();
  366. self.isEditing = ko.observable(true);
  367. self.isEditing.subscribe(function(newVal){
  368. $(document).trigger("editingToggled");
  369. });
  370. self.toggleEditing = function () {
  371. self.isEditing(! self.isEditing());
  372. };
  373. self.assistContent = ko.observable();
  374. self.init = function() {
  375. $.each(notebooks, function(index, notebook) {
  376. self.loadNotebook(notebook);
  377. if (self.selectedNotebook() == null){
  378. self.selectedNotebook(self.notebooks()[0]);
  379. }
  380. });
  381. };
  382. self.loadNotebook = function(notebook) {
  383. self.notebooks.push(new Notebook(self, notebook));
  384. };
  385. self.newNotebook = function() {
  386. self.notebooks.push(new Notebook(self, {}));
  387. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  388. };
  389. self.saveNotebook = function() {
  390. self.selectedNotebook().save();
  391. };
  392. }
  393. function logGA(page) {
  394. if (typeof trackOnGA == 'function') {
  395. trackOnGA('editor/' + page);
  396. }
  397. }