sqoop.jobs.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. },
  31. 'setExport': function(){
  32. this.type("EXPORT");
  33. },
  34. 'initialize': function(attrs) {
  35. var self = this;
  36. var attrs = $.extend(true, attrs, {});
  37. attrs = transform_keys(attrs, {
  38. 'connector-id': 'connector_id',
  39. 'connection-id': 'connection_id'
  40. });
  41. attrs = transform_values(attrs, {
  42. 'connector': to_forms,
  43. 'framework': to_forms
  44. });
  45. return attrs;
  46. }
  47. });
  48. var Job = koify.Node.extend({
  49. 'identifier': 'job',
  50. 'persists': true,
  51. 'modelClass': JobModel,
  52. 'base_url': '/sqoop/api/jobs/',
  53. 'initialize': function() {
  54. var self = this;
  55. self.parent.initialize.apply(self, arguments);
  56. self.createdFormatted = ko.computed(function() {
  57. if (self.created()) {
  58. return moment(self.created()).format('MM/DD/YYYY hh:mm A');
  59. } else {
  60. return 0;
  61. }
  62. });
  63. self.updatedFormatted = ko.computed(function() {
  64. if (self.updated()) {
  65. return moment(self.updated()).format('MM/DD/YYYY hh:mm A');
  66. } else {
  67. return 0;
  68. }
  69. });
  70. self.selected = ko.observable();
  71. self.submission = ko.computed({
  72. owner: self,
  73. read: function () {
  74. return submissions.setDefaultSubmission(self.id());
  75. },
  76. write: function (submission) {
  77. submissions.putSubmission(submission);
  78. if (self.runningInterval == 0 && $.inArray(submission.status(), ['BOOTING', 'RUNNING']) != -1) {
  79. self.runningInterval = setInterval(function() {
  80. if (!self.isRunning()) {
  81. clearInterval(self.runningInterval);
  82. self.runningInterval = 0;
  83. }
  84. self.getStatus();
  85. }, 2000);
  86. }
  87. self.id.valueHasMutated();
  88. }
  89. });
  90. self.persisted = ko.computed(function() {
  91. return self.id() > -1;
  92. });
  93. self.isRunning = ko.computed(function() {
  94. return self.submission() && $.inArray(self.submission().status(), ['BOOTING', 'RUNNING']) > -1;
  95. });
  96. self.outputDirectoryFilebrowserURL = ko.computed(function() {
  97. var output_directory = null;
  98. $.each(self.framework(), function(index, form) {
  99. if (form.name() == 'output') {
  100. $.each(form.inputs(), function(index, input) {
  101. if (input.name() == 'output.outputDirectory') {
  102. output_directory = input.value();
  103. }
  104. });
  105. }
  106. });
  107. return (output_directory) ? '/filebrowser/view' + output_directory : null;
  108. });
  109. self.runningInterval = 0;
  110. },
  111. 'start': function(options) {
  112. var self = this;
  113. $(document).trigger('start.job', [options, self]);
  114. var options = $.extend({
  115. type: 'POST',
  116. success: function(data) {
  117. switch(data.status) {
  118. case 0:
  119. self.submission(new submissions.Submission({modelDict: data.submission}));
  120. $(document).trigger('started.job', [self, options, data]);
  121. break;
  122. default:
  123. case 1:
  124. var error = data.errors[0];
  125. $(document).trigger('start_fail.job', [self, options, error.exception]);
  126. break;
  127. }
  128. }
  129. }, options);
  130. self.request('/sqoop/api/jobs/' + self.id() + '/start', options);
  131. },
  132. 'stop': function(options) {
  133. var self = this;
  134. $(document).trigger('start.job', [options, self]);
  135. var options = $.extend({
  136. type: 'POST',
  137. success: function(data) {
  138. switch(data.status) {
  139. case 0:
  140. self.submission(new submissions.Submission({modelDict: data.submission}));
  141. $(document).trigger('stopped.job', [self, options, data.submission]);
  142. break;
  143. default:
  144. case 1:
  145. $(document).trigger('stop_fail.job', [self, options, data]);
  146. break;
  147. }
  148. }
  149. }, options);
  150. self.request('/sqoop/api/jobs/' + self.id() + '/stop', options);
  151. },
  152. 'getStatus': function(options) {
  153. var self = this;
  154. $(document).trigger('get_status.job', [self, options]);
  155. var options = $.extend({
  156. type: 'GET',
  157. success: function(data) {
  158. switch(data.status) {
  159. case 0:
  160. self.submission(new submissions.Submission({modelDict: data.submission}));
  161. $(document).trigger('got_status.job', [self, options, data.submission]);
  162. break;
  163. default:
  164. case 1:
  165. $(document).trigger('get_status_fail.job', [self, options, data]);
  166. break;
  167. }
  168. }
  169. }, options);
  170. self.request('/sqoop/api/jobs/' + self.id() + '/status', options);
  171. }
  172. });
  173. function fetch_jobs(options) {
  174. $(document).trigger('load.jobs', [options]);
  175. var request = $.extend({
  176. url: '/sqoop/api/jobs/',
  177. dataType: 'json',
  178. type: 'GET',
  179. success: fetcher_success('jobs', Job, options)
  180. }, options || {});
  181. $.ajax(request);
  182. }
  183. function put_job(job) {
  184. job_registry[job.id()] = job;
  185. }
  186. function get_job(id) {
  187. return job_registry[id];
  188. }
  189. function sync_jobs(jobs) {
  190. job_registry = {};
  191. $.each(jobs, function(index, job) {
  192. put_job(job);
  193. });
  194. }
  195. $(document).on('loaded.jobs', function(e, nodes, options) {
  196. sync_jobs(nodes);
  197. });
  198. return {
  199. 'JobModel': JobModel,
  200. 'Job': Job,
  201. 'fetchJobs': fetch_jobs,
  202. 'putJob': put_job,
  203. 'getJob': get_job,
  204. }
  205. })($);