spark.vm.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. $.each(results, function(key, value) {
  74. newRows.push([key, value]);
  75. });
  76. self.rows(newRows);
  77. };
  78. self.updateAppNames = function(appNames) {
  79. var newAppNames = [];
  80. $.each(appNames, function(key, value) {
  81. newAppNames.push({
  82. 'name': ko.observable(key),
  83. 'nice_name': ko.observable(key)
  84. });
  85. });
  86. self.appNames(newAppNames);
  87. var last = newAppNames.length > 0 ? newAppNames[0].name() : null;
  88. if (last) {
  89. self.appName(last);
  90. }
  91. };
  92. self.updateContexts = function(contexts) {
  93. var newContexts = [];
  94. $.each(contexts, function(index, value) {
  95. newContexts.push(createDropdownItem(value));
  96. });
  97. self.contexts(newContexts);
  98. var last = newContexts.length > 0 ? newContexts[0].name() : null;
  99. if (last) {
  100. self.context(last);
  101. }
  102. };
  103. function createDropdownItem(item) {
  104. return {
  105. 'name': ko.observable(item),
  106. 'nice_name': ko.observable(item)
  107. };
  108. };
  109. self.loadDesign = function(design) {
  110. self.query.id(design.id);
  111. self.query.name(design.name);
  112. self.query.description(design.desc);
  113. self.query.appName(design.appName);
  114. self.query.classPath(design.classPath);
  115. self.query.autoContext(design.autoContext);
  116. self.query.params(design.params);
  117. self.appName(design.appName);
  118. self.chooseAppName(design.appName);
  119. self.autoContext(design.autoContext);
  120. self.context(design.context);
  121. self.chooseContext(design.context);
  122. self.classPath(design.classPath);
  123. };
  124. self.chooseAppName = function(value, e) {
  125. $.each(self.appNames(), function(index, appName) {
  126. if (appName.name() == value.name()) {
  127. self.selectedAppName(index);
  128. }
  129. });
  130. };
  131. self.chooseContext = function(value, e) {
  132. $.each(self.contexts(), function(index, context) {
  133. if (context.name() == value.name()) {
  134. self.selectedContext(index);
  135. }
  136. });
  137. };
  138. var error_fn = function(jqXHR, status, errorThrown) {
  139. try {
  140. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  141. } catch(e) {
  142. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  143. }
  144. };
  145. self.saveQuery = function() {
  146. var self = this;
  147. if (self.query.name()) {
  148. var data = ko.mapping.toJS(self.query);
  149. data['saveform-name'] = data['name'];
  150. data['saveform-desc'] = data['description'];
  151. data['query-appName'] = self.appName().name;
  152. data['query-classPath'] = self.classPath();
  153. data['query-autoContext'] = self.autoContext();
  154. data['query-context'] = self.context().name;
  155. data['query-params'] = '';
  156. var url = '/spark/api/save_query/';
  157. if (self.query.id() && self.query.id() != -1) {
  158. url += self.query.id() + '/';
  159. }
  160. var request = {
  161. url: url,
  162. dataType: 'json',
  163. type: 'POST',
  164. success: function(data) {
  165. if (data.status == 0) {
  166. $(document).trigger('saved.query', data);
  167. } else {
  168. self.query.errors.push(data.message);
  169. }
  170. },
  171. error: function() {
  172. $(document).trigger('error.query');
  173. },
  174. data: data
  175. };
  176. $.ajax(request);
  177. }
  178. };
  179. self.executeQuery = function() {
  180. var data = ko.mapping.toJS(self.query);
  181. data.appName = self.appName().name;
  182. data.classPath = self.classPath();
  183. data.autoContext = self.autoContext();
  184. data.context = self.context().name;
  185. var request = {
  186. url: '/spark/api/execute',
  187. dataType: 'json',
  188. type: 'POST',
  189. success: function(data) {
  190. self.query.errors.removeAll();
  191. self.rows.removeAll();
  192. if (data.status == 0) {
  193. $(document).trigger('execute.query', data);
  194. self.query.id(data.design);
  195. self.query.jobId(data.results.result.jobId);
  196. self.query.context(data.results.result.context);
  197. self.checkQueryStatus();
  198. } else {
  199. self.query.errors.push(data.message);
  200. }
  201. },
  202. error: error_fn,
  203. data: data
  204. };
  205. $.ajax(request);
  206. };
  207. self.checkQueryStatus = function() {
  208. var timerId = 0;
  209. var request = {
  210. url: '/spark/api/job/' + self.query.jobId(),
  211. dataType: 'json',
  212. type: 'GET',
  213. success: function(data) {
  214. // Script finished
  215. if (data.results.status == 'OK' || data.results.status == 'ERROR') {
  216. clearInterval(timerId);
  217. self.updateResults(data.results.result);
  218. self.resultsEmpty($.isEmptyObject(data.results.result));
  219. $(document).trigger('executed.query', data);
  220. }
  221. },
  222. error: error_fn
  223. };
  224. timerId = setInterval(function(){
  225. $.ajax(request);
  226. }, 1000);
  227. };
  228. self.openQuery = function(jobId) {
  229. self.query.jobId(jobId);
  230. $(document).trigger('execute.query');
  231. self.checkQueryStatus();
  232. };
  233. self.fetchAppNames = function() {
  234. var request = {
  235. url: '/spark/api/jars',
  236. dataType: 'json',
  237. type: 'GET',
  238. success: function(data) {
  239. self.updateAppNames(data.jars);
  240. },
  241. error: error_fn
  242. };
  243. $.ajax(request);
  244. };
  245. self.fetchContexts = function() {
  246. var request = {
  247. url: '/spark/api/contexts',
  248. dataType: 'json',
  249. type: 'GET',
  250. success: function(data) {
  251. self.updateContexts(data.contexts);
  252. },
  253. error: error_fn
  254. };
  255. $.ajax(request);
  256. };
  257. self.createContext = function() {
  258. var data = $("#createContextForm").serialize(); // Not koified
  259. var request = {
  260. url: '/spark/api/create_context',
  261. dataType: 'json',
  262. type: 'POST',
  263. success: function(result) {
  264. self.query.errors.removeAll();
  265. if (result.status == 'OK') {
  266. self.contexts().push(createDropdownItem(result.name));
  267. self.context(result.name);
  268. self.autoContext(false);
  269. $(document).trigger('created.context', data);
  270. } else {
  271. $(document).trigger('error', result.result);
  272. }
  273. },
  274. error: error_fn,
  275. data: data
  276. };
  277. $.ajax(request);
  278. };
  279. }