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. }
  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.isLoading.subscribe(function(value) { // fixes problem with too fast rendering engines that display chunks of html before KO bindings
  89. if (!value){
  90. $(document).trigger('notloading');
  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 connection.
  96. if (!self.connection()) {
  97. return self.connectors()[0];
  98. }
  99. var connectorArr = ko.utils.arrayFilter(self.connectors(), function (connector) {
  100. return connector.id() == self.connection().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.persistedConnections = ko.computed(function() {
  110. return ko.utils.arrayFilter(self.connections(), function (connection) {
  111. return connection.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.selectedJobs = ko.computed(function() {
  125. return ko.utils.arrayFilter(self.jobs(), function (job) {
  126. return job.selected();
  127. });
  128. });
  129. self.job = ko.computed(function() {
  130. if (self.selectedJobs().length > 0) {
  131. return self.selectedJobs()[0];
  132. }
  133. return null;
  134. });
  135. self.allJobsSelected = ko.computed(function () {
  136. return self.selectedJobs().length > 0 && self.selectedJobs().length == self.jobs().length;
  137. });
  138. // Update forms for connectors, jobs, and connections.
  139. // In sqoop, the connector and framework provides
  140. // attributes that need to be filled in for connections
  141. // and jobs. The framework and connector will provide
  142. // different forms for IMPORT and EXPORT jobs.
  143. self.framework.subscribe(function(value) {
  144. // We assume that the framework components
  145. // are not going to change so we do not update connection
  146. // and job objects unless they lack forms.
  147. if (value) {
  148. if (self.editConnection() && self.editConnection().framework().length == 0) {
  149. self.editConnection().framework(value.con_forms());
  150. }
  151. if (self.job() && self.job().framework().length == 0) {
  152. var type = self.job().type().toUpperCase();
  153. self.job().framework(value.job_forms[type]());
  154. }
  155. }
  156. });
  157. self.connector.subscribe(function(value) {
  158. // We assume that the connectors component
  159. // are not going to change so we do not update connection
  160. // and job objects unless they lack forms.
  161. if (value) {
  162. if (self.editConnection() && self.editConnection().connector().length == 0) {
  163. self.editConnection().connector(value.con_forms());
  164. }
  165. if (self.job() && self.job().connector().length == 0) {
  166. var type = self.job().type().toUpperCase();
  167. self.job().connector(value.job_forms[type]());
  168. }
  169. }
  170. });
  171. // Forms are swapped between IMPORT and EXPORT types.
  172. // Use of "beforeChange" subscription event to
  173. // remove subscriptions and help with swapping.
  174. var job_type_subscriptions = [];
  175. var old_connector_forms = {
  176. 'IMPORT': null,
  177. 'EXPORT': null
  178. };
  179. var old_framework_forms = {
  180. 'IMPORT': null,
  181. 'EXPORT': null
  182. };
  183. self.job.subscribe(function(old_job) {
  184. if (job_type_subscriptions) {
  185. $.each(job_type_subscriptions, function(index, subscription) {
  186. subscription.dispose();
  187. });
  188. }
  189. }, self, "beforeChange");
  190. self.job.subscribe(function(job) {
  191. if (job) {
  192. var type = job.type().toUpperCase();
  193. if (self.connector() && job.connector().length == 0) {
  194. job.connector(self.connector().job_forms[type]());
  195. }
  196. if (self.framework() && job.framework().length == 0) {
  197. job.framework(self.framework().job_forms[type]());
  198. }
  199. job_type_subscriptions.push(job.type.subscribe(function(new_type) {
  200. var connector = old_connector_forms[new_type] || self.connector().job_forms[new_type]();
  201. var framework = old_framework_forms[new_type] || self.framework().job_forms[new_type]();
  202. old_connector_forms[new_type] = null;
  203. old_framework_forms[new_type] = null;
  204. job.connector(connector);
  205. job.framework(framework);
  206. }));
  207. job_type_subscriptions.push(job.type.subscribe(function(old_type) {
  208. if (job.connector().length > 0) {
  209. old_connector_forms[old_type] = job.connector();
  210. }
  211. if (job.framework().length > 0) {
  212. old_framework_forms[old_type] = job.framework();
  213. }
  214. }, self, "beforeChange"));
  215. }
  216. });
  217. self.editConnection.subscribe(function() {
  218. if (self.editConnection()) {
  219. if (self.connector() && self.editConnection().connector().length == 0) {
  220. self.editConnection().connector(self.connector().con_forms());
  221. }
  222. if (self.framework() && !self.editConnection().framework().length == 0) {
  223. self.editConnection().framework(self.framework().con_forms());
  224. }
  225. }
  226. });
  227. self.job.subscribe(function() {
  228. self.errors({});
  229. self.warnings({});
  230. });
  231. self.newConnection = function() {
  232. var self = this;
  233. if (!self.connection() || self.connection().persisted()) {
  234. var conn = create_connection();
  235. self.editConnection(conn);
  236. }
  237. };
  238. self.saveConnection = function() {
  239. var connection = self.editConnection();
  240. if (connection) {
  241. connection.connector_id(self.connector().id());
  242. connection.save();
  243. }
  244. };
  245. self.getConnectionById = function(id) {
  246. var connection = null;
  247. $.each(self.connections(), function(index, conn) {
  248. if (conn.id() == id) {
  249. connection = conn;
  250. }
  251. });
  252. return connection;
  253. };
  254. self.chooseConnectionById = function(id) {
  255. var self = this;
  256. self.editConnection(self.getConnectionById(id) || self.editConnection());
  257. };
  258. self.deselectAllConnections = function() {
  259. $.each(self.connections(), function(index, value) {
  260. value.selected(false);
  261. });
  262. };
  263. self.newJob = function(defaultName) {
  264. var self = this;
  265. if (!self.job() || self.job().persisted()) {
  266. var job = create_job();
  267. job.name(defaultName);
  268. self.jobs.push(job);
  269. self.deselectAllJobs();
  270. job.selected(true);
  271. }
  272. };
  273. self.saveJob = function() {
  274. var job = self.job();
  275. if (job) {
  276. job.connector_id(self.connector().id());
  277. job.connection_id(self.connection().id());
  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. return self[component]().resources[name + '.label'];
  313. };
  314. self.help = function(component, name) {
  315. var self = this;
  316. return self[component]().resources[name + '.help'];
  317. };
  318. self.getDatabaseByConnectionId = function(id) {
  319. var self = this;
  320. var connection = self.getConnectionById(id);
  321. if (connection) {
  322. var connection_string = connection.connectionString();
  323. if (connection_string) {
  324. var connection_string_parts = connection.connectionString().split(':');
  325. if (connection_string_parts.length > 2) {
  326. return connection_string_parts[1].toUpperCase();
  327. }
  328. }
  329. }
  330. return null;
  331. };
  332. self.showModal = function(name) {
  333. var self = this;
  334. self.modal.name(name);
  335. $('#modal-container').modal();
  336. };
  337. self.showDeleteJobModal = function() {
  338. var self = this;
  339. var name = 'delete-job-modal';
  340. self.showModal(name);
  341. };
  342. self.showDeleteConnectionModal = function() {
  343. var self = this;
  344. var name = 'delete-connection-modal';
  345. self.showModal(name);
  346. }
  347. self.showFileChooser = function showFileChooser() {
  348. var inputPath = this;
  349. var path = inputPath.value();
  350. $("#filechooser").jHueFileChooser({
  351. initialPath: path,
  352. onFolderChoose: function (filePath) {
  353. inputPath.value(filePath);
  354. $("#chooseFile").modal("hide");
  355. },
  356. onFileChoose:function (filePath) {
  357. inputPath.value(filePath);
  358. $("#chooseFile").modal("hide");
  359. },
  360. createFolder: false,
  361. selectFolder: true,
  362. uploadFile:true
  363. });
  364. $("#chooseFile").modal("show");
  365. };
  366. })();
  367. //// Event handling
  368. function set_framework(e, framework, options) {
  369. viewModel.framework(framework);
  370. }
  371. function set_connectors(e, connectors, options) {
  372. viewModel.connectors(connectors);
  373. }
  374. function set_connections(e, connections, options) {
  375. viewModel.connections(connections);
  376. if (viewModel.connections().length > 0) {
  377. viewModel.connection(viewModel.connections()[0]);
  378. }
  379. }
  380. function set_jobs(e, jobs, options) {
  381. viewModel.jobs.removeAll();
  382. viewModel.jobs(jobs);
  383. }
  384. function update_job_submissions(e, submissions, options) {
  385. $.each(submissions, function(index, submission) {
  386. var job = jobs.getJob(submission.job());
  387. if (job) {
  388. job.submission(submission);
  389. }
  390. });
  391. }
  392. $(document).on('loaded.framework', set_framework);
  393. $(document).on('loaded.connectors', set_connectors);
  394. $(document).on('loaded.connections', set_connections);
  395. $(document).on('loaded.jobs', set_jobs);
  396. $(document).on('loaded.submissions', update_job_submissions);
  397. $(document).on('saved.connection', function(){ connections.fetchConnections(); });
  398. $(document).on('saved.job', function() { jobs.fetchJobs(); });
  399. $(document).on('cloned.connection', function(){ connections.fetchConnections(); });
  400. $(document).on('cloned.job', function() { jobs.fetchJobs(); });
  401. $(document).on('deleted.connection', function(){ connections.fetchConnections(); });
  402. $(document).on('deleted.job', function() { jobs.fetchJobs(); });