workflow.js 62 KB

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