workflow-editor.ko.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. ko.bindingHandlers.droppable = {
  17. init: function(element, valueAccessor) {
  18. var _dropElement = $(element);
  19. var _options = valueAccessor();
  20. if (_options.enabled){
  21. var _dropOptions = {
  22. hoverClass: 'drop-target-highlight',
  23. drop: _options.onDrop
  24. };
  25. _dropElement.droppable(_dropOptions);
  26. }
  27. }
  28. };
  29. function magicLayout(vm) {
  30. loadLayout(vm, vm.initial.layout);
  31. $(document).trigger("magicLayout");
  32. }
  33. function loadColumns(viewModel, json_layout) {
  34. var _columns = [];
  35. $(json_layout).each(function (cnt, json_col) {
  36. var _rows = [];
  37. $(json_col.rows).each(function (rcnt, json_row) {
  38. var row = new Row([], viewModel);
  39. $(json_row.widgets).each(function (wcnt, widget) {
  40. row.addWidget(new Widget({
  41. size: widget.size,
  42. id: widget.id,
  43. name: widget.name,
  44. widgetType: widget.widgetType,
  45. properties: widget.properties,
  46. offset: widget.offset,
  47. loading: true,
  48. vm: viewModel
  49. }));
  50. });
  51. row.columns(loadColumns(viewModel, json_row.columns));
  52. _rows.push(row);
  53. });
  54. var column = new Column(json_col.size, _rows);
  55. _columns = _columns.concat(column);
  56. });
  57. return _columns;
  58. }
  59. function loadLayout(viewModel, json_layout) {
  60. viewModel.columns(loadColumns(viewModel, json_layout));
  61. }
  62. // End dashboard lib
  63. var Node = function (node) {
  64. var self = this;
  65. var type = typeof node.widgetType != "undefined" ? node.widgetType : node.type;
  66. self.id = ko.observable(typeof node.id != "undefined" && node.id != null ? node.id : UUID());
  67. self.name = ko.observable(typeof node.name != "undefined" && node.name != null ? node.name : "");
  68. self.type = ko.observable(typeof type != "undefined" && type != null ? type : "");
  69. self.properties = ko.mapping.fromJS(typeof node.properties != "undefined" && node.properties != null ? node.properties : {});
  70. self.children = ko.mapping.fromJS(typeof node.children != "undefined" && node.children != null ? node.children : []);
  71. self.get_link = function(name) {
  72. var _link = null;
  73. $.each(self.children(), function(index, link) {
  74. if (name in link) {
  75. _link = link;
  76. return false;
  77. }
  78. });
  79. return _link;
  80. }
  81. self.set_link = function(name, node_id) {
  82. var _link = self.get_link(name);
  83. if (_link == null) {
  84. _link = {}
  85. self.children.push(_link);
  86. }
  87. _link[name] = node_id;
  88. self.children.valueHasMutated();
  89. }
  90. self.remove_link = function(name, child) {
  91. var _link = null;
  92. $.each(self.children(), function(index, link) {
  93. var _l = ko.mapping.toJS(link);
  94. if (name in _l && _l[name] == child) {
  95. _link = link;
  96. return false;
  97. }
  98. });
  99. if (_link != null) {
  100. self.children.remove(_link);
  101. }
  102. }
  103. }
  104. var Workflow = function (vm, workflow) {
  105. var self = this;
  106. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  107. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  108. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  109. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  110. self.nodes = ko.observableArray([]);
  111. self.movedNode = null;
  112. self.nodeIds = ko.computed(function() {
  113. var mapping = [];
  114. $.each(self.nodes(), function(index, node) {
  115. mapping.push(node.id());
  116. });
  117. return mapping;
  118. });
  119. self.nodeNamesMapping = ko.computed(function() {
  120. var mapping = {};
  121. $.each(self.nodes(), function(index, node) {
  122. mapping[node.id()] = node.name();
  123. });
  124. return mapping;
  125. });
  126. self.linkMapping = ko.computed(function() {
  127. var mapping = {};
  128. $.each(self.nodes(), function(index, node) {
  129. var links = []
  130. $.each(node.children(), function(index, link) {
  131. if ('to' in link) {
  132. links.push(link['to']);
  133. }
  134. });
  135. mapping[node.id()] = links
  136. });
  137. return mapping;
  138. });
  139. self.linkMapping.subscribe(function(newVal){
  140. $(document).trigger("drawArrows");
  141. });
  142. self.loadNodes = function(workflow) {
  143. var nodes = []
  144. $.each(workflow.nodes, function(index, node) {
  145. var _node = new Node(node);
  146. nodes.push(_node);
  147. });
  148. self.nodes(nodes)
  149. }
  150. self.newNode = function(widget, callback) {
  151. $.ajax({
  152. type: "POST",
  153. url: "/oozie/editor/workflow/new_node/",
  154. data: {
  155. "workflow": ko.mapping.toJSON(workflow),
  156. "node": ko.mapping.toJSON(widget)
  157. },
  158. success: function (data) {
  159. if (data.status == 0) {
  160. viewModel.addActionProperties(data.properties);
  161. viewModel.addActionWorkflows(data.workflows);
  162. if (callback){
  163. callback(widget);
  164. }
  165. }
  166. },
  167. async: false
  168. });
  169. };
  170. self.addNode = function(widget) {
  171. // Todo get parent cell, link nodes... when we have the new layout
  172. $.post("/oozie/editor/workflow/add_node/", {
  173. "workflow": ko.mapping.toJSON(workflow),
  174. "node": ko.mapping.toJSON(widget),
  175. "properties": ko.mapping.toJSON(viewModel.addActionProperties()),
  176. "subworkflow": viewModel.selectedSubWorkflow() ? ko.mapping.toJSON(viewModel.selectedSubWorkflow()) : '{}',
  177. }, function (data) {
  178. if (data.status == 0) {
  179. var _node = ko.mapping.toJS(widget);
  180. _node.properties = data.properties;
  181. _node.name = data.name;
  182. if (self.movedNode) {
  183. var node = self.movedNode;
  184. } else {
  185. var node = new Node(_node);
  186. }
  187. self.nodes.push(node);
  188. if (vm.currentlyCreatingFork) {
  189. // Added to the side ?
  190. var parentWidget = vm.getWidgetPredecessor(node.id());
  191. var parent = self.getNodeById(parentWidget.id());
  192. if (self.getNodeById(parentWidget.id()) == null) { // New fork
  193. vm.currentlyCreatedJoin.properties['fork_id'] = vm.currentlyCreatedFork.id;
  194. vm.currentlyCreatedFork.properties['join_id'] = vm.currentlyCreatedJoin.id;
  195. var fork = new Node(vm.currentlyCreatedFork);
  196. var join = new Node(vm.currentlyCreatedJoin);
  197. self.nodes.push(fork);
  198. self.nodes.push(join);
  199. var forkParent = self.getNodeById(vm.getWidgetPredecessor(parentWidget.id()).id());
  200. var afterParentId = ko.mapping.toJS(forkParent.get_link('to')).to;
  201. var afterParent = self.getNodeById(afterParentId);
  202. fork.children.push({'to': afterParentId, 'condition': ''});
  203. fork.children.push({'to': node.id(), 'condition': ''});
  204. forkParent.get_link('to')['to'] = fork.id();
  205. var belowJoin = vm.getWidgetSuccessor(join.id());
  206. join.set_link('to', belowJoin.id());
  207. if (afterParent.type() == 'fork-widget') {
  208. self.getNodeById(afterParent.properties.join_id()).set_link('to', join.id());
  209. } else {
  210. afterParent.set_link('to', join.id());
  211. }
  212. node.set_link('to', join.id());
  213. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  214. } else {
  215. // Just add to existing fork
  216. var join = vm.getWidgetSuccessor(node.id());
  217. node.set_link('to', join.id());
  218. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  219. parent.children.push({'to': node.id(), 'condition': ''});
  220. }
  221. } else {
  222. var parentWidget = vm.getWidgetPredecessor(node.id());
  223. var parent = self.getNodeById(parentWidget.id());
  224. if (widget.widgetType() == 'kill-widget') {
  225. parent.set_link('to', node.id());
  226. } else if (parentWidget.widgetType() == 'fork-widget') {
  227. var child = vm.getWidgetSuccessor(node.id());
  228. parent.remove_link('to', child.id());
  229. parent.children.push({'to': node.id(), 'condition': ''});
  230. node.set_link('to', child.id());
  231. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  232. } else {
  233. // Parent is regular node
  234. node.set_link('to', parent.get_link('to')['to']);
  235. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  236. parent.set_link('to', node.id());
  237. }
  238. }
  239. vm.currentlyCreatingFork = false;
  240. } else {
  241. $(document).trigger("error", data.message);
  242. }
  243. }).fail(function (xhr, textStatus, errorThrown) {
  244. $(document).trigger("error", xhr.responseText);
  245. });
  246. };
  247. self.removeNode = function(node_id) {
  248. var node = self.getNodeById(node_id);
  249. var parents = self.getParents(node_id);
  250. var parent = null;
  251. var childLink = node.get_link('to');
  252. var childId = ko.mapping.toJS(childLink)['to'];
  253. $.each(parents, function(index, _parent) {
  254. _parent.remove_link('to', node_id);
  255. _parent.children.unshift({'to': childId});
  256. parent = _parent;
  257. });
  258. self.nodes.remove(node);
  259. // If need to remove fork
  260. if (parent.type() == 'fork-widget') {
  261. var fork = parent;
  262. var join = self.getNodeById(childId);
  263. if (join.type() == 'join-widget') {
  264. if (fork.children().length == 2) {
  265. // Link top to above and delete fork
  266. fork.remove_link('to', childId);
  267. var forkParent = self.getParents(fork.id())[0];
  268. forkParent.set_link('to', ko.mapping.toJS(fork.get_link('to'))['to']); // Only link
  269. self.nodes.remove(fork);
  270. // Link bottom to child of join
  271. var beboreJoin = self.getParents(childId)[0];
  272. var joinChildId = ko.mapping.toJS(join.get_link('to'))['to'];
  273. beboreJoin.set_link('to', joinChildId);
  274. self.nodes.remove(join);
  275. } else {
  276. parent.remove_link('to', childId);
  277. }
  278. }
  279. } else if (parent.type() == 'decision-widget') {
  280. parent.remove_link('to', childId);
  281. }
  282. };
  283. self.moveNode = function(widget) {
  284. var node = self.getNodeById(widget.id());
  285. self.movedNode = node;
  286. self.removeNode(node.id());
  287. self.addNode(widget);
  288. self.movedNode = null;
  289. };
  290. self.getParents = function(node_id) { // Join nodes can have multiple parents
  291. var _nodes = [];
  292. $.each(self.nodes(), function (index, node) {
  293. $.each(node.children(), function(index, link) {
  294. var _link = ko.mapping.toJS(link);
  295. if ('to' in _link && _link.to == node_id) {
  296. _nodes.push(node);
  297. }
  298. })
  299. });
  300. return _nodes;
  301. };
  302. self.getNodeById = function (node_id) {
  303. var _node = null;
  304. $.each(self.nodes(), function (index, node) {
  305. if (node.id() == node_id) {
  306. _node = node;
  307. return false;
  308. }
  309. });
  310. return _node;
  311. };
  312. }
  313. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json, workflow_properties_json) {
  314. var self = this;
  315. self.isNested = ko.observable(true);
  316. self.isEditing = ko.observable(true);
  317. self.isEditing.subscribe(function(newVal){
  318. $(document).trigger("editingToggled");
  319. });
  320. self.toggleEditing = function () {
  321. self.isEditing(! self.isEditing());
  322. };
  323. self.columns = ko.observable([]);
  324. self.previewColumns = ko.observable("");
  325. self.workflow = new Workflow(self, workflow_json);
  326. self.credentials = ko.mapping.fromJSON(credentials_json);
  327. self.inited = ko.observable(self.columns().length > 0);
  328. self.init = function(callback) {
  329. self.workflow_properties = ko.mapping.fromJS(workflow_properties_json);
  330. loadLayout(self, layout_json);
  331. self.workflow.loadNodes(workflow_json);
  332. }
  333. self.depen = ko.observableArray(workflow_json.dependencies);
  334. self.addActionProperties = ko.observableArray([]);
  335. self.addActionWorkflows = ko.observableArray([]);
  336. self.selectedSubWorkflow = ko.observable();
  337. self.currentlyDraggedWidget = ko.observable(null);
  338. self.currentlyDraggedOp = ko.observable("move");
  339. self.currentlyDraggedWidget.subscribe(function (widget) {
  340. toggleSideDrop(widget, false);
  341. });
  342. self.setCurrentlyDraggedWidget = function (widget, op) {
  343. self.currentlyDraggedOp($(op).hasClass("fa-copy") ? "copy" : "move");
  344. self.currentlyDraggedWidget(widget);
  345. }
  346. self.enableSideDrop = function (widget) {
  347. toggleSideDrop(widget, true);
  348. }
  349. function toggleSideDrop(widget, enable) {
  350. if (widget != null && widget.id() != "" && self.currentlyDraggedOp() == "move") {
  351. var _row = self.getWidgetParentRow(widget.id());
  352. if (_row) {
  353. _row.enableOozieDropOnSide(enable);
  354. _row.enableOozieDropOnBefore(enable);
  355. var _col = self.getRowParentColumn(_row.id());
  356. if (self.getColumnParentRow(_col.id())!=null){
  357. _col.enableOozieDropOnBefore(enable);
  358. _col.enableOozieDropOnAfter(enable);
  359. }
  360. var _prevRow = self.getPrevRow(_row);
  361. if (_prevRow){
  362. if (_prevRow.widgets().length > 0 && _prevRow.widgets()[0].widgetType() == "start-widget"){
  363. _prevRow.enableOozieDropOnSide(enable);
  364. self.getRowParentColumn(_prevRow.id()).enableOozieDropOnBefore(enable);
  365. }
  366. }
  367. var _nextRow = self.getNextRow(_row);
  368. if (_nextRow){
  369. _nextRow.enableOozieDropOnBefore(enable);
  370. if (_nextRow.widgets().length > 0 && _nextRow.widgets()[0].widgetType() == "end-widget"){
  371. _nextRow.enableOozieDropOnSide(enable);
  372. self.getRowParentColumn(_nextRow.id()).enableOozieDropOnAfter(enable);
  373. }
  374. }
  375. }
  376. }
  377. }
  378. self.currentlyCreatingFork = false;
  379. self.currentlyCreatedFork = null;
  380. self.currentlyCreatedJoin = null;
  381. self.isDragging = ko.observable(false);
  382. self.addDraggedWidget = function (target, atBeginning) {
  383. if (self.currentlyDraggedWidget() != null) {
  384. var _parentCol = target instanceof Column ? target : self.getRowParentColumn(target.id());
  385. var _newRow = null;
  386. if (typeof atBeginning != "undefined") {
  387. if (_parentCol.rows().length > 0 && _parentCol.rows()[0].widgets() && _parentCol.rows()[0].widgets().length > 0 && _parentCol.rows()[0].widgets()[0].widgetType() == "start-widget") {
  388. if (atBeginning) {
  389. _newRow = _parentCol.addEmptyRow(false, 1);
  390. }
  391. else {
  392. _newRow = _parentCol.addEmptyRow(false, _parentCol.rows().length - 1);
  393. }
  394. }
  395. else {
  396. _newRow = _parentCol.addEmptyRow(atBeginning);
  397. }
  398. }
  399. else {
  400. var _rowIdx = 0;
  401. $.each(_parentCol.rows(), function (i, irow) {
  402. if (irow.id() == target.id()) {
  403. _rowIdx = i;
  404. }
  405. });
  406. _newRow = _parentCol.addEmptyRow(false, _rowIdx);
  407. }
  408. var _w = new Widget({
  409. size: self.currentlyDraggedWidget().size(),
  410. id: UUID(),
  411. name: self.currentlyDraggedWidget().name(),
  412. widgetType: self.currentlyDraggedWidget().widgetType(),
  413. properties: self.currentlyDraggedWidget().properties(),
  414. offset: self.currentlyDraggedWidget().offset(),
  415. loading: true,
  416. vm: self
  417. });
  418. if (self.currentlyDraggedWidget().id() != "" && self.currentlyDraggedOp() == "move"){
  419. self.removeWidgetById(self.currentlyDraggedWidget().id());
  420. _w = self.currentlyDraggedWidget();
  421. }
  422. _newRow.widgets([_w]);
  423. return _w;
  424. }
  425. }
  426. self.addSideDraggedWidget = function (row, atBeginning) {
  427. if (self.currentlyDraggedWidget() != null) {
  428. var _parentCol = self.getRowParentColumn(row.id());
  429. var _rowIdx = 0;
  430. $.each(_parentCol.rows(), function (i, irow) {
  431. if (irow.id() == row.id()) {
  432. _rowIdx = i;
  433. }
  434. });
  435. var _addForkAndJoin = (row.columns().length == 0);
  436. if (_addForkAndJoin) {
  437. var _forkRow = _parentCol.addEmptyRow(false, _rowIdx);
  438. var _id = UUID();
  439. var _fork = new Widget({
  440. size: 12,
  441. id: _id,
  442. name: 'fork' + '-' + _id.slice(0, 4),
  443. widgetType: "fork-widget",
  444. properties: {},
  445. offset: 0,
  446. loading: true,
  447. vm: self
  448. });
  449. _forkRow.widgets([_fork]);
  450. }
  451. var _w = new Widget({
  452. size: self.currentlyDraggedWidget().size(),
  453. id: UUID(),
  454. name: self.currentlyDraggedWidget().name(),
  455. widgetType: self.currentlyDraggedWidget().widgetType(),
  456. properties: self.currentlyDraggedWidget().properties(),
  457. offset: self.currentlyDraggedWidget().offset(),
  458. loading: true,
  459. vm: self
  460. });
  461. if (self.currentlyDraggedWidget().id() != ""){
  462. self.removeWidgetById(self.currentlyDraggedWidget().id());
  463. _w = self.currentlyDraggedWidget();
  464. }
  465. if (row.columns().length == 0) {
  466. var _col = row.addColumn(null, atBeginning);
  467. if (row.widgets().length > 0) {
  468. var _row = _col.addEmptyRow();
  469. row.widgets().forEach(function (widget) {
  470. _row.addWidget(widget);
  471. });
  472. if (row.widgets()[0].widgetType() == "fork-widget") {
  473. var _widgetsRow = self.getNextRow(row);
  474. var _joinRow = self.getNextRow(_widgetsRow);
  475. _col.rows.push(_widgetsRow);
  476. _col.rows.push(_joinRow);
  477. self.getRowParentColumn(row.id()).rows.remove(_widgetsRow);
  478. self.getRowParentColumn(row.id()).rows.remove(_joinRow);
  479. }
  480. row.widgets([]);
  481. }
  482. }
  483. var _col = row.addColumn(null, atBeginning);
  484. var _row = new Row([_w], self);
  485. _col.addRow(_row);
  486. if (_addForkAndJoin) {
  487. var _joinRow = _parentCol.addEmptyRow(false, _rowIdx + 2);
  488. var _id = UUID();
  489. var _join = new Widget({
  490. size: 12,
  491. id: _id,
  492. name: "join" + '-' + _id.slice(0, 4),
  493. widgetType: "join-widget",
  494. properties: {},
  495. offset: 0,
  496. loading: true,
  497. vm: self
  498. });
  499. _joinRow.widgets([_join]);
  500. self.currentlyCreatedFork = ko.mapping.toJS(_fork);
  501. self.currentlyCreatedJoin = ko.mapping.toJS(_join);
  502. }
  503. self.currentlyCreatingFork = true;
  504. return _w;
  505. }
  506. }
  507. self.getWidgetById = function (widget_id) {
  508. var _widget = null;
  509. $.each(self.columns(), function (i, col) {
  510. $.each(col.rows(), function (j, row) {
  511. $.each(row.widgets(), function (z, widget) {
  512. if (widget.id() == widget_id){
  513. _widget = widget;
  514. return false;
  515. }
  516. });
  517. });
  518. });
  519. return _widget;
  520. }
  521. self.removeWidget = function (widget_json) {
  522. self.workflow.removeNode(widget_json.id());
  523. self.removeWidgetById(widget_json.id());
  524. }
  525. self.removeWidgetById = function (widget_id) {
  526. $.each(self.columns(), function (i, col) {
  527. self.deeplyRemoveWidgetById(widget_id, col, self)
  528. });
  529. }
  530. self.deeplyRemoveWidgetById = function (widget_id, col, parent) {
  531. if (col) {
  532. $.each(col.rows(), function (j, row) {
  533. if (row && row.widgets()){
  534. $.each(row.widgets(), function (z, widget) {
  535. if (widget.id() == widget_id) {
  536. row.widgets.remove(widget);
  537. col.rows.remove(row);
  538. }
  539. });
  540. }
  541. if (row && row.columns()) {
  542. $.each(row.columns(), function (i, icol) {
  543. self.deeplyRemoveWidgetById(widget_id, icol, row);
  544. });
  545. }
  546. });
  547. if (col.rows().length == 0) {
  548. parent.columns.remove(col);
  549. if (parent.columns().length > 1) {
  550. var _size = Math.max(1, Math.floor(12 / (parent.columns().length)));
  551. parent.columns().forEach(function (icol) {
  552. icol.size(_size);
  553. });
  554. }
  555. else {
  556. var _rows = parent.columns()[0].rows();
  557. var _parentRows = self.getRowParentColumn(parent.id()).rows;
  558. var _prevRowIdx = -1;
  559. for (var i = 0; i < _parentRows().length; i++) {
  560. if (_parentRows()[i].id() == parent.id()) {
  561. break;
  562. }
  563. _prevRowIdx = i;
  564. }
  565. if (_prevRowIdx > -1 && _parentRows()[_prevRowIdx].widgets().length > 0 && _parentRows()[_prevRowIdx].widgets()[0].widgetType() == "fork-widget"){
  566. _parentRows.remove(_parentRows()[_prevRowIdx]);
  567. _parentRows.remove(_parentRows()[_prevRowIdx+1]);
  568. }
  569. for (var i=0;i<_rows.length;i++){
  570. if (i==0){
  571. parent.widgets(_rows[i].widgets());
  572. }
  573. else {
  574. _parentRows.push(_rows[i]);
  575. }
  576. }
  577. parent.columns([]);
  578. }
  579. }
  580. }
  581. }
  582. self.getWidgetRelative = function (widget_id, isPredecessor) {
  583. var _row = self.getWidgetParentRow(widget_id);
  584. var _col = self.getRowParentColumn(_row.id());
  585. var _nextRow = null;
  586. for (var i = 0; i < _col.rows().length; i++) {
  587. if (_col.rows()[i].id() == _row.id()) {
  588. if (!isPredecessor && _col.rows().length >= i + 1) {
  589. _nextRow = _col.rows()[i + 1];
  590. }
  591. break;
  592. }
  593. _nextRow = _col.rows()[i];
  594. }
  595. if (_nextRow != null) {
  596. return _nextRow.widgets()[0];
  597. }
  598. else {
  599. var _parentRow = self.getColumnParentRow(_col.id());
  600. if (_parentRow) {
  601. var _parentColumn = self.getRowParentColumn(_parentRow.id());
  602. var _nextParentRow = null;
  603. for (var i = 0; i < _parentColumn.rows().length; i++) {
  604. if (_parentColumn.rows()[i].id() == _parentRow.id()) {
  605. if (!isPredecessor && _parentColumn.rows().length >= i + 1) {
  606. _nextParentRow = _parentColumn.rows()[i + 1];
  607. }
  608. break;
  609. }
  610. _nextParentRow = _parentColumn.rows()[i];
  611. }
  612. if (_nextParentRow != null) {
  613. return _nextParentRow.widgets()[0];
  614. }
  615. }
  616. }
  617. return null;
  618. }
  619. self.getWidgetPredecessor = function (widget_id) {
  620. return self.getWidgetRelative(widget_id, true);
  621. }
  622. self.getWidgetSuccessor = function (widget_id) {
  623. return self.getWidgetRelative(widget_id, false);
  624. }
  625. self.isRowAfterFork = function (row) {
  626. var _parentColumn = self.getRowParentColumn(row.id());
  627. var _prevRow = null;
  628. for (var i = 0; i < _parentColumn.rows().length; i++) {
  629. var _currentRow = _parentColumn.rows()[i];
  630. if (_currentRow.id() == row.id()){
  631. break;
  632. }
  633. _prevRow = _currentRow;
  634. }
  635. if (_prevRow != null){
  636. return _prevRow.widgets().length > 0 && (_prevRow.widgets()[0].widgetType() == "fork-widget" || _prevRow.widgets()[0].widgetType() == "decision-widget");
  637. }
  638. return false;
  639. }
  640. self.isRowBeforeJoin = function (row) {
  641. return row.widgets().length > 0 && row.widgets()[0].widgetType() == "join-widget";
  642. }
  643. self.getNextRow = function (row) {
  644. var _parentColumn = self.getRowParentColumn(row.id());
  645. var _nextParentRow = null;
  646. for (var i = 0; i < _parentColumn.rows().length; i++) {
  647. if (_parentColumn.rows()[i].id() == row.id()) {
  648. if (_parentColumn.rows().length >= i + 1) {
  649. _nextParentRow = _parentColumn.rows()[i + 1];
  650. }
  651. break;
  652. }
  653. _nextParentRow = _parentColumn.rows()[i];
  654. }
  655. return _nextParentRow;
  656. }
  657. self.getPrevRow = function (row) {
  658. var _parentColumn = self.getRowParentColumn(row.id());
  659. var _prevParentRow = null;
  660. for (var i = 0; i < _parentColumn.rows().length; i++) {
  661. if (_parentColumn.rows()[i].id() == row.id()) {
  662. if (i > 0) {
  663. _prevParentRow = _parentColumn.rows()[i - 1];
  664. }
  665. break;
  666. }
  667. _prevParentRow = _parentColumn.rows()[i];
  668. }
  669. return _prevParentRow;
  670. }
  671. self.getWidgetParentRow = function (widget_id) {
  672. var _row = null;
  673. for (var i = 0; i < self.columns().length; i++) {
  674. _row = self.traverseColumnForWidget(widget_id, self.columns()[i]);
  675. if (_row != null) {
  676. break;
  677. }
  678. }
  679. return _row;
  680. }
  681. self.getRowParentColumn = function (row_id) {
  682. var _column = null;
  683. for (var i = 0; i < self.columns().length; i++) {
  684. _column = self.traverseColumnForColumn(row_id, self.columns()[i]);
  685. }
  686. return _column;
  687. }
  688. self.getColumnParentRow = function (col_id) {
  689. var _row = null;
  690. for (var i = 0; i < self.columns().length; i++) {
  691. _row = self.traverseColumnForRow(col_id, self.columns()[i]);
  692. if (_row != null) {
  693. break;
  694. }
  695. }
  696. return _row;
  697. }
  698. self.getRowParentRow = function (row_id) {
  699. var _col = self.getRowParentColumn(row_id);
  700. if (_col != null) {
  701. return self.getColumnParentRow(_col.id());
  702. }
  703. }
  704. self.traverseColumnForColumn = function (row_id, col) {
  705. var _column = null;
  706. if (col) {
  707. for (var j = 0; j < col.rows().length; j++) {
  708. var row = col.rows()[j];
  709. if (row.id() == row_id) {
  710. _column = col;
  711. break;
  712. }
  713. for (var z = 0; z < row.columns().length; z++) {
  714. _column = self.traverseColumnForColumn(row_id, row.columns()[z]);
  715. if (_column != null) {
  716. break;
  717. }
  718. }
  719. }
  720. }
  721. return _column;
  722. }
  723. self.traverseColumnForRow = function (col_id, col) {
  724. var _row = null;
  725. if (col) {
  726. for (var j = 0; j < col.rows().length; j++) {
  727. var row = col.rows()[j];
  728. for (var z = 0; z < row.columns().length; z++) {
  729. var _col = row.columns()[z];
  730. if (_col.id() == col_id) {
  731. _row = row;
  732. }
  733. else {
  734. _row = self.traverseColumnForRow(col_id, _col);
  735. }
  736. if (_row != null) {
  737. break;
  738. }
  739. }
  740. }
  741. }
  742. return _row;
  743. }
  744. self.traverseColumnForWidget = function (widget_id, col) {
  745. var _row = null;
  746. if (col) {
  747. for (var j = 0; j < col.rows().length; j++) {
  748. var row = col.rows()[j];
  749. for (var z = 0; z < row.widgets().length; z++) {
  750. var widget = row.widgets()[z];
  751. if (widget.id() == widget_id) {
  752. _row = row;
  753. break;
  754. }
  755. }
  756. if (_row != null) {
  757. break;
  758. }
  759. for (var z = 0; z < row.columns().length; z++) {
  760. _row = self.traverseColumnForWidget(widget_id, row.columns()[z]);
  761. if (_row != null) {
  762. break;
  763. }
  764. }
  765. }
  766. }
  767. return _row;
  768. }
  769. self.convertToDecision = function (widget, node) {
  770. if (widget.widgetType() == "fork-widget"){
  771. var _row = self.getWidgetParentRow(widget.id());
  772. var _next = self.getNextRow(_row);
  773. while (_next.widgets().length == 0){
  774. _next = self.getNextRow(_next);
  775. }
  776. // Remove the join
  777. self.workflow.removeNode(_next.widgets()[0].id());
  778. self.removeWidgetById(_next.widgets()[0].id());
  779. node.children.push({'to': '33430f0f-ebfa-c3ec-f237-3e77efa03d0a', 'condition': 'default'});
  780. widget.widgetType("decision-widget");
  781. node.type("decision-widget");
  782. var _newName = "decision-" + node.id().slice(0, 4);
  783. node.name(_newName);
  784. widget.name(_newName);
  785. $(document).trigger("drawArrows");
  786. }
  787. }
  788. self.save = function () {
  789. $.post("/oozie/editor/workflow/save/", {
  790. "layout": ko.mapping.toJSON(self.columns),
  791. "workflow": ko.mapping.toJSON(self.workflow)
  792. }, function (data) {
  793. if (data.status == 0) {
  794. self.workflow.id(data.id);
  795. $(document).trigger("info", data.message);
  796. if (window.location.search.indexOf("workflow") == -1) {
  797. window.location.hash = '#workflow=' + data.id;
  798. }
  799. }
  800. else {
  801. $(document).trigger("error", data.message);
  802. }
  803. }).fail(function (xhr, textStatus, errorThrown) {
  804. $(document).trigger("error", xhr.responseText);
  805. });
  806. };
  807. self.gen_xml = function () {
  808. $.post("/oozie/editor/workflow/gen_xml/", {
  809. "layout": ko.mapping.toJSON(self.columns),
  810. "workflow": ko.mapping.toJSON(self.workflow)
  811. }, function (data) {
  812. if (data.status == 0) {
  813. console.log(data.xml);
  814. }
  815. else {
  816. $(document).trigger("error", data.message);
  817. }
  818. }).fail(function (xhr, textStatus, errorThrown) {
  819. $(document).trigger("error", xhr.responseText);
  820. });
  821. };
  822. self.import_workflows = function () {
  823. $.post("/oozie/editor/workflow/import_workflows/", {
  824. }, function (data) {
  825. if (data.status == 0) {
  826. console.log(data.json);
  827. }
  828. else {
  829. $(document).trigger("error", data.message);
  830. }
  831. }).fail(function (xhr, textStatus, errorThrown) {
  832. $(document).trigger("error", xhr.responseText);
  833. });
  834. };
  835. self.showSubmitPopup = function () {
  836. // If self.workflow.id() == null, need to save wf for now
  837. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  838. }, function (data) {
  839. $(document).trigger("showSubmitPopup", data);
  840. }).fail(function (xhr, textStatus, errorThrown) {
  841. $(document).trigger("error", xhr.responseText);
  842. });
  843. };
  844. function bareWidgetBuilder(name, type){
  845. return new Widget({
  846. size: 12,
  847. id: "",
  848. name: name,
  849. widgetType: type
  850. });
  851. }
  852. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  853. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  854. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  855. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  856. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  857. self.draggableSqoopAction = ko.observable(bareWidgetBuilder("Sqoop 1", "sqoop-widget"));
  858. self.draggableShellAction = ko.observable(bareWidgetBuilder("Shell", "shell-widget"));
  859. self.draggableSshAction = ko.observable(bareWidgetBuilder("Ssh", "ssh-widget"));
  860. self.draggableFsAction = ko.observable(bareWidgetBuilder("HDFS Fs", "fs-widget"));
  861. self.draggableEmailAction = ko.observable(bareWidgetBuilder("Email", "email-widget"));
  862. self.draggableStreamingAction = ko.observable(bareWidgetBuilder("Streaming", "streaming-widget"));
  863. self.draggableDistCpAction = ko.observable(bareWidgetBuilder("Distcp", "distcp-widget"));
  864. self.draggableKillNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  865. };