sqoop.jobs.js 9.2 KB

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