workflow.node.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. function format_errors_mapping(model) {
  17. var errors = {};
  18. for(var key in model) {
  19. switch(key) {
  20. case 'child_links':
  21. case 'node_ptr':
  22. case 'initialize':
  23. case 'toString':
  24. break;
  25. default:
  26. errors[key] = [];
  27. break;
  28. }
  29. }
  30. return errors;
  31. }
  32. /**
  33. * Node
  34. * Displays node in a graph and handles graph manipulation.
  35. * The majority of nodes require similar logic.
  36. * This modules takes advantage of that fact.
  37. */
  38. var NodeModule = function($, IdGeneratorTable, NodeFields) {
  39. var META_LINKS = ['related', 'default', 'error'];
  40. var linkTypeChooser = function(parent, child) {
  41. if (child.node_type() == 'kill') {
  42. return 'error';
  43. }
  44. switch(parent.node_type()) {
  45. case 'start':
  46. return (child.node_type() == 'end') ? 'related' : 'to';
  47. case 'fork':
  48. return (child.node_type() == 'join') ? 'related' : 'start';
  49. case 'decision':
  50. return (child.node_type() == 'decisionend') ? 'related' : 'start';
  51. case 'join':
  52. case 'decisionend':
  53. return 'to';
  54. default:
  55. return 'ok';
  56. };
  57. };
  58. var module = function(workflow, model, registry) {
  59. var self = this;
  60. self.map(model);
  61. self.links = ko.computed(function() {
  62. var links = self.child_links().filter(function(element, index, arr) {
  63. return $.inArray(element.name(), META_LINKS) == -1;
  64. });
  65. return links;
  66. });
  67. self.meta_links = ko.computed(function() {
  68. var links = self.child_links().filter(function(element, index, arr) {
  69. return $.inArray(element.name(), META_LINKS) != -1;
  70. });
  71. return links;
  72. });
  73. self.non_error_links = ko.computed(function() {
  74. var links = self.child_links().filter(function(element, index, arr) {
  75. return element.name() != 'error';
  76. });
  77. return links;
  78. });
  79. self._workflow = workflow;
  80. self.registry = registry;
  81. self.children = ko.observableArray([]);
  82. self.model = model;
  83. self.errors = ko.mapping.fromJS(format_errors_mapping(model));
  84. self.edit_template = model.node_type + 'EditTemplate';
  85. switch(model.node_type) {
  86. case 'start':
  87. case 'end':
  88. self.view_template = ko.observable('disabledNodeTemplate');
  89. break;
  90. case 'kill':
  91. self.view_template = ko.observable('emptyTemplate');
  92. break;
  93. case 'fork':
  94. self.view_template = ko.observable('forkTemplate');
  95. break;
  96. case 'join':
  97. self.view_template = ko.observable('joinTemplate');
  98. break;
  99. case 'decision':
  100. self.view_template = ko.observable('decisionTemplate');
  101. break;
  102. case 'decisionend':
  103. self.view_template = ko.observable('decisionEndTemplate');
  104. break;
  105. default:
  106. self.view_template = ko.observable('nodeTemplate');
  107. break;
  108. }
  109. // Data manipulation
  110. if ('files' in model) {
  111. //// WARNING: The following order should be preserved!
  112. // Need to represent files as some thing else for knockout mappings.
  113. // The KO idiom "value" requires a named parameter.
  114. self._files = self.files;
  115. self.files = ko.observableArray([]);
  116. // ['file', ...] => [{'name': 'file', 'dummy': ''}, ...].
  117. $.each(self._files(), function(index, filename) {
  118. var prop = { name: ko.observable(filename), dummy: ko.observable("") };
  119. prop.name.subscribe(function(value) {
  120. self.files.valueHasMutated();
  121. });
  122. prop.dummy.subscribe(function(value) {
  123. self.files.valueHasMutated();
  124. });
  125. self.files.push(prop);
  126. });
  127. // [{'name': 'file', 'dummy': ''}, ...] => ['file', ...].
  128. self.files.subscribe(function(value) {
  129. self._files.removeAll();
  130. $.each(self.files(), function(index, file) {
  131. self._files.push(file.name);
  132. });
  133. });
  134. self.addFile = function() {
  135. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  136. prop.name.subscribe(function(value) {
  137. self.files.valueHasMutated();
  138. });
  139. prop.dummy.subscribe(function(value) {
  140. self.files.valueHasMutated();
  141. });
  142. self.files.push(prop);
  143. };
  144. self.removeFile = function(val) {
  145. self.files.remove(val);
  146. };
  147. }
  148. self.initialize.apply(self, arguments);
  149. return self;
  150. };
  151. $.extend(true, module.prototype, NodeFields, {
  152. // Data.
  153. children: null,
  154. model: null,
  155. // Normal stuff
  156. /**
  157. * Called when creating a new node
  158. */
  159. initialize: function(workflow, model, registry) {},
  160. toString: function() {
  161. return '';
  162. },
  163. /**
  164. * Fetches registry
  165. */
  166. getRegistry: function() {
  167. return registry;
  168. },
  169. /**
  170. * Maps a model to self
  171. * Called when creating a new node before any thing else
  172. */
  173. map: function(model) {
  174. var self = this;
  175. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  176. // MAPPING_OPTIONS comes from /oozie/static/js/models.js
  177. var mapping = ko.mapping.fromJS(model, MAPPING_OPTIONS);
  178. $.extend(self, mapping);
  179. $.each(mapping, function(key, value) {
  180. var key = key;
  181. if (ko.isObservable(self[key])) {
  182. self[key].subscribe(function(value) {
  183. model[key] = ko.mapping.toJS(value);
  184. });
  185. }
  186. });
  187. $.each(self.child_links(), function(index, link) {
  188. var $index = index;
  189. link.comment.subscribe(function(value) {
  190. self.model.child_links[$index].comment = value;
  191. });
  192. link.child.subscribe(function(value) {
  193. self.model.child_links[$index].child = value;
  194. });
  195. });
  196. },
  197. validate: function( ) {
  198. var self = this;
  199. var options = {};
  200. data = $.extend(true, {}, self.model);
  201. var success = false;
  202. var request = $.extend({
  203. url: '/oozie/workflows/' + self._workflow.id() + '/nodes/' + self.node_type() + '/validate',
  204. type: 'POST',
  205. data: { node: JSON.stringify(data) },
  206. success: function(data) {
  207. ko.mapping.fromJS(data.data, self.errors);
  208. success = data.status == 0;
  209. },
  210. async: false
  211. }, options);
  212. $.ajax(request);
  213. return success;
  214. },
  215. // Hierarchy manipulation.
  216. /**
  217. * Append node to self
  218. * Does not support multiple children.
  219. * Ensures single child.
  220. * Ensures no cycles.
  221. * 1. Finds all children and attaches them to node (cleans node first).
  222. * 2. Remove all children from self.
  223. * 3. Attach node to self.
  224. */
  225. append: function(node) {
  226. var self = this;
  227. // Not fork nor decision nor self
  228. if ($.inArray(self.node_type(), ['fork', 'decision']) == -1 && node.id() != self.id() && !self.isChild(node)) {
  229. node.removeAllChildren();
  230. $.each(self.links(), function(index, link) {
  231. node.addChild(self.registry.get(link.child()));
  232. });
  233. self.removeAllChildren();
  234. self.addChild(node);
  235. }
  236. },
  237. /**
  238. * Find all parents of current node
  239. */
  240. findParents: function() {
  241. var self = this;
  242. var parents = [];
  243. $.each(self.registry.nodes, function(id, node) {
  244. $.each(node.links(), function(index, link) {
  245. if (link.child() == self.id()) {
  246. parents.push(node);
  247. }
  248. });
  249. });
  250. return parents;
  251. },
  252. findErrorParents: function() {
  253. var self = this;
  254. var parents = [];
  255. $.each(self.registry.nodes, function(id, node) {
  256. $.each(node.meta_links(), function(index, link) {
  257. if (link.child() == self.id()) {
  258. parents.push(node);
  259. }
  260. });
  261. });
  262. return parents;
  263. },
  264. /**
  265. * Find all children of current node
  266. */
  267. findChildren: function() {
  268. var self = this;
  269. var children = [];
  270. $.each(self.links(), function(index, link) {
  271. children.push(self.registry.get(link.child()));
  272. });
  273. return children;
  274. },
  275. /**
  276. * Detach current node from the graph
  277. * 1. Takes children of self node, removes them from self node, and adds them to each parent of self node.
  278. * 2. The self node is then removed from every parent.
  279. * 3. Does not support multiple children since we do not automatically fork.
  280. */
  281. detach: function() {
  282. var self = this;
  283. $.each(self.findParents(), function(index, parent) {
  284. $.each(self.links(), function(index, link) {
  285. var node = self.registry.get(link.child());
  286. parent.replaceChild(self, node);
  287. });
  288. });
  289. // Error links of parents reset to kill node.
  290. $.each(self.findErrorParents(), function(index, parent) {
  291. parent.putErrorChild(self._workflow.kill);
  292. });
  293. $(self).trigger('detached');
  294. self.removeAllChildren();
  295. },
  296. /**
  297. * Add child
  298. * Update child links for this node.
  299. */
  300. addChild: function(node, link_type) {
  301. var self = this;
  302. var link_type = link_type || linkTypeChooser(self, node);
  303. var link = {
  304. parent: ko.observable(self.id()),
  305. child: ko.observable(node.id()),
  306. name: ko.observable(link_type),
  307. comment: ko.observable('')
  308. };
  309. self.child_links.unshift(link);
  310. },
  311. /**
  312. * Remove child node
  313. * 1. Find child node link
  314. * 2. Remove child node link
  315. */
  316. removeChild: function(node) {
  317. var self = this;
  318. var spliceIndex = -1;
  319. $.each(self.child_links(), function(index, link) {
  320. if (link.child() == node.id()) {
  321. spliceIndex = index;
  322. }
  323. });
  324. if (spliceIndex > -1) {
  325. self.child_links.splice(spliceIndex, 1);
  326. }
  327. return spliceIndex != -1;
  328. },
  329. /**
  330. * Remove error child
  331. * 1. Find child node link
  332. * 2. Remove child node link
  333. */
  334. removeErrorChildren: function() {
  335. var self = this;
  336. var spliceIndexes = [];
  337. $.each(self.child_links(), function(index, link) {
  338. if (link.name() == 'error') {
  339. spliceIndexes.push(index);
  340. }
  341. });
  342. var spliceCount = 0;
  343. if (spliceIndexes.length > 0) {
  344. $.each(spliceIndexes, function(index, spliceIndex) {
  345. self.child_links.splice(spliceIndex - spliceCount++, 1);
  346. });
  347. }
  348. return spliceIndexes.length > 0;
  349. },
  350. /**
  351. * Remove all children
  352. * Removes all children except for related, default, and error links
  353. * Note: we hold on to related, default, and error links because
  354. * we have to.
  355. */
  356. removeAllChildren: function() {
  357. var self = this;
  358. var keep_links = [];
  359. $.each(self.child_links(), function(index, link) {
  360. if ($.inArray(link.name(), META_LINKS) > -1) {
  361. keep_links.push(link);
  362. }
  363. });
  364. self.child_links.removeAll();
  365. $.each(keep_links, function(index, link) {
  366. self.child_links.push(link);
  367. });
  368. },
  369. /**
  370. * Replace child node with another node in the following way:
  371. * 1. Find child index
  372. * 2. Remove child index
  373. * 3. Remove and remember every element after child
  374. * 4. Add replacement node
  375. * 5. Add every child that was remembered
  376. */
  377. replaceChild: function(child, replacement) {
  378. var self = this;
  379. var index = -1;
  380. $.each(self.non_error_links(), function(i, link) {
  381. if (link.child() == child.id()) {
  382. index = i;
  383. }
  384. });
  385. if (index > -1) {
  386. self.child_links.splice(index, 1);
  387. var links = self.child_links.splice(index);
  388. var link = {
  389. parent: ko.observable(self.id()),
  390. child: ko.observable(replacement.id()),
  391. name: ko.observable(linkTypeChooser(self, replacement)),
  392. comment: ko.observable('')
  393. };
  394. self.child_links.push(link);
  395. $.each(links, function(index, link) {
  396. self.child_links.push(link);
  397. });
  398. }
  399. return index != -1;
  400. },
  401. /**
  402. * Replace or add error node with another node in the following way:
  403. * 1. Find child index
  404. * 2. Remove child index
  405. * 3. Remove and remember every element after child
  406. * 4. Add replacement node
  407. * 5. Add every child that was remembered
  408. */
  409. putErrorChild: function(node) {
  410. var self = this;
  411. var index = -1;
  412. $.each(self.child_links(), function(i, link) {
  413. if (link.name() == 'error') {
  414. index = i;
  415. }
  416. });
  417. var link = {
  418. parent: ko.observable(self.id()),
  419. child: ko.observable(node.id()),
  420. name: ko.observable('error'),
  421. comment: ko.observable('')
  422. };
  423. if (index > -1) {
  424. var child_links = self.child_links();
  425. child_links.splice(index, 1);
  426. var links = child_links.splice(index);
  427. child_links.push(link);
  428. $.each(links, function(index, link) {
  429. child_links.push(link);
  430. });
  431. self.child_links(child_links);
  432. } else {
  433. self.child_links.push(link);
  434. }
  435. return index != -1;
  436. },
  437. /**
  438. * Get the error child
  439. */
  440. getErrorChild: function() {
  441. var self = this;
  442. var children = [];
  443. $.each(self.meta_links(), function(index, link) {
  444. if (link.name() == 'error') {
  445. children.push(self.registry.get(link.child()));
  446. }
  447. });
  448. return (children.length > 0) ? children[0] : null;
  449. },
  450. isChild: function(node) {
  451. var self = this;
  452. var res = false;
  453. $.each(self.links(), function(index, link) {
  454. if (link.child() == node.id()) {
  455. res = true;
  456. }
  457. });
  458. return res;
  459. },
  460. erase: function() {
  461. var self = this;
  462. self.registry.remove(self.id());
  463. }
  464. });
  465. return module;
  466. };