flow_v1.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('FlowControllerV1', ['$scope', '$stateParams', 'FlowServiceV1', '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. }
  87. };
  88. $scope.flowRuleDialog = {
  89. title: '新增流控规则',
  90. type: 'add',
  91. confirmBtnText: '新增',
  92. showAdvanceButton: true,
  93. };
  94. flowRuleDialog = ngDialog.open({
  95. template: '/app/views/dialog/flow-rule-dialog.html',
  96. width: 680,
  97. overlay: true,
  98. scope: $scope
  99. });
  100. };
  101. $scope.saveRule = function () {
  102. if (!FlowService.checkRuleValid($scope.currentRule)) {
  103. return;
  104. }
  105. if ($scope.flowRuleDialog.type === 'add') {
  106. addNewRule($scope.currentRule);
  107. } else if ($scope.flowRuleDialog.type === 'edit') {
  108. saveRule($scope.currentRule, true);
  109. }
  110. };
  111. var confirmDialog;
  112. $scope.deleteRule = function (rule) {
  113. $scope.currentRule = rule;
  114. $scope.confirmDialog = {
  115. title: '删除流控规则',
  116. type: 'delete_rule',
  117. attentionTitle: '请确认是否删除如下流控规则',
  118. attention: '资源名: ' + rule.resource + ', 流控应用: ' + rule.limitApp
  119. + ', 阈值类型: ' + (rule.grade == 0 ? '线程数' : 'QPS') + ', 阈值: ' + rule.count,
  120. confirmBtnText: '删除',
  121. };
  122. confirmDialog = ngDialog.open({
  123. template: '/app/views/dialog/confirm-dialog.html',
  124. scope: $scope,
  125. overlay: true
  126. });
  127. };
  128. $scope.confirm = function () {
  129. if ($scope.confirmDialog.type === 'delete_rule') {
  130. deleteRule($scope.currentRule);
  131. } else {
  132. console.error('error');
  133. }
  134. };
  135. function deleteRule(rule) {
  136. FlowService.deleteRule(rule).success(function (data) {
  137. if (data.code == 0) {
  138. getMachineRules();
  139. confirmDialog.close();
  140. } else {
  141. alert('失败:' + data.msg);
  142. }
  143. });
  144. };
  145. function addNewRule(rule) {
  146. FlowService.newRule(rule).success(function (data) {
  147. if (data.code === 0) {
  148. getMachineRules();
  149. flowRuleDialog.close();
  150. } else {
  151. alert('失败:' + data.msg);
  152. }
  153. });
  154. };
  155. $scope.onOpenAdvanceClick = function () {
  156. $scope.flowRuleDialog.showAdvanceButton = false;
  157. };
  158. $scope.onCloseAdvanceClick = function () {
  159. $scope.flowRuleDialog.showAdvanceButton = true;
  160. };
  161. function saveRule(rule, edit) {
  162. FlowService.saveRule(rule).success(function (data) {
  163. if (data.code === 0) {
  164. getMachineRules();
  165. if (edit) {
  166. flowRuleDialog.close();
  167. } else {
  168. confirmDialog.close();
  169. }
  170. } else {
  171. alert('失败:' + data.msg);
  172. }
  173. });
  174. }
  175. queryAppMachines();
  176. function queryAppMachines() {
  177. MachineService.getAppMachines($scope.app).success(
  178. function (data) {
  179. if (data.code == 0) {
  180. // $scope.machines = data.data;
  181. if (data.data) {
  182. $scope.machines = [];
  183. $scope.macsInputOptions = [];
  184. data.data.forEach(function (item) {
  185. if (item.healthy) {
  186. $scope.macsInputOptions.push({
  187. text: item.ip + ':' + item.port,
  188. value: item.ip + ':' + item.port
  189. });
  190. }
  191. });
  192. }
  193. if ($scope.macsInputOptions.length > 0) {
  194. $scope.macInputModel = $scope.macsInputOptions[0].value;
  195. }
  196. } else {
  197. $scope.macsInputOptions = [];
  198. }
  199. }
  200. );
  201. };
  202. $scope.$watch('macInputModel', function () {
  203. if ($scope.macInputModel) {
  204. getMachineRules();
  205. }
  206. });
  207. }]);