workflow.js 58 KB

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