seninel部署
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

469 行
15KB

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