workflow-editor.ko.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. // if was dragged on the sides ?
  157. // get neighbor
  158. // else
  159. // get parent
  160. var parentWidget = vm.getWidgetPredecessor(node.id());
  161. var parent = self.getNodeById(parentWidget.id());
  162. if (parentWidget.widgetType() == 'start-widget') {
  163. // Star node link to new node
  164. parent.set_link('to', node.id());
  165. // Link to end
  166. node.set_link('ok', '33430f0f-ebfa-c3ec-f237-3e77efa03d0a');
  167. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  168. } else if (parentWidget.widgetType() == 'pig-widget') {
  169. // Parent regular node
  170. node.set_link('ok', parent.get_link('ok')['ok']);
  171. node.set_link('error', '17c9c895-5a16-7443-bb81-f34b30b21548');
  172. parent.set_link('ok', node.id());
  173. }
  174. // Parent fork/decision/join...
  175. } else {
  176. $(document).trigger("error", data.message);
  177. }
  178. }).fail(function (xhr, textStatus, errorThrown) {
  179. $(document).trigger("error", xhr.responseText);
  180. });
  181. };
  182. self.getNodeById = function (node_id) {
  183. var _node = null;
  184. $.each(self.nodes(), function (index, node) {
  185. if (node.id() == node_id) {
  186. _node = node;
  187. return false;
  188. }
  189. });
  190. return _node;
  191. }
  192. }
  193. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json) {
  194. var self = this;
  195. self.isNested = ko.observable(true);
  196. self.isEditing = ko.observable(true);
  197. self.toggleEditing = function () {
  198. self.isEditing(! self.isEditing());
  199. };
  200. self.columns = ko.observable([]);
  201. self.previewColumns = ko.observable("");
  202. self.workflow = new Workflow(self, workflow_json);
  203. self.credentials = ko.mapping.fromJSON(credentials_json);
  204. self.inited = ko.observable(self.columns().length > 0);
  205. self.init = function(callback) {
  206. loadLayout(self, layout_json);
  207. self.workflow.loadNodes(workflow_json);
  208. }
  209. self.depen = ko.observableArray(workflow_json.dependencies);
  210. self.addActionProperties = ko.observableArray([]);
  211. self.addActionWorkflows = ko.observableArray([]);
  212. self.selectedSubWorkflow = ko.observable();
  213. self.currentlyDraggedWidget = null;
  214. self.isDragging = ko.observable(false);
  215. self.setCurrentDraggedWidget = function (widget) {
  216. self.currentlyDraggedWidget = widget;
  217. }
  218. self.addDraggedWidget = function (row, atBeginning) {
  219. if (self.currentlyDraggedWidget != null) {
  220. var _parentCol = self.getRowParentColumn(row.id());
  221. var _rowIdx = 0;
  222. $.each(_parentCol.rows(), function (i, irow) {
  223. if (irow.id() == row.id()) {
  224. _rowIdx = i;
  225. }
  226. });
  227. var _forkRow = _parentCol.addEmptyRow(false, _rowIdx);
  228. var _fork = new Widget({
  229. size: 12,
  230. id: UUID(),
  231. name: "fork",
  232. widgetType: "fork-widget",
  233. properties: {},
  234. offset: 0,
  235. loading: true,
  236. vm: self
  237. });
  238. _forkRow.widgets([_fork]);
  239. var _w = new Widget({
  240. size: self.currentlyDraggedWidget.size(),
  241. id: UUID(),
  242. name: self.currentlyDraggedWidget.name(),
  243. widgetType: self.currentlyDraggedWidget.widgetType(),
  244. properties: self.currentlyDraggedWidget.properties(),
  245. offset: self.currentlyDraggedWidget.offset(),
  246. loading: true,
  247. vm: self
  248. });
  249. var _col = row.addEmptyColumn(atBeginning);
  250. var _row = new Row([_w], self);
  251. _col.addRow(_row);
  252. var _joinRow = _parentCol.addEmptyRow(false, _rowIdx + 2);
  253. var _join = new Widget({
  254. size: 12,
  255. id: UUID(),
  256. name: "join",
  257. widgetType: "join-widget",
  258. properties: {},
  259. offset: 0,
  260. loading: true,
  261. vm: self
  262. });
  263. _joinRow.widgets([_join]);
  264. self.currentlyDraggedWidget = null;
  265. return _w;
  266. }
  267. }
  268. self.getWidgetById = function (widget_id) {
  269. var _widget = null;
  270. $.each(self.columns(), function (i, col) {
  271. $.each(col.rows(), function (j, row) {
  272. $.each(row.widgets(), function (z, widget) {
  273. if (widget.id() == widget_id){
  274. _widget = widget;
  275. return false;
  276. }
  277. });
  278. });
  279. });
  280. return _widget;
  281. }
  282. self.removeWidget = function (widget_json) {
  283. self.removeWidgetById(widget_json.id());
  284. }
  285. self.removeWidgetById = function (widget_id) {
  286. $.each(self.columns(), function (i, col) {
  287. self.deeplyRemoveWidgetById(widget_id, col, self)
  288. });
  289. }
  290. self.deeplyRemoveWidgetById = function (widget_id, col, parent) {
  291. if (col) {
  292. $.each(col.rows(), function (j, row) {
  293. if (row && row.widgets()){
  294. $.each(row.widgets(), function (z, widget) {
  295. if (widget.id() == widget_id) {
  296. row.widgets.remove(widget);
  297. col.rows.remove(row);
  298. }
  299. });
  300. }
  301. if (row && row.columns()) {
  302. $.each(row.columns(), function (i, icol) {
  303. self.deeplyRemoveWidgetById(widget_id, icol, row);
  304. });
  305. }
  306. });
  307. if (col.rows().length == 0) {
  308. parent.columns.remove(col);
  309. if (parent.columns().length > 1) {
  310. var _size = Math.max(1, Math.floor(12 / (parent.columns().length)));
  311. parent.columns().forEach(function (icol) {
  312. icol.size(_size);
  313. });
  314. }
  315. else {
  316. var _rows = parent.columns()[0].rows();
  317. for (var i=0;i<_rows.length;i++){
  318. if (i==0){
  319. parent.widgets(_rows[i].widgets());
  320. }
  321. else {
  322. self.getRowParentColumn(parent.id()).rows.push(_rows[i]);
  323. }
  324. }
  325. parent.columns([]);
  326. }
  327. }
  328. }
  329. }
  330. self.getWidgetPredecessor = function (widget_id) {
  331. var _row = self.getWidgetParentRow(widget_id);
  332. var _col = self.getRowParentColumn(_row.id());
  333. var _prevRow = null;
  334. for (var i = 0; i < _col.rows().length; i++) {
  335. if (_col.rows()[i].id() == _row.id()) {
  336. break;
  337. }
  338. _prevRow = _col.rows()[i];
  339. }
  340. if (_prevRow != null) {
  341. return _prevRow.widgets()[0];
  342. }
  343. else {
  344. var _parentRow = self.getColumnParentRow(_col.id());
  345. var _parentColumn = self.getRowParentColumn(_parentRow.id());
  346. var _prevParentRow = null;
  347. for (var i = 0; i < _parentColumn.rows().length; i++) {
  348. if (_parentColumn.rows()[i].id() == _parentRow.id()) {
  349. break;
  350. }
  351. _prevParentRow = _parentColumn.rows()[i];
  352. }
  353. if (_prevParentRow != null) {
  354. return _prevParentRow.widgets()[0];
  355. }
  356. }
  357. return null;
  358. }
  359. self.getWidgetParentRow = function (widget_id) {
  360. var _row = null;
  361. for (var i = 0; i < self.columns().length; i++) {
  362. _row = self.traverseColumnForWidget(widget_id, self.columns()[i]);
  363. if (_row != null) {
  364. break;
  365. }
  366. }
  367. return _row;
  368. }
  369. self.getRowParentColumn = function (row_id) {
  370. var _column = null;
  371. for (var i = 0; i < self.columns().length; i++) {
  372. _column = self.traverseColumnForColumn(row_id, self.columns()[i]);
  373. }
  374. return _column;
  375. }
  376. self.getColumnParentRow = function (col_id) {
  377. var _row = null;
  378. for (var i = 0; i < self.columns().length; i++) {
  379. _row = self.traverseColumnForRow(col_id, self.columns()[i]);
  380. if (_row != null) {
  381. break;
  382. }
  383. }
  384. return _row;
  385. }
  386. self.getRowParentRow = function (row_id) {
  387. var _col = self.getRowParentColumn(row_id);
  388. if (_col != null) {
  389. return self.getColumnParentRow(_col.id());
  390. }
  391. }
  392. self.traverseColumnForColumn = function (row_id, col) {
  393. var _column = null;
  394. if (col) {
  395. for (var j = 0; j < col.rows().length; j++) {
  396. var row = col.rows()[j];
  397. if (row.id() == row_id) {
  398. _column = col;
  399. break;
  400. }
  401. for (var z = 0; z < row.columns().length; z++) {
  402. _column = self.traverseColumnForColumn(row_id, row.columns()[z]);
  403. if (_column != null) {
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. return _column;
  410. }
  411. self.traverseColumnForRow = function (col_id, col) {
  412. var _row = null;
  413. if (col) {
  414. for (var j = 0; j < col.rows().length; j++) {
  415. var row = col.rows()[j];
  416. for (var z = 0; z < row.columns().length; z++) {
  417. var _col = row.columns()[z];
  418. if (_col.id() == col_id) {
  419. _row = row;
  420. }
  421. else {
  422. _row = self.traverseColumnForRow(col_id, _col);
  423. }
  424. if (_row != null) {
  425. break;
  426. }
  427. }
  428. }
  429. }
  430. return _row;
  431. }
  432. self.traverseColumnForWidget = function (widget_id, col) {
  433. var _row = null;
  434. if (col) {
  435. for (var j = 0; j < col.rows().length; j++) {
  436. var row = col.rows()[j];
  437. for (var z = 0; z < row.widgets().length; z++) {
  438. var widget = row.widgets()[z];
  439. if (widget.id() == widget_id) {
  440. _row = row;
  441. break;
  442. }
  443. }
  444. for (var z = 0; z < row.columns().length; z++) {
  445. _row = self.traverseColumnForWidget(widget_id, row.columns()[z]);
  446. if (_row != null) {
  447. break;
  448. }
  449. }
  450. }
  451. }
  452. return _row;
  453. }
  454. self.save = function () {
  455. $.post("/oozie/editor/workflow/save/", {
  456. "layout": ko.mapping.toJSON(self.columns),
  457. "workflow": ko.mapping.toJSON(self.workflow)
  458. }, function (data) {
  459. if (data.status == 0) {
  460. self.workflow.id(data.id);
  461. $(document).trigger("info", data.message);
  462. if (window.location.search.indexOf("workflow") == -1) {
  463. window.location.hash = '#workflow=' + data.id;
  464. }
  465. }
  466. else {
  467. $(document).trigger("error", data.message);
  468. }
  469. }).fail(function (xhr, textStatus, errorThrown) {
  470. $(document).trigger("error", xhr.responseText);
  471. });
  472. };
  473. self.gen_xml = function () {
  474. $.post("/oozie/editor/workflow/gen_xml/", {
  475. "layout": ko.mapping.toJSON(self.columns),
  476. "workflow": ko.mapping.toJSON(self.workflow)
  477. }, function (data) {
  478. if (data.status == 0) {
  479. alert(data.xml);
  480. }
  481. else {
  482. $(document).trigger("error", data.message);
  483. }
  484. }).fail(function (xhr, textStatus, errorThrown) {
  485. $(document).trigger("error", xhr.responseText);
  486. });
  487. };
  488. self.showSubmitPopup = function () {
  489. // If self.workflow.id() == null, need to save wf for now
  490. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  491. }, function (data) {
  492. $(document).trigger("showSubmitPopup", data);
  493. }).fail(function (xhr, textStatus, errorThrown) {
  494. $(document).trigger("error", xhr.responseText);
  495. });
  496. };
  497. function bareWidgetBuilder(name, type){
  498. return new Widget({
  499. size: 12,
  500. id: UUID(),
  501. name: name,
  502. widgetType: type
  503. });
  504. }
  505. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  506. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  507. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  508. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  509. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  510. self.draggableStopNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  511. };