sqoop.jobs.js 9.5 KB

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