flow_v2.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('FlowControllerV2', ['$scope', '$stateParams', 'FlowServiceV2', 'ngDialog',
  3. 'MachineService',
  4. function ($scope, $stateParams, FlowService, ngDialog,
  5. MachineService) {
  6. $scope.app = $stateParams.app;
  7. $scope.rulesPageConfig = {
  8. pageSize: 10,
  9. currentPageIndex: 1,
  10. totalPage: 1,
  11. totalCount: 0,
  12. };
  13. $scope.macsInputConfig = {
  14. searchField: ['text', 'value'],
  15. persist: true,
  16. create: false,
  17. maxItems: 1,
  18. render: {
  19. item: function (data, escape) {
  20. return '<div>' + escape(data.text) + '</div>';
  21. }
  22. },
  23. onChange: function (value, oldValue) {
  24. $scope.macInputModel = value;
  25. }
  26. };
  27. $scope.generateThresholdTypeShow = (rule) => {
  28. if (!rule.clusterMode) {
  29. return '单机';
  30. }
  31. if (rule.clusterConfig.thresholdType === 0) {
  32. return '集群均摊';
  33. } else if (rule.clusterConfig.thresholdType === 1) {
  34. return '集群总体';
  35. } else {
  36. return '集群';
  37. }
  38. };
  39. getMachineRules();
  40. function getMachineRules() {
  41. if (!$scope.macInputModel) {
  42. return;
  43. }
  44. var mac = $scope.macInputModel.split(':');
  45. FlowService.queryMachineRules($scope.app, mac[0], mac[1]).success(
  46. function (data) {
  47. if (data.code == 0 && data.data) {
  48. $scope.rules = data.data;
  49. $scope.rulesPageConfig.totalCount = $scope.rules.length;
  50. } else {
  51. $scope.rules = [];
  52. $scope.rulesPageConfig.totalCount = 0;
  53. }
  54. });
  55. };
  56. $scope.getMachineRules = getMachineRules;
  57. var flowRuleDialog;
  58. $scope.editRule = function (rule) {
  59. $scope.currentRule = angular.copy(rule);
  60. $scope.flowRuleDialog = {
  61. title: '编辑流控规则',
  62. type: 'edit',
  63. confirmBtnText: '保存',
  64. showAdvanceButton: rule.controlBehavior == 0 && rule.strategy == 0
  65. };
  66. flowRuleDialog = ngDialog.open({
  67. template: '/app/views/dialog/flow-rule-dialog.html',
  68. width: 680,
  69. overlay: true,
  70. scope: $scope
  71. });
  72. };
  73. $scope.addNewRule = function () {
  74. var mac = $scope.macInputModel.split(':');
  75. $scope.currentRule = {
  76. grade: 1,
  77. strategy: 0,
  78. controlBehavior: 0,
  79. app: $scope.app,
  80. ip: mac[0],
  81. port: mac[1],
  82. limitApp: 'default',
  83. clusterMode: false,
  84. clusterConfig: {
  85. thresholdType: 0,
  86. fallbackToLocalWhenFail: true
  87. }
  88. };
  89. $scope.flowRuleDialog = {
  90. title: '新增流控规则',
  91. type: 'add',
  92. confirmBtnText: '新增',
  93. showAdvanceButton: true,
  94. };
  95. flowRuleDialog = ngDialog.open({
  96. template: '/app/views/dialog/flow-rule-dialog.html',
  97. width: 680,
  98. overlay: true,
  99. scope: $scope
  100. });
  101. };
  102. $scope.saveRule = function () {
  103. if (!FlowService.checkRuleValid($scope.currentRule)) {
  104. return;
  105. }
  106. if ($scope.flowRuleDialog.type === 'add') {
  107. addNewRule($scope.currentRule);
  108. } else if ($scope.flowRuleDialog.type === 'edit') {
  109. saveRule($scope.currentRule, true);
  110. }
  111. };
  112. var confirmDialog;
  113. $scope.deleteRule = function (rule) {
  114. $scope.currentRule = rule;
  115. $scope.confirmDialog = {
  116. title: '删除流控规则',
  117. type: 'delete_rule',
  118. attentionTitle: '请确认是否删除如下流控规则',
  119. attention: '资源名: ' + rule.resource + ', 流控应用: ' + rule.limitApp
  120. + ', 阈值类型: ' + (rule.grade == 0 ? '线程数' : 'QPS') + ', 阈值: ' + rule.count,
  121. confirmBtnText: '删除',
  122. };
  123. confirmDialog = ngDialog.open({
  124. template: '/app/views/dialog/confirm-dialog.html',
  125. scope: $scope,
  126. overlay: true
  127. });
  128. };
  129. $scope.confirm = function () {
  130. if ($scope.confirmDialog.type === 'delete_rule') {
  131. deleteRule($scope.currentRule);
  132. } else {
  133. console.error('error');
  134. }
  135. };
  136. function deleteRule(rule) {
  137. FlowService.deleteRule(rule).success(function (data) {
  138. if (data.code == 0) {
  139. getMachineRules();
  140. confirmDialog.close();
  141. } else {
  142. alert('失败!');
  143. }
  144. });
  145. };
  146. function addNewRule(rule) {
  147. FlowService.newRule(rule).success(function (data) {
  148. if (data.code == 0) {
  149. getMachineRules();
  150. flowRuleDialog.close();
  151. } else {
  152. alert('失败!');
  153. }
  154. });
  155. };
  156. $scope.onOpenAdvanceClick = function () {
  157. $scope.flowRuleDialog.showAdvanceButton = false;
  158. };
  159. $scope.onCloseAdvanceClick = function () {
  160. $scope.flowRuleDialog.showAdvanceButton = true;
  161. };
  162. function saveRule(rule, edit) {
  163. FlowService.saveRule(rule).success(function (data) {
  164. if (data.code == 0) {
  165. getMachineRules();
  166. if (edit) {
  167. flowRuleDialog.close();
  168. } else {
  169. confirmDialog.close();
  170. }
  171. } else {
  172. alert('失败!');
  173. }
  174. });
  175. }
  176. queryAppMachines();
  177. function queryAppMachines() {
  178. MachineService.getAppMachines($scope.app).success(
  179. function (data) {
  180. if (data.code == 0) {
  181. // $scope.machines = data.data;
  182. if (data.data) {
  183. $scope.machines = [];
  184. $scope.macsInputOptions = [];
  185. data.data.forEach(function (item) {
  186. if (item.healthy) {
  187. $scope.macsInputOptions.push({
  188. text: item.ip + ':' + item.port,
  189. value: item.ip + ':' + item.port
  190. });
  191. }
  192. });
  193. }
  194. if ($scope.macsInputOptions.length > 0) {
  195. $scope.macInputModel = $scope.macsInputOptions[0].value;
  196. }
  197. } else {
  198. $scope.macsInputOptions = [];
  199. }
  200. }
  201. );
  202. };
  203. $scope.$watch('macInputModel', function () {
  204. if ($scope.macInputModel) {
  205. getMachineRules();
  206. }
  207. });
  208. }]);