spark.vm.js 8.7 KB

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