sqoop.jobs.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.storageType = ko.computed(function() {
  110. var storage_type = 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.storageType') {
  115. storage_type = input.value();
  116. }
  117. });
  118. }
  119. });
  120. return storage_type;
  121. });
  122. self.runningInterval = 0;
  123. },
  124. 'start': function(options) {
  125. var self = this;
  126. $(document).trigger('start.job', [options, self]);
  127. var options = $.extend({
  128. type: 'POST',
  129. success: function(data) {
  130. switch(data.status) {
  131. case 0:
  132. self.submission(new submissions.Submission({modelDict: data.submission}));
  133. $(document).trigger('started.job', [self, options, data]);
  134. break;
  135. default:
  136. case 1:
  137. var error = data.errors[0];
  138. $(document).trigger('start_fail.job', [self, options, error.exception]);
  139. break;
  140. }
  141. }
  142. }, options);
  143. self.request('/sqoop/api/jobs/' + self.id() + '/start', options);
  144. },
  145. 'stop': function(options) {
  146. var self = this;
  147. $(document).trigger('start.job', [options, self]);
  148. var options = $.extend({
  149. type: 'POST',
  150. success: function(data) {
  151. switch(data.status) {
  152. case 0:
  153. self.submission(new submissions.Submission({modelDict: data.submission}));
  154. $(document).trigger('stopped.job', [self, options, data.submission]);
  155. break;
  156. default:
  157. case 1:
  158. $(document).trigger('stop_fail.job', [self, options, data]);
  159. break;
  160. }
  161. }
  162. }, options);
  163. self.request('/sqoop/api/jobs/' + self.id() + '/stop', options);
  164. },
  165. 'getStatus': function(options) {
  166. var self = this;
  167. $(document).trigger('get_status.job', [self, options]);
  168. var options = $.extend({
  169. type: 'GET',
  170. success: function(data) {
  171. switch(data.status) {
  172. case 0:
  173. self.submission(new submissions.Submission({modelDict: data.submission}));
  174. $(document).trigger('got_status.job', [self, options, data.submission]);
  175. break;
  176. default:
  177. case 1:
  178. $(document).trigger('get_status_fail.job', [self, options, data]);
  179. break;
  180. }
  181. }
  182. }, options);
  183. self.request('/sqoop/api/jobs/' + self.id() + '/status', options);
  184. }
  185. });
  186. function fetch_jobs(options) {
  187. $(document).trigger('load.jobs', [options]);
  188. var request = $.extend({
  189. url: '/sqoop/api/jobs/',
  190. dataType: 'json',
  191. type: 'GET',
  192. success: fetcher_success('jobs', Job, options)
  193. }, options || {});
  194. $.ajax(request);
  195. }
  196. function put_job(job) {
  197. job_registry[job.id()] = job;
  198. }
  199. function get_job(id) {
  200. return job_registry[id];
  201. }
  202. function sync_jobs(jobs) {
  203. job_registry = {};
  204. $.each(jobs, function(index, job) {
  205. put_job(job);
  206. });
  207. }
  208. $(document).on('loaded.jobs', function(e, nodes, options) {
  209. sync_jobs(nodes);
  210. });
  211. return {
  212. 'JobModel': JobModel,
  213. 'Job': Job,
  214. 'fetchJobs': fetch_jobs,
  215. 'putJob': put_job,
  216. 'getJob': get_job,
  217. }
  218. })($);