workflow-editor.ko.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 loadLayout(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. $(json_row.columns).each(function (ccnt, column) {
  52. var _irows = [];
  53. $(column.rows).each(function (ircnt, json_irow) {
  54. var _irow = new Row([], viewModel);
  55. $(json_irow.widgets).each(function (iwcnt, iwidget) {
  56. _irow.addWidget(new Widget({
  57. size:iwidget.size,
  58. id: iwidget.id,
  59. name: iwidget.name,
  60. widgetType: iwidget.widgetType,
  61. properties: iwidget.properties,
  62. offset: iwidget.offset,
  63. loading: true,
  64. vm: viewModel
  65. }));
  66. });
  67. _irows.push(_irow);
  68. });
  69. row.addColumn(new Column(column.size, _irows));
  70. });
  71. _rows.push(row);
  72. });
  73. var column = new Column(json_col.size, _rows);
  74. _columns = _columns.concat(column);
  75. });
  76. viewModel.columns(_columns);
  77. }
  78. // End dashboard lib
  79. var Node = function (node) {
  80. var self = this;
  81. var type = typeof node.widgetType != "undefined" ? node.widgetType : node.type;
  82. self.id = ko.observable(typeof node.id != "undefined" && node.id != null ? node.id : UUID());
  83. self.name = ko.observable(typeof node.name != "undefined" && node.name != null ? node.name : "");
  84. self.type = ko.observable(typeof type != "undefined" && type != null ? type : "");
  85. self.properties = ko.mapping.fromJS(typeof node.properties != "undefined" && node.properties != null ? node.properties : {});
  86. self.children = ko.mapping.fromJS(typeof node.children != "undefined" && node.children != null ? node.children : []);
  87. self.get_link = function(name) {
  88. var _link = null;
  89. $.each(self.children(), function(index, link) {
  90. if (name in link) {
  91. _link = link;
  92. return false;
  93. }
  94. });
  95. return _link;
  96. }
  97. self.set_link = function(name, node_id) {
  98. var _link = self.get_link(name);
  99. if (_link == null) {
  100. _link = {};
  101. self.children.push(_link);
  102. }
  103. _link[name] = node_id;
  104. }
  105. }
  106. var Workflow = function (vm, workflow) {
  107. var self = this;
  108. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  109. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  110. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  111. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  112. self.nodes = ko.observableArray([]);
  113. self.loadNodes = function(workflow) {
  114. var nodes = []
  115. $.each(workflow.nodes, function(index, node) {
  116. var _node = new Node(node);
  117. nodes.push(_node);
  118. });
  119. self.nodes(nodes)
  120. }
  121. self.newNode = function(widget) {
  122. $.ajax({
  123. type: "POST",
  124. url: "/oozie/editor/workflow/new_node/",
  125. data: {
  126. "workflow": ko.mapping.toJSON(workflow),
  127. "node": ko.mapping.toJSON(widget)
  128. },
  129. success: function (data) {
  130. if (data.status == 0) {
  131. viewModel.addActionProperties(data.properties);
  132. viewModel.addActionWorkflows(data.workflows);
  133. }
  134. },
  135. async: false
  136. });
  137. };
  138. self.addNode = function(widget) {
  139. // Todo get parent cell, link nodes... when we have the new layout
  140. $.post("/oozie/editor/workflow/add_node/", {
  141. "workflow": ko.mapping.toJSON(workflow),
  142. "node": ko.mapping.toJSON(widget),
  143. "properties": ko.mapping.toJSON(viewModel.addActionProperties()),
  144. "subworkflow": viewModel.selectedSubWorkflow() ? ko.mapping.toJSON(viewModel.selectedSubWorkflow()) : '{}',
  145. }, function (data) {
  146. if (data.status == 0) {
  147. var _node = ko.mapping.toJS(widget);
  148. _node.properties = data.properties;
  149. _node.name = data.name;
  150. var node = new Node(_node);
  151. // Add to list of nodes
  152. var end = self.nodes.pop();
  153. self.nodes.push(node);
  154. self.nodes.push(end);
  155. // if node != kill node
  156. // Added to the side ?
  157. if (true) {
  158. var parent = self.getNodeById('3f107997-04cc-8733-60a9-a4bb62cebffc');
  159. if (parent.type() != 'fork-widget') {
  160. var fork = new Node({});
  161. fork.name('fork' + '-' + fork.id().slice(0, 4));
  162. fork.type('fork-widget');
  163. var join = new Node({});
  164. join.name('join' + '-' + fork.id().slice(0, 4));
  165. join.type('join-widget');
  166. // Start node
  167. var afterStartId = ko.mapping.toJS(parent.get_link('to')).to;
  168. var afterStart = self.getNodeById(afterStartId);
  169. fork.children.push({'to': afterStartId});
  170. fork.children.push({'to': node.id()});
  171. parent.get_link('to')['to'] = fork.id();
  172. join.set_link('to', afterStart.get_link('ok')['ok']);
  173. afterStart.set_link('ok', join.id());
  174. node.set_link('ok', join.id());
  175. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  176. var end = self.nodes.pop();
  177. self.nodes.push(fork);
  178. self.nodes.push(join);
  179. self.nodes.push(end)
  180. // Regular node
  181. // Join node
  182. } else {
  183. // Just add to existing fork
  184. }
  185. } else {
  186. var parentWidget = vm.getWidgetPredecessor(node.id());
  187. var parent = self.getNodeById(parentWidget.id());
  188. if (parentWidget.widgetType() == 'start-widget') {
  189. // Star node link to new node
  190. parent.set_link('to', node.id());
  191. // Link to end
  192. node.set_link('ok', '33430f0f-ebfa-c3ec-f237-3e77efa03d0a');
  193. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  194. } else if (parentWidget.widgetType() == 'pig-widget') {
  195. // Parent regular node
  196. node.set_link('ok', parent.get_link('ok')['ok']);
  197. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  198. parent.set_link('ok', node.id());
  199. }
  200. // Parent fork/decision/join...
  201. }
  202. } else {
  203. $(document).trigger("error", data.message);
  204. }
  205. }).fail(function (xhr, textStatus, errorThrown) {
  206. $(document).trigger("error", xhr.responseText);
  207. });
  208. };
  209. self.getNodeById = function (node_id) {
  210. var _node = null;
  211. $.each(self.nodes(), function (index, node) {
  212. if (node.id() == node_id) {
  213. _node = node;
  214. return false;
  215. }
  216. });
  217. return _node;
  218. }
  219. }
  220. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json) {
  221. var self = this;
  222. self.isNested = ko.observable(true);
  223. self.isEditing = ko.observable(true);
  224. self.toggleEditing = function () {
  225. self.isEditing(! self.isEditing());
  226. };
  227. self.columns = ko.observable([]);
  228. self.previewColumns = ko.observable("");
  229. self.workflow = new Workflow(self, workflow_json);
  230. self.credentials = ko.mapping.fromJSON(credentials_json);
  231. self.inited = ko.observable(self.columns().length > 0);
  232. self.init = function(callback) {
  233. loadLayout(self, layout_json);
  234. self.workflow.loadNodes(workflow_json);
  235. }
  236. self.depen = ko.observableArray(workflow_json.dependencies);
  237. self.addActionProperties = ko.observableArray([]);
  238. self.addActionWorkflows = ko.observableArray([]);
  239. self.selectedSubWorkflow = ko.observable();
  240. self.currentlyDraggedWidget = null;
  241. self.isDragging = ko.observable(false);
  242. self.setCurrentDraggedWidget = function (widget) {
  243. self.currentlyDraggedWidget = widget;
  244. }
  245. self.addDraggedWidget = function (row, atBeginning) {
  246. if (self.currentlyDraggedWidget != null) {
  247. var _parentCol = self.getRowParentColumn(row.id());
  248. var _rowIdx = 0;
  249. $.each(_parentCol.rows(), function (i, irow) {
  250. if (irow.id() == row.id()) {
  251. _rowIdx = i;
  252. }
  253. });
  254. var _forkRow = _parentCol.addEmptyRow(false, _rowIdx);
  255. var _fork = new Widget({
  256. size: 12,
  257. id: UUID(),
  258. name: "fork",
  259. widgetType: "fork-widget",
  260. properties: {},
  261. offset: 0,
  262. loading: true,
  263. vm: self
  264. });
  265. _forkRow.widgets([_fork]);
  266. var _w = new Widget({
  267. size: self.currentlyDraggedWidget.size(),
  268. id: UUID(),
  269. name: self.currentlyDraggedWidget.name(),
  270. widgetType: self.currentlyDraggedWidget.widgetType(),
  271. properties: self.currentlyDraggedWidget.properties(),
  272. offset: self.currentlyDraggedWidget.offset(),
  273. loading: true,
  274. vm: self
  275. });
  276. var _col = row.addEmptyColumn(atBeginning);
  277. var _row = new Row([_w], self);
  278. _col.addRow(_row);
  279. var _joinRow = _parentCol.addEmptyRow(false, _rowIdx + 2);
  280. var _join = new Widget({
  281. size: 12,
  282. id: UUID(),
  283. name: "join",
  284. widgetType: "join-widget",
  285. properties: {},
  286. offset: 0,
  287. loading: true,
  288. vm: self
  289. });
  290. _joinRow.widgets([_join]);
  291. self.currentlyDraggedWidget = null;
  292. return _w;
  293. }
  294. }
  295. self.getWidgetById = function (widget_id) {
  296. var _widget = null;
  297. $.each(self.columns(), function (i, col) {
  298. $.each(col.rows(), function (j, row) {
  299. $.each(row.widgets(), function (z, widget) {
  300. if (widget.id() == widget_id){
  301. _widget = widget;
  302. return false;
  303. }
  304. });
  305. });
  306. });
  307. return _widget;
  308. }
  309. self.removeWidget = function (widget_json) {
  310. self.removeWidgetById(widget_json.id());
  311. }
  312. self.removeWidgetById = function (widget_id) {
  313. $.each(self.columns(), function (i, col) {
  314. self.deeplyRemoveWidgetById(widget_id, col, self)
  315. });
  316. }
  317. self.deeplyRemoveWidgetById = function (widget_id, col, parent) {
  318. if (col) {
  319. $.each(col.rows(), function (j, row) {
  320. if (row && row.widgets()){
  321. $.each(row.widgets(), function (z, widget) {
  322. if (widget.id() == widget_id) {
  323. row.widgets.remove(widget);
  324. col.rows.remove(row);
  325. }
  326. });
  327. }
  328. if (row && row.columns()) {
  329. $.each(row.columns(), function (i, icol) {
  330. self.deeplyRemoveWidgetById(widget_id, icol, row);
  331. });
  332. }
  333. });
  334. if (col.rows().length == 0) {
  335. parent.columns.remove(col);
  336. if (parent.columns().length > 1) {
  337. var _size = Math.max(1, Math.floor(12 / (parent.columns().length)));
  338. parent.columns().forEach(function (icol) {
  339. icol.size(_size);
  340. });
  341. }
  342. else {
  343. var _rows = parent.columns()[0].rows();
  344. var _parentRows = self.getRowParentColumn(parent.id()).rows;
  345. var _prevRowIdx = -1;
  346. for (var i = 0; i < _parentRows().length; i++) {
  347. if (_parentRows()[i].id() == parent.id()) {
  348. break;
  349. }
  350. _prevRowIdx = i;
  351. }
  352. if (_prevRowIdx > -1 && _parentRows()[_prevRowIdx].widgets().length > 0 && _parentRows()[_prevRowIdx].widgets()[0].widgetType() == "fork-widget"){
  353. _parentRows.remove(_parentRows()[_prevRowIdx]);
  354. _parentRows.remove(_parentRows()[_prevRowIdx+1]);
  355. }
  356. for (var i=0;i<_rows.length;i++){
  357. if (i==0){
  358. parent.widgets(_rows[i].widgets());
  359. }
  360. else {
  361. _parentRows.push(_rows[i]);
  362. }
  363. }
  364. parent.columns([]);
  365. }
  366. }
  367. }
  368. }
  369. self.getWidgetPredecessor = function (widget_id) {
  370. var _row = self.getWidgetParentRow(widget_id);
  371. var _col = self.getRowParentColumn(_row.id());
  372. var _prevRow = null;
  373. for (var i = 0; i < _col.rows().length; i++) {
  374. if (_col.rows()[i].id() == _row.id()) {
  375. break;
  376. }
  377. _prevRow = _col.rows()[i];
  378. }
  379. if (_prevRow != null) {
  380. return _prevRow.widgets()[0];
  381. }
  382. else {
  383. var _parentRow = self.getColumnParentRow(_col.id());
  384. var _parentColumn = self.getRowParentColumn(_parentRow.id());
  385. var _prevParentRow = null;
  386. for (var i = 0; i < _parentColumn.rows().length; i++) {
  387. if (_parentColumn.rows()[i].id() == _parentRow.id()) {
  388. break;
  389. }
  390. _prevParentRow = _parentColumn.rows()[i];
  391. }
  392. if (_prevParentRow != null) {
  393. return _prevParentRow.widgets()[0];
  394. }
  395. }
  396. return null;
  397. }
  398. self.getWidgetParentRow = function (widget_id) {
  399. var _row = null;
  400. for (var i = 0; i < self.columns().length; i++) {
  401. _row = self.traverseColumnForWidget(widget_id, self.columns()[i]);
  402. if (_row != null) {
  403. break;
  404. }
  405. }
  406. return _row;
  407. }
  408. self.getRowParentColumn = function (row_id) {
  409. var _column = null;
  410. for (var i = 0; i < self.columns().length; i++) {
  411. _column = self.traverseColumnForColumn(row_id, self.columns()[i]);
  412. }
  413. return _column;
  414. }
  415. self.getColumnParentRow = function (col_id) {
  416. var _row = null;
  417. for (var i = 0; i < self.columns().length; i++) {
  418. _row = self.traverseColumnForRow(col_id, self.columns()[i]);
  419. if (_row != null) {
  420. break;
  421. }
  422. }
  423. return _row;
  424. }
  425. self.getRowParentRow = function (row_id) {
  426. var _col = self.getRowParentColumn(row_id);
  427. if (_col != null) {
  428. return self.getColumnParentRow(_col.id());
  429. }
  430. }
  431. self.traverseColumnForColumn = function (row_id, col) {
  432. var _column = null;
  433. if (col) {
  434. for (var j = 0; j < col.rows().length; j++) {
  435. var row = col.rows()[j];
  436. if (row.id() == row_id) {
  437. _column = col;
  438. break;
  439. }
  440. for (var z = 0; z < row.columns().length; z++) {
  441. _column = self.traverseColumnForColumn(row_id, row.columns()[z]);
  442. if (_column != null) {
  443. break;
  444. }
  445. }
  446. }
  447. }
  448. return _column;
  449. }
  450. self.traverseColumnForRow = function (col_id, col) {
  451. var _row = null;
  452. if (col) {
  453. for (var j = 0; j < col.rows().length; j++) {
  454. var row = col.rows()[j];
  455. for (var z = 0; z < row.columns().length; z++) {
  456. var _col = row.columns()[z];
  457. if (_col.id() == col_id) {
  458. _row = row;
  459. }
  460. else {
  461. _row = self.traverseColumnForRow(col_id, _col);
  462. }
  463. if (_row != null) {
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. return _row;
  470. }
  471. self.traverseColumnForWidget = function (widget_id, col) {
  472. var _row = null;
  473. if (col) {
  474. for (var j = 0; j < col.rows().length; j++) {
  475. var row = col.rows()[j];
  476. for (var z = 0; z < row.widgets().length; z++) {
  477. var widget = row.widgets()[z];
  478. if (widget.id() == widget_id) {
  479. _row = row;
  480. break;
  481. }
  482. }
  483. for (var z = 0; z < row.columns().length; z++) {
  484. _row = self.traverseColumnForWidget(widget_id, row.columns()[z]);
  485. if (_row != null) {
  486. break;
  487. }
  488. }
  489. }
  490. }
  491. return _row;
  492. }
  493. self.save = function () {
  494. $.post("/oozie/editor/workflow/save/", {
  495. "layout": ko.mapping.toJSON(self.columns),
  496. "workflow": ko.mapping.toJSON(self.workflow)
  497. }, function (data) {
  498. if (data.status == 0) {
  499. self.workflow.id(data.id);
  500. $(document).trigger("info", data.message);
  501. if (window.location.search.indexOf("workflow") == -1) {
  502. window.location.hash = '#workflow=' + data.id;
  503. }
  504. }
  505. else {
  506. $(document).trigger("error", data.message);
  507. }
  508. }).fail(function (xhr, textStatus, errorThrown) {
  509. $(document).trigger("error", xhr.responseText);
  510. });
  511. };
  512. self.gen_xml = function () {
  513. $.post("/oozie/editor/workflow/gen_xml/", {
  514. "layout": ko.mapping.toJSON(self.columns),
  515. "workflow": ko.mapping.toJSON(self.workflow)
  516. }, function (data) {
  517. if (data.status == 0) {
  518. alert(data.xml);
  519. }
  520. else {
  521. $(document).trigger("error", data.message);
  522. }
  523. }).fail(function (xhr, textStatus, errorThrown) {
  524. $(document).trigger("error", xhr.responseText);
  525. });
  526. };
  527. self.showSubmitPopup = function () {
  528. // If self.workflow.id() == null, need to save wf for now
  529. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  530. }, function (data) {
  531. $(document).trigger("showSubmitPopup", data);
  532. }).fail(function (xhr, textStatus, errorThrown) {
  533. $(document).trigger("error", xhr.responseText);
  534. });
  535. };
  536. function bareWidgetBuilder(name, type){
  537. return new Widget({
  538. size: 12,
  539. id: UUID(),
  540. name: name,
  541. widgetType: type
  542. });
  543. }
  544. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  545. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  546. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  547. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  548. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  549. self.draggableStopNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  550. };