sqoop.jobs.js 5.9 KB

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