workflow-editor.ko.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. function magicLayout(vm) {
  17. loadLayout(vm, vm.initial.layout);
  18. $(document).trigger("magicLayout");
  19. }
  20. function loadLayout(viewModel, json_layout) {
  21. var _columns = [];
  22. $(json_layout).each(function (cnt, json_col) {
  23. var _rows = [];
  24. $(json_col.rows).each(function (rcnt, json_row) {
  25. var row = new Row([], viewModel);
  26. $(json_row.widgets).each(function (wcnt, widget) {
  27. row.addWidget(new Widget({
  28. size:widget.size,
  29. id: widget.id,
  30. name: widget.name,
  31. widgetType: widget.widgetType,
  32. properties: widget.properties,
  33. offset: widget.offset,
  34. loading: true,
  35. vm: viewModel
  36. }));
  37. });
  38. _rows.push(row);
  39. });
  40. var column = new Column(json_col.size, _rows);
  41. _columns = _columns.concat(column);
  42. });
  43. viewModel.columns(_columns);
  44. }
  45. // End dashboard lib
  46. var Node = function (node) {
  47. var self = this;
  48. var type = typeof node.widgetType != "undefined" ? node.widgetType : node.type;
  49. self.id = ko.observable(typeof node.id != "undefined" && node.id != null ? node.id : UUID());
  50. self.name = ko.observable(typeof node.name != "undefined" && node.name != null ? node.name : "");
  51. self.type = ko.observable(typeof type != "undefined" && type != null ? type : "");
  52. self.properties = ko.mapping.fromJS(typeof node.properties != "undefined" && node.properties != null ? node.properties : {});
  53. self.children = ko.mapping.fromJS(typeof node.children != "undefined" && node.children != null ? node.children : []);
  54. }
  55. var Workflow = function (vm, workflow) {
  56. var self = this;
  57. self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : null);
  58. self.uuid = ko.observable(typeof workflow.uuid != "undefined" && workflow.uuid != null ? workflow.uuid : UUID());
  59. self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : "");
  60. self.properties = ko.mapping.fromJS(typeof workflow.properties != "undefined" && workflow.properties != null ? workflow.properties : {});
  61. self.nodes = ko.observableArray([]);
  62. self.loadNodes = function(workflow) {
  63. var nodes = []
  64. $.each(workflow.nodes, function(index, node) {
  65. var _node = new Node(node);
  66. nodes.push(_node);
  67. });
  68. self.nodes(nodes)
  69. }
  70. self.newNode = function(widget) {
  71. $.ajax({
  72. type: "POST",
  73. url: "/oozie/editor/workflow/new_node/",
  74. data: {
  75. "workflow": ko.mapping.toJSON(workflow),
  76. "node": ko.mapping.toJSON(widget)
  77. },
  78. success: function (data) {
  79. if (data.status == 0) {
  80. viewModel.addActionProperties(data.properties);
  81. viewModel.addActionWorkflows(data.workflows);
  82. }
  83. },
  84. async: false
  85. });
  86. };
  87. self.addNode = function(widget) {
  88. // Todo get parent cell, link nodes... when we have the new layout
  89. $.post("/oozie/editor/workflow/add_node/", {
  90. "workflow": ko.mapping.toJSON(workflow),
  91. "node": ko.mapping.toJSON(widget),
  92. "properties": ko.mapping.toJSON(viewModel.addActionProperties()),
  93. "subworkflow": viewModel.selectedSubWorkflow() ? ko.mapping.toJSON(viewModel.selectedSubWorkflow()) : '{}',
  94. }, function (data) {
  95. if (data.status == 0) {
  96. var _node = ko.mapping.toJS(widget);
  97. _node.properties = data.properties;
  98. _node.name = data.name;
  99. var node = new Node(_node);
  100. node.children().push({'to': '33430f0f-ebfa-c3ec-f237-3e77efa03d0a'}) // Link to child
  101. // Add to list of nodes
  102. var end = self.nodes.pop();
  103. self.nodes.push(node);
  104. self.nodes.push(end);
  105. self.nodes()[0].children.removeAll(); // Parent link to new node
  106. self.nodes()[0].children().push({'to': node.id()});
  107. } else {
  108. $(document).trigger("error", data.message);
  109. }
  110. }).fail(function (xhr, textStatus, errorThrown) {
  111. $(document).trigger("error", xhr.responseText);
  112. });
  113. };
  114. self.getNodeById = function (node_id) {
  115. var _node = null;
  116. $.each(self.nodes(), function (index, node) {
  117. if (node.id() == node_id) {
  118. _node = node;
  119. return false;
  120. }
  121. });
  122. return _node;
  123. }
  124. }
  125. var WorkflowEditorViewModel = function (layout_json, workflow_json, credentials_json) {
  126. var self = this;
  127. self.isEditing = ko.observable(true);
  128. self.toggleEditing = function () {
  129. self.isEditing(! self.isEditing());
  130. };
  131. self.columns = ko.observable([]);
  132. self.previewColumns = ko.observable("");
  133. self.workflow = new Workflow(self, workflow_json);
  134. self.credentials = ko.mapping.fromJSON(credentials_json);
  135. self.inited = ko.observable(self.columns().length > 0);
  136. self.init = function(callback) {
  137. loadLayout(self, layout_json);
  138. self.workflow.loadNodes(workflow_json);
  139. }
  140. self.depen = ko.observableArray(workflow_json.dependencies);
  141. self.addActionProperties = ko.observableArray([]);
  142. self.addActionWorkflows = ko.observableArray([]);
  143. self.selectedSubWorkflow = ko.observable();
  144. self.getWidgetById = function (widget_id) {
  145. var _widget = null;
  146. $.each(self.columns(), function (i, col) {
  147. $.each(col.rows(), function (j, row) {
  148. $.each(row.widgets(), function (z, widget) {
  149. if (widget.id() == widget_id){
  150. _widget = widget;
  151. return false;
  152. }
  153. });
  154. });
  155. });
  156. return _widget;
  157. }
  158. self.save = function () {
  159. $.post("/oozie/editor/workflow/save/", {
  160. "layout": ko.mapping.toJSON(self.columns),
  161. "workflow": ko.mapping.toJSON(self.workflow)
  162. }, function (data) {
  163. if (data.status == 0) {
  164. self.workflow.id(data.id);
  165. $(document).trigger("info", data.message);
  166. if (window.location.search.indexOf("workflow") == -1) {
  167. window.location.hash = '#workflow=' + data.id;
  168. }
  169. }
  170. else {
  171. $(document).trigger("error", data.message);
  172. }
  173. }).fail(function (xhr, textStatus, errorThrown) {
  174. $(document).trigger("error", xhr.responseText);
  175. });
  176. };
  177. self.gen_xml = function () {
  178. $.post("/oozie/editor/workflow/gen_xml/", {
  179. "layout": ko.mapping.toJSON(self.columns),
  180. "workflow": ko.mapping.toJSON(self.workflow)
  181. }, function (data) {
  182. if (data.status == 0) {
  183. alert(data.xml);
  184. }
  185. else {
  186. $(document).trigger("error", data.message);
  187. }
  188. }).fail(function (xhr, textStatus, errorThrown) {
  189. $(document).trigger("error", xhr.responseText);
  190. });
  191. };
  192. self.showSubmitPopup = function () {
  193. // If self.workflow.id() == null, need to save wf for now
  194. $.get("/oozie/editor/workflow/submit/" + self.workflow.id(), {
  195. }, function (data) {
  196. $(document).trigger("showSubmitPopup", data);
  197. }).fail(function (xhr, textStatus, errorThrown) {
  198. $(document).trigger("error", xhr.responseText);
  199. });
  200. };
  201. self.removeWidgetById = function (widget_id) {
  202. $.each(self.columns(), function (i, col) {
  203. $.each(col.rows(), function (j, row) {
  204. $.each(row.widgets(), function (z, widget) {
  205. if (widget.id() == widget_id){
  206. row.widgets.remove(widget);
  207. }
  208. });
  209. });
  210. });
  211. }
  212. function bareWidgetBuilder(name, type){
  213. return new Widget({
  214. size: 12,
  215. id: UUID(),
  216. name: name,
  217. widgetType: type
  218. });
  219. }
  220. self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget"));
  221. self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget"));
  222. self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget"));
  223. self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget"));
  224. self.draggableSubworkflowAction = ko.observable(bareWidgetBuilder("Sub workflow", "subworkflow-widget"));
  225. self.draggableForkNode = ko.observable(bareWidgetBuilder("Fork", "fork-widget"));
  226. self.draggableDecisionNode = ko.observable(bareWidgetBuilder("Decision", "decision-widget"));
  227. self.draggableStopNode = ko.observable(bareWidgetBuilder("Kill", "kill-widget"));
  228. };