sqoop.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. //// Menu items
  17. function highlightMainMenu(mainSection) {
  18. $(".nav-pills li").removeClass("active");
  19. $("a[href='#" + mainSection + "']").parent().addClass("active");
  20. }
  21. function highlightMenu(section) {
  22. $(".nav-list li").removeClass("active");
  23. $("li[data-section='" + section + "']").addClass("active");
  24. }
  25. function showMainSection(mainSection) {
  26. if ($("#" + mainSection).is(":hidden")) {
  27. $(".mainSection").hide();
  28. $("#" + mainSection).show();
  29. highlightMainMenu(mainSection);
  30. }
  31. }
  32. function showSection(mainSection, section) {
  33. showMainSection(mainSection);
  34. $(document).trigger('show_section', section);
  35. if ($("#" + section).is(":hidden")) {
  36. $(".section").hide();
  37. $("#" + section).show();
  38. highlightMenu(section);
  39. $(document).trigger('shown_section', section);
  40. }
  41. }
  42. function showSubsection(mainSection, section, subSection) {
  43. showSection(mainSection, section);
  44. if ($("#" + subSection).is(":hidden")) {
  45. $(".subSection").hide();
  46. $("#" + subSection).show();
  47. }
  48. }
  49. //// Constructors
  50. function create_link(attrs, options) {
  51. var options = options || {};
  52. options.modelDict = attrs || {};
  53. var node = new links.Link(options);
  54. // Need a copy of the configs so that when editing
  55. // we don't re-use configs.
  56. $.each(viewModel.link().link_config_values(), function(index, config) {
  57. node.connector.push($.extend(true, {}, config));
  58. });
  59. return node;
  60. }
  61. function create_job(attrs, options) {
  62. var options = options || {};
  63. options.modelDict = attrs || {};
  64. var node = new jobs.Job(options);
  65. return node;
  66. }
  67. //// View Models
  68. var viewModel = new (function() {
  69. var self = this;
  70. self.jobWizard = new wizard.Wizard();
  71. self.sqoop_errors = ko.observableArray([]);
  72. self.errors = ko.observable({});
  73. self.warnings = ko.observable({});
  74. self.driver = ko.observable();
  75. self.connectors = ko.observableArray();
  76. self.links = ko.observableArray();
  77. self.jobs = ko.observableArray();
  78. self.link = ko.observable();
  79. self.editLink = ko.observable();
  80. self.modal = {
  81. 'name': ko.observable()
  82. };
  83. self.filter = ko.observable("");
  84. self.shownSection = ko.observable("");
  85. self.isDirty = ko.observable(false);
  86. self.isLoading = ko.observable(false);
  87. self.isReady = ko.observable(false);
  88. self.isReady.subscribe(function(value) { // fixes problem with too fast rendering engines that display chunks of html before KO bindings
  89. if (value){
  90. $(document).trigger('isready');
  91. }
  92. });
  93. // Must always have a value.
  94. self.connector = ko.computed(function() {
  95. // Fall back to first connector so that a connector is selected when we are creating a link.
  96. if (!self.link()) {
  97. return self.connectors()[0];
  98. }
  99. var connectorArr = ko.utils.arrayFilter(self.connectors(), function (connector) {
  100. return connector.id() == self.link().connector_id();
  101. });
  102. return (connectorArr.length > 0) ? connectorArr[0] : self.connectors()[0];
  103. });
  104. self.persistedJobs = ko.computed(function() {
  105. return ko.utils.arrayFilter(self.jobs(), function (job) {
  106. return job.persisted();
  107. });
  108. });
  109. self.persistedLinks = ko.computed(function() {
  110. return ko.utils.arrayFilter(self.links(), function (link) {
  111. return link.persisted();
  112. });
  113. });
  114. self.filteredJobs = ko.computed(function() {
  115. var filter = self.filter().toLowerCase();
  116. return ko.utils.arrayFilter(self.persistedJobs(), function (job) {
  117. if (job.name()) {
  118. return job.name().toLowerCase().indexOf(filter) > -1 || job.type().toLowerCase().indexOf(filter) > -1;
  119. } else {
  120. return false;
  121. }
  122. });
  123. });
  124. self.filteredLinks = ko.computed(function() {
  125. var filter = self.filter().toLowerCase();
  126. return ko.utils.arrayFilter(self.persistedLinks(), function (link) {
  127. if (link.name()) {
  128. return link.name().toLowerCase().indexOf(filter) > -1 || link.type().toLowerCase().indexOf(filter) > -1;
  129. } else {
  130. return false;
  131. }
  132. });
  133. });
  134. self.selectedJobs = ko.computed(function() {
  135. return ko.utils.arrayFilter(self.jobs(), function (job) {
  136. return job.selected();
  137. });
  138. });
  139. self.job = ko.computed(function() {
  140. if (self.selectedJobs().length > 0) {
  141. return self.selectedJobs()[0];
  142. }
  143. return null;
  144. });
  145. self.allJobsSelected = ko.computed(function () {
  146. return self.selectedJobs().length > 0 && self.selectedJobs().length == self.jobs().length;
  147. });
  148. // The driver and connector provide configurations for job
  149. self.driver.subscribe(function(value) {
  150. // We assume that the driver components
  151. // are not going to change so w do not update job objects unless they lack configs.
  152. if (value) {
  153. if (self.job() && self.job().driver_config_values().length == 0) {
  154. self.job().driver_config_values(value.job_config());
  155. }
  156. }
  157. });
  158. self.connector.subscribe(function(value) {
  159. // We assume that the connectors component
  160. // are not going to change so we do not update link
  161. // and job objects unless they lack configs.
  162. if (value) {
  163. if (self.editLink() && self.editLink().link_config_values().length == 0) {
  164. self.editLink().link_config_values(value.link_config());
  165. }
  166. if (self.job() && self.job().from_config_values().length == 0) {
  167. self.job().from_config_values(value.job_config['FROM']());
  168. }
  169. if (self.job() && self.job().to_config_values().length == 0) {
  170. self.job().to_config_values(value.job_config['TO']());
  171. }
  172. }
  173. });
  174. // Forms are swapped between FROM and TO types.
  175. // Use of "beforeChange" subscription event to
  176. // remove subscriptions and help with swapping.
  177. var job_type_subscriptions = [];
  178. var old_connector_configs = {
  179. 'FROM': null,
  180. 'TO': null
  181. };
  182. var old_driver_configs = {
  183. 'FROM': null,
  184. 'TO': null
  185. };
  186. self.job.subscribe(function(old_job) {
  187. if (job_type_subscriptions) {
  188. $.each(job_type_subscriptions, function(index, subscription) {
  189. subscription.dispose();
  190. });
  191. }
  192. }, self, "beforeChange");
  193. self.job.subscribe(function(job) {
  194. if (job) {
  195. if (self.from_config_values() && job.from_config_values().length == 0) {
  196. job.from_config_values(self.from_config_values());
  197. }
  198. if (self.to_config_values() && job.to_config_values().length == 0) {
  199. job.to_config_values(self.to_config_values());
  200. }
  201. if (self.driver_config_values() && job.driver_config_values().length == 0) {
  202. job.driver_config_values(self.driver_config_values());
  203. }
  204. /*job_type_subscriptions.push(job.type.subscribe(function(new_type) {
  205. var connector = old_connector_configs[new_type] || self.connector().job_configs[new_type]();
  206. var driver = old_driver_configs[new_type] || self.driver().job_configs[new_type]();
  207. old_connector_configs[new_type] = null;
  208. old_driver_configs[new_type] = null;
  209. job.connector(connector);
  210. job.driver(driver);
  211. }));
  212. job_type_subscriptions.push(job.type.subscribe(function(old_type) {
  213. if (job.connector().length > 0) {
  214. old_connector_configs[old_type] = job.connector();
  215. }
  216. if (job.driver_config_values().length > 0) {
  217. old_driver_configs[old_type] = job.driver();
  218. }
  219. }, self, "beforeChange"));*/
  220. }
  221. });
  222. self.editLink.subscribe(function() {
  223. if (self.editLink()) {
  224. if (self.link_config_values() && self.editLink().link_config_values().length == 0) {
  225. self.editLink().link_config_values(self.link_config_values());
  226. }
  227. }
  228. });
  229. self.job.subscribe(function() {
  230. self.errors({});
  231. self.warnings({});
  232. });
  233. self.newLink = function() {
  234. var self = this;
  235. if (!self.link() || self.link().persisted()) {
  236. var conn = create_link();
  237. self.editLink(conn);
  238. }
  239. };
  240. self.saveLink = function() {
  241. var link = self.editLink();
  242. if (link) {
  243. link.connector_id(self.connector().id());
  244. link.save();
  245. }
  246. };
  247. self.getLinkById = function(id) {
  248. var link = null;
  249. $.each(self.links(), function(index, conn) {
  250. if (conn.id() == id) {
  251. link = conn;
  252. }
  253. });
  254. return link;
  255. };
  256. self.chooseLinkById = function(id) {
  257. var self = this;
  258. self.editLink(self.getLinkById(id) || self.editLink());
  259. };
  260. self.deselectAllLinks = function() {
  261. $.each(self.links(), function(index, value) {
  262. value.selected(false);
  263. });
  264. };
  265. self.newJob = function(defaultName) {
  266. var self = this;
  267. if (!self.job() || self.job().persisted()) {
  268. var job = create_job();
  269. job.name(defaultName);
  270. self.jobs.push(job);
  271. self.deselectAllJobs();
  272. job.selected(true);
  273. }
  274. };
  275. self.saveJob = function() {
  276. var job = self.job();
  277. if (job) {
  278. if (!self.link()) {
  279. $(document).trigger('link_missing.job', [self, null, {}]);
  280. return;
  281. }
  282. job.connector_id((self.connector()) ? self.connector().id() : null);
  283. job.link_id((self.link()) ? self.link().id() : null);
  284. job.save();
  285. }
  286. };
  287. self.chooseJobById = function(id) {
  288. var found_job = false;
  289. $.each(self.jobs(), function(index, job) {
  290. if (job.id() == id) {
  291. found_job = true;
  292. job.selected(true);
  293. } else {
  294. job.selected(false);
  295. }
  296. });
  297. return found_job;
  298. };
  299. self.toggleAllJobsSelected = function() {
  300. if (!self.allJobsSelected()) {
  301. self.selectAllJobs();
  302. } else {
  303. self.deselectAllJobs();
  304. }
  305. };
  306. self.selectAllJobs = function() {
  307. $.each(self.jobs(), function(index, value) {
  308. value.selected(true);
  309. });
  310. };
  311. self.deselectAllJobs = function() {
  312. $.each(self.jobs(), function(index, value) {
  313. value.selected(false);
  314. });
  315. };
  316. self.label = function(component, name) {
  317. var self = this;
  318. return self[component]().resources[name + '.label'];
  319. };
  320. self.help = function(component, name) {
  321. var self = this;
  322. return self[component]().resources[name + '.help'];
  323. };
  324. self.getDatabaseByLinkId = function(id) {
  325. var self = this;
  326. var link = self.getLinkById(id);
  327. if (link) {
  328. var link_string = link.linkString();
  329. if (link_string) {
  330. var link_string_parts = link.linkString().split(':');
  331. if (link_string_parts.length > 2) {
  332. return link_string_parts[1].toUpperCase();
  333. }
  334. }
  335. }
  336. return null;
  337. };
  338. self.showModal = function(name) {
  339. var self = this;
  340. self.modal.name(name);
  341. $('#modal-container').modal();
  342. };
  343. self.showDeleteJobModal = function() {
  344. var self = this;
  345. var name = 'delete-job-modal';
  346. self.showModal(name);
  347. };
  348. self.showDeleteLinkModal = function() {
  349. var self = this;
  350. var name = 'delete-link-modal';
  351. self.showModal(name);
  352. }
  353. self.showFileChooser = function showFileChooser() {
  354. var inputPath = this;
  355. var path = inputPath.value();
  356. $("#filechooser").jHueFileChooser({
  357. initialPath: path,
  358. onFolderChoose: function (filePath) {
  359. inputPath.value(filePath);
  360. $("#chooseFile").modal("hide");
  361. },
  362. onFileChoose:function (filePath) {
  363. inputPath.value(filePath);
  364. $("#chooseFile").modal("hide");
  365. },
  366. createFolder: false,
  367. selectFolder: true,
  368. uploadFile:true
  369. });
  370. $("#chooseFile").modal("show");
  371. };
  372. })();
  373. //// Event handling
  374. function set_driver(e, driver, options) {
  375. viewModel.driver(driver);
  376. }
  377. function set_connectors(e, connectors, options) {
  378. viewModel.connectors(connectors);
  379. }
  380. function set_links(e, links, options) {
  381. viewModel.links(links);
  382. if (viewModel.links().length > 0) {
  383. viewModel.link(viewModel.links()[0]);
  384. }
  385. }
  386. function set_jobs(e, jobs, options) {
  387. viewModel.jobs.removeAll();
  388. viewModel.jobs(jobs);
  389. }
  390. function update_job_submissions(e, submissions, options) {
  391. $.each(submissions, function(index, submission) {
  392. var job = jobs.getJob(submission.job());
  393. if (job) {
  394. job.submission(submission);
  395. }
  396. });
  397. }
  398. $(document).on('loaded.driver', set_driver);
  399. $(document).on('loaded.connectors', set_connectors);
  400. $(document).on('loaded.links', set_links);
  401. $(document).on('loaded.jobs', set_jobs);
  402. $(document).on('loaded.submissions', update_job_submissions);