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