sqoop.js 11 KB

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