degrade.js 5.7 KB

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