sqoop.jobs.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. var jobs = (function($) {
  17. var job_registry = {};
  18. var JobModel = koify.Model.extend({
  19. 'id': -1,
  20. 'name': null,
  21. 'type': 'IMPORT',
  22. 'connector_id': 0,
  23. 'connection_id': 0,
  24. 'connector': [],
  25. 'framework': [],
  26. 'creation_date': null,
  27. 'creation_user': null,
  28. 'update_date': null,
  29. 'update_user': null,
  30. 'setImport': function(){
  31. this.type("IMPORT");
  32. // Huge hack for now
  33. $('a').filter(function(index) { return $(this).text() === "Step 2: To"; }).text("Step 2: From");
  34. $('a').filter(function(index) { return $(this).text() === "Step 3: From"; }).text("Step 3: To");
  35. },
  36. 'setExport': function(){
  37. this.type("EXPORT");
  38. $('a').filter(function(index) { return $(this).text() === "Step 2: From"; }).text("Step 2: To");
  39. $('a').filter(function(index) { return $(this).text() === "Step 3: To"; }).text("Step 3: From");
  40. },
  41. 'initialize': function(attrs) {
  42. var self = this;
  43. var _attrs = $.extend(true, {}, attrs);
  44. _attrs = transform_keys(_attrs, {
  45. 'connector-id': 'connector_id',
  46. 'connection-id': 'connection_id'
  47. });
  48. _attrs = transform_values(_attrs, {
  49. 'connector': to_forms,
  50. 'framework': to_forms
  51. });
  52. return _attrs;
  53. }
  54. });
  55. var Job = koify.Node.extend({
  56. 'identifier': 'job',
  57. 'persists': true,
  58. 'model_class': JobModel,
  59. 'base_url': '/sqoop/api/jobs/',
  60. 'initialize': function() {
  61. var self = this;
  62. self.parent.initialize.apply(self, arguments);
  63. self.createdFormatted = ko.computed(function() {
  64. if (self.creation_date()) {
  65. return moment(self.creation_date()).format('MM/DD/YYYY hh:mm A');
  66. } else {
  67. return 0;
  68. }
  69. });
  70. self.updatedFormatted = ko.computed(function() {
  71. if (self.update_date()) {
  72. return moment(self.update_date()).format('MM/DD/YYYY hh:mm A');
  73. } else {
  74. return 0;
  75. }
  76. });
  77. self.selected = ko.observable();
  78. self.submission = ko.computed({
  79. owner: self,
  80. read: function () {
  81. return submissions.setDefaultSubmission(this.id());
  82. },
  83. write: function (submission) {
  84. submissions.putSubmission(submission);
  85. self.id.valueHasMutated();
  86. if (self.runningInterval == 0 && self.isRunning()) {
  87. self.runningInterval = setInterval(function() {
  88. if (!self.isRunning()) {
  89. clearInterval(self.runningInterval);
  90. self.runningInterval = 0;
  91. }
  92. self.getStatus();
  93. }, 2000);
  94. }
  95. }
  96. });
  97. self.persisted = ko.computed(function() {
  98. return self.id() > -1;
  99. });
  100. self.isRunning = ko.computed(function() {
  101. return self.submission() && $.inArray(self.submission().status(), ['BOOTING', 'RUNNING']) > -1;
  102. });
  103. self.hasSucceeded = ko.computed(function() {
  104. return self.submission() && $.inArray(self.submission().status(), ['SUCCEEDED']) > -1;
  105. });
  106. self.hasFailed = ko.computed(function() {
  107. return self.submission() && $.inArray(self.submission().status(), ['FAILURE_ON_SUBMIT', 'FAILED']) > -1;
  108. });
  109. self.outputDirectoryFilebrowserURL = ko.computed(function() {
  110. var output_directory = null;
  111. $.each(self.framework(), function(index, form) {
  112. if (form.name() == 'output') {
  113. $.each(form.inputs(), function(index, input) {
  114. if (input.name() == 'output.outputDirectory') {
  115. output_directory = input.value();
  116. }
  117. });
  118. }
  119. });
  120. return (output_directory) ? '/filebrowser/view' + output_directory : null;
  121. });
  122. self.inputDirectoryFilebrowserURL = ko.computed(function() {
  123. var input_directory = null;
  124. $.each(self.framework(), function(index, form) {
  125. if (form.name() == 'input') {
  126. $.each(form.inputs(), function(index, input) {
  127. if (input.name() == 'input.inputDirectory') {
  128. input_directory = input.value();
  129. }
  130. });
  131. }
  132. });
  133. return (input_directory) ? '/filebrowser/view' + input_directory : null;
  134. });
  135. self.storageType = ko.computed(function() {
  136. var storage_type = null;
  137. $.each(self.framework(), function(index, form) {
  138. if (form.name() == 'input') {
  139. storage_type = 'HDFS'; // Hardcoded for now
  140. } else if (form.name() == 'output') {
  141. $.each(form.inputs(), function(index, input) {
  142. if (input.name() == 'output.storageType') {
  143. storage_type = input.value();
  144. }
  145. });
  146. }
  147. });
  148. return storage_type;
  149. });
  150. self.table = ko.computed(function() {
  151. var table = null;
  152. $.each(self.connector(), function(index, form) {
  153. if (form.name() == 'table') {
  154. $.each(form.inputs(), function(index, input) {
  155. if (input.name() == 'table.tableName') {
  156. table = input.value();
  157. }
  158. });
  159. }
  160. });
  161. return table;
  162. });
  163. self.runningInterval = 0;
  164. },
  165. map: function() {
  166. var self = this;
  167. var mapping_options = $.extend(true, {
  168. 'ignore': ['parent', 'initialize']
  169. }, forms.MapProperties);
  170. if ('__ko_mapping__' in self) {
  171. ko.mapping.fromJS(self.model, mapping_options, self);
  172. } else {
  173. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  174. $.extend(self, mapped);
  175. }
  176. },
  177. 'start': function(options) {
  178. var self = this;
  179. $(document).trigger('start.job', [options, self]);
  180. var options = $.extend({
  181. type: 'POST',
  182. success: function(data) {
  183. switch(data.status) {
  184. case 0:
  185. self.submission(new submissions.Submission({modelDict: data.submission}));
  186. $(document).trigger('started.job', [self, options, data.submission]);
  187. break;
  188. default:
  189. case 1:
  190. var error = data.errors[0];
  191. $(document).trigger('start_fail.job', [self, options, error]);
  192. break;
  193. }
  194. }
  195. }, options);
  196. self.request('/sqoop/api/jobs/' + self.id() + '/start', options);
  197. },
  198. 'stop': function(options) {
  199. var self = this;
  200. $(document).trigger('start.job', [options, self]);
  201. var options = $.extend({
  202. type: 'POST',
  203. success: function(data) {
  204. switch(data.status) {
  205. case 0:
  206. self.submission(new submissions.Submission({modelDict: data.submission}));
  207. $(document).trigger('stopped.job', [self, options, data.submission]);
  208. break;
  209. default:
  210. case 1:
  211. $(document).trigger('stop_fail.job', [self, options, data]);
  212. break;
  213. }
  214. }
  215. }, options);
  216. self.request('/sqoop/api/jobs/' + self.id() + '/stop', options);
  217. },
  218. 'getStatus': function(options) {
  219. var self = this;
  220. $(document).trigger('get_status.job', [self, options]);
  221. var options = $.extend({
  222. type: 'GET',
  223. success: function(data) {
  224. switch(data.status) {
  225. case 0:
  226. self.submission(new submissions.Submission({modelDict: data.submission}));
  227. $(document).trigger('got_status.job', [self, options, data.submission]);
  228. break;
  229. default:
  230. case 1:
  231. $(document).trigger('get_status_fail.job', [self, options, data]);
  232. break;
  233. }
  234. }
  235. }, options);
  236. self.request('/sqoop/api/jobs/' + self.id() + '/status', options);
  237. }
  238. });
  239. function fetch_jobs(options) {
  240. $(document).trigger('load.jobs', [options]);
  241. var request = $.extend({
  242. url: '/sqoop/api/jobs/',
  243. dataType: 'json',
  244. type: 'GET',
  245. success: fetcher_success('jobs', Job, options),
  246. error: fetcher_error('jobs', Job, options)
  247. }, options || {});
  248. $.ajax(request);
  249. }
  250. function put_job(job) {
  251. job_registry[job.id()] = job;
  252. }
  253. function get_job(id) {
  254. return job_registry[id];
  255. }
  256. function sync_jobs(jobs) {
  257. job_registry = {};
  258. $.each(jobs, function(index, job) {
  259. put_job(job);
  260. });
  261. }
  262. $(document).on('loaded.jobs', function(e, nodes, options) {
  263. sync_jobs(nodes);
  264. });
  265. return {
  266. 'JobModel': JobModel,
  267. 'Job': Job,
  268. 'fetchJobs': fetch_jobs,
  269. 'putJob': put_job,
  270. 'getJob': get_job,
  271. }
  272. })($);