spark.vm.js 13 KB

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