workflow.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  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. // adding missing .filter for IE8
  17. if (!('filter' in Array.prototype)) {
  18. Array.prototype.filter= function(filter, that /*opt*/) {
  19. var other= [], v;
  20. for (var i=0, n= this.length; i<n; i++)
  21. if (i in this && filter.call(that, v= this[i], i, this))
  22. other.push(v);
  23. return other;
  24. };
  25. }
  26. // Since knockout maps arrays without calling "update" nor "create"
  27. // Provide a JSON string that will be parsed in custom 'create' and 'update' functions.
  28. var normalize_model_fields = (function() {
  29. var FIELDS = ['parameters', 'job_properties', 'files', 'archives', 'prepares', 'params',
  30. 'deletes', 'mkdirs', 'moves', 'chmods', 'touchzs'];
  31. function fn(node_model) {
  32. $.each(FIELDS, function(index, field) {
  33. if (field in node_model && $.isArray(node_model[field])) {
  34. node_model[field] = JSON.stringify(node_model[field]);
  35. }
  36. });
  37. }
  38. return fn;
  39. })();
  40. /**
  41. * Registry of models
  42. * - Each model should have an ID attribute.
  43. */
  44. var RegistryModule = function($) {
  45. var module = function() {
  46. var self = this;
  47. self.nodes = {};
  48. module.prototype.initialize.apply(self, arguments);
  49. return self;
  50. };
  51. $.extend(module.prototype, {
  52. // Normal stuff
  53. initialize: function() {},
  54. toString: function() {
  55. var self = this;
  56. var s = $.map(self.nodes, function(node) {
  57. return node.id();
  58. }).join();
  59. return s;
  60. },
  61. add: function(id, node) {
  62. var self = this;
  63. $(self).trigger('registry:add');
  64. self.nodes[String(id)] = node;
  65. },
  66. remove: function(id) {
  67. var self = this;
  68. $(self).trigger('registry:remove');
  69. delete self.nodes[String(id)];
  70. },
  71. get: function(id) {
  72. var self = this;
  73. return self.nodes[id];
  74. },
  75. clear: function() {
  76. var self = this;
  77. delete self.nodes;
  78. self.nodes = {};
  79. }
  80. });
  81. return module;
  82. };
  83. var Registry = RegistryModule($);
  84. /**
  85. * Modal Control
  86. * Displays a single node in a modal window of your choosing.
  87. * Make sure to update the 'context' member and 'template' members.
  88. * IMPORTANT: Use 'setTemplate', then 'show'.
  89. */
  90. var ModalModule = function($, ko) {
  91. var module = function(modal, template) {
  92. var self = this;
  93. self.el = self.modal = $(modal);
  94. self.context = ko.observable();
  95. self.template = ko.observable(template || '');
  96. self.bound = false;
  97. };
  98. module.prototype.show = function(context) {
  99. var self = this;
  100. if (context) {
  101. self.context(context);
  102. }
  103. ko.applyBindings(self, self.modal[0]);
  104. self.modal.modal('show');
  105. };
  106. module.prototype.hide = function(context) {
  107. var self = this;
  108. self.el.modal('hide');
  109. };
  110. module.prototype.setTemplate = function(template) {
  111. var self = this;
  112. ko.cleanNode(self.modal[0]);
  113. self.template( template );
  114. };
  115. module.prototype.recenter = function(offset_x, offset_y) {
  116. var self = this;
  117. var top = ( ($(window).height() - self.modal.outerHeight(false)) / 2 );
  118. var left = ( ($(window).width() - self.modal.outerWidth(false)) / 2 );
  119. if (top < 0) {
  120. top = 0;
  121. }
  122. if (left < 0) {
  123. left = 0;
  124. }
  125. top += offset_y || 0;
  126. left += offset_x || 0;
  127. self.modal.css({top: top +'px', left: left+'px'});
  128. };
  129. module.prototype.addDecorations = function () {
  130. $(".popover").remove();
  131. $("input[name='job_xml']:not(.pathChooser)").addClass("pathChooser").after(getFileBrowseButton($("input[name='job_xml']:not(.pathChooser)")));
  132. $("input[name='jar_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='jar_path']")));
  133. $("input[name='script_path']").addClass("pathChooser").after(getFileBrowseButton($("input[name='script_path']")));
  134. $("input[name='command']").addClass("pathChooser").after(getFileBrowseButton($("input[name='command']")));
  135. $("*[rel=popover]").popover({
  136. placement:'left',
  137. trigger:'hover'
  138. });
  139. }
  140. return module;
  141. };
  142. var Modal = ModalModule($, ko);
  143. /**
  144. * ID Generator
  145. * Generate a new ID starting from 1.
  146. * - Accepts a prefix that will be prepended like so: <prefix>:<id number>
  147. */
  148. var IdGeneratorModule = function($) {
  149. return function(options) {
  150. var self = this;
  151. $.extend(self, options);
  152. self.counter = 1;
  153. self.nextId = function() {
  154. return ((self.prefix) ? self.prefix + ':' : '') + self.counter++;
  155. };
  156. };
  157. };
  158. var IdGenerator = IdGeneratorModule($);
  159. var ModelModule = function($) {
  160. var module = function(attrs) {
  161. var self = this;
  162. $.extend(self, attrs);
  163. module.prototype.initialize.apply(self, arguments);
  164. return self;
  165. };
  166. $.extend(module.prototype, {
  167. // Normal stuff
  168. initialize: function(){},
  169. toString: function() {
  170. var self = this;
  171. return JSON.stringify(self, null, '\t');
  172. }
  173. });
  174. return module;
  175. };
  176. var WorkflowModel = ModelModule($);
  177. $.extend(WorkflowModel.prototype, {
  178. id: 0,
  179. name: '',
  180. description: '',
  181. start: 0,
  182. end: 0,
  183. schema_version: 0.4,
  184. deployment_dir: '',
  185. is_shared: true,
  186. parameters: '[]',
  187. job_xml: ''
  188. });
  189. var NodeModel = ModelModule($);
  190. $.extend(NodeModel.prototype, {
  191. id: 0,
  192. name: '',
  193. description: '',
  194. node_type: '',
  195. workflow: 0,
  196. child_links: []
  197. });
  198. var ForkModel = ModelModule($);
  199. $.extend(ForkModel.prototype, {
  200. id: 0,
  201. name: '',
  202. description: '',
  203. node_type: 'fork',
  204. workflow: 0,
  205. child_links: []
  206. });
  207. var DecisionModel = ModelModule($);
  208. $.extend(DecisionModel.prototype, {
  209. id: 0,
  210. name: '',
  211. description: '',
  212. node_type: 'decision',
  213. workflow: 0,
  214. child_links: []
  215. });
  216. var DistCPModel = ModelModule($);
  217. $.extend(DistCPModel.prototype, {
  218. id: 0,
  219. name: '',
  220. description: '',
  221. node_type: 'distcp',
  222. workflow: 0,
  223. job_properties: '[]',
  224. prepares: '[]',
  225. job_xml: '',
  226. params: '[]',
  227. child_links: []
  228. });
  229. var MapReduceModel = ModelModule($);
  230. $.extend(MapReduceModel.prototype, {
  231. id: 0,
  232. name: '',
  233. description: '',
  234. node_type: 'mapreduce',
  235. workflow: 0,
  236. files: '[]',
  237. archives: '[]',
  238. job_properties: '[]',
  239. jar_path: '',
  240. prepares: '[]',
  241. job_xml: '',
  242. child_links: []
  243. });
  244. var StreamingModel = ModelModule($);
  245. $.extend(StreamingModel.prototype, {
  246. id: 0,
  247. name: '',
  248. description: '',
  249. node_type: 'streaming',
  250. workflow: 0,
  251. files: '[]',
  252. archives: '[]',
  253. job_properties: '[]',
  254. mapper: '',
  255. reducer: '',
  256. child_links: []
  257. });
  258. var JavaModel = ModelModule($);
  259. $.extend(JavaModel.prototype, {
  260. id: 0,
  261. name: '',
  262. description: '',
  263. node_type: 'java',
  264. workflow: 0,
  265. files: '[]',
  266. archives: '[]',
  267. job_properties: '[]',
  268. jar_path: '',
  269. prepares: '[]',
  270. job_xml: '',
  271. main_class: '',
  272. args: '',
  273. java_opts: '',
  274. child_links: []
  275. });
  276. var PigModel = ModelModule($);
  277. $.extend(PigModel.prototype, {
  278. id: 0,
  279. name: '',
  280. description: '',
  281. node_type: 'pig',
  282. workflow: 0,
  283. files: '[]',
  284. archives: '[]',
  285. job_properties: '[]',
  286. prepares: '[]',
  287. job_xml: '',
  288. params: '[]',
  289. script_path: '',
  290. child_links: []
  291. });
  292. var HiveModel = ModelModule($);
  293. $.extend(HiveModel.prototype, {
  294. id: 0,
  295. name: '',
  296. description: '',
  297. node_type: 'hive',
  298. workflow: 0,
  299. files: '[]',
  300. archives: '[]',
  301. job_properties: '[]',
  302. prepares: '[]',
  303. job_xml: '',
  304. params: '[]',
  305. script_path: '',
  306. child_links: []
  307. });
  308. var SqoopModel = ModelModule($);
  309. $.extend(SqoopModel.prototype, {
  310. id: 0,
  311. name: '',
  312. description: '',
  313. node_type: 'sqoop',
  314. workflow: 0,
  315. files: '[]',
  316. archives: '[]',
  317. job_properties: '[]',
  318. prepares: '[]',
  319. job_xml: '',
  320. params: '[]',
  321. script_path: '',
  322. child_links: []
  323. });
  324. var ShellModel = ModelModule($);
  325. $.extend(ShellModel.prototype, {
  326. id: 0,
  327. name: '',
  328. description: '',
  329. node_type: 'shell',
  330. workflow: 0,
  331. files: '[]',
  332. archives: '[]',
  333. job_properties: '[]',
  334. prepares: '[]',
  335. job_xml: '',
  336. params: '[]',
  337. command: '',
  338. capture_output: false,
  339. child_links: []
  340. });
  341. var SshModel = ModelModule($);
  342. $.extend(SshModel.prototype, {
  343. id: 0,
  344. name: '',
  345. description: '',
  346. node_type: 'ssh',
  347. workflow: 0,
  348. user: '',
  349. host: '',
  350. params: '[]',
  351. command: '',
  352. capture_output: false,
  353. child_links: []
  354. });
  355. var FsModel = ModelModule($);
  356. $.extend(FsModel.prototype, {
  357. id: 0,
  358. name: '',
  359. description: '',
  360. node_type: 'fs',
  361. workflow: 0,
  362. deletes: '[]',
  363. mkdirs: '[]',
  364. moves: '[]',
  365. chmods: '[]',
  366. touchzs: '[]',
  367. child_links: []
  368. });
  369. var EmailModel = ModelModule($);
  370. $.extend(EmailModel.prototype, {
  371. id: 0,
  372. name: '',
  373. description: '',
  374. node_type: 'email',
  375. workflow: 0,
  376. to: '',
  377. cc: '',
  378. subject: '',
  379. body: '',
  380. child_links: []
  381. });
  382. function nodeModelChooser(node_type) {
  383. switch(node_type) {
  384. case 'mapreduce':
  385. return MapReduceModel;
  386. case 'streaming':
  387. return StreamingModel;
  388. case 'java':
  389. return JavaModel;
  390. case 'pig':
  391. return PigModel;
  392. case 'hive':
  393. return HiveModel;
  394. case 'sqoop':
  395. return SqoopModel;
  396. case 'shell':
  397. return ShellModel;
  398. case 'ssh':
  399. return SshModel;
  400. case 'distcp':
  401. return DistCPModel;
  402. case 'fs':
  403. return FsModel;
  404. case 'email':
  405. return EmailModel;
  406. case 'fork':
  407. return ForkModel;
  408. case 'decision':
  409. return DecisionModel;
  410. default:
  411. return NodeModel;
  412. }
  413. }
  414. var IdGeneratorTable = {
  415. mapreduce: new IdGenerator({prefix: 'mapreduce'}),
  416. streaming: new IdGenerator({prefix: 'streaming'}),
  417. java: new IdGenerator({prefix: 'java'}),
  418. pig: new IdGenerator({prefix: 'pig'}),
  419. hive: new IdGenerator({prefix: 'hive'}),
  420. sqoop: new IdGenerator({prefix: 'sqoop'}),
  421. shell: new IdGenerator({prefix: 'shell'}),
  422. ssh: new IdGenerator({prefix: 'ssh'}),
  423. distcp: new IdGenerator({prefix: 'distcp'}),
  424. fs: new IdGenerator({prefix: 'fs'}),
  425. email: new IdGenerator({prefix: 'email'}),
  426. fork: new IdGenerator({prefix: 'fork'}),
  427. decision: new IdGenerator({prefix: 'decision'}),
  428. join: new IdGenerator({prefix: 'join'}),
  429. kill: new IdGenerator({prefix: 'kill'})
  430. };
  431. var NodeModule = function($, IdGeneratorTable) {
  432. var META_LINKS = ['related', 'default', 'error'];
  433. var linkTypeChooser = function(parent, child) {
  434. if (child.node_type() == 'kill') {
  435. return 'error';
  436. }
  437. switch(parent.node_type()) {
  438. case 'start':
  439. return (child.node_type() == 'end') ? 'related' : 'to';
  440. case 'fork':
  441. return (child.node_type() == 'join') ? 'related' : 'start';
  442. case 'decision':
  443. return 'start';
  444. case 'join':
  445. return 'to';
  446. default:
  447. return 'ok';
  448. };
  449. };
  450. var module = function(workflow, model, registry) {
  451. var self = this;
  452. self.map(model);
  453. self.links = ko.computed(function() {
  454. var links = self.child_links().filter(function(element, index, arr) {
  455. return $.inArray(element.name(), META_LINKS) == -1;
  456. });
  457. return links;
  458. });
  459. self.meta_links = ko.computed(function() {
  460. var links = self.child_links().filter(function(element, index, arr) {
  461. return $.inArray(element.name(), META_LINKS) != -1;
  462. });
  463. return links;
  464. });
  465. self._workflow = workflow;
  466. self.registry = registry;
  467. self.children = ko.observableArray([]);
  468. self.model = model;
  469. var errors = {};
  470. for(var key in model) {
  471. switch(key) {
  472. case 'child_links':
  473. case 'node_ptr':
  474. case 'initialize':
  475. case 'toString':
  476. break;
  477. default:
  478. errors[key] = [];
  479. break;
  480. }
  481. }
  482. self.errors = ko.mapping.fromJS(errors);
  483. self.edit_template = model.node_type + 'EditTemplate';
  484. switch(model.node_type) {
  485. case 'start':
  486. self.view_template = ko.observable('startTemplate');
  487. break;
  488. case 'kill':
  489. case 'end':
  490. self.view_template = ko.observable('emptyTemplate');
  491. break;
  492. case 'fork':
  493. self.view_template = ko.observable('forkTemplate');
  494. break;
  495. case 'decision':
  496. self.view_template = ko.observable('decisionTemplate');
  497. break;
  498. case 'join':
  499. self.view_template = ko.observable('joinTemplate');
  500. break;
  501. default:
  502. self.view_template = ko.observable('nodeTemplate');
  503. break;
  504. }
  505. // Data manipulation
  506. if ('job_properties' in model) {
  507. self.addProp = function() {
  508. var prop = { name: ko.observable(""), value: ko.observable("") };
  509. prop.name.subscribe(function(value) {
  510. self.job_properties.valueHasMutated();
  511. });
  512. prop.value.subscribe(function(value) {
  513. self.job_properties.valueHasMutated();
  514. });
  515. self.job_properties.push(prop);
  516. // $(".propKey:last").each(addAutoComplete);
  517. };
  518. self.removeProp = function(val) {
  519. self.job_properties.remove(val);
  520. };
  521. }
  522. if ('files' in model) {
  523. self.addFile = function() {
  524. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  525. prop.name.subscribe(function(value) {
  526. self.files.valueHasMutated();
  527. });
  528. prop.dummy.subscribe(function(value) {
  529. self.files.valueHasMutated();
  530. });
  531. self.files.push(prop);
  532. };
  533. self.removeFile = function(val) {
  534. self.files.remove(val);
  535. };
  536. }
  537. if ('archives' in model) {
  538. self.addArchive = function() {
  539. var prop = { name: ko.observable(""), dummy: ko.observable("") };
  540. prop.name.subscribe(function(value) {
  541. self.archives.valueHasMutated();
  542. });
  543. prop.dummy.subscribe(function(value) {
  544. self.archives.valueHasMutated();
  545. });
  546. self.archives.push(prop);
  547. };
  548. self.removeArchive = function(val) {
  549. self.archives.remove(val);
  550. };
  551. }
  552. if ('params' in model) {
  553. self.addParam = function() {
  554. var prop = { value: ko.observable(""), type: ko.observable("param") };
  555. prop.value.subscribe(function(value) {
  556. self.params.valueHasMutated();
  557. });
  558. prop.type.subscribe(function(value) {
  559. self.params.valueHasMutated();
  560. });
  561. self.params.push(prop);
  562. };
  563. self.addArgument = function() {
  564. var prop = { value: ko.observable(""), type: ko.observable("argument") };
  565. prop.value.subscribe(function(value) {
  566. self.params.valueHasMutated();
  567. });
  568. prop.type.subscribe(function(value) {
  569. self.params.valueHasMutated();
  570. });
  571. self.params.push(prop);
  572. };
  573. self.addArg = function() {
  574. var prop = { value: ko.observable(""), type: ko.observable("arg") };
  575. prop.value.subscribe(function(value) {
  576. self.params.valueHasMutated();
  577. });
  578. prop.type.subscribe(function(value) {
  579. self.params.valueHasMutated();
  580. });
  581. self.params.push(prop);
  582. };
  583. self.addEnvVar = function() {
  584. var prop = { value: ko.observable(""), type: ko.observable("env-var") };
  585. prop.value.subscribe(function(value) {
  586. self.params.valueHasMutated();
  587. });
  588. prop.type.subscribe(function(value) {
  589. self.params.valueHasMutated();
  590. });
  591. self.params.push(prop);
  592. };
  593. self.removeParam = function(val) {
  594. self.params.remove(val);
  595. };
  596. }
  597. if ('prepares' in model) {
  598. self.addPrepareDelete = function() {
  599. var prop = { value: ko.observable(""), type: ko.observable("delete") };
  600. prop.value.subscribe(function(value) {
  601. self.prepares.valueHasMutated();
  602. });
  603. prop.type.subscribe(function(value) {
  604. self.prepares.valueHasMutated();
  605. });
  606. self.prepares.push(prop);
  607. };
  608. self.addPrepareMkdir = function() {
  609. var prop = { value: ko.observable(""), type: ko.observable("mkdir") };
  610. prop.value.subscribe(function(value) {
  611. self.prepares.valueHasMutated();
  612. });
  613. prop.type.subscribe(function(value) {
  614. self.prepares.valueHasMutated();
  615. });
  616. self.prepares.push(prop);
  617. };
  618. self.removePrepare = function(val) {
  619. self.prepares.remove(val);
  620. };
  621. }
  622. if ('deletes' in model) {
  623. self.addDelete = function() {
  624. var prop = { name: ko.observable("") };
  625. prop.name.subscribe(function(value) {
  626. self.deletes.valueHasMutated();
  627. });
  628. self.deletes.push(prop);
  629. };
  630. self.removeDelete = function(val) {
  631. self.deletes.remove(val);
  632. };
  633. }
  634. if ('mkdirs' in model) {
  635. self.addMkdir = function() {
  636. var prop = { name: ko.observable("") };
  637. prop.name.subscribe(function(value) {
  638. self.mkdirs.valueHasMutated();
  639. });
  640. self.mkdirs.push(prop);
  641. };
  642. self.removeMkdir = function(val) {
  643. self.mkdirs.remove(val);
  644. };
  645. }
  646. if ('moves' in model) {
  647. self.addMove = function() {
  648. var prop = { source: ko.observable(""), destination: ko.observable("") };
  649. prop.source.subscribe(function(value) {
  650. self.moves.valueHasMutated();
  651. });
  652. prop.destination.subscribe(function(value) {
  653. self.moves.valueHasMutated();
  654. });
  655. self.moves.push(prop);
  656. };
  657. self.removeMove = function(val) {
  658. self.moves.remove(val);
  659. };
  660. }
  661. if ('chmods' in model) {
  662. self.addChmod = function() {
  663. var prop = { path: ko.observable(""), permissions: ko.observable(""), recursive: ko.observable("") };
  664. prop.path.subscribe(function(value) {
  665. self.chmods.valueHasMutated();
  666. });
  667. prop.permissions.subscribe(function(value) {
  668. self.chmods.valueHasMutated();
  669. });
  670. prop.recursive.subscribe(function(value) {
  671. self.chmods.valueHasMutated();
  672. });
  673. self.chmods.push(prop);
  674. };
  675. self.removeChmod = function(val) {
  676. self.chmods.remove(val);
  677. };
  678. }
  679. if ('touchzs' in model) {
  680. self.addTouchz = function() {
  681. var prop = { name: ko.observable("") };
  682. prop.name.subscribe(function(value) {
  683. self.touchzs.valueHasMutated();
  684. });
  685. self.touchzs.push(prop);
  686. };
  687. self.removeTouchz = function(val) {
  688. self.touchzs.remove(val);
  689. };
  690. }
  691. self.initialize.apply(self, arguments);
  692. return self;
  693. };
  694. $.extend(module.prototype, {
  695. // Data.
  696. children: null,
  697. model: null,
  698. // Normal stuff
  699. /**
  700. * Called when creating a new node
  701. */
  702. initialize: function(workflow, model, registry) {},
  703. toString: function() {
  704. return '';
  705. },
  706. /**
  707. * Fetches registry
  708. */
  709. getRegistry: function() {
  710. return registry;
  711. },
  712. /**
  713. * Maps a model to self
  714. * Called when creating a new node before any thing else
  715. */
  716. map: function(model) {
  717. var self = this;
  718. var map_params = function(options, subscribe) {
  719. options.data = ($.type(options.data) == "string") ? $.parseJSON(options.data) : options.data;
  720. if ($.isArray(options.data)) {
  721. var mapping = ko.mapping.fromJS(options.data);
  722. $.each(mapping(), function(index, value) {
  723. subscribe(value);
  724. });
  725. return mapping;
  726. } else {
  727. var mapping = ko.mapping.fromJS(options.data, {});
  728. subscribe(mapping);
  729. return mapping;
  730. }
  731. };
  732. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  733. var mapping = ko.mapping.fromJS(model, {
  734. ignore: ['initialize', 'toString'],
  735. job_properties: {
  736. create: function(options) {
  737. var parent = options.parent;
  738. var subscribe = function(mapping) {
  739. mapping.name.subscribe(function(value) {
  740. parent.job_properties.valueHasMutated();
  741. });
  742. mapping.value.subscribe(function(value) {
  743. parent.job_properties.valueHasMutated();
  744. });
  745. };
  746. return map_params(options, subscribe);
  747. },
  748. update: function(options) {
  749. var parent = options.parent;
  750. var subscribe = function(mapping) {
  751. mapping.name.subscribe(function(value) {
  752. parent.job_properties.valueHasMutated();
  753. });
  754. mapping.value.subscribe(function(value) {
  755. parent.job_properties.valueHasMutated();
  756. });
  757. };
  758. return map_params(options, subscribe);
  759. }
  760. },
  761. files: {
  762. create: function(options) {
  763. return map_params(options, function() {});
  764. },
  765. update: function(options) {
  766. return map_params(options, function() {});
  767. }
  768. },
  769. archives: {
  770. create: function(options) {
  771. var parent = options.parent;
  772. var subscribe = function(mapping) {
  773. mapping.name.subscribe(function(value) {
  774. parent.archives.valueHasMutated();
  775. });
  776. mapping.dummy.subscribe(function(value) {
  777. parent.archives.valueHasMutated();
  778. });
  779. };
  780. return map_params(options, subscribe);
  781. },
  782. update: function(options) {
  783. var parent = options.parent;
  784. var subscribe = function(mapping) {
  785. mapping.name.subscribe(function(value) {
  786. parent.archives.valueHasMutated();
  787. });
  788. mapping.dummy.subscribe(function(value) {
  789. parent.archives.valueHasMutated();
  790. });
  791. };
  792. return map_params(options, subscribe);
  793. }
  794. },
  795. params: {
  796. create: function(options) {
  797. var parent = options.parent;
  798. var subscribe = function(mapping) {
  799. mapping.value.subscribe(function(value) {
  800. parent.params.valueHasMutated();
  801. });
  802. mapping.type.subscribe(function(value) {
  803. parent.params.valueHasMutated();
  804. });
  805. };
  806. return map_params(options, subscribe);
  807. },
  808. update: function(options) {
  809. var parent = options.parent;
  810. var subscribe = function(mapping) {
  811. mapping.value.subscribe(function(value) {
  812. parent.params.valueHasMutated();
  813. });
  814. mapping.type.subscribe(function(value) {
  815. parent.params.valueHasMutated();
  816. });
  817. };
  818. return map_params(options, subscribe);
  819. }
  820. },
  821. prepares: {
  822. create: function(options) {
  823. var parent = options.parent;
  824. var subscribe = function(mapping) {
  825. mapping.value.subscribe(function(value) {
  826. parent.prepares.valueHasMutated();
  827. });
  828. mapping.type.subscribe(function(value) {
  829. parent.prepares.valueHasMutated();
  830. });
  831. };
  832. return map_params(options, subscribe);
  833. },
  834. update: function(options) {
  835. var parent = options.parent;
  836. var subscribe = function(mapping) {
  837. mapping.value.subscribe(function(value) {
  838. parent.prepares.valueHasMutated();
  839. });
  840. mapping.type.subscribe(function(value) {
  841. parent.prepares.valueHasMutated();
  842. });
  843. };
  844. return map_params(options, subscribe);
  845. }
  846. },
  847. deletes: {
  848. create: function(options) {
  849. return map_params(options, function() {});
  850. },
  851. update: function(options) {
  852. return map_params(options, function() {});
  853. }
  854. },
  855. mkdirs: {
  856. create: function(options) {
  857. return map_params(options, function() {});
  858. },
  859. update: function(options) {
  860. return map_params(options, function() {});
  861. }
  862. },
  863. moves: {
  864. create: function(options) {
  865. var parent = options.parent;
  866. var subscribe = function(mapping) {
  867. mapping.source.subscribe(function(value) {
  868. parent.moves.valueHasMutated();
  869. });
  870. mapping.destination.subscribe(function(value) {
  871. parent.moves.valueHasMutated();
  872. });
  873. };
  874. return map_params(options, subscribe);
  875. },
  876. update: function(options) {
  877. var parent = options.parent;
  878. var subscribe = function(mapping) {
  879. mapping.source.subscribe(function(value) {
  880. parent.moves.valueHasMutated();
  881. });
  882. mapping.destination.subscribe(function(value) {
  883. parent.moves.valueHasMutated();
  884. });
  885. };
  886. return map_params(options, subscribe);
  887. }
  888. },
  889. chmods: {
  890. create: function(options) {
  891. var parent = options.parent;
  892. var subscribe = function(mapping) {
  893. mapping.path.subscribe(function(value) {
  894. parent.chmods.valueHasMutated();
  895. });
  896. mapping.permissions.subscribe(function(value) {
  897. parent.chmods.valueHasMutated();
  898. });
  899. mapping.recursive.subscribe(function(value) {
  900. parent.chmods.valueHasMutated();
  901. });
  902. };
  903. return map_params(options, subscribe);
  904. },
  905. update: function(options) {
  906. var parent = options.parent;
  907. var subscribe = function(mapping) {
  908. mapping.path.subscribe(function(value) {
  909. parent.chmods.valueHasMutated();
  910. });
  911. mapping.permissions.subscribe(function(value) {
  912. parent.chmods.valueHasMutated();
  913. });
  914. mapping.recursive.subscribe(function(value) {
  915. parent.chmods.valueHasMutated();
  916. });
  917. };
  918. return map_params(options, subscribe);
  919. }
  920. },
  921. touchzs: {
  922. create: function(options) {
  923. return map_params(options, function() {});
  924. },
  925. update: function(options) {
  926. return map_params(options, function() {});
  927. }
  928. },
  929. });
  930. $.extend(self, mapping);
  931. $.each(mapping, function(key, value) {
  932. var key = key;
  933. if (ko.isObservable(self[key])) {
  934. self[key].subscribe(function(value) {
  935. model[key] = ko.mapping.toJS(value);
  936. });
  937. }
  938. });
  939. $.each(self.child_links(), function(index, link) {
  940. var $index = index;
  941. link.comment.subscribe(function(value) {
  942. self.model.child_links[$index].comment = value;
  943. });
  944. link.child.subscribe(function(value) {
  945. self.model.child_links[$index].child = value;
  946. });
  947. });
  948. },
  949. validate: function( ) {
  950. var self = this;
  951. var options = {};
  952. data = $.extend(true, {}, self.model);
  953. var success = false;
  954. var request = $.extend({
  955. url: '/oozie/workflows/' + self._workflow.id() + '/actions/' + self.node_type() + '/validate',
  956. type: 'POST',
  957. data: { node: JSON.stringify(data) },
  958. success: function(data) {
  959. ko.mapping.fromJS(data.data, self.errors);
  960. success = data.status == 0;
  961. },
  962. async: false,
  963. }, options);
  964. $.ajax(request);
  965. return success;
  966. },
  967. // Hierarchy manipulation.
  968. /**
  969. * Append node to self
  970. * Does not support multiple children.
  971. * Ensures single child.
  972. * Ensures no cycles.
  973. * 1. Finds all children and attaches them to node (cleans node first).
  974. * 2. Remove all children from self.
  975. * 3. Attach node to self.
  976. */
  977. append: function(node) {
  978. var self = this;
  979. // Not fork nor decision nor self
  980. if ($.inArray(self.node_type(), ['fork', 'decision']) == -1 && node.id() != self.id()) {
  981. node.removeAllChildren();
  982. $.each(self.links(), function(index, link) {
  983. node.addChild(registry.get(link.child()));
  984. });
  985. self.removeAllChildren();
  986. self.addChild(node);
  987. }
  988. },
  989. /**
  990. * Find all parents of current node
  991. */
  992. findParents: function() {
  993. var self = this;
  994. var parents = [];
  995. $.each(registry.nodes, function(id, node) {
  996. $.each(node.links(), function(index, link) {
  997. if (link.child() == self.id()) {
  998. parents.push(node);
  999. }
  1000. });
  1001. });
  1002. return parents;
  1003. },
  1004. /**
  1005. * Find all children of current node
  1006. */
  1007. findChildren: function() {
  1008. var self = this;
  1009. var children = [];
  1010. $.each(self.links(), function(index, link) {
  1011. children.push(registry.get(link.child()));
  1012. });
  1013. return children;
  1014. },
  1015. /**
  1016. * Detach current node from the graph
  1017. * 1. Takes children of self node, removes them from self node, and adds them to each parent of self node.
  1018. * 2. The self node is then removed from every parent.
  1019. * 3. Does not support multiple children since we do not automatically fork.
  1020. */
  1021. detach: function() {
  1022. var self = this;
  1023. $.each(self.findParents(), function(index, parent) {
  1024. $.each(self.links(), function(index, link) {
  1025. var node = registry.get(link.child());
  1026. parent.replaceChild(self, node);
  1027. });
  1028. });
  1029. $(self).trigger('detached');
  1030. self.removeAllChildren();
  1031. },
  1032. /**
  1033. * Add child
  1034. * Update child links for this node.
  1035. */
  1036. addChild: function(node) {
  1037. var self = this;
  1038. var link = {
  1039. parent: ko.observable(self.id()),
  1040. child: ko.observable(node.id()),
  1041. name: ko.observable(linkTypeChooser(self, node)),
  1042. comment: ko.observable('')
  1043. };
  1044. self.child_links.unshift(link);
  1045. },
  1046. /**
  1047. * Remove child node
  1048. * 1. Find child node link
  1049. * 2. Remove child node link
  1050. */
  1051. removeChild: function(node) {
  1052. var self = this;
  1053. var spliceIndex = -1;
  1054. $.each(self.child_links(), function(index, link) {
  1055. if (link.child() == node.id()) {
  1056. spliceIndex = index;
  1057. }
  1058. });
  1059. if (spliceIndex > -1) {
  1060. self.child_links.splice(spliceIndex, 1);
  1061. }
  1062. return spliceIndex != -1;
  1063. },
  1064. /**
  1065. * Remove all children
  1066. * Removes all children except for related, default, and error links
  1067. * Note: we hold on to related, default, and error links because
  1068. * we have to.
  1069. */
  1070. removeAllChildren: function() {
  1071. var self = this;
  1072. var keep_links = [];
  1073. $.each(self.child_links(), function(index, link) {
  1074. if ($.inArray(link.name(), META_LINKS) > -1) {
  1075. keep_links.push(link);
  1076. }
  1077. });
  1078. self.child_links.removeAll();
  1079. $.each(keep_links, function(index, link) {
  1080. self.child_links.push(link);
  1081. });
  1082. },
  1083. /**
  1084. * Replace child node with another node in the following way:
  1085. * 1. Find child index
  1086. * 2. Remove child index
  1087. * 3. Remove and remember every element after child
  1088. * 4. Add replacement node
  1089. * 5. Add every child that was remembered
  1090. */
  1091. replaceChild: function(child, replacement) {
  1092. var self = this;
  1093. var index = -1;
  1094. $.each(self.child_links(), function(i, link) {
  1095. if (link.child() == child.id()) {
  1096. index = i;
  1097. }
  1098. });
  1099. if (index > -1) {
  1100. self.child_links.splice(index, 1);
  1101. var links = self.child_links.splice(index);
  1102. var link = {
  1103. parent: ko.observable(self.id()),
  1104. child: ko.observable(replacement.id()),
  1105. name: ko.observable(linkTypeChooser(self, replacement)),
  1106. comment: ko.observable(''),
  1107. };
  1108. self.child_links.push(link);
  1109. $.each(links, function(index, link) {
  1110. self.child_links.push(link);
  1111. });
  1112. }
  1113. return index != -1;
  1114. },
  1115. isChild: function(node) {
  1116. var self = this;
  1117. var res = false;
  1118. $.each(self.links(), function(index, link) {
  1119. if (link.child() == node.id()) {
  1120. res = true;
  1121. }
  1122. });
  1123. return res;
  1124. },
  1125. erase: function() {
  1126. var self = this;
  1127. self.registry.remove(self.id());
  1128. }
  1129. });
  1130. return module;
  1131. };
  1132. var Node = NodeModule($, IdGeneratorTable);
  1133. var StartNode = NodeModule($, IdGeneratorTable);
  1134. $.extend(StartNode.prototype, Node.prototype, {
  1135. /**
  1136. * Same as addChild for nodes, except if we reach the end,
  1137. * we add the end node to our child_links in the form of a
  1138. * 'related' link and 'start' link.
  1139. * We should always have a start link in an empty workflow!
  1140. */
  1141. replaceChild: function(child, replacement) {
  1142. var self = this;
  1143. var index = -1;
  1144. $.each(self.child_links(), function(i, link) {
  1145. if (link.child() == child.id()) {
  1146. index = i;
  1147. }
  1148. });
  1149. if (index > -1) {
  1150. self.child_links.splice(index, 1);
  1151. var links = self.child_links.splice(index);
  1152. var link = {
  1153. parent: ko.observable(self.id()),
  1154. child: ko.observable(replacement.id()),
  1155. name: ko.observable('to'),
  1156. comment: ko.observable(''),
  1157. };
  1158. self.child_links.push(link);
  1159. $.each(links, function(index, link) {
  1160. self.child_links.push(link);
  1161. });
  1162. }
  1163. return index != -1;
  1164. },
  1165. });
  1166. var ForkNode = NodeModule($, IdGeneratorTable);
  1167. $.extend(ForkNode.prototype, Node.prototype, {
  1168. // Join nodes are connected through 'related' links
  1169. join: function() {
  1170. var self = this;
  1171. var join = null;
  1172. $.each(self.child_links(), function(index, link) {
  1173. if (link.name() == 'related') {
  1174. join = self.registry.get(link.child());
  1175. }
  1176. });
  1177. return join;
  1178. },
  1179. /**
  1180. * Append a node to the current fork
  1181. * Also adds join node to node.
  1182. * When adding the join node, append will remove all the children from the join!
  1183. * We need to make sure the join remembers its children.
  1184. * NOTE: Cannot append a fork! Use addChild or replaceChild instead!
  1185. */
  1186. append: function(node) {
  1187. var self = this;
  1188. if (node.node_type() != 'fork') {
  1189. var join = self.join();
  1190. if (join.id() != node.id()) {
  1191. var children = join.findChildren();
  1192. self.addChild(node);
  1193. node.append(join);
  1194. // remember children
  1195. $.each(children, function(index, child) {
  1196. join.addChild(child);
  1197. });
  1198. }
  1199. }
  1200. },
  1201. /**
  1202. * Replace child node with another node
  1203. * 1. Remove child if the replacement is related join
  1204. * 2. Apply Node.replaceChild for all other causes
  1205. * NOTE: can assume the only join this fork will ever see is the related join!
  1206. * This is because the related will always be the closest join for children.
  1207. * Also, the operations allowed that would make this all possible: detach, append;
  1208. * are inherently going to remove any other joins if they exist.
  1209. * This is also easier given nodes are contained within Forks!
  1210. */
  1211. replaceChild: function(child, replacement) {
  1212. var self = this;
  1213. var ret = true;
  1214. if (self.join().id() == replacement.id()) {
  1215. ret = self.removeChild(child);
  1216. } else {
  1217. ret = Node.prototype.replaceChild.apply(self, arguments);
  1218. }
  1219. var links = self.links().filter(function(element, index, arr) {
  1220. return self.registry.get(element.child()).node_type() != 'join';
  1221. });
  1222. if (links.length < 2) {
  1223. self.detach();
  1224. self.join().detach();
  1225. self.erase();
  1226. self.join().erase();
  1227. }
  1228. return ret;
  1229. },
  1230. /**
  1231. * Converts fork node into decision node in the following way:
  1232. * 1. Copies contents of current fork node into a new decision node
  1233. * 2. Detach fork node
  1234. * 3. Erase fork node
  1235. * 4. Append decision node to parent
  1236. */
  1237. convertToDecision: function() {
  1238. var self = this;
  1239. var join = self.join();
  1240. var end = null;
  1241. var child = join.findChildren()[0];
  1242. if (child.findParents().length == 1) {
  1243. end = child;
  1244. }
  1245. // Attaches child of join to parents of join
  1246. join.detach();
  1247. var decision_model = new DecisionModel({
  1248. id: IdGeneratorTable['decision'].nextId(),
  1249. name: self.name(),
  1250. description: self.description(),
  1251. node_type: 'decision',
  1252. workflow: self.workflow(),
  1253. child_links: self.model.child_links
  1254. });
  1255. var default_link = {
  1256. parent: decision_model.id,
  1257. child: self._workflow.end(),
  1258. name: 'default',
  1259. comment: ''
  1260. };
  1261. decision_model.child_links.push(default_link);
  1262. $.each(decision_model.child_links, function(index, link) {
  1263. link.parent = decision_model.id;
  1264. });
  1265. var decision_node = new DecisionNode(self._workflow, decision_model, self.registry);
  1266. if (end) {
  1267. decision_node.addEnd(end);
  1268. }
  1269. var parents = self.findParents();
  1270. decision_node.removeChild(join);
  1271. join.erase();
  1272. self.erase();
  1273. self.registry.add(decision_node.id(), decision_node);
  1274. $.each(parents, function(index, parent) {
  1275. parent.replaceChild(self, decision_node);
  1276. });
  1277. }
  1278. });
  1279. var DecisionNode = NodeModule($, IdGeneratorTable);
  1280. $.extend(DecisionNode.prototype, ForkNode.prototype, {
  1281. initialize: function(workflow, model, registry) {
  1282. var self = this;
  1283. var registry = registry;
  1284. var end = null;
  1285. $.each(self.child_links(), function(index, link) {
  1286. if (link.name() == 'related') {
  1287. $(registry).bind('registry:add', function(e) {
  1288. var end = self.end();
  1289. if (end) {
  1290. self.removeEnd();
  1291. self.addEnd(end);
  1292. $(registry).unbind(e);
  1293. }
  1294. });
  1295. }
  1296. });
  1297. },
  1298. // Only a single end allowed for now!
  1299. // Finds end for branch
  1300. findEnd: function( ) {
  1301. var self = this;
  1302. var end = self.end();
  1303. if (!end) {
  1304. return self._findEnd( self, 0 );
  1305. }
  1306. return end;
  1307. },
  1308. _findEnd: function( node, count ) {
  1309. var self = this;
  1310. var end = null;
  1311. if (node.findParents().length > 1 && --count == 0) {
  1312. return node;
  1313. }
  1314. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1315. count++;
  1316. }
  1317. $.each(node.findChildren(), function(index, node) {
  1318. if (end == null) {
  1319. end = self._findEnd(node, count);
  1320. }
  1321. });
  1322. return end;
  1323. },
  1324. end: function() {
  1325. var self = this;
  1326. var end = null;
  1327. $.each(self.child_links(), function(index, link) {
  1328. if (link.name() == 'related') {
  1329. end = self.registry.get(link.child());
  1330. }
  1331. });
  1332. return end;
  1333. },
  1334. addEnd: function(node) {
  1335. var self = this;
  1336. self.child_links.push({
  1337. parent: ko.observable(self.id()),
  1338. child: ko.observable(node.id()),
  1339. name: ko.observable('related'),
  1340. comment: ko.observable(''),
  1341. });
  1342. $(node).one('detached', function(e) {
  1343. var end = self.end();
  1344. if (end.links().length == 1) {
  1345. self.removeEnd();
  1346. var new_end = self.findEnd();
  1347. self.addEnd(new_end);
  1348. } else {
  1349. // Should never hit this case.
  1350. self.removeEnd();
  1351. }
  1352. });
  1353. },
  1354. removeEnd: function() {
  1355. var self = this;
  1356. var spliceIndex = -1;
  1357. $.each(self.child_links(), function(index, link) {
  1358. if (link.name() == 'related') {
  1359. spliceIndex = index;
  1360. }
  1361. });
  1362. if (spliceIndex > -1) {
  1363. var end = registry.get(self.child_links()[spliceIndex].child());
  1364. self.child_links.splice(spliceIndex, 1);
  1365. return true;
  1366. }
  1367. return false;
  1368. },
  1369. isChild: function(node) {
  1370. var self = this;
  1371. return self._isChild(node, self, 0);
  1372. },
  1373. _isChild: function( test_child, node, count ) {
  1374. var self = this;
  1375. var result = node.id() == test_child.id();
  1376. if (node.findParents().length > 1 && --count == 0) {
  1377. return result;
  1378. }
  1379. if (node.node_type() == 'decision' || node.node_type() == 'fork') {
  1380. count++;
  1381. }
  1382. $.each(node.findChildren(), function(index, node) {
  1383. if (!result) {
  1384. result = self._isChild(test_child, node, count);
  1385. }
  1386. });
  1387. return result;
  1388. },
  1389. /**
  1390. * Append a node to the current decision
  1391. * Also appends end node to node.
  1392. * NOTE: Cannot append a decision! Use addChild or replaceChild instead!
  1393. */
  1394. append: function(node) {
  1395. var self = this;
  1396. var end = self.findEnd();
  1397. if (end.id() == node.id()) {
  1398. return false;
  1399. }
  1400. self.addChild(node);
  1401. node.append(end);
  1402. return true;
  1403. },
  1404. /**
  1405. * Replace child node with another node
  1406. * 1. Remove child if the replacement is end
  1407. * 2. Apply Node.replaceChild for all other causes
  1408. */
  1409. replaceChild: function(child, replacement) {
  1410. var self = this;
  1411. var ret = true;
  1412. var end = self.findEnd();
  1413. if (end && end.id() == replacement.id()) {
  1414. ret = self.removeChild(child);
  1415. } else {
  1416. ret = Node.prototype.replaceChild.apply(self, arguments);
  1417. }
  1418. if (self.links().length < 2) {
  1419. self.detach();
  1420. self.erase();
  1421. }
  1422. return ret;
  1423. }
  1424. });
  1425. /**
  1426. * Workflow module
  1427. */
  1428. var WorkflowModule = function($, NodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable) {
  1429. var module = function(options) {
  1430. var self = this;
  1431. // @see http://knockoutjs.com/documentation/plugins-mapping.html
  1432. var mapping = ko.mapping.fromJS(options.model, {
  1433. ignore: ['initialize', 'toString'],
  1434. job_properties: {
  1435. create: function(options) {
  1436. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1437. var parent = options.parent;
  1438. mapping.name.subscribe(function(value) {
  1439. parent.job_properties.valueHasMutated();
  1440. });
  1441. mapping.value.subscribe(function(value) {
  1442. parent.job_properties.valueHasMutated();
  1443. });
  1444. return mapping;
  1445. },
  1446. update: function(options) {
  1447. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1448. var parent = options.parent;
  1449. mapping.name.subscribe(function(value) {
  1450. parent.job_properties.valueHasMutated();
  1451. });
  1452. mapping.value.subscribe(function(value) {
  1453. parent.job_properties.valueHasMutated();
  1454. });
  1455. return mapping;
  1456. }
  1457. },
  1458. parameters: {
  1459. // Will receive individual objects to subscribe.
  1460. // Containing array is mapped automagically
  1461. create: function(options) {
  1462. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1463. var parent = options.parent;
  1464. mapping.name.subscribe(function(value) {
  1465. parent.parameters.valueHasMutated();
  1466. });
  1467. mapping.value.subscribe(function(value) {
  1468. parent.parameters.valueHasMutated();
  1469. });
  1470. return mapping;
  1471. },
  1472. update: function(options) {
  1473. var mapping = ko.mapping.fromJS($.parseJSON(options.data) || options.data);
  1474. var parent = options.parent;
  1475. mapping.name.subscribe(function(value) {
  1476. parent.parameters.valueHasMutated();
  1477. });
  1478. mapping.value.subscribe(function(value) {
  1479. parent.parameters.valueHasMutated();
  1480. });
  1481. return mapping;
  1482. },
  1483. }
  1484. });
  1485. $.extend(self, mapping);
  1486. $.each(mapping['__ko_mapping__'].mappedProperties, function(key, value) {
  1487. var key = key;
  1488. self[key].subscribe(function(value) {
  1489. self.is_dirty( true );
  1490. self.model[key] = ko.mapping.toJS(value);
  1491. });
  1492. });
  1493. self.model = options.model;
  1494. self.registry = options.registry;
  1495. self.options = options;
  1496. self.el = $('#workflow');
  1497. self.nodes = ko.observableArray([]);
  1498. self.kill = null;
  1499. self.is_dirty = ko.observable( false );
  1500. self.url = ko.computed(function() {
  1501. return '/oozie/workflows/' + self.id()
  1502. });
  1503. // Events
  1504. self.el.on('workflow:rebuild', function() {
  1505. self.rebuild();
  1506. });
  1507. self.el.on('workflow:events:load', function() {
  1508. self.dragAndDropEvents();
  1509. });
  1510. self.el.on('workflow:droppables:load', function() {
  1511. self.droppables();
  1512. });
  1513. self.el.on('workflow:draggables:load', function() {
  1514. self.draggables();
  1515. });
  1516. self.dragAndDropEvents();
  1517. self.el.trigger('workflow:events:loaded');
  1518. module.prototype.initialize.apply(self, arguments);
  1519. return self;
  1520. };
  1521. $.extend(module.prototype, {
  1522. // Normal stuff
  1523. initialize: function(options) {
  1524. var self = this;
  1525. if ('data' in options) {
  1526. // Initialize nodes
  1527. if (options.data.nodes) {
  1528. self.registry.clear();
  1529. $.each(options.data.nodes, function(index, node) {
  1530. var NodeModel = NodeModelChooser(node.node_type);
  1531. var model = new NodeModel(node);
  1532. var temp = null;
  1533. switch(node.node_type) {
  1534. case 'start':
  1535. temp = new StartNode(self, model, self.registry);
  1536. break;
  1537. case 'fork':
  1538. temp = new ForkNode(self, model, self.registry);
  1539. break;
  1540. case 'decision':
  1541. temp = new DecisionNode(self, model, self.registry);
  1542. break;
  1543. case 'kill':
  1544. temp = self.kill = new Node(self, model, self.registry);
  1545. break;
  1546. default:
  1547. temp = new Node(self, model, self.registry);
  1548. break;
  1549. }
  1550. self.registry.add(temp.id(), temp);
  1551. });
  1552. }
  1553. // Update data
  1554. $.each(options.data, function (key, value) {
  1555. if (key in self) {
  1556. switch(key) {
  1557. case 'job_properties':
  1558. case 'parameters':
  1559. // These may be serialized JSON data since that is how they are stored
  1560. self[key].removeAll();
  1561. var arr = $.parseJSON(value) || value;
  1562. $.each(arr, function(index, obj) {
  1563. var mapping = ko.mapping.fromJS(obj);
  1564. mapping.name.subscribe(function(value) {
  1565. self[key].valueHasMutated();
  1566. });
  1567. mapping.value.subscribe(function(value) {
  1568. self[key].valueHasMutated();
  1569. });
  1570. self[key].push(mapping);
  1571. });
  1572. break;
  1573. case 'nodes':
  1574. break;
  1575. default:
  1576. self[key](value);
  1577. break;
  1578. }
  1579. }
  1580. });
  1581. workflow.is_dirty( false );
  1582. }
  1583. if (!self.kill) {
  1584. var kill_json = {
  1585. "description": "",
  1586. "workflow": self.id(),
  1587. "child_links": [],
  1588. "node_type": "kill",
  1589. "message": "Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]",
  1590. "name": "kill",
  1591. "id": IdGeneratorTable['kill'].nextId()
  1592. };
  1593. var NodeModel = NodeModelChooser(kill_json.node_type);
  1594. var model = new NodeModel(kill_json);
  1595. self.kill = new Node(self, model, self.registry);
  1596. self.registry.add(self.kill.id(), self.kill);
  1597. }
  1598. },
  1599. toString: function() {
  1600. return '';
  1601. var s = '[';
  1602. $.each(self.registry.nodes, function(key, node) {
  1603. s += node.model.toString() + ",\n";
  1604. });
  1605. return s + ']';
  1606. },
  1607. // Data manipulation
  1608. cloneNode: function(node, event) {
  1609. var self = this;
  1610. var model_copy = $.extend(true, {}, node.model);
  1611. var template = model_copy.node_type + 'EditTemplate';
  1612. var NodeModel = NodeModelChooser(node.node_type());
  1613. model_copy.id = IdGeneratorTable[model_copy.node_type].nextId();
  1614. model_copy.name += '-copy';
  1615. model_copy.child_links = [];
  1616. var model = new NodeModel(model_copy);
  1617. var new_node = new Node(self, model, self.registry);
  1618. self.registry.add(new_node.id(), new_node);
  1619. new_node.addChild(self.kill);
  1620. node.append(new_node);
  1621. self.rebuild();
  1622. modal.setTemplate(template);
  1623. modal.show(new_node);
  1624. modal.recenter(280, 250);
  1625. modal.addDecorations();
  1626. // $(".propKey").each(addAutoComplete);
  1627. workflow.is_dirty( true );
  1628. },
  1629. removeNode: function(node, event) {
  1630. var self = this;
  1631. node.detach();
  1632. node.erase();
  1633. self.rebuild();
  1634. workflow.is_dirty( true );
  1635. },
  1636. save: function( options ) {},
  1637. load: function() {
  1638. var self = this;
  1639. $.getJSON(self.url(), function(data) {
  1640. if (data.status == 0) {
  1641. self.reload(data.data);
  1642. } else {
  1643. $.jHueNotify.error("${ _('Received invalid response from server: ') }" + JSON.stringify(data));
  1644. }
  1645. });
  1646. },
  1647. reload: function(data) {
  1648. var self = this;
  1649. // Clear all children
  1650. $.each(self.registry.nodes, function(index, node) {
  1651. node.children.removeAll();
  1652. });
  1653. self.nodes.removeAll();
  1654. self.initialize({data: data});
  1655. self.rebuild();
  1656. self.el.trigger('workflow:loaded');
  1657. },
  1658. addParameter: function(data, event) {
  1659. var self = this;
  1660. var prop = { name: ko.observable(""), value: ko.observable("") };
  1661. // force bubble up to containing observable array.
  1662. prop.name.subscribe(function(){
  1663. self.parameters.valueHasMutated();
  1664. });
  1665. prop.value.subscribe(function(){
  1666. self.parameters.valueHasMutated();
  1667. });
  1668. self.parameters.push(prop);
  1669. },
  1670. removeParameter: function(data, event) {
  1671. var self = this;
  1672. self.parameters.remove(data);
  1673. },
  1674. addJobProperty: function(data, event) {
  1675. var self = this;
  1676. var prop = { name: ko.observable(""), value: ko.observable("") };
  1677. // force bubble up to containing observable array.
  1678. prop.name.subscribe(function(){
  1679. self.parameters.valueHasMutated();
  1680. });
  1681. prop.value.subscribe(function(){
  1682. self.parameters.valueHasMutated();
  1683. });
  1684. self.job_properties.push(prop);
  1685. },
  1686. removeJobProperty: function(data, event) {
  1687. var self = this;
  1688. self.job_properties.remove(data);
  1689. },
  1690. // Workflow UI
  1691. // Function to build nodes... recursively.
  1692. build: function() {
  1693. var self = this;
  1694. var maximum = 50;
  1695. var count = 0;
  1696. var methodChooser = function(node, collection, skip_parents_check) {
  1697. if (count++ >= maximum) {
  1698. if (window.error) {
  1699. console.error('Hit maximum number of node recursion: ' + maximum);
  1700. }
  1701. return null;
  1702. }
  1703. if (!node) {
  1704. return node;
  1705. }
  1706. var parents = node.findParents();
  1707. // Found end of decision node or found join!
  1708. if (parents.length > 1 && !skip_parents_check) {
  1709. return node;
  1710. }
  1711. switch(node.node_type()) {
  1712. case 'start':
  1713. case 'end':
  1714. case 'kill':
  1715. case 'fork':
  1716. case 'decision':
  1717. case 'join':
  1718. return control(node, collection);
  1719. default:
  1720. return normal(node, collection);
  1721. }
  1722. };
  1723. var normal = function(node, collection) {
  1724. collection.push(node);
  1725. var retNode = null;
  1726. $.each(node.links(), function(index, link) {
  1727. var next_node = self.registry.get(link.child());
  1728. retNode = methodChooser(next_node, collection, false, true);
  1729. });
  1730. return retNode;
  1731. };
  1732. var control = function(node, collection, skip_parents_check) {
  1733. switch(node.node_type()) {
  1734. case 'start':
  1735. case 'end':
  1736. case 'kill':
  1737. case 'join':
  1738. return normal(node, collection, false, true);
  1739. case 'fork':
  1740. collection.push(node);
  1741. // Wait for join.
  1742. // Iterate through all children and add them to child collection.
  1743. var join = null;
  1744. $.each(node.links(), function(index, link) {
  1745. var next_node = self.registry.get(link.child());
  1746. var collection = ko.observableArray([]);
  1747. node.children.push(collection);
  1748. join = methodChooser(next_node, collection, false, true);
  1749. });
  1750. // Add join to collection, then find its single child.
  1751. return methodChooser(join, collection, true, true);
  1752. case 'decision':
  1753. collection.push(node);
  1754. // Waits for end, then runs through children of end node
  1755. var end = null;
  1756. $.each(node.links(), function(index, link) {
  1757. var next_node = self.registry.get(link.child());
  1758. var collection = ko.observableArray([]);
  1759. node.children.push(collection);
  1760. end = methodChooser(next_node, collection, true, true);
  1761. });
  1762. // Add end
  1763. if (node.end() && end.id() == node.end().id()) {
  1764. return methodChooser(end, collection, true, true);
  1765. } else {
  1766. return end;
  1767. }
  1768. default:
  1769. // Should never get here.
  1770. return node;
  1771. }
  1772. };
  1773. methodChooser(self.registry.get(self.start()), self.nodes, false, true);
  1774. },
  1775. rebuild: function() {
  1776. var self = this;
  1777. // Clear all children
  1778. $.each(self.registry.nodes, function(index, node) {
  1779. node.children.removeAll();
  1780. });
  1781. self.nodes.removeAll();
  1782. // Rebuild
  1783. self.build();
  1784. self.draggables();
  1785. self.droppables();
  1786. self.el.trigger('workflow:rebuilt');
  1787. },
  1788. draggables: function() {
  1789. var self = this;
  1790. self.el.find('.node-action').each(function(index, el) {
  1791. if (!$(el).hasClass('ui-draggable')) {
  1792. $(el).draggable({
  1793. containment: [ self.el.offset().left - 10, self.el.offset().top - 10,
  1794. self.el.offset().left + self.el.outerWidth(), self.el.offset().top + self.el.outerHeight() ],
  1795. handle: '.node',
  1796. refreshPositions: true,
  1797. revert: true
  1798. });
  1799. }
  1800. });
  1801. },
  1802. droppables: function() {
  1803. var self = this;
  1804. self.el.find('.node-link').each(function(index, el) {
  1805. $(el).droppable({
  1806. 'hoverClass': 'node-link-hover',
  1807. 'greedy': true,
  1808. 'accept': '.node-action',
  1809. 'tolerance': 'pointer'
  1810. });
  1811. });
  1812. self.el.find('.node-decision-end').each(function(index, el) {
  1813. $(el).droppable({
  1814. 'hoverClass': 'node-link-hover',
  1815. 'greedy': true,
  1816. 'accept': '.node-action',
  1817. 'tolerance': 'pointer'
  1818. });
  1819. });
  1820. self.el.find('.node-fork .action').each(function(index, el) {
  1821. $(el).droppable({
  1822. 'hoverClass': 'node-fork-hover',
  1823. 'greedy': true,
  1824. 'accept': '.node-action',
  1825. 'tolerance': 'pointer'
  1826. });
  1827. });
  1828. self.el.find('.node-decision .action').each(function(index, el) {
  1829. $(el).droppable({
  1830. 'hoverClass': 'node-fork-hover',
  1831. 'greedy': true,
  1832. 'accept': '.node-action',
  1833. 'tolerance': 'pointer'
  1834. });
  1835. });
  1836. self.el.find('.node-action .action').each(function(index, el) {
  1837. $(el).droppable({
  1838. 'hoverClass': 'node-action-hover',
  1839. 'greedy': true,
  1840. 'accept': '.node-action',
  1841. 'tolerance': 'pointer'
  1842. });
  1843. });
  1844. },
  1845. dragAndDropEvents: function() {
  1846. var self = this;
  1847. // Build event delegations.
  1848. // Drop on node link
  1849. self.el.on('drop', '.node-link', function(e, ui) {
  1850. // draggable should be a node.
  1851. // droppable should be a link.
  1852. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1853. var droppable = ko.contextFor(this).$data;
  1854. // If newParent is fork, prepend to child instead.
  1855. // This will make it so that we can drop and drop to the top of a node list within a fork.
  1856. var newParent = self.registry.get(droppable.parent());
  1857. if (newParent.id() != draggable.id()) {
  1858. if (newParent.isChild(draggable)) {
  1859. if (draggable.findParents().length > 1) {
  1860. // End of decision tree is being dragged to the bottom of a branch
  1861. draggable.detach();
  1862. newParent.append(draggable);
  1863. workflow.is_dirty( true );
  1864. self.rebuild();
  1865. }
  1866. } else {
  1867. switch(newParent.node_type()) {
  1868. case 'fork':
  1869. case 'decision':
  1870. draggable.detach();
  1871. var child = self.registry.get(droppable.child());
  1872. newParent.replaceChild(child, draggable);
  1873. draggable.addChild(child);
  1874. break;
  1875. case 'join':
  1876. // Join may disappear when we detach...
  1877. // Remember its children and append to child.
  1878. var parents = newParent.findParents();
  1879. draggable.detach();
  1880. if (newParent.findParents().length < 2) {
  1881. $.each(parents, function(index, parent) {
  1882. parent.append(draggable);
  1883. });
  1884. } else {
  1885. newParent.append(draggable);
  1886. }
  1887. break;
  1888. default:
  1889. draggable.detach();
  1890. newParent.append(draggable);
  1891. break;
  1892. }
  1893. workflow.is_dirty( true );
  1894. self.rebuild();
  1895. }
  1896. }
  1897. // Prevent bubbling events
  1898. return false;
  1899. });
  1900. // Drop on fork
  1901. self.el.on('drop', '.node-fork', function(e, ui) {
  1902. // draggable should be a node.
  1903. // droppable should be a fork.
  1904. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1905. var droppable = ko.contextFor(this).$data;
  1906. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1907. draggable.detach();
  1908. droppable.append(draggable);
  1909. self.rebuild();
  1910. }
  1911. // Prevent bubbling events
  1912. return false;
  1913. });
  1914. // Drop on decision
  1915. self.el.on('drop', '.node-decision', function(e, ui) {
  1916. // draggable should be a node.
  1917. // droppable should be a fork.
  1918. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1919. var droppable = ko.contextFor(this).$data;
  1920. if (!droppable.isChild(draggable) && droppable.id() != draggable.id()) {
  1921. draggable.detach();
  1922. droppable.append(draggable);
  1923. self.rebuild();
  1924. }
  1925. // Prevent bubbling events
  1926. return false;
  1927. });
  1928. // Drop on decision end link
  1929. self.el.on('drop', '.node-decision-end', function(e, ui) {
  1930. // draggable should be a node.
  1931. // droppable should be a decision end link... which should give us the decision node.
  1932. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1933. var droppable = ko.contextFor(this).$data;
  1934. var end = droppable.findEnd(droppable);
  1935. if (end.id() != draggable.id()) {
  1936. draggable.detach();
  1937. $.each(end.findParents(), function(index, parent) {
  1938. if (droppable.isChild(parent)) {
  1939. parent.append(draggable);
  1940. }
  1941. });
  1942. droppable.removeEnd();
  1943. droppable.addEnd(draggable);
  1944. self.rebuild();
  1945. }
  1946. // Prevent bubbling events
  1947. return false;
  1948. });
  1949. // Drop on action
  1950. self.el.on('drop', '.node-action', function(e, ui) {
  1951. // draggable should be a node.
  1952. // droppable should be a node.
  1953. var draggable = ko.contextFor(ui.draggable[0]).$data;
  1954. var droppable = ko.contextFor(this).$data;
  1955. // Create a fork and join programatically.
  1956. var newParents = droppable.findParents();
  1957. // skip forking beneathe a decision node
  1958. if (droppable.id() != draggable.id() && newParents.length == 1 && draggable.findParents().length == 1) {
  1959. var ForkModel = NodeModelChooser('fork');
  1960. var JoinModel = NodeModelChooser('join');
  1961. var fork = new ForkModel({
  1962. id: IdGeneratorTable['fork'].nextId(),
  1963. description: "",
  1964. workflow: self.id,
  1965. node_type: "fork",
  1966. child_links: []
  1967. });
  1968. var forkNode = new ForkNode(self, fork, self.registry);
  1969. var join = new JoinModel({
  1970. id: IdGeneratorTable['join'].nextId(),
  1971. description: "",
  1972. workflow: self.id,
  1973. node_type: "join",
  1974. child_links: []
  1975. });
  1976. var joinNode = new Node(self, join, self.registry);
  1977. self.registry.add(forkNode.id(), forkNode);
  1978. self.registry.add(joinNode.id(), joinNode);
  1979. forkNode.addChild(joinNode);
  1980. // Handles fork creation.
  1981. $.each(newParents, function(index, parent) {
  1982. parent.replaceChild(droppable, forkNode);
  1983. });
  1984. draggable.detach();
  1985. forkNode.append(draggable);
  1986. forkNode.append(droppable);
  1987. self.rebuild();
  1988. }
  1989. // Prevent bubbling events.
  1990. return false;
  1991. });
  1992. }
  1993. });
  1994. return module;
  1995. };
  1996. var Workflow = WorkflowModule($, nodeModelChooser, Node, ForkNode, DecisionNode, IdGeneratorTable);