identity.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('IdentityCtl', ['$scope', '$stateParams', 'IdentityService',
  3. 'ngDialog',
  4. // 'FlowServiceV1',
  5. 'FlowServiceV2',
  6. 'DegradeService', 'AuthorityRuleService', 'ParamFlowService', 'MachineService',
  7. '$interval', '$location', '$timeout',
  8. function ($scope, $stateParams, IdentityService, ngDialog,
  9. FlowService, DegradeService, AuthorityRuleService, ParamFlowService, MachineService, $interval, $location, $timeout) {
  10. $scope.app = $stateParams.app;
  11. $scope.currentPage = 1;
  12. $scope.pageSize = 16;
  13. $scope.totalPage = 1;
  14. $scope.totalCount = 0;
  15. $scope.identities = [];
  16. // 数据自动刷新频率, 默认10s
  17. var DATA_REFRESH_INTERVAL = 30;
  18. $scope.isExpand = true;
  19. $scope.searchKey = '';
  20. $scope.firstExpandAll = false;
  21. $scope.isTreeView = true;
  22. $scope.macsInputConfig = {
  23. searchField: ['text', 'value'],
  24. persist: true,
  25. create: false,
  26. maxItems: 1,
  27. render: {
  28. item: function (data, escape) {
  29. return '<div>' + escape(data.text) + '</div>';
  30. }
  31. },
  32. onChange: function (value, oldValue) {
  33. $scope.macInputModel = value;
  34. }
  35. };
  36. $scope.table = null;
  37. var flowRuleDialog;
  38. var flowRuleDialogScope;
  39. $scope.addNewFlowRule = function (resource) {
  40. if (!$scope.macInputModel) {
  41. return;
  42. }
  43. var mac = $scope.macInputModel.split(':');
  44. flowRuleDialogScope = $scope.$new(true);
  45. flowRuleDialogScope.currentRule = {
  46. enable: false,
  47. strategy: 0,
  48. grade: 1,
  49. controlBehavior: 0,
  50. resource: resource,
  51. limitApp: 'default',
  52. clusterMode: false,
  53. clusterConfig: {
  54. thresholdType: 0
  55. },
  56. app: $scope.app,
  57. ip: mac[0],
  58. port: mac[1]
  59. };
  60. flowRuleDialogScope.flowRuleDialog = {
  61. title: '新增流控规则',
  62. type: 'add',
  63. confirmBtnText: '新增',
  64. saveAndContinueBtnText: '新增并继续添加',
  65. showAdvanceButton: true
  66. };
  67. // $scope.flowRuleDialog = {
  68. // showAdvanceButton : true
  69. // };
  70. flowRuleDialogScope.saveRule = saveFlowRule;
  71. flowRuleDialogScope.saveRuleAndContinue = saveFlowRuleAndContinue;
  72. flowRuleDialogScope.onOpenAdvanceClick = function () {
  73. flowRuleDialogScope.flowRuleDialog.showAdvanceButton = false;
  74. };
  75. flowRuleDialogScope.onCloseAdvanceClick = function () {
  76. flowRuleDialogScope.flowRuleDialog.showAdvanceButton = true;
  77. };
  78. flowRuleDialog = ngDialog.open({
  79. template: '/app/views/dialog/flow-rule-dialog.html',
  80. width: 680,
  81. overlay: true,
  82. scope: flowRuleDialogScope
  83. });
  84. };
  85. function saveFlowRule() {
  86. if (!FlowService.checkRuleValid(flowRuleDialogScope.currentRule)) {
  87. return;
  88. }
  89. FlowService.newRule(flowRuleDialogScope.currentRule).success(function (data) {
  90. if (data.code === 0) {
  91. flowRuleDialog.close();
  92. // let url = '/dashboard/flow/' + $scope.app;
  93. let url = '/dashboard/v2/flow/' + $scope.app;
  94. $location.path(url);
  95. } else {
  96. alert('失败:' + data.msg);
  97. }
  98. }).error((data, header, config, status) => {
  99. alert('未知错误');
  100. });
  101. }
  102. function saveFlowRuleAndContinue() {
  103. if (!FlowService.checkRuleValid(flowRuleDialogScope.currentRule)) {
  104. return;
  105. }
  106. FlowService.newRule(flowRuleDialogScope.currentRule).success(function (data) {
  107. if (data.code === 0) {
  108. flowRuleDialog.close();
  109. } else {
  110. alert('失败:' + data.msg);
  111. }
  112. });
  113. }
  114. var degradeRuleDialog;
  115. var degradeRuleDialogScope;
  116. $scope.addNewDegradeRule = function (resource) {
  117. if (!$scope.macInputModel) {
  118. return;
  119. }
  120. var mac = $scope.macInputModel.split(':');
  121. degradeRuleDialogScope = $scope.$new(true);
  122. degradeRuleDialogScope.currentRule = {
  123. enable: false,
  124. grade: 0,
  125. strategy: 0,
  126. resource: resource,
  127. limitApp: 'default',
  128. minRequestAmount: 5,
  129. statIntervalMs: 1000,
  130. app: $scope.app,
  131. ip: mac[0],
  132. port: mac[1]
  133. };
  134. degradeRuleDialogScope.degradeRuleDialog = {
  135. title: '新增熔断规则',
  136. type: 'add',
  137. confirmBtnText: '新增',
  138. saveAndContinueBtnText: '新增并继续添加'
  139. };
  140. degradeRuleDialogScope.saveRule = saveDegradeRule;
  141. degradeRuleDialogScope.saveRuleAndContinue = saveDegradeRuleAndContinue;
  142. degradeRuleDialog = ngDialog.open({
  143. template: '/app/views/dialog/degrade-rule-dialog.html',
  144. width: 680,
  145. overlay: true,
  146. scope: degradeRuleDialogScope
  147. });
  148. };
  149. function saveDegradeRule() {
  150. if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
  151. return;
  152. }
  153. DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
  154. if (data.code === 0) {
  155. degradeRuleDialog.close();
  156. var url = '/dashboard/degrade/' + $scope.app;
  157. $location.path(url);
  158. } else {
  159. alert('失败:' + data.msg);
  160. }
  161. });
  162. }
  163. function saveDegradeRuleAndContinue() {
  164. if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
  165. return;
  166. }
  167. DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
  168. if (data.code === 0) {
  169. degradeRuleDialog.close();
  170. } else {
  171. alert('失败:' + data.msg);
  172. }
  173. });
  174. }
  175. let authorityRuleDialog;
  176. let authorityRuleDialogScope;
  177. function saveAuthorityRule() {
  178. let ruleEntity = authorityRuleDialogScope.currentRule;
  179. if (!AuthorityRuleService.checkRuleValid(ruleEntity.rule)) {
  180. return;
  181. }
  182. AuthorityRuleService.addNewRule(ruleEntity).success((data) => {
  183. if (data.success) {
  184. authorityRuleDialog.close();
  185. let url = '/dashboard/authority/' + $scope.app;
  186. $location.path(url);
  187. } else {
  188. alert('添加规则失败:' + data.msg);
  189. }
  190. }).error((data) => {
  191. if (data) {
  192. alert('添加规则失败:' + data.msg);
  193. } else {
  194. alert("添加规则失败:未知错误");
  195. }
  196. });
  197. }
  198. function saveAuthorityRuleAndContinue() {
  199. let ruleEntity = authorityRuleDialogScope.currentRule;
  200. if (!AuthorityRuleService.checkRuleValid(ruleEntity.rule)) {
  201. return;
  202. }
  203. AuthorityRuleService.addNewRule(ruleEntity).success((data) => {
  204. if (data.success) {
  205. authorityRuleDialog.close();
  206. } else {
  207. alert('添加规则失败:' + data.msg);
  208. }
  209. }).error((data) => {
  210. if (data) {
  211. alert('添加规则失败:' + data.msg);
  212. } else {
  213. alert("添加规则失败:未知错误");
  214. }
  215. });
  216. }
  217. $scope.addNewAuthorityRule = function (resource) {
  218. if (!$scope.macInputModel) {
  219. return;
  220. }
  221. let mac = $scope.macInputModel.split(':');
  222. authorityRuleDialogScope = $scope.$new(true);
  223. authorityRuleDialogScope.currentRule = {
  224. app: $scope.app,
  225. ip: mac[0],
  226. port: mac[1],
  227. rule: {
  228. resource: resource,
  229. strategy: 0,
  230. limitApp: '',
  231. }
  232. };
  233. authorityRuleDialogScope.authorityRuleDialog = {
  234. title: '新增授权规则',
  235. type: 'add',
  236. confirmBtnText: '新增',
  237. saveAndContinueBtnText: '新增并继续添加'
  238. };
  239. authorityRuleDialogScope.saveRule = saveAuthorityRule;
  240. authorityRuleDialogScope.saveRuleAndContinue = saveAuthorityRuleAndContinue;
  241. authorityRuleDialog = ngDialog.open({
  242. template: '/app/views/dialog/authority-rule-dialog.html',
  243. width: 680,
  244. overlay: true,
  245. scope: authorityRuleDialogScope
  246. });
  247. };
  248. let paramFlowRuleDialog;
  249. let paramFlowRuleDialogScope;
  250. function saveParamFlowRule() {
  251. let ruleEntity = paramFlowRuleDialogScope.currentRule;
  252. if (!ParamFlowService.checkRuleValid(ruleEntity.rule)) {
  253. return;
  254. }
  255. ParamFlowService.addNewRule(ruleEntity).success((data) => {
  256. if (data.success) {
  257. paramFlowRuleDialog.close();
  258. let url = '/dashboard/paramFlow/' + $scope.app;
  259. $location.path(url);
  260. } else {
  261. alert('添加热点规则失败:' + data.msg);
  262. }
  263. }).error((data) => {
  264. if (data) {
  265. alert('添加热点规则失败:' + data.msg);
  266. } else {
  267. alert("添加热点规则失败:未知错误");
  268. }
  269. });
  270. }
  271. function saveParamFlowRuleAndContinue() {
  272. let ruleEntity = paramFlowRuleDialogScope.currentRule;
  273. if (!ParamFlowService.checkRuleValid(ruleEntity.rule)) {
  274. return;
  275. }
  276. ParamFlowService.addNewRule(ruleEntity).success((data) => {
  277. if (data.success) {
  278. paramFlowRuleDialog.close();
  279. } else {
  280. alert('添加热点规则失败:' + data.msg);
  281. }
  282. }).error((data) => {
  283. if (data) {
  284. alert('添加热点规则失败:' + data.msg);
  285. } else {
  286. alert("添加热点规则失败:未知错误");
  287. }
  288. });
  289. }
  290. $scope.addNewParamFlowRule = function (resource) {
  291. if (!$scope.macInputModel) {
  292. return;
  293. }
  294. let mac = $scope.macInputModel.split(':');
  295. paramFlowRuleDialogScope = $scope.$new(true);
  296. paramFlowRuleDialogScope.currentRule = {
  297. app: $scope.app,
  298. ip: mac[0],
  299. port: mac[1],
  300. rule: {
  301. resource: resource,
  302. grade: 1,
  303. paramFlowItemList: [],
  304. count: 0,
  305. limitApp: 'default',
  306. controlBehavior: 0,
  307. durationInSec: 1,
  308. burstCount: 0,
  309. maxQueueingTimeMs: 0,
  310. clusterMode: false,
  311. clusterConfig: {
  312. thresholdType: 0,
  313. fallbackToLocalWhenFail: true,
  314. }
  315. }
  316. };
  317. paramFlowRuleDialogScope.paramFlowRuleDialog = {
  318. title: '新增热点规则',
  319. type: 'add',
  320. confirmBtnText: '新增',
  321. saveAndContinueBtnText: '新增并继续添加',
  322. supportAdvanced: false,
  323. showAdvanceButton: true
  324. };
  325. paramFlowRuleDialogScope.saveRule = saveParamFlowRule;
  326. paramFlowRuleDialogScope.saveRuleAndContinue = saveParamFlowRuleAndContinue;
  327. // paramFlowRuleDialogScope.onOpenAdvanceClick = function () {
  328. // paramFlowRuleDialogScope.paramFlowRuleDialog.showAdvanceButton = false;
  329. // };
  330. // paramFlowRuleDialogScope.onCloseAdvanceClick = function () {
  331. // paramFlowRuleDialogScope.paramFlowRuleDialog.showAdvanceButton = true;
  332. // };
  333. paramFlowRuleDialog = ngDialog.open({
  334. template: '/app/views/dialog/param-flow-rule-dialog.html',
  335. width: 680,
  336. overlay: true,
  337. scope: paramFlowRuleDialogScope
  338. });
  339. };
  340. var searchHandler;
  341. $scope.searchChange = function (searchKey) {
  342. $timeout.cancel(searchHandler);
  343. searchHandler = $timeout(function () {
  344. $scope.searchKey = searchKey;
  345. $scope.isExpand = true;
  346. $scope.firstExpandAll = true;
  347. reInitIdentityDatas();
  348. $scope.firstExpandAll = false;
  349. }, 600);
  350. };
  351. $scope.initTreeTable = function () {
  352. // if (!$scope.table) {
  353. com_github_culmat_jsTreeTable.register(window);
  354. $scope.table = window.treeTable($('#identities'));
  355. // }
  356. };
  357. $scope.expandAll = function () {
  358. $scope.isExpand = true;
  359. };
  360. $scope.collapseAll = function () {
  361. $scope.isExpand = false;
  362. };
  363. $scope.treeView = function () {
  364. $scope.isTreeView = true;
  365. queryIdentities();
  366. };
  367. $scope.listView = function () {
  368. $scope.isTreeView = false;
  369. queryIdentities();
  370. };
  371. function queryAppMachines() {
  372. MachineService.getAppMachines($scope.app).success(
  373. function (data) {
  374. if (data.code === 0) {
  375. if (data.data) {
  376. $scope.machines = [];
  377. $scope.macsInputOptions = [];
  378. data.data.forEach(function (item) {
  379. if (item.healthy) {
  380. $scope.macsInputOptions.push({
  381. text: item.ip + ':' + item.port,
  382. value: item.ip + ':' + item.port
  383. });
  384. }
  385. });
  386. }
  387. if ($scope.macsInputOptions.length > 0) {
  388. $scope.macInputModel = $scope.macsInputOptions[0].value;
  389. }
  390. } else {
  391. $scope.macsInputOptions = [];
  392. }
  393. }
  394. );
  395. }
  396. // Fetch all machines by current app name.
  397. queryAppMachines();
  398. $scope.$watch('macInputModel', function () {
  399. if ($scope.macInputModel) {
  400. reInitIdentityDatas();
  401. }
  402. });
  403. $scope.$on('$destroy', function () {
  404. $interval.cancel(intervalId);
  405. });
  406. var intervalId;
  407. function reInitIdentityDatas() {
  408. // $interval.cancel(intervalId);
  409. queryIdentities();
  410. // intervalId = $interval(function () {
  411. // queryIdentities();
  412. // }, DATA_REFRESH_INTERVAL * 1000);
  413. };
  414. function queryIdentities() {
  415. var mac = $scope.macInputModel.split(':');
  416. if (mac == null || mac.length < 2) {
  417. return;
  418. }
  419. if ($scope.isTreeView) {
  420. IdentityService.fetchIdentityOfMachine(mac[0], mac[1], $scope.searchKey).success(
  421. function (data) {
  422. if (data.code == 0 && data.data) {
  423. $scope.identities = data.data;
  424. $scope.totalCount = $scope.identities.length;
  425. } else {
  426. $scope.identities = [];
  427. $scope.totalCount = 0;
  428. }
  429. }
  430. );
  431. } else {
  432. IdentityService.fetchClusterNodeOfMachine(mac[0], mac[1], $scope.searchKey).success(
  433. function (data) {
  434. if (data.code == 0 && data.data) {
  435. $scope.identities = data.data;
  436. $scope.totalCount = $scope.identities.length;
  437. } else {
  438. $scope.identities = [];
  439. $scope.totalCount = 0;
  440. }
  441. }
  442. );
  443. }
  444. };
  445. $scope.queryIdentities = queryIdentities;
  446. }]);