workflow.js 60 KB

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