| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- var app = angular.module('sentinelDashboardApp');
- app.controller('GatewayIdentityCtl', ['$scope', '$stateParams', 'IdentityService',
- 'ngDialog', 'GatewayFlowService', 'GatewayApiService', 'DegradeService', 'MachineService',
- '$interval', '$location', '$timeout',
- function ($scope, $stateParams, IdentityService, ngDialog,
- GatewayFlowService, GatewayApiService, DegradeService, MachineService, $interval, $location, $timeout) {
- $scope.app = $stateParams.app;
- $scope.currentPage = 1;
- $scope.pageSize = 16;
- $scope.totalPage = 1;
- $scope.totalCount = 0;
- $scope.identities = [];
- $scope.searchKey = '';
- $scope.macsInputConfig = {
- searchField: ['text', 'value'],
- persist: true,
- create: false,
- maxItems: 1,
- render: {
- item: function (data, escape) {
- return '<div>' + escape(data.text) + '</div>';
- }
- },
- onChange: function (value, oldValue) {
- $scope.macInputModel = value;
- }
- };
- $scope.table = null;
- getApiNames();
- function getApiNames() {
- if (!$scope.macInputModel) {
- return;
- }
- var mac = $scope.macInputModel.split(':');
- GatewayApiService.queryApis($scope.app, mac[0], mac[1]).success(
- function (data) {
- if (data.code == 0 && data.data) {
- $scope.apiNames = [];
- data.data.forEach(function (api) {
- $scope.apiNames.push(api["apiName"]);
- });
- }
- });
- }
- var gatewayFlowRuleDialog;
- var gatewayFlowRuleDialogScope;
- $scope.addNewGatewayFlowRule = function (resource) {
- if (!$scope.macInputModel) {
- return;
- }
- var mac = $scope.macInputModel.split(':');
- gatewayFlowRuleDialogScope = $scope.$new(true);
- gatewayFlowRuleDialogScope.apiNames = $scope.apiNames;
- gatewayFlowRuleDialogScope.intervalUnits = [{val: 0, desc: '秒'}, {val: 1, desc: '分'}, {val: 2, desc: '时'}, {val: 3, desc: '天'}];
- gatewayFlowRuleDialogScope.currentRule = {
- grade: 1,
- app: $scope.app,
- ip: mac[0],
- port: mac[1],
- resourceMode: gatewayFlowRuleDialogScope.apiNames.indexOf(resource) == -1 ? 0 : 1,
- resource: resource,
- interval: 1,
- intervalUnit: 0,
- controlBehavior: 0,
- burst: 0,
- maxQueueingTimeoutMs: 0
- };
- gatewayFlowRuleDialogScope.gatewayFlowRuleDialog = {
- title: '新增网关流控规则',
- type: 'add',
- confirmBtnText: '新增',
- saveAndContinueBtnText: '新增并继续添加',
- showAdvanceButton: true
- };
- gatewayFlowRuleDialogScope.useRouteID = function() {
- gatewayFlowRuleDialogScope.currentRule.resource = '';
- };
- gatewayFlowRuleDialogScope.useCustormAPI = function() {
- gatewayFlowRuleDialogScope.currentRule.resource = '';
- };
- gatewayFlowRuleDialogScope.useParamItem = function () {
- gatewayFlowRuleDialogScope.currentRule.paramItem = {
- parseStrategy: 0,
- matchStrategy: 0
- };
- };
- gatewayFlowRuleDialogScope.notUseParamItem = function () {
- gatewayFlowRuleDialogScope.currentRule.paramItem = null;
- };
- gatewayFlowRuleDialogScope.useParamItemVal = function() {
- gatewayFlowRuleDialogScope.currentRule.paramItem.pattern = "";
- };
- gatewayFlowRuleDialogScope.notUseParamItemVal = function() {
- gatewayFlowRuleDialogScope.currentRule.paramItem.pattern = null;
- };
- gatewayFlowRuleDialogScope.saveRule = saveGatewayFlowRule;
- gatewayFlowRuleDialogScope.saveRuleAndContinue = saveGatewayFlowRuleAndContinue;
- gatewayFlowRuleDialogScope.onOpenAdvanceClick = function () {
- gatewayFlowRuleDialogScope.gatewayFlowRuleDialog.showAdvanceButton = false;
- };
- gatewayFlowRuleDialogScope.onCloseAdvanceClick = function () {
- gatewayFlowRuleDialogScope.gatewayFlowRuleDialog.showAdvanceButton = true;
- };
- gatewayFlowRuleDialog = ngDialog.open({
- template: '/app/views/dialog/gateway/flow-rule-dialog.html',
- width: 780,
- overlay: true,
- scope: gatewayFlowRuleDialogScope
- });
- };
- function saveGatewayFlowRule() {
- if (!GatewayFlowService.checkRuleValid(gatewayFlowRuleDialogScope.currentRule)) {
- return;
- }
- GatewayFlowService.newRule(gatewayFlowRuleDialogScope.currentRule).success(function (data) {
- if (data.code === 0) {
- gatewayFlowRuleDialog.close();
- let url = '/dashboard/gateway/flow/' + $scope.app;
- $location.path(url);
- } else {
- alert('失败!');
- }
- }).error((data, header, config, status) => {
- alert('未知错误');
- });
- }
- function saveGatewayFlowRuleAndContinue() {
- if (!GatewayFlowService.checkRuleValid(gatewayFlowRuleDialogScope.currentRule)) {
- return;
- }
- GatewayFlowService.newRule(gatewayFlowRuleDialogScope.currentRule).success(function (data) {
- if (data.code == 0) {
- gatewayFlowRuleDialog.close();
- } else {
- alert('失败!');
- }
- });
- }
- var degradeRuleDialog;
- $scope.addNewDegradeRule = function (resource) {
- if (!$scope.macInputModel) {
- return;
- }
- var mac = $scope.macInputModel.split(':');
- degradeRuleDialogScope = $scope.$new(true);
- degradeRuleDialogScope.currentRule = {
- enable: false,
- grade: 0,
- strategy: 0,
- resource: resource,
- limitApp: 'default',
- app: $scope.app,
- ip: mac[0],
- port: mac[1]
- };
- degradeRuleDialogScope.degradeRuleDialog = {
- title: '新增降级规则',
- type: 'add',
- confirmBtnText: '新增',
- saveAndContinueBtnText: '新增并继续添加'
- };
- degradeRuleDialogScope.saveRule = saveDegradeRule;
- degradeRuleDialogScope.saveRuleAndContinue = saveDegradeRuleAndContinue;
- degradeRuleDialog = ngDialog.open({
- template: '/app/views/dialog/degrade-rule-dialog.html',
- width: 680,
- overlay: true,
- scope: degradeRuleDialogScope
- });
- };
- function saveDegradeRule() {
- if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
- return;
- }
- DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
- if (data.code == 0) {
- degradeRuleDialog.close();
- var url = '/dashboard/degrade/' + $scope.app;
- $location.path(url);
- } else {
- alert('失败!');
- }
- });
- }
- function saveDegradeRuleAndContinue() {
- if (!DegradeService.checkRuleValid(degradeRuleDialogScope.currentRule)) {
- return;
- }
- DegradeService.newRule(degradeRuleDialogScope.currentRule).success(function (data) {
- if (data.code == 0) {
- degradeRuleDialog.close();
- } else {
- alert('失败!');
- }
- });
- }
- var searchHandler;
- $scope.searchChange = function (searchKey) {
- $timeout.cancel(searchHandler);
- searchHandler = $timeout(function () {
- $scope.searchKey = searchKey;
- reInitIdentityDatas();
- }, 600);
- };
- function queryAppMachines() {
- MachineService.getAppMachines($scope.app).success(
- function (data) {
- if (data.code === 0) {
- if (data.data) {
- $scope.machines = [];
- $scope.macsInputOptions = [];
- data.data.forEach(function (item) {
- if (item.healthy) {
- $scope.macsInputOptions.push({
- text: item.ip + ':' + item.port,
- value: item.ip + ':' + item.port
- });
- }
- });
- }
- if ($scope.macsInputOptions.length > 0) {
- $scope.macInputModel = $scope.macsInputOptions[0].value;
- }
- } else {
- $scope.macsInputOptions = [];
- }
- }
- );
- }
- // Fetch all machines by current app name.
- queryAppMachines();
- $scope.$watch('macInputModel', function () {
- if ($scope.macInputModel) {
- reInitIdentityDatas();
- }
- });
- $scope.$on('$destroy', function () {
- $interval.cancel(intervalId);
- });
- var intervalId;
- function reInitIdentityDatas() {
- getApiNames();
- queryIdentities();
- };
- function queryIdentities() {
- var mac = $scope.macInputModel.split(':');
- if (mac == null || mac.length < 2) {
- return;
- }
- IdentityService.fetchClusterNodeOfMachine(mac[0], mac[1], $scope.searchKey).success(
- function (data) {
- if (data.code == 0 && data.data) {
- $scope.identities = data.data;
- $scope.totalCount = $scope.identities.length;
- } else {
- $scope.identities = [];
- $scope.totalCount = 0;
- }
- }
- );
- };
- $scope.queryIdentities = queryIdentities;
- }]);
|