spark.vm.js 7.9 KB

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