spark.vm.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if (self.query.id() == -1) {
  200. self.query.id(data.design_id);
  201. $(document).trigger('savedas.query', data);
  202. } else {
  203. $(document).trigger('saved.query', data);
  204. }
  205. } else {
  206. self.query.errors.push(data.message);
  207. }
  208. },
  209. error: function() {
  210. $(document).trigger('error.query');
  211. },
  212. data: data
  213. };
  214. $.ajax(request);
  215. }
  216. };
  217. self.executeQuery = function() {
  218. var data = ko.mapping.toJS(self.query);
  219. data.appName = self.appName().name;
  220. data.classPath = self.classPath();
  221. data.autoContext = self.autoContext();
  222. data.context = self.context() ? self.context().name : '';
  223. data.params = ko.toJSON(self.query.params());
  224. self.resultsEmpty(false);
  225. var request = {
  226. url: '/spark/api/execute',
  227. dataType: 'json',
  228. type: 'POST',
  229. success: function(data) {
  230. self.query.errors.removeAll();
  231. self.rows.removeAll();
  232. if (data.status == 0) {
  233. $(document).trigger('execute.query', data);
  234. self.query.id(data.design);
  235. self.query.jobId(data.results.result.jobId);
  236. window.location.hash = 'jobId=' + data.results.result.jobId;
  237. self.query.context(data.results.result.context);
  238. self.checkQueryStatus();
  239. } else {
  240. self.query.errors.push(data.message);
  241. }
  242. },
  243. error: error_fn,
  244. data: data
  245. };
  246. $.ajax(request);
  247. };
  248. self.checkQueryStatus = function() {
  249. var timerId = 0;
  250. var request = {
  251. url: '/spark/api/job/' + self.query.jobId(),
  252. dataType: 'json',
  253. type: 'GET',
  254. success: function(data) {
  255. // Script finished
  256. if (data.results.status == 'OK' || data.results.status == 'ERROR') {
  257. clearInterval(timerId);
  258. if (data.results.status == 'OK') {
  259. self.updateResults(data.results.result);
  260. self.resultsEmpty($.isEmptyObject(data.results.result));
  261. } else {
  262. self.query.errors.push(data.results.ERROR.message);
  263. }
  264. $(document).trigger('executed.query', data);
  265. }
  266. },
  267. error: error_fn
  268. };
  269. timerId = setInterval(function(){
  270. $.ajax(request);
  271. }, 1000);
  272. };
  273. self.openQuery = function(jobId) {
  274. self.query.jobId(jobId);
  275. $(document).trigger('execute.query');
  276. self.checkQueryStatus();
  277. };
  278. self.fetchAppNames = function() {
  279. var request = {
  280. url: '/spark/api/jars',
  281. dataType: 'json',
  282. type: 'GET',
  283. success: function(data) {
  284. if (data.error != null) {
  285. $.jHueNotify.error(data.error);
  286. }
  287. else {
  288. self.updateAppNames(data.jars);
  289. }
  290. },
  291. error: error_fn
  292. };
  293. $.ajax(request);
  294. };
  295. self.fetchContexts = function() {
  296. var request = {
  297. url: '/spark/api/contexts',
  298. dataType: 'json',
  299. type: 'GET',
  300. success: function(data) {
  301. if (data.error != null) {
  302. $.jHueNotify.error(data.error);
  303. }
  304. else {
  305. self.updateContexts(data.contexts);
  306. }
  307. },
  308. error: error_fn
  309. };
  310. $.ajax(request);
  311. };
  312. self.createContext = function() {
  313. var data = $("#createContextForm").serialize(); // Not koified
  314. $("#createContextBtn").attr("data-loading-text", $("#createContextBtn").text() + " ...");
  315. $("#createContextBtn").button("loading");
  316. var request = {
  317. url: '/spark/api/create_context',
  318. dataType: 'json',
  319. type: 'POST',
  320. success: function(result) {
  321. self.query.errors.removeAll();
  322. if (result.status == 'OK') {
  323. self.contexts.push(createDropdownItem(result.name));
  324. self.context(result.name);
  325. self.autoContext(false);
  326. $(document).trigger('created.context', data);
  327. } else {
  328. $(document).trigger('error', result.result);
  329. }
  330. },
  331. error: error_fn,
  332. data: data
  333. };
  334. $.ajax(request);
  335. };
  336. self.showFileChooser = function() {
  337. var inputPath = this;
  338. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  339. $("#filechooser").jHueFileChooser({
  340. initialPath: path,
  341. onFileChoose: function (filePath) {
  342. inputPath.value(filePath);
  343. $("#chooseFile").modal("hide");
  344. },
  345. createFolder: false
  346. });
  347. $("#chooseFile").modal("show");
  348. };
  349. }