workflow.js 61 KB

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