Skip to content

Commit

Permalink
dashboard: code and frontend style refinement for alibaba#927 and ali…
Browse files Browse the repository at this point in the history
…baba#869

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Jul 23, 2019
1 parent 5e85965 commit e50be35
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,38 @@
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.AuthUser;
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
import com.alibaba.csp.sentinel.util.StringUtil;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
import com.alibaba.csp.sentinel.dashboard.domain.Result;
import com.alibaba.csp.sentinel.dashboard.repository.rule.InMemSystemRuleStore;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
* @author leyou(lihao)
*/
@Controller
@RequestMapping(value = "/system", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/system")
public class SystemController {
private static Logger logger = LoggerFactory.getLogger(SystemController.class);

private final Logger logger = LoggerFactory.getLogger(SystemController.class);

@Autowired
private InMemSystemRuleStore repository;
private RuleRepository<SystemRuleEntity, Long> repository;
@Autowired
private SentinelApiClient sentinelApiClient;
@Autowired
private AuthService<HttpServletRequest> authService;

@ResponseBody
@RequestMapping("/rules.json")
Result<List<SystemRuleEntity>> queryMachineRules(HttpServletRequest request, String app, String ip, Integer port) {
AuthUser authUser = authService.getAuthUser(request);
authUser.authTarget(app, PrivilegeType.READ_RULE);
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
Expand All @@ -68,12 +64,28 @@ Result<List<SystemRuleEntity>> queryMachineRules(HttpServletRequest request, Str
if (port == null) {
return Result.ofFail(-1, "port can't be null");
}
if (port <= 0 || port > 65535) {
return Result.ofFail(-1, "port should be in (0, 65535)");
}
return null;
}

@GetMapping("/rules.json")
public Result<List<SystemRuleEntity>> apiQueryMachineRules(HttpServletRequest request, String app, String ip,
Integer port) {
AuthUser authUser = authService.getAuthUser(request);
authUser.authTarget(app, PrivilegeType.READ_RULE);

Result<List<SystemRuleEntity>> checkResult = checkBasicParams(app, ip, port);
if (checkResult != null) {
return checkResult;
}
try {
List<SystemRuleEntity> rules = sentinelApiClient.fetchSystemRuleOfMachine(app, ip, port);
rules = repository.saveAll(rules);
return Result.ofSuccess(rules);
} catch (Throwable throwable) {
logger.error("queryApps error:", throwable);
logger.error("Query machine system rules error", throwable);
return Result.ofThrowable(-1, throwable);
}
}
Expand All @@ -88,65 +100,38 @@ private int countNotNullAndNotNegative(Number... values) {
return notNullCount;
}

/**
* @modify
* 目前代码里做了如下几点修改:
* 1】修改属性名称avgLoad 修改为highestSystemLoad
* 2】修改属性名称avgCpu 修改为highestCpuUsage
* 3】调用countNotNullAndNotNegative非空校验方法里增加参数highestCpuUsage,
* 4】修改notNullCount部分判断错误提示语,因为目前countNotNullAndNotNegative里针对=0的情况也做了限制
* 5】对于highestSystemLoad、highestCpuUsage增加>1 的这个判断
* @author [email protected]
* @time 2019年7月17日 18:30:32
* @modify
*
* @param request
* @param app
* @param ip
* @param port
* @param highestSystemLoad
* @param highestCpuUsage
* @param avgRt
* @param maxThread
* @param qps
* @return
*/
@ResponseBody
@RequestMapping("/new.json")
Result<?> add(HttpServletRequest request,
String app, String ip, Integer port, Double highestSystemLoad,Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
public Result<SystemRuleEntity> apiAdd(HttpServletRequest request, String app, String ip, Integer port,
Double highestSystemLoad, Double highestCpuUsage, Long avgRt,
Long maxThread, Double qps) {
AuthUser authUser = authService.getAuthUser(request);
authUser.authTarget(app, PrivilegeType.WRITE_RULE);
if (StringUtil.isBlank(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
if (StringUtil.isBlank(ip)) {
return Result.ofFail(-1, "ip can't be null or empty");
}
if (port == null) {
return Result.ofFail(-1, "port can't be null");

Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
if (checkResult != null) {
return checkResult;
}

int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage);
int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps, highestCpuUsage);
if (notNullCount != 1) {
return Result.ofFail(-1, "only one of [highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage] "
+ "value must be set > 0, but " + notNullCount + " values get");
}
if ( null!=highestCpuUsage && 1 < highestCpuUsage ) {
return Result.ofFail(-1, "highestCpuUsage must <= 1");
if (null != highestCpuUsage && highestCpuUsage > 1) {
return Result.ofFail(-1, "highestCpuUsage must between [0.0, 1.0]");
}
SystemRuleEntity entity = new SystemRuleEntity();
entity.setApp(app.trim());
entity.setIp(ip.trim());
entity.setPort(port);
// -1 is a fake value
if ( null != highestSystemLoad ) {
if (null != highestSystemLoad) {
entity.setHighestSystemLoad(highestSystemLoad);
} else {
entity.setHighestSystemLoad(-1D);
}

if ( null != highestCpuUsage ) {
if (null != highestCpuUsage) {
entity.setHighestCpuUsage(highestCpuUsage);
} else {
entity.setHighestCpuUsage(-1D);
Expand All @@ -173,39 +158,19 @@ Result<?> add(HttpServletRequest request,
try {
entity = repository.save(entity);
} catch (Throwable throwable) {
logger.error("add error:", throwable);
logger.error("Add SystemRule error", throwable);
return Result.ofThrowable(-1, throwable);
}
if (!publishRules(app, ip, port)) {
logger.info("publish system rules fail after rule add");
logger.warn("Publish system rules fail after rule add");
}
return Result.ofSuccess(entity);
}

/**
* @modify
* 目前代码里做了如下几点修改:
* 1】修改属性名称avgLoad 修改为highestSystemLoad
* 2】修改属性名称avgCpu 修改为highestCpuUsage
* 1】对于highestSystemLoad、highestCpuUsage增加>1 的这个判断,调整原先<0的判断,调整为<=0 等于0的这种规则设置了也没有意义
* @author [email protected]
* @time 2019年7月17日 18:30:32
* @modify
*
* @param request
* @param id
* @param app
* @param highestSystemLoad
* @param highestCpuUsage
* @param avgRt
* @param maxThread
* @param qps
* @return
*/
@ResponseBody
@RequestMapping("/save.json")
Result<?> updateIfNotNull(HttpServletRequest request,
Long id, String app, Double highestSystemLoad,Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
@GetMapping("/save.json")
public Result<SystemRuleEntity> apiUpdateIfNotNull(HttpServletRequest request,
Long id, String app, Double highestSystemLoad, Double highestCpuUsage,
Long avgRt, Long maxThread, Double qps) {
AuthUser authUser = authService.getAuthUser(request);
if (id == null) {
return Result.ofFail(-1, "id can't be null");
Expand All @@ -219,7 +184,7 @@ Result<?> updateIfNotNull(HttpServletRequest request,
entity.setApp(app.trim());
}
if (highestSystemLoad != null) {
if (highestSystemLoad <= 0) {
if (highestSystemLoad < 0) {
return Result.ofFail(-1, "highestSystemLoad must >= 0");
}
entity.setHighestSystemLoad(highestSystemLoad);
Expand Down Expand Up @@ -265,9 +230,8 @@ Result<?> updateIfNotNull(HttpServletRequest request,
return Result.ofSuccess(entity);
}

@ResponseBody
@RequestMapping("/delete.json")
Result<?> delete(HttpServletRequest request, Long id) {
public Result<?> delete(HttpServletRequest request, Long id) {
AuthUser authUser = authService.getAuthUser(request);
if (id == null) {
return Result.ofFail(-1, "id can't be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,10 @@ public class SystemRuleEntity implements RuleEntity {
private String app;
private String ip;
private Integer port;
/**
* 对应SystemRule 里的属性highestSystemLoad,这里做下调整
* @author [email protected]
* @time 2019年7月17日 18:30:32
*/
private Double highestSystemLoad;
private Long avgRt;
private Long maxThread;
private Double qps;
/**
* 对应SystemRule 里的属性highestCpuUsage
* @author [email protected]
* @time 2019年7月17日 18:30:32
*/
private Double highestCpuUsage;

private Date gmtCreate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
if (!$scope.macInputModel) {
return;
}
var mac = $scope.macInputModel.split(':');
let mac = $scope.macInputModel.split(':');
SystemService.queryMachineRules($scope.app, mac[0], mac[1]).success(
function (data) {
if (data.code == 0 && data.data) {
if (data.code === 0 && data.data) {
$scope.rules = data.data;
$.each($scope.rules, function (idx, rule) {
// rule.orginEnable = rule.enable;
if (rule.highestSystemLoad >= 0) {
rule.grade = 0;
} else if (rule.avgRt >= 0) {
Expand All @@ -46,7 +45,7 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
rule.grade = 2;
} else if (rule.qps >= 0) {
rule.grade = 3;
}else if (rule.highestCpuUsage >= 0) {
} else if (rule.highestCpuUsage >= 0) {
rule.grade = 4;
}
});
Expand All @@ -56,7 +55,8 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
$scope.rulesPageConfig.totalCount = 0;
}
});
};
}

$scope.getMachineRules = getMachineRules;
var systemRuleDialog;
$scope.editRule = function (rule) {
Expand Down Expand Up @@ -96,21 +96,20 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
};

$scope.saveRule = function () {
if ($scope.systemRuleDialog.type == 'add') {
if ($scope.systemRuleDialog.type === 'add') {
addNewRule($scope.currentRule);
} else if ($scope.systemRuleDialog.type == 'edit') {
} else if ($scope.systemRuleDialog.type === 'edit') {
saveRule($scope.currentRule, true);
}
}

};

var confirmDialog;
$scope.deleteRule = function (rule) {
$scope.currentRule = rule;
var ruleTypeDesc = '';
var ruleTypeCount = null;
if (rule.highestSystemLoad != -1) {
ruleTypeDesc = '系统LOAD';
ruleTypeDesc = 'LOAD';
ruleTypeCount = rule.highestSystemLoad;
} else if (rule.avgRt != -1) {
ruleTypeDesc = 'RT';
Expand Down Expand Up @@ -142,7 +141,7 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo


$scope.confirm = function () {
if ($scope.confirmDialog.type == 'delete_rule') {
if ($scope.confirmDialog.type === 'delete_rule') {
deleteRule($scope.currentRule);
// } else if ($scope.confirmDialog.type == 'enable_rule') {
// $scope.currentRule.enable = true;
Expand All @@ -161,56 +160,55 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo

function deleteRule(rule) {
SystemService.deleteRule(rule).success(function (data) {
if (data.code == 0) {
if (data.code === 0) {
getMachineRules();
confirmDialog.close();
} else if(data.msg!=null){
alert(data.msg);
} else{
alert('失败!');
} else if (data.msg != null) {
alert('失败:' + data.msg);
} else {
alert('失败:未知错误');
}
});
};
}

function addNewRule(rule) {
if (rule.grade == 4 && (rule.highestCpuUsage < 0 || rule.highestCpuUsage > 1)) {
alert('CPU 使用率模式的取值范围应为 [0.0, 1.0],对应 0% - 100%');
return;
}
SystemService.newRule(rule).success(function (data) {
if (data.code == 0) {
if (data.code === 0) {
getMachineRules();
systemRuleDialog.close();
} else if(data.msg!=null){
alert(data.msg);
}else{
alert('失败!');
} else if (data.msg != null) {
alert('失败:' + data.msg);
} else {
alert('失败:未知错误');
}
});
};
}

function saveRule(rule, edit) {
SystemService.saveRule(rule).success(function (data) {
if (data.code == 0) {
// if (rule.enable) {
// rule.orginEnable = true;
// } else {
// rule.orginEnable = false;
// }
if (data.code === 0) {
getMachineRules();
if (edit) {
systemRuleDialog.close();
} else {
confirmDialog.close();
}
} else if(data.msg!=null){
alert(data.msg);
}else{
alert('失败!');
} else if (data.msg != null) {
alert('失败:' + data.msg);
} else {
alert('失败:未知错误');
}
});
}
queryAppMachines();
function queryAppMachines() {
MachineService.getAppMachines($scope.app).success(
function (data) {
if (data.code == 0) {
if (data.code === 0) {
// $scope.machines = data.data;
if (data.data) {
$scope.machines = [];
Expand Down
Loading

0 comments on commit e50be35

Please sign in to comment.