workflow.js 61 KB

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