sqoop.js 13 KB

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