sqoop.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. return node;
  55. }
  56. function create_job(attrs, options) {
  57. var options = options || {};
  58. options.modelDict = attrs || {};
  59. var node = new jobs.Job(options);
  60. return node;
  61. }
  62. //// View Models
  63. var viewModel = new (function() {
  64. var self = this;
  65. self.jobWizard = new wizard.Wizard();
  66. self.sqoop_errors = ko.observableArray([]);
  67. self.errors = ko.observable({});
  68. self.warnings = ko.observable({});
  69. self.driver = ko.observable();
  70. self.connectors = ko.observableArray();
  71. self.links = ko.observableArray();
  72. self.link = ko.observable();
  73. self.jobs = ko.observableArray();
  74. self.from_link = ko.observable();
  75. self.to_link = ko.observable();
  76. self.editLink = ko.observable();
  77. self.modal = {
  78. 'name': ko.observable()
  79. };
  80. self.filter = ko.observable("");
  81. self.shownSection = ko.observable("");
  82. self.isDirty = ko.observable(false);
  83. self.isLoading = ko.observable(false);
  84. self.isReady = ko.observable(false);
  85. self.isReady.subscribe(function(value) { // fixes problem with too fast rendering engines that display chunks of html before KO bindings
  86. if (value){
  87. $(document).trigger('isready');
  88. }
  89. });
  90. self.connector = ko.computed(function() {
  91. if (self.link() && self.link().connector_id()) {
  92. for (var index in self.connectors()) {
  93. if (self.connectors()[index].id() == self.link().connector_id()) {
  94. return self.connectors()[index];
  95. }
  96. }
  97. }
  98. });
  99. // Must always have a value.
  100. self.from_connector = ko.computed(function() {
  101. // Fall back to first connector so that a connector is selected when we are creating a link.
  102. if (!self.from_link()) {
  103. return self.connectors()[0];
  104. }
  105. var connectorArr = ko.utils.arrayFilter(self.connectors(), function (connector) {
  106. return connector.id() == self.from_link().connector_id();
  107. });
  108. return (connectorArr.length > 0) ? connectorArr[0] : self.connectors()[0];
  109. });
  110. // Must always have a value.
  111. self.to_connector = ko.computed(function() {
  112. // Fall back to first connector so that a connector is selected when we are creating a link.
  113. if (!self.to_link()) {
  114. return self.connectors()[0];
  115. }
  116. var connectorArr = ko.utils.arrayFilter(self.connectors(), function (connector) {
  117. return connector.id() == self.to_link().connector_id();
  118. });
  119. return (connectorArr.length > 0) ? connectorArr[0] : self.connectors()[0];
  120. });
  121. self.persistedJobs = ko.computed(function() {
  122. return ko.utils.arrayFilter(self.jobs(), function (job) {
  123. return job.persisted();
  124. });
  125. });
  126. self.persistedLinks = ko.computed(function() {
  127. return ko.utils.arrayFilter(self.links(), function (link) {
  128. return link.persisted();
  129. });
  130. });
  131. self.filteredJobs = ko.computed(function() {
  132. var filter = self.filter().toLowerCase();
  133. return ko.utils.arrayFilter(self.persistedJobs(), function (job) {
  134. if (job.name()) {
  135. return job.name().toLowerCase().indexOf(filter) > -1 || job.fromLabel().toLowerCase().indexOf(filter) > -1 || job.toLabel().toLowerCase().indexOf(filter) > -1;
  136. } else {
  137. return false;
  138. }
  139. });
  140. });
  141. self.filteredLinks = ko.computed(function() {
  142. var filter = self.filter().toLowerCase();
  143. return ko.utils.arrayFilter(self.persistedLinks(), function (link) {
  144. if (link.name()) {
  145. return link.name().toLowerCase().indexOf(filter) > -1 || link.type().toLowerCase().indexOf(filter) > -1;
  146. } else {
  147. return false;
  148. }
  149. });
  150. });
  151. self.selectedJobs = ko.computed(function() {
  152. return ko.utils.arrayFilter(self.jobs(), function (job) {
  153. return job.selected();
  154. });
  155. });
  156. self.job = ko.computed(function() {
  157. if (self.selectedJobs().length > 0) {
  158. return self.selectedJobs()[0];
  159. }
  160. return null;
  161. });
  162. self.allJobsSelected = ko.computed(function () {
  163. return self.selectedJobs().length > 0 && self.selectedJobs().length == self.jobs().length;
  164. });
  165. // Make sure jobs have connectors and links and links have connectors
  166. self.connectors.subscribe(function() {
  167. $.each(self.links(), function(index, link) {
  168. link.connectors(self.connectors());
  169. });
  170. $.each(self.jobs(), function(index, job) {
  171. job.connectors(self.connectors());
  172. });
  173. });
  174. self.links.subscribe(function() {
  175. $.each(self.links(), function(index, link) {
  176. link.connectors(self.connectors());
  177. });
  178. $.each(self.jobs(), function(index, job) {
  179. job.links(self.links());
  180. });
  181. });
  182. self.jobs.subscribe(function() {
  183. $.each(self.jobs(), function(index, job) {
  184. job.connectors(self.connectors());
  185. job.links(self.links());
  186. });
  187. });
  188. self.connector.subscribe(function() {
  189. if (self.connector() && self.link() && !self.link().persisted()) {
  190. self.link().link_config_values(self.connector()['link-config']());
  191. }
  192. });
  193. self.links.subscribe(function() {
  194. if (self.links().length > 0) {
  195. if (!self.from_link()) {
  196. self.from_link(self.links()[0]);
  197. }
  198. if (!self.to_link()) {
  199. self.to_link(self.links()[0]);
  200. }
  201. }
  202. });
  203. self.from_link.subscribe(function() {
  204. if (self.from_link() && self.from_connector() && self.job()) {
  205. self.from_link().link_config_values(self.from_connector()['link-config']());
  206. self.job().from_config_values(self.from_connector()['job-config'].FROM());
  207. }
  208. });
  209. self.to_link.subscribe(function() {
  210. if (self.to_link() && self.to_connector() && self.job()) {
  211. self.to_link().link_config_values(self.to_connector()['link-config']());
  212. self.job().to_config_values(self.to_connector()['job-config'].TO());
  213. }
  214. });
  215. self.job.subscribe(function() {
  216. self.errors({});
  217. self.warnings({});
  218. if (self.job() && !self.job().persisted()) {
  219. if (self.from_connector()) {
  220. self.job().from_config_values(self.from_connector()['job-config'].FROM());
  221. }
  222. if (self.to_connector()) {
  223. self.job().to_config_values(self.to_connector()['job-config'].TO());
  224. }
  225. }
  226. });
  227. self.newLink = function() {
  228. var self = this;
  229. self.link(create_link());
  230. };
  231. self.saveLink = function() {
  232. if (self.link()) {
  233. if (!self.link().persisted()) {
  234. self.link().connector_id(self.connector().id());
  235. }
  236. self.link().save();
  237. }
  238. };
  239. self.getLinkById = function(id) {
  240. var link = null;
  241. $.each(self.links(), function(index, conn) {
  242. if (conn.id() == id) {
  243. link = conn;
  244. }
  245. });
  246. return link;
  247. };
  248. self.chooseLinkById = function(id) {
  249. var self = this;
  250. self.link(self.getLinkById(id) || self.link());
  251. };
  252. self.deselectAllLinks = function() {
  253. $.each(self.links(), function(index, value) {
  254. value.selected(false);
  255. });
  256. };
  257. self.newJob = function(defaultName) {
  258. var self = this;
  259. if (!self.job() || self.job().persisted()) {
  260. var job = create_job();
  261. job.name(defaultName);
  262. self.jobs.push(job);
  263. self.deselectAllJobs();
  264. job.selected(true);
  265. }
  266. };
  267. self.saveJob = function() {
  268. var job = self.job();
  269. if (job) {
  270. if (!job.from_link_id()) {
  271. job.from_connector_id((self.from_connector()) ? self.from_connector().id() : null);
  272. job.from_link_id((self.from_link()) ? self.from_link().id() : null);
  273. }
  274. if (!job.to_link_id()) {
  275. job.to_connector_id((self.to_connector()) ? self.to_connector().id() : null);
  276. job.to_link_id((self.to_link()) ? self.to_link().id() : null);
  277. }
  278. job.save();
  279. }
  280. };
  281. self.chooseJobById = function(id) {
  282. var found_job = false;
  283. $.each(self.jobs(), function(index, job) {
  284. if (job.id() == id) {
  285. found_job = true;
  286. job.selected(true);
  287. } else {
  288. job.selected(false);
  289. }
  290. });
  291. return found_job;
  292. };
  293. self.toggleAllJobsSelected = function() {
  294. if (!self.allJobsSelected()) {
  295. self.selectAllJobs();
  296. } else {
  297. self.deselectAllJobs();
  298. }
  299. };
  300. self.selectAllJobs = function() {
  301. $.each(self.jobs(), function(index, value) {
  302. value.selected(true);
  303. });
  304. };
  305. self.deselectAllJobs = function() {
  306. $.each(self.jobs(), function(index, value) {
  307. value.selected(false);
  308. });
  309. };
  310. self.label = function(component, name) {
  311. var self = this;
  312. if (component == 'connector') {
  313. for (var index in self.connectors()) {
  314. var label = self.connectors()[index]['all-config-resources'][name + '.label'];
  315. if (label) {
  316. return label;
  317. }
  318. }
  319. } else {
  320. return self['driver']()['all-config-resources'][name + '.label'];
  321. }
  322. };
  323. self.help = function(component, name) {
  324. var self = this;
  325. if (component == 'connector') {
  326. for (var index in self.connectors()) {
  327. var help = self.connectors()[index]['all-config-resources'][name + '.help'];
  328. if (help) {
  329. return help;
  330. }
  331. }
  332. } else {
  333. return self[component]()['all-config-resources'][name + '.help'];
  334. }
  335. };
  336. self.getDatabaseByLinkId = function(id) {
  337. var self = this;
  338. var link = self.getLinkById(id);
  339. if (link) {
  340. var link_string = link.linkString();
  341. if (link_string) {
  342. var link_string_parts = link.linkString().split(':');
  343. if (link_string_parts.length > 2) {
  344. return link_string_parts[1].toUpperCase();
  345. }
  346. }
  347. }
  348. return null;
  349. };
  350. self.showModal = function(name) {
  351. var self = this;
  352. self.modal.name(name);
  353. $('#modal-container').modal();
  354. };
  355. self.showDeleteJobModal = function() {
  356. var self = this;
  357. var name = 'delete-job-modal';
  358. self.showModal(name);
  359. };
  360. self.showDeleteLinkModal = function() {
  361. var self = this;
  362. var name = 'delete-link-modal';
  363. self.showModal(name);
  364. }
  365. self.showFileChooser = function showFileChooser() {
  366. var inputPath = this;
  367. var path = inputPath.value();
  368. $("#filechooser").jHueFileChooser({
  369. initialPath: path,
  370. onFolderChoose: function (filePath) {
  371. inputPath.value(filePath);
  372. $("#chooseFile").modal("hide");
  373. },
  374. onFileChoose:function (filePath) {
  375. inputPath.value(filePath);
  376. $("#chooseFile").modal("hide");
  377. },
  378. createFolder: false,
  379. selectFolder: true,
  380. uploadFile:true
  381. });
  382. $("#chooseFile").modal("show");
  383. };
  384. })();
  385. //// Event handling
  386. function set_driver(e, driver, options) {
  387. viewModel.driver(driver);
  388. }
  389. function set_connectors(e, connectors, options) {
  390. ko.utils.arrayFilter(connectors, function (connector) {
  391. return $.inArray(connector.name(), connectors.CONNECTOR_NAMES) != -1;
  392. });
  393. viewModel.connectors(connectors);
  394. }
  395. function set_links(e, links, options) {
  396. viewModel.links(links);
  397. }
  398. function set_jobs(e, jobs, options) {
  399. viewModel.jobs.removeAll();
  400. viewModel.jobs(jobs);
  401. }
  402. function update_job_submissions(e, submissions, options) {
  403. $.each(submissions, function(index, submission) {
  404. var job = jobs.getJob(submission.job());
  405. if (job) {
  406. job.submission(submission);
  407. }
  408. });
  409. }
  410. $(document).on('loaded.driver', set_driver);
  411. $(document).on('loaded.connectors', set_connectors);
  412. $(document).on('loaded.links', set_links);
  413. $(document).on('loaded.jobs', set_jobs);
  414. $(document).on('loaded.submissions', update_job_submissions);