workflow.node.js 14 KB

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