identity.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('GatewayIdentityCtl', ['$scope', '$stateParams', 'IdentityService',
  3. 'ngDialog', 'GatewayFlowService', 'GatewayApiService', 'DegradeService', 'MachineService',
  4. '$interval', '$location', '$timeout',
  5. function ($scope, $stateParams, IdentityService, ngDialog,
  6. GatewayFlowService, GatewayApiService, DegradeService, MachineService, $interval, $location, $timeout) {
  7. $scope.app = $stateParams.app;
  8. $scope.currentPage = 1;
  9. $scope.pageSize = 16;
  10. $scope.totalPage = 1;
  11. $scope.totalCount = 0;
  12. $scope.identities = [];
  13. $scope.searchKey = '';
  14. $scope.macsInputConfig = {
  15. searchField: ['text', 'value'],
  16. persist: true,
  17. create: false,
  18. maxItems: 1,
  19. render: {
  20. item: function (data, escape) {
  21. return '<div>' + escape(data.text) + '</div>';
  22. }
  23. },
  24. onChange: function (value, oldValue) {
  25. $scope.macInputModel = value;
  26. }
  27. };
  28. $scope.table = null;
  29. getApiNames();
  30. function getApiNames() {
  31. if (!$scope.macInputModel) {
  32. return;
  33. }
  34. var mac = $scope.macInputModel.split(':');
  35. GatewayApiService.queryApis($scope.app, mac[0], mac[1]).success(
  36. function (data) {
  37. if (data.code == 0 && data.data) {
  38. $scope.apiNames = [];
  39. data.data.forEach(function (api) {
  40. $scope.apiNames.push(api["apiName"]);
  41. });
  42. }
  43. });
  44. }
  45. var gatewayFlowRuleDialog;
  46. var gatewayFlowRuleDialogScope;
  47. $scope.addNewGatewayFlowRule = function (resource) {
  48. if (!$scope.macInputModel) {
  49. return;
  50. }
  51. var mac = $scope.macInputModel.split(':');
  52. gatewayFlowRuleDialogScope = $scope.$new(true);
  53. gatewayFlowRuleDialogScope.apiNames = $scope.apiNames;
  54. gatewayFlowRuleDialogScope.intervalUnits = [{val: 0, desc: '秒'}, {val: 1, desc: '分'}, {val: 2, desc: '时'}, {val: 3, desc: '天'}];
  55. gatewayFlowRuleDialogScope.currentRule = {
  56. grade: 1,
  57. app: $scope.app,
  58. ip: mac[0],
  59. port: mac[1],
  60. resourceMode: gatewayFlowRuleDialogScope.apiNames.indexOf(resource) == -1 ? 0 : 1,
  61. resource: resource,
  62. interval: 1,
  63. intervalUnit: 0,
  64. controlBehavior: 0,
  65. burst: 0,
  66. maxQueueingTimeoutMs: 0
  67. };
  68. gatewayFlowRuleDialogScope.gatewayFlowRuleDialog = {
  69. title: '新增网关流控规则',
  70. type: 'add',
  71. confirmBtnText: '新增',
  72. saveAndContinueBtnText: '新增并继续添加',
  73. showAdvanceButton: true
  74. };
  75. gatewayFlowRuleDialogScope.useRouteID = function() {
  76. gatewayFlowRuleDialogScope.currentRule.resource = '';
  77. };
  78. gatewayFlowRuleDialogScope.useCustormAPI = function() {
  79. gatewayFlowRuleDialogScope.currentRule.resource = '';
  80. };
  81. gatewayFlowRuleDialogScope.useParamItem = function () {
  82. gatewayFlowRuleDialogScope.currentRule.paramItem = {
  83. parseStrategy: 0,
  84. matchStrategy: 0
  85. };
  86. };
  87. gatewayFlowRuleDialogScope.notUseParamItem = function () {
  88. gatewayFlowRuleDialogScope.currentRule.paramItem = null;
  89. };
  90. gatewayFlowRuleDialogScope.useParamItemVal = function() {
  91. gatewayFlowRuleDialogScope.currentRule.paramItem.pattern = "";
  92. };
  93. gatewayFlowRuleDialogScope.notUseParamItemVal = function() {
  94. gatewayFlowRuleDialogScope.currentRule.paramItem.pattern = null;
  95. };
  96. gatewayFlowRuleDialogScope.saveRule = saveGatewayFlowRule;
  97. gatewayFlowRuleDialogScope.saveRuleAndContinue = saveGatewayFlowRuleAndContinue;
  98. gatewayFlowRuleDialogScope.onOpenAdvanceClick = function () {
  99. gatewayFlowRuleDialogScope.gatewayFlowRuleDialog.showAdvanceButton = false;
  100. };
  101. gatewayFlowRuleDialogScope.onCloseAdvanceClick = function () {
  102. gatewayFlowRuleDialogScope.gatewayFlowRuleDialog.showAdvanceButton = true;
  103. };
  104. gatewayFlowRuleDialog = ngDialog.open({
  105. template: '/app/views/dialog/gateway/flow-rule-dialog.html',
  106. width: 780,
  107. overlay: true,
  108. scope: gatewayFlowRuleDialogScope
  109. });
  110. };
  111. function saveGatewayFlowRule() {
  112. if (!GatewayFlowService.checkRuleValid(gatewayFlowRuleDialogScope.currentRule)) {
  113. return;
  114. }
  115. GatewayFlowService.newRule(gatewayFlowRuleDialogScope.currentRule).success(function (data) {
  116. if (data.code === 0) {
  117. gatewayFlowRuleDialog.close();
  118. let url = '/dashboard/gateway/flow/' + $scope.app;
  119. $location.path(url);
  120. } else {
  121. alert('失败!');
  122. }
  123. }).error((data, header, config, status) => {
  124. alert('未知错误');
  125. });
  126. }
  127. function saveGatewayFlowRuleAndContinue() {
  128. if (!GatewayFlowService.checkRuleValid(gatewayFlowRuleDialogScope.currentRule)) {
  129. return;
  130. }
  131. GatewayFlowService.newRule(gatewayFlowRuleDialogScope.currentRule).success(function (data) {
  132. if (data.code == 0) {
  133. gatewayFlowRuleDialog.close();
  134. } else {
  135. alert('失败!');
  136. }
  137. });
  138. }
  139. var degradeRuleDialog;
  140. $scope.addNewDegradeRule = function (resource) {
  141. if (!$scope.macInputModel) {
  142. return;
  143. }
  144. var mac = $scope.macInputModel.split(':');
  145. degradeRuleDialogScope = $scope.$new(true);
  146. degradeRuleDialogScope.currentRule = {
  147. enable: false,
  148. grade: 0,
  149. strategy: 0,
  150. resource: resource,
  151. limitApp: 'default',
  152. app: $scope.app,
  153. ip: mac[0],
  154. port: mac[1]
  155. };
  156. degradeRuleDialogScope.degradeRuleDialog = {
  157. title: '新增降级规则',
  158. type: 'add',
  159. confirmBtnText: '新增',
  160. saveAndContinueBtnText: '新增并继续添加'
  161. };
  162. degradeRuleDialogScope.saveRule = saveDegradeRule;
  163. degradeRuleDialogScope.saveRuleAndContinue = saveDegradeRuleAndContinue;
  164. degradeRuleDialog = ngDialog.open({
  165. template: '/app/views/dialog/degrade-rule-dialog.html',
  166. width: 680,
  167. overlay: true,
  168. scope: degradeRuleDialogScope
  169. });
  170. };
  171. function saveDegradeRule() {
  172. if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
  173. return;
  174. }
  175. DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
  176. if (data.code == 0) {
  177. degradeRuleDialog.close();
  178. var url = '/dashboard/degrade/' + $scope.app;
  179. $location.path(url);
  180. } else {
  181. alert('失败!');
  182. }
  183. });
  184. }
  185. function saveDegradeRuleAndContinue() {
  186. if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
  187. return;
  188. }
  189. DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
  190. if (data.code == 0) {
  191. degradeRuleDialog.close();
  192. } else {
  193. alert('失败!');
  194. }
  195. });
  196. }
  197. var searchHandler;
  198. $scope.searchChange = function (searchKey) {
  199. $timeout.cancel(searchHandler);
  200. searchHandler = $timeout(function () {
  201. $scope.searchKey = searchKey;
  202. reInitIdentityDatas();
  203. }, 600);
  204. };
  205. function queryAppMachines() {
  206. MachineService.getAppMachines($scope.app).success(
  207. function (data) {
  208. if (data.code === 0) {
  209. if (data.data) {
  210. $scope.machines = [];
  211. $scope.macsInputOptions = [];
  212. data.data.forEach(function (item) {
  213. if (item.healthy) {
  214. $scope.macsInputOptions.push({
  215. text: item.ip + ':' + item.port,
  216. value: item.ip + ':' + item.port
  217. });
  218. }
  219. });
  220. }
  221. if ($scope.macsInputOptions.length > 0) {
  222. $scope.macInputModel = $scope.macsInputOptions[0].value;
  223. }
  224. } else {
  225. $scope.macsInputOptions = [];
  226. }
  227. }
  228. );
  229. }
  230. // Fetch all machines by current app name.
  231. queryAppMachines();
  232. $scope.$watch('macInputModel', function () {
  233. if ($scope.macInputModel) {
  234. reInitIdentityDatas();
  235. }
  236. });
  237. $scope.$on('$destroy', function () {
  238. $interval.cancel(intervalId);
  239. });
  240. var intervalId;
  241. function reInitIdentityDatas() {
  242. getApiNames();
  243. queryIdentities();
  244. };
  245. function queryIdentities() {
  246. var mac = $scope.macInputModel.split(':');
  247. if (mac == null || mac.length < 2) {
  248. return;
  249. }
  250. IdentityService.fetchClusterNodeOfMachine(mac[0], mac[1], $scope.searchKey).success(
  251. function (data) {
  252. if (data.code == 0 && data.data) {
  253. $scope.identities = data.data;
  254. $scope.totalCount = $scope.identities.length;
  255. } else {
  256. $scope.identities = [];
  257. $scope.totalCount = 0;
  258. }
  259. }
  260. );
  261. };
  262. $scope.queryIdentities = queryIdentities;
  263. }]);