workflow.js 61 KB

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