spark.vm.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // init()
  132. // checkStatus()
  133. self.create_session = function() {
  134. $.post("/spark/api/create_session", {
  135. notebook: ko.mapping.toJSON(notebook),
  136. snippet: ko.mapping.toJSON(self)
  137. }, function (data) {
  138. if (data.status == 0) {
  139. notebook.addSession(ko.mapping.fromJS(data.session));
  140. self.status('ready');
  141. }
  142. else {
  143. $(document).trigger("error", data.message);
  144. }
  145. }).fail(function (xhr, textStatus, errorThrown) {
  146. $(document).trigger("error", xhr.responseText);
  147. });
  148. };
  149. self.execute = function() {
  150. $(".jHueNotify").hide();
  151. logGA('/execute/' + self.type());
  152. self.result.clear();
  153. self.status('running');
  154. $.post("/spark/api/execute", {
  155. notebook: ko.mapping.toJSON(notebook),
  156. snippet: ko.mapping.toJSON(self)
  157. }, function (data) {
  158. if (data.status == 0) {
  159. $.each(data.handle, function(key, val) {
  160. self.result.handle()[key] = val;
  161. });
  162. self.checkStatus();
  163. }
  164. else if (data.status == -2) {
  165. self.create_session();
  166. } else {
  167. $(document).trigger("error", data.message);
  168. }
  169. }).fail(function (xhr, textStatus, errorThrown) {
  170. $(document).trigger("error", xhr.responseText);
  171. });
  172. };
  173. self.checkStatus = function() {
  174. $.post("/spark/api/check_status", {
  175. notebook: ko.mapping.toJSON(notebook),
  176. snippet: ko.mapping.toJSON(self)
  177. }, function (data) {
  178. if (data.status == 0) {
  179. self.status(data.query_status.status);
  180. if (self.status() == 'running') {
  181. setTimeout(self.checkStatus, 1000);
  182. } else {
  183. self.fetchResult();
  184. }
  185. } else if (data.status == -2) {
  186. self.create_session();
  187. } else {
  188. $(document).trigger("error", data.message);
  189. }
  190. }).fail(function (xhr, textStatus, errorThrown) {
  191. $(document).trigger("error", xhr.responseText);
  192. });
  193. };
  194. self.fetchResult = function() {
  195. $.post("/spark/api/fetch_result", {
  196. notebook: ko.mapping.toJSON(notebook),
  197. snippet: ko.mapping.toJSON(self)
  198. }, function (data) {
  199. if (data.status == 0) {
  200. self.result.meta(data.result.meta);
  201. self.result.data(data.result.data);
  202. // move resultsets to n rows
  203. // check if N rows fetched...
  204. } else if (data.status == -2) {
  205. self.create_session();
  206. } else {
  207. $(document).trigger("error", data.message);
  208. }
  209. }).fail(function (xhr, textStatus, errorThrown) {
  210. $(document).trigger("error", xhr.responseText);
  211. });
  212. };
  213. self.fetchResultMetadata = function() {
  214. };
  215. self.cancel = function() {
  216. };
  217. }
  218. var Notebook = function (vm, notebook) {
  219. var self = this;
  220. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
  221. self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
  222. self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
  223. self.snippets = ko.observableArray();
  224. self.selectedSnippet = ko.observable('scala');
  225. self.availableSnippets = ko.observableArray(['hive', 'scala', 'sql', 'python', 'text', 'pig', 'impala']); // presto, mysql, oracle, sqlite, postgres, phoenix
  226. self.sessions = ko.mapping.fromJS(typeof notebook.sessions != "undefined" && notebook.sessions != null ? notebook.sessions : []);
  227. self.getSession = function(session_type) {
  228. var _s = null;
  229. $.each(self.sessions(), function (index, s) {
  230. if (s.type() == session_type) {
  231. _s = s;
  232. return false;
  233. }
  234. });
  235. return _s;
  236. };
  237. self.addSession = function(session) {
  238. var toRemove = []
  239. $.each(self.sessions(), function (index, s) {
  240. if (s.type() == session.type()) {
  241. toRemove.push(s);
  242. }
  243. });
  244. $.each(toRemove, function (index, s) {
  245. self.sessions.remove(s);
  246. });
  247. self.sessions.push(session);
  248. };
  249. self.addSnippet = function(snippet) {
  250. var _snippet = new Snippet(self, snippet);
  251. self.snippets.push(_snippet);
  252. if (self.getSession(_snippet.type()) == null) {
  253. _snippet.create_session();
  254. }
  255. };
  256. self.newSnippet = function() {
  257. var snippet = new Snippet(self, {type: self.selectedSnippet(), result: {}});
  258. self.snippets.push(snippet);
  259. if (self.getSession(self.selectedSnippet()) == null) {
  260. snippet.create_session();
  261. }
  262. };
  263. if (notebook.snippets) {
  264. $.each(notebook.snippets, function(index, snippet) {
  265. self.addSnippet(snippet);
  266. });
  267. }
  268. self.save = function () {
  269. $.post("/spark/api/notebook/save", {
  270. "notebook": ko.mapping.toJSON(self)
  271. }, function (data) {
  272. if (data.status == 0) {
  273. self.id(data.id);
  274. $(document).trigger("info", data.message);
  275. if (window.location.search.indexOf("notebook") == -1) {
  276. window.location.hash = '#notebook=' + data.id;
  277. }
  278. }
  279. else {
  280. $(document).trigger("error", data.message);
  281. }
  282. }).fail(function (xhr, textStatus, errorThrown) {
  283. $(document).trigger("error", xhr.responseText);
  284. });
  285. };
  286. }
  287. function EditorViewModel(notebooks) {
  288. var self = this;
  289. self.notebooks = ko.observableArray();
  290. self.selectedNotebook = ko.observable();
  291. self.isEditing = ko.observable(true);
  292. self.isEditing.subscribe(function(newVal){
  293. $(document).trigger("editingToggled");
  294. });
  295. self.toggleEditing = function () {
  296. self.isEditing(! self.isEditing());
  297. };
  298. self.assistContent = ko.observable();
  299. self.init = function() {
  300. $.each(notebooks, function(index, notebook) {
  301. self.loadNotebook(notebook);
  302. if (self.selectedNotebook() == null){
  303. self.selectedNotebook(self.notebooks()[0]);
  304. }
  305. });
  306. };
  307. self.loadNotebook = function(notebook) {
  308. self.notebooks.push(new Notebook(self, notebook));
  309. };
  310. self.newNotebook = function() {
  311. self.notebooks.push(new Notebook(self, {}));
  312. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  313. };
  314. self.saveNotebook = function() {
  315. self.selectedNotebook().save();
  316. };
  317. }
  318. function logGA(page) {
  319. if (typeof trackOnGA == 'function') {
  320. trackOnGA('editor/' + page);
  321. }
  322. }