sqoop.jobs.js 8.7 KB

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