coordinator-editor.ko.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var Coordinator = function (vm, coordinator) {
  17. var self = this;
  18. self.id = ko.observable(typeof coordinator.id != "undefined" && coordinator.id != null ? coordinator.id : null);
  19. self.uuid = ko.observable(typeof coordinator.uuid != "undefined" && coordinator.uuid != null ? coordinator.uuid : UUID());
  20. self.name = ko.observable(typeof coordinator.name != "undefined" && coordinator.name != null ? coordinator.name : "");
  21. self.properties = ko.mapping.fromJS(typeof coordinator.properties != "undefined" && coordinator.properties != null ? coordinator.properties : {});
  22. self.variables = ko.mapping.fromJS(typeof coordinator.variables != "undefined" && coordinator.variables != null ? coordinator.variables : []);
  23. self.variablesUI = ko.observableArray(['parameter', 'input_path', 'output_path']);
  24. self.showAdvancedFrequencyUI = ko.observable(typeof coordinator.showAdvancedFrequencyUI != "undefined" && coordinator.showAdvancedFrequencyUI != null ? coordinator.showAdvancedFrequencyUI : false);
  25. self.workflowParameters = ko.mapping.fromJS(typeof coordinator.workflowParameters != "undefined" && coordinator.workflowParameters != null ? coordinator.workflowParameters : []);
  26. self.properties.workflow.subscribe(function(newVal) {
  27. if (newVal) {
  28. $.get("/oozie/editor/workflow/parameters/", {
  29. "uuid": self.properties.workflow(),
  30. }, function (data) {
  31. self.workflowParameters(data.parameters);
  32. // Pre-add the variables
  33. $.each(data.parameters, function(index, param) {
  34. if (self.variables().length < data.parameters.length) {
  35. self.addVariable();
  36. }
  37. self.variables()[self.variables().length - 1].workflow_variable(param['name']);
  38. });
  39. }).fail(function (xhr, textStatus, errorThrown) {
  40. $(document).trigger("error", xhr.responseText);
  41. });
  42. }
  43. });
  44. self.properties.cron_advanced.subscribe(function(value) {
  45. if (value || ! vm.isEditing()) {
  46. coordCron.disable();
  47. } else {
  48. coordCron.enable();
  49. }
  50. });
  51. self.addVariable = function() {
  52. var _var = {
  53. 'workflow_variable': '', // Variable we want to fill in the workflow
  54. 'dataset_type': 'parameter',
  55. 'uuid': UUID(), // Dataset
  56. 'dataset_variable': '', // Aka property or URI
  57. 'show_advanced': false,
  58. 'use_done_flag': false,
  59. 'done_flag': '_SUCCESS',
  60. 'timezone': 'America/Los_Angeles',
  61. 'same_timezone': true,
  62. 'instance_choice': 'default',
  63. 'is_advanced_start_instance': false,
  64. 'start_instance': '0',
  65. 'advanced_start_instance': '${coord:current(0)}',
  66. 'is_advanced_end_instance': false,
  67. 'advanced_end_instance': '${coord:current(0)}',
  68. 'end_instance': '0',
  69. 'cron_frequency': '0 0 * * *',
  70. 'frequency_number': 1,
  71. 'frequency_unit': 'days',
  72. 'start': new Date(),
  73. 'same_start': true,
  74. 'shared_dataset_uuid': '' // If reusing a shared dataset
  75. };
  76. self.variables.push(ko.mapping.fromJS(_var));
  77. };
  78. }
  79. var CoordinatorEditorViewModel = function (coordinator_json, credentials_json, workflows_json, can_edit_json) {
  80. var self = this;
  81. self.canEdit = ko.mapping.fromJS(can_edit_json);
  82. self.isEditing = ko.observable(self.canEdit());
  83. self.isEditing.subscribe(function(newVal){
  84. $(document).trigger("editingToggled");
  85. self.coordinator.properties.cron_advanced.valueHasMutated();
  86. });
  87. self.toggleEditing = function () {
  88. self.isEditing(! self.isEditing());
  89. };
  90. self.workflows = ko.mapping.fromJS(workflows_json);
  91. self.coordinator = new Coordinator(self, coordinator_json);
  92. self.credentials = ko.mapping.fromJS(credentials_json);
  93. self.workflowModalFilter = ko.observable("");
  94. self.filteredModalWorkflows = ko.computed(function() {
  95. var _filter = self.workflowModalFilter().toLowerCase();
  96. if (! _filter) {
  97. return self.workflows();
  98. }
  99. else {
  100. return ko.utils.arrayFilter(self.workflows(), function(wf) {
  101. return wf.name().toLowerCase().indexOf(_filter.toLowerCase()) > -1;
  102. });
  103. }
  104. }, self);
  105. self.getWorkflowById = function (uuid) {
  106. var _wfs = ko.utils.arrayFilter(self.workflows(), function(wf) {
  107. return wf.uuid() == uuid;
  108. });
  109. if (_wfs.length > 0){
  110. return _wfs[0];
  111. }
  112. return null;
  113. }
  114. self.save = function () {
  115. $.post("/oozie/editor/coordinator/save/", {
  116. "coordinator": ko.mapping.toJSON(self.coordinator)
  117. }, function (data) {
  118. if (data.status == 0) {
  119. self.coordinator.id(data.id);
  120. $(document).trigger("info", data.message);
  121. if (window.location.search.indexOf("coordinator") == -1) {
  122. window.location.hash = '#coordinator=' + data.id;
  123. }
  124. }
  125. else {
  126. $(document).trigger("error", data.message);
  127. }
  128. }).fail(function (xhr, textStatus, errorThrown) {
  129. $(document).trigger("error", xhr.responseText);
  130. });
  131. };
  132. self.gen_xml = function () {
  133. $(".jHueNotify").hide();
  134. logGA('gen_xml');
  135. $.post("/oozie/editor/coordinator/gen_xml/", {
  136. "coordinator": ko.mapping.toJSON(self.coordinator)
  137. }, function (data) {
  138. if (data.status == 0) {
  139. console.log(data.xml);
  140. }
  141. else {
  142. $(document).trigger("error", data.message);
  143. }
  144. }).fail(function (xhr, textStatus, errorThrown) {
  145. $(document).trigger("error", xhr.responseText);
  146. });
  147. };
  148. self.showSubmitPopup = function () {
  149. $.get("/oozie/editor/coordinator/submit/" + self.coordinator.id(), {
  150. }, function (data) {
  151. $(document).trigger("showSubmitPopup", data);
  152. }).fail(function (xhr, textStatus, errorThrown) {
  153. $(document).trigger("error", xhr.responseText);
  154. });
  155. };
  156. };
  157. function logGA(page) {
  158. if (typeof trackOnGA == 'function') {
  159. trackOnGA('oozie/editor/coordinator/' + page);
  160. }
  161. }