spark.vm.js 12 KB

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