workflow.js 61 KB

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