workflow.node.js 14 KB

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