sqoop.jobs.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(this.id());
  75. },
  76. write: function (submission) {
  77. submissions.putSubmission(submission);
  78. self.id.valueHasMutated();
  79. if (self.runningInterval == 0 && self.isRunning()) {
  80. self.runningInterval = setInterval(function() {
  81. if (!self.isRunning()) {
  82. clearInterval(self.runningInterval);
  83. self.runningInterval = 0;
  84. }
  85. self.getStatus();
  86. }, 2000);
  87. }
  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.hasSucceeded = ko.computed(function() {
  97. return self.submission() && $.inArray(self.submission().status(), ['SUCCEEDED']) > -1;
  98. });
  99. self.hasFailed = ko.computed(function() {
  100. return self.submission() && $.inArray(self.submission().status(), ['FAILURE_ON_SUBMIT', 'FAILED']) > -1;
  101. });
  102. self.outputDirectoryFilebrowserURL = ko.computed(function() {
  103. var output_directory = null;
  104. $.each(self.framework(), function(index, form) {
  105. if (form.name() == 'output') {
  106. $.each(form.inputs(), function(index, input) {
  107. if (input.name() == 'output.outputDirectory') {
  108. output_directory = input.value();
  109. }
  110. });
  111. }
  112. });
  113. return (output_directory) ? '/filebrowser/view' + output_directory : null;
  114. });
  115. self.storageType = ko.computed(function() {
  116. var storage_type = null;
  117. $.each(self.framework(), function(index, form) {
  118. if (form.name() == 'output') {
  119. $.each(form.inputs(), function(index, input) {
  120. if (input.name() == 'output.storageType') {
  121. storage_type = input.value();
  122. }
  123. });
  124. }
  125. });
  126. return storage_type;
  127. });
  128. self.runningInterval = 0;
  129. },
  130. 'start': function(options) {
  131. var self = this;
  132. $(document).trigger('start.job', [options, self]);
  133. var options = $.extend({
  134. type: 'POST',
  135. success: function(data) {
  136. switch(data.status) {
  137. case 0:
  138. self.submission(new submissions.Submission({modelDict: data.submission}));
  139. $(document).trigger('started.job', [self, options, data.submission]);
  140. break;
  141. default:
  142. case 1:
  143. var error = data.errors[0];
  144. $(document).trigger('start_fail.job', [self, options, error.exception]);
  145. break;
  146. }
  147. }
  148. }, options);
  149. self.request('/sqoop/api/jobs/' + self.id() + '/start', options);
  150. },
  151. 'stop': function(options) {
  152. var self = this;
  153. $(document).trigger('start.job', [options, self]);
  154. var options = $.extend({
  155. type: 'POST',
  156. success: function(data) {
  157. switch(data.status) {
  158. case 0:
  159. self.submission(new submissions.Submission({modelDict: data.submission}));
  160. $(document).trigger('stopped.job', [self, options, data.submission]);
  161. break;
  162. default:
  163. case 1:
  164. $(document).trigger('stop_fail.job', [self, options, data]);
  165. break;
  166. }
  167. }
  168. }, options);
  169. self.request('/sqoop/api/jobs/' + self.id() + '/stop', options);
  170. },
  171. 'getStatus': function(options) {
  172. var self = this;
  173. $(document).trigger('get_status.job', [self, options]);
  174. var options = $.extend({
  175. type: 'GET',
  176. success: function(data) {
  177. switch(data.status) {
  178. case 0:
  179. self.submission(new submissions.Submission({modelDict: data.submission}));
  180. $(document).trigger('got_status.job', [self, options, data.submission]);
  181. break;
  182. default:
  183. case 1:
  184. $(document).trigger('get_status_fail.job', [self, options, data]);
  185. break;
  186. }
  187. }
  188. }, options);
  189. self.request('/sqoop/api/jobs/' + self.id() + '/status', options);
  190. }
  191. });
  192. function fetch_jobs(options) {
  193. $(document).trigger('load.jobs', [options]);
  194. var request = $.extend({
  195. url: '/sqoop/api/jobs/',
  196. dataType: 'json',
  197. type: 'GET',
  198. success: fetcher_success('jobs', Job, options)
  199. }, options || {});
  200. $.ajax(request);
  201. }
  202. function put_job(job) {
  203. job_registry[job.id()] = job;
  204. }
  205. function get_job(id) {
  206. return job_registry[id];
  207. }
  208. function sync_jobs(jobs) {
  209. job_registry = {};
  210. $.each(jobs, function(index, job) {
  211. put_job(job);
  212. });
  213. }
  214. $(document).on('loaded.jobs', function(e, nodes, options) {
  215. sync_jobs(nodes);
  216. });
  217. return {
  218. 'JobModel': JobModel,
  219. 'Job': Job,
  220. 'fetchJobs': fetch_jobs,
  221. 'putJob': put_job,
  222. 'getJob': get_job,
  223. }
  224. })($);