flow.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('GatewayFlowCtl', ['$scope', '$stateParams', 'GatewayFlowService', 'GatewayApiService', 'ngDialog', 'MachineService',
  3. function ($scope, $stateParams, GatewayFlowService, GatewayApiService, ngDialog, MachineService) {
  4. $scope.app = $stateParams.app;
  5. $scope.rulesPageConfig = {
  6. pageSize: 10,
  7. currentPageIndex: 1,
  8. totalPage: 1,
  9. totalCount: 0,
  10. };
  11. $scope.macsInputConfig = {
  12. searchField: ['text', 'value'],
  13. persist: true,
  14. create: false,
  15. maxItems: 1,
  16. render: {
  17. item: function (data, escape) {
  18. return '<div>' + escape(data.text) + '</div>';
  19. }
  20. },
  21. onChange: function (value, oldValue) {
  22. $scope.macInputModel = value;
  23. }
  24. };
  25. getMachineRules();
  26. function getMachineRules() {
  27. if (!$scope.macInputModel) {
  28. return;
  29. }
  30. var mac = $scope.macInputModel.split(':');
  31. GatewayFlowService.queryRules($scope.app, mac[0], mac[1]).success(
  32. function (data) {
  33. if (data.code == 0 && data.data) {
  34. $scope.rules = data.data;
  35. $scope.rulesPageConfig.totalCount = $scope.rules.length;
  36. } else {
  37. $scope.rules = [];
  38. $scope.rulesPageConfig.totalCount = 0;
  39. }
  40. });
  41. };
  42. $scope.getMachineRules = getMachineRules;
  43. getApiNames();
  44. function getApiNames() {
  45. if (!$scope.macInputModel) {
  46. return;
  47. }
  48. var mac = $scope.macInputModel.split(':');
  49. GatewayApiService.queryApis($scope.app, mac[0], mac[1]).success(
  50. function (data) {
  51. if (data.code == 0 && data.data) {
  52. $scope.apiNames = [];
  53. data.data.forEach(function (api) {
  54. $scope.apiNames.push(api["apiName"]);
  55. });
  56. }
  57. });
  58. }
  59. $scope.intervalUnits = [{val: 0, desc: '秒'}, {val: 1, desc: '分'}, {val: 2, desc: '时'}, {val: 3, desc: '天'}];
  60. var gatewayFlowRuleDialog;
  61. $scope.editRule = function (rule) {
  62. $scope.currentRule = angular.copy(rule);
  63. $scope.gatewayFlowRuleDialog = {
  64. title: '编辑网关流控规则',
  65. type: 'edit',
  66. confirmBtnText: '保存'
  67. };
  68. gatewayFlowRuleDialog = ngDialog.open({
  69. template: '/app/views/dialog/gateway/flow-rule-dialog.html',
  70. width: 780,
  71. overlay: true,
  72. scope: $scope
  73. });
  74. };
  75. $scope.addNewRule = function () {
  76. var mac = $scope.macInputModel.split(':');
  77. $scope.currentRule = {
  78. grade: 1,
  79. app: $scope.app,
  80. ip: mac[0],
  81. port: mac[1],
  82. resourceMode: 0,
  83. interval: 1,
  84. intervalUnit: 0,
  85. controlBehavior: 0,
  86. burst: 0,
  87. maxQueueingTimeoutMs: 0
  88. };
  89. $scope.gatewayFlowRuleDialog = {
  90. title: '新增网关流控规则',
  91. type: 'add',
  92. confirmBtnText: '新增'
  93. };
  94. gatewayFlowRuleDialog = ngDialog.open({
  95. template: '/app/views/dialog/gateway/flow-rule-dialog.html',
  96. width: 780,
  97. overlay: true,
  98. scope: $scope
  99. });
  100. };
  101. $scope.saveRule = function () {
  102. if (!GatewayFlowService.checkRuleValid($scope.currentRule)) {
  103. return;
  104. }
  105. if ($scope.gatewayFlowRuleDialog.type === 'add') {
  106. addNewRule($scope.currentRule);
  107. } else if ($scope.gatewayFlowRuleDialog.type === 'edit') {
  108. saveRule($scope.currentRule, true);
  109. }
  110. };
  111. $scope.useRouteID = function() {
  112. $scope.currentRule.resource = '';
  113. };
  114. $scope.useCustormAPI = function() {
  115. $scope.currentRule.resource = '';
  116. };
  117. $scope.useParamItem = function () {
  118. $scope.currentRule.paramItem = {
  119. parseStrategy: 0,
  120. matchStrategy: 0
  121. };
  122. };
  123. $scope.notUseParamItem = function () {
  124. $scope.currentRule.paramItem = null;
  125. };
  126. $scope.useParamItemVal = function() {
  127. $scope.currentRule.paramItem.pattern = "";
  128. $scope.currentRule.paramItem.matchStrategy = 0;
  129. };
  130. $scope.notUseParamItemVal = function() {
  131. $scope.currentRule.paramItem.pattern = null;
  132. $scope.currentRule.paramItem.matchStrategy = null;
  133. };
  134. function addNewRule(rule) {
  135. GatewayFlowService.newRule(rule).success(function (data) {
  136. if (data.code == 0) {
  137. getMachineRules();
  138. gatewayFlowRuleDialog.close();
  139. } else {
  140. alert('新增网关流控规则失败!' + data.msg);
  141. }
  142. });
  143. };
  144. function saveRule(rule, edit) {
  145. GatewayFlowService.saveRule(rule).success(function (data) {
  146. if (data.code == 0) {
  147. getMachineRules();
  148. if (edit) {
  149. gatewayFlowRuleDialog.close();
  150. } else {
  151. confirmDialog.close();
  152. }
  153. } else {
  154. alert('修改网关流控规则失败!' + data.msg);
  155. }
  156. });
  157. };
  158. var confirmDialog;
  159. $scope.deleteRule = function (rule) {
  160. $scope.currentRule = rule;
  161. $scope.confirmDialog = {
  162. title: '删除网关流控规则',
  163. type: 'delete_rule',
  164. attentionTitle: '请确认是否删除如下规则',
  165. attention: 'API名称: ' + rule.resource + ', ' + (rule.grade == 1 ? 'QPS阈值' : '线程数') + ': ' + rule.count,
  166. confirmBtnText: '删除',
  167. };
  168. confirmDialog = ngDialog.open({
  169. template: '/app/views/dialog/confirm-dialog.html',
  170. scope: $scope,
  171. overlay: true
  172. });
  173. };
  174. $scope.confirm = function () {
  175. if ($scope.confirmDialog.type == 'delete_rule') {
  176. deleteRule($scope.currentRule);
  177. } else {
  178. console.error('error');
  179. }
  180. };
  181. function deleteRule(rule) {
  182. GatewayFlowService.deleteRule(rule).success(function (data) {
  183. if (data.code == 0) {
  184. getMachineRules();
  185. confirmDialog.close();
  186. } else {
  187. alert('删除网关流控规则失败!' + data.msg);
  188. }
  189. });
  190. };
  191. queryAppMachines();
  192. function queryAppMachines() {
  193. MachineService.getAppMachines($scope.app).success(
  194. function (data) {
  195. if (data.code == 0) {
  196. if (data.data) {
  197. $scope.machines = [];
  198. $scope.macsInputOptions = [];
  199. data.data.forEach(function (item) {
  200. if (item.healthy) {
  201. $scope.macsInputOptions.push({
  202. text: item.ip + ':' + item.port,
  203. value: item.ip + ':' + item.port
  204. });
  205. }
  206. });
  207. }
  208. if ($scope.macsInputOptions.length > 0) {
  209. $scope.macInputModel = $scope.macsInputOptions[0].value;
  210. }
  211. } else {
  212. $scope.macsInputOptions = [];
  213. }
  214. }
  215. );
  216. };
  217. $scope.$watch('macInputModel', function () {
  218. if ($scope.macInputModel) {
  219. getMachineRules();
  220. getApiNames();
  221. }
  222. });
  223. }]
  224. );