spark.vm.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 SparkParameter = function (property) {
  17. var self = this;
  18. self.name = ko.observable(property.name);
  19. self.value = ko.observable(property.value);
  20. };
  21. function sparkViewModel() {
  22. var self = this;
  23. self.appNames = ko.observableArray(); // List of jars
  24. self.selectedAppName = ko.observable(0);
  25. self.autoContext = ko.observable(true);
  26. self.contexts = ko.observableArray(); // List of contexts
  27. self.selectedContext = ko.observable(0);
  28. self.classPath = ko.observable('');
  29. self.autoContext.forEditing = ko.computed({
  30. read: function() {
  31. return this.autoContext().toString();
  32. },
  33. write: function(newValue) {
  34. this.autoContext(newValue === "true");
  35. },
  36. owner: this
  37. });
  38. self.query = ko.mapping.fromJS({
  39. 'id': -1,
  40. 'jobId': null,
  41. 'name': null,
  42. 'description': null,
  43. 'errors': [],
  44. 'appName': '',
  45. 'classPath': '',
  46. 'context': '',
  47. 'autoContext': true,
  48. 'params': []
  49. });
  50. self.rows = ko.observableArray();
  51. self.resultsEmpty = ko.observable(false);
  52. self.appName = ko.computed({
  53. 'read': function() {
  54. if (self.appNames().length > 0) {
  55. return self.appNames()[self.selectedAppName()];
  56. } else {
  57. return null;
  58. }
  59. },
  60. 'write': function(value) {
  61. var filtered = $.each(self.appNames(), function(index, appName) {
  62. if (appName.name() == value) {
  63. self.selectedAppName(index);
  64. }
  65. });
  66. }
  67. });
  68. self.context = ko.computed({
  69. 'read': function() {
  70. if (self.contexts().length > 0) {
  71. return self.contexts()[self.selectedContext()];
  72. } else {
  73. return null;
  74. }
  75. },
  76. 'write': function(value) {
  77. var filtered = $.each(self.contexts(), function(index, context) {
  78. if (context.name() == value) {
  79. self.selectedContext(index);
  80. }
  81. });
  82. }
  83. });
  84. self.updateResults = function(results) {
  85. self.rows.removeAll();
  86. var newRows = [];
  87. // Is a list of map
  88. if ($.inArray($.type(results), ['array', 'object']) != -1) {
  89. $.each(results, function(key, value) {
  90. newRows.push([key, value]);
  91. });
  92. } else {
  93. newRows.push([0, results]);
  94. }
  95. self.rows(newRows);
  96. };
  97. self.updateAppNames = function(appNames) {
  98. var newAppNames = [];
  99. $.each(appNames, function(key, value) {
  100. newAppNames.push({
  101. 'name': ko.observable(key),
  102. 'nice_name': ko.observable(key)
  103. });
  104. });
  105. self.appNames(newAppNames);
  106. // Load back appName or guess
  107. if (self.query.appName()) {
  108. viewModel.setAppName( self.query.appName());
  109. }
  110. };
  111. self.updateContexts = function(contexts) {
  112. var newContexts = [];
  113. $.each(contexts, function(index, value) {
  114. newContexts.push(createDropdownItem(value));
  115. });
  116. self.contexts(newContexts);
  117. var last = newContexts.length > 0 ? newContexts[0].name() : null;
  118. if (last) {
  119. self.context(last);
  120. }
  121. };
  122. self.addParam = function() {
  123. self.query.params.push(new SparkParameter({name: "", value: ""}));
  124. };
  125. self.removeParam = function() {
  126. self.query.params.remove(this);
  127. };
  128. function createDropdownItem(item) {
  129. return {
  130. 'name': ko.observable(item),
  131. 'nice_name': ko.observable(item)
  132. };
  133. };
  134. self.loadDesign = function(design) {
  135. self.query.id(design.id);
  136. self.query.name(design.name);
  137. self.query.description(design.desc);
  138. self.query.appName(design.appName);
  139. self.query.classPath(design.classPath);
  140. self.query.autoContext(design.autoContext);
  141. self.query.params(design.params);
  142. self.appName(design.appName);
  143. self.chooseAppName(self.appName());
  144. self.autoContext(design.autoContext);
  145. self.context(design.context);
  146. self.chooseContext(design.context);
  147. self.classPath(design.classPath);
  148. };
  149. self.chooseAppName = function(value, e) {
  150. $.each(self.appNames(), function(index, appName) {
  151. if (appName.name() == value.name()) {
  152. self.selectedAppName(index);
  153. }
  154. });
  155. };
  156. self.setAppName = function(name) {
  157. $.each(self.appNames(), function(index, appName) {
  158. if (appName.name() == name) {
  159. self.appName(name);
  160. self.selectedAppName(index);
  161. }
  162. });
  163. };
  164. self.chooseContext = function(value, e) {
  165. $.each(self.contexts(), function(index, context) {
  166. if (context.name() == value.name()) {
  167. self.selectedContext(index);
  168. }
  169. });
  170. };
  171. var error_fn = function(jqXHR, status, errorThrown) {
  172. try {
  173. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  174. } catch(e) {
  175. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  176. }
  177. };
  178. self.saveQuery = function() {
  179. var self = this;
  180. if (self.query.name()) {
  181. var data = ko.mapping.toJS(self.query);
  182. data['saveform-name'] = data['name'];
  183. data['saveform-desc'] = data['description'];
  184. data['query-appName'] = self.appName().name;
  185. data['query-classPath'] = self.classPath();
  186. data['query-autoContext'] = self.autoContext();
  187. data['query-context'] = self.context() ? self.context().name : '';
  188. data['query-params'] = ko.toJSON(self.query.params());
  189. var url = '/spark/api/save_query/';
  190. if (self.query.id() && self.query.id() != -1) {
  191. url += self.query.id() + '/';
  192. }
  193. var request = {
  194. url: url,
  195. dataType: 'json',
  196. type: 'POST',
  197. success: function(data) {
  198. if (data.status == 0) {
  199. $(document).trigger('saved.query', data);
  200. } else {
  201. self.query.errors.push(data.message);
  202. }
  203. },
  204. error: function() {
  205. $(document).trigger('error.query');
  206. },
  207. data: data
  208. };
  209. $.ajax(request);
  210. }
  211. };
  212. self.executeQuery = function() {
  213. var data = ko.mapping.toJS(self.query);
  214. data.appName = self.appName().name;
  215. data.classPath = self.classPath();
  216. data.autoContext = self.autoContext();
  217. data.context = self.context() ? self.context().name : '';
  218. data.params = ko.toJSON(self.query.params());
  219. var request = {
  220. url: '/spark/api/execute',
  221. dataType: 'json',
  222. type: 'POST',
  223. success: function(data) {
  224. self.query.errors.removeAll();
  225. self.rows.removeAll();
  226. if (data.status == 0) {
  227. $(document).trigger('execute.query', data);
  228. self.query.id(data.design);
  229. self.query.jobId(data.results.result.jobId);
  230. window.location.hash = 'jobId=' + data.results.result.jobId;
  231. self.query.context(data.results.result.context);
  232. self.checkQueryStatus();
  233. } else {
  234. self.query.errors.push(data.message);
  235. }
  236. },
  237. error: error_fn,
  238. data: data
  239. };
  240. $.ajax(request);
  241. };
  242. self.checkQueryStatus = function() {
  243. var timerId = 0;
  244. var request = {
  245. url: '/spark/api/job/' + self.query.jobId() + '?' + Math.random(),
  246. dataType: 'json',
  247. type: 'GET',
  248. success: function(data) {
  249. // Script finished
  250. if (data.results.status == 'OK' || data.results.status == 'ERROR') {
  251. clearInterval(timerId);
  252. self.updateResults(data.results.result);
  253. self.resultsEmpty($.isEmptyObject(data.results.result));
  254. $(document).trigger('executed.query', data);
  255. }
  256. },
  257. error: error_fn
  258. };
  259. timerId = setInterval(function(){
  260. $.ajax(request);
  261. }, 1000);
  262. };
  263. self.openQuery = function(jobId) {
  264. self.query.jobId(jobId);
  265. $(document).trigger('execute.query');
  266. self.checkQueryStatus();
  267. };
  268. self.fetchAppNames = function() {
  269. var request = {
  270. url: '/spark/api/jars?' + Math.random(),
  271. dataType: 'json',
  272. type: 'GET',
  273. success: function(data) {
  274. if (data.error != null) {
  275. $.jHueNotify.error(data.error);
  276. }
  277. else {
  278. self.updateAppNames(data.jars);
  279. }
  280. },
  281. error: error_fn
  282. };
  283. $.ajax(request);
  284. };
  285. self.fetchContexts = function() {
  286. var request = {
  287. url: '/spark/api/contexts?' + Math.random(),
  288. dataType: 'json',
  289. type: 'GET',
  290. success: function(data) {
  291. if (data.error != null) {
  292. $.jHueNotify.error(data.error);
  293. }
  294. else {
  295. self.updateContexts(data.contexts);
  296. }
  297. },
  298. error: error_fn
  299. };
  300. $.ajax(request);
  301. };
  302. self.createContext = function() {
  303. var data = $("#createContextForm").serialize(); // Not koified
  304. $("#createContextBtn").attr("data-loading-text", $("#createContextBtn").text() + " ...");
  305. $("#createContextBtn").button("loading");
  306. var request = {
  307. url: '/spark/api/create_context',
  308. dataType: 'json',
  309. type: 'POST',
  310. success: function(result) {
  311. self.query.errors.removeAll();
  312. if (result.status == 'OK') {
  313. self.contexts.push(createDropdownItem(result.name));
  314. self.context(result.name);
  315. self.autoContext(false);
  316. $(document).trigger('created.context', data);
  317. } else {
  318. $(document).trigger('error', result.result);
  319. }
  320. },
  321. error: error_fn,
  322. data: data
  323. };
  324. $.ajax(request);
  325. };
  326. self.showFileChooser = function() {
  327. var inputPath = this;
  328. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  329. $("#filechooser").jHueFileChooser({
  330. initialPath: path,
  331. onFileChoose: function (filePath) {
  332. inputPath.value(filePath);
  333. $("#chooseFile").modal("hide");
  334. },
  335. createFolder: false
  336. });
  337. $("#chooseFile").modal("show");
  338. };
  339. }