workflow.js 61 KB

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