workflow.js 61 KB

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