spark.vm.js 10 KB

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