workflow.node.js 14 KB

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