spark.vm.js 8.5 KB

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