workflow.js 61 KB

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