sqoop.js 14 KB

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