Skip to content

Commit

Permalink
dashboard: fix the problem that dashboard could not correctly display…
Browse files Browse the repository at this point in the history
… the SystemRule in CPU usage strategy (#927)
  • Loading branch information
Crazy10552 authored and sczyh30 committed Jul 23, 2019
1 parent d19df2a commit 5e85965
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,33 @@ 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 avgLoad, Long avgRt, Long maxThread, Double qps) {
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)) {
Expand All @@ -103,21 +126,32 @@ Result<?> add(HttpServletRequest request,
if (port == null) {
return Result.ofFail(-1, "port can't be null");
}
int notNullCount = countNotNullAndNotNegative(avgLoad, avgRt, maxThread, qps);

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

if ( null != highestCpuUsage ) {
entity.setHighestCpuUsage(highestCpuUsage);
} else {
entity.setAvgLoad(-1D);
entity.setHighestCpuUsage(-1D);
}

if (avgRt != null) {
entity.setAvgRt(avgRt);
} else {
Expand Down Expand Up @@ -148,10 +182,30 @@ Result<?> add(HttpServletRequest request,
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 avgLoad, Long avgRt, Long maxThread, Double qps) {
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 @@ -164,11 +218,20 @@ Result<?> updateIfNotNull(HttpServletRequest request,
if (StringUtil.isNotBlank(app)) {
entity.setApp(app.trim());
}
if (avgLoad != null) {
if (avgLoad < 0) {
return Result.ofFail(-1, "avgLoad must >= 0");
if (highestSystemLoad != null) {
if (highestSystemLoad <= 0) {
return Result.ofFail(-1, "highestSystemLoad must >= 0");
}
entity.setHighestSystemLoad(highestSystemLoad);
}
if (highestCpuUsage != null) {
if (highestCpuUsage < 0) {
return Result.ofFail(-1, "highestCpuUsage must >= 0");
}
if (highestCpuUsage > 1) {
return Result.ofFail(-1, "highestCpuUsage must <= 1");
}
entity.setAvgLoad(avgLoad);
entity.setHighestCpuUsage(highestCpuUsage);
}
if (avgRt != null) {
if (avgRt < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.alibaba.csp.sentinel.dashboard.datasource.entity.rule;

import java.util.Date;

import com.alibaba.csp.sentinel.slots.system.SystemRule;

import java.util.Date;

/**
* @author leyou
*/
Expand All @@ -29,10 +29,21 @@ public class SystemRuleEntity implements RuleEntity {
private String app;
private String ip;
private Integer port;
private Double avgLoad;
/**
* 对应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;
private Date gmtModified;
Expand All @@ -42,7 +53,8 @@ public static SystemRuleEntity fromSystemRule(String app, String ip, Integer por
entity.setApp(app);
entity.setIp(ip);
entity.setPort(port);
entity.setAvgLoad(rule.getHighestSystemLoad());
entity.setHighestSystemLoad(rule.getHighestSystemLoad());
entity.setHighestCpuUsage(rule.getHighestCpuUsage());
entity.setAvgRt(rule.getAvgRt());
entity.setMaxThread(rule.getMaxThread());
entity.setQps(rule.getQps());
Expand Down Expand Up @@ -86,12 +98,12 @@ public void setApp(String app) {
this.app = app;
}

public Double getAvgLoad() {
return avgLoad;
public Double getHighestSystemLoad() {
return highestSystemLoad;
}

public void setAvgLoad(Double avgLoad) {
this.avgLoad = avgLoad;
public void setHighestSystemLoad(Double highestSystemLoad) {
this.highestSystemLoad = highestSystemLoad;
}

public Long getAvgRt() {
Expand All @@ -118,6 +130,14 @@ public void setQps(Double qps) {
this.qps = qps;
}

public Double getHighestCpuUsage() {
return highestCpuUsage;
}

public void setHighestCpuUsage(Double highestCpuUsage) {
this.highestCpuUsage = highestCpuUsage;
}

@Override
public Date getGmtCreate() {
return gmtCreate;
Expand All @@ -138,10 +158,11 @@ public void setGmtModified(Date gmtModified) {
@Override
public SystemRule toRule() {
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(avgLoad);
rule.setHighestSystemLoad(highestSystemLoad);
rule.setAvgRt(avgRt);
rule.setMaxThread(maxThread);
rule.setQps(qps);
rule.setHighestCpuUsage(highestCpuUsage);
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
$scope.rules = data.data;
$.each($scope.rules, function (idx, rule) {
// rule.orginEnable = rule.enable;
if (rule.avgLoad >= 0) {
if (rule.highestSystemLoad >= 0) {
rule.grade = 0;
} else if (rule.avgRt >= 0) {
rule.grade = 1;
} else if (rule.maxThread >= 0) {
rule.grade = 2;
} else if (rule.qps >= 0) {
rule.grade = 3;
}else if (rule.highestCpuUsage >= 0) {
rule.grade = 4;
}
});
$scope.rulesPageConfig.totalCount = $scope.rules.length;
Expand Down Expand Up @@ -107,9 +109,9 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
$scope.currentRule = rule;
var ruleTypeDesc = '';
var ruleTypeCount = null;
if (rule.avgLoad != -1) {
ruleTypeDesc = 'LOAD';
ruleTypeCount = rule.avgLoad;
if (rule.highestSystemLoad != -1) {
ruleTypeDesc = '系统LOAD';
ruleTypeCount = rule.highestSystemLoad;
} else if (rule.avgRt != -1) {
ruleTypeDesc = 'RT';
ruleTypeCount = rule.avgRt;
Expand All @@ -119,6 +121,9 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
} else if (rule.qps != -1) {
ruleTypeDesc = 'QPS';
ruleTypeCount = rule.qps;
}else if (rule.highestCpuUsage != -1) {
ruleTypeDesc = 'CPU 使用率';
ruleTypeCount = rule.highestCpuUsage;
}

$scope.confirmDialog = {
Expand Down Expand Up @@ -159,8 +164,10 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
if (data.code == 0) {
getMachineRules();
confirmDialog.close();
} else {
alert
} else if(data.msg!=null){
alert(data.msg);
} else{
alert('失败!');
}
});
};
Expand All @@ -170,8 +177,10 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
if (data.code == 0) {
getMachineRules();
systemRuleDialog.close();
} else {
alert('失败!');
} else if(data.msg!=null){
alert(data.msg);
}else{
alert('失败!');
}
});
};
Expand All @@ -190,8 +199,10 @@ app.controller('SystemCtl', ['$scope', '$stateParams', 'SystemService', 'ngDialo
} else {
confirmDialog.close();
}
} else {
alert('失败!');
} else if(data.msg!=null){
alert(data.msg);
}else{
alert('失败!');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ app.service('SystemService', ['$http', function ($http) {
port: rule.port
};
if (rule.grade == 0) {// avgLoad
param.avgLoad = rule.avgLoad;
param.highestSystemLoad = rule.highestSystemLoad;
} else if (rule.grade == 1) {// avgRt
param.avgRt = rule.avgRt;
} else if (rule.grade == 2) {// maxThread
param.maxThread = rule.maxThread;
} else if (rule.grade == 3) {// qps
param.qps = rule.qps;
}else if (rule.grade == 4) {// cpu
param.highestCpuUsage = rule.highestCpuUsage;
}

return $http({
Expand All @@ -42,14 +44,17 @@ app.service('SystemService', ['$http', function ($http) {
id: rule.id,
};
if (rule.grade == 0) {// avgLoad
param.avgLoad = rule.avgLoad;
param.highestSystemLoad = rule.highestSystemLoad;
} else if (rule.grade == 1) {// avgRt
param.avgRt = rule.avgRt;
} else if (rule.grade == 2) {// maxThread
param.maxThread = rule.maxThread;
} else if (rule.grade == 3) {// qps
param.qps = rule.qps;
}else if (rule.grade == 4) {// cpu
param.highestCpuUsage = rule.highestCpuUsage;
}

return $http({
url: '/system/save.json',
params: param,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@
<label class="col-sm-2 control-label">阈值类型</label>
<div class="col-sm-9">
<div class="form-control" ng-if="systemRuleDialog.type == 'edit'" align="center">
<!--avgLoad -->
<input type="radio" name="grade" value="0" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;LOAD&nbsp;&nbsp;
<!--highestSystemLoad -->
<input type="radio" name="grade" value="0" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;系统LOAD&nbsp;&nbsp;
<!--avgRt -->
<input type="radio" name="grade" value="1" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;RT&nbsp;&nbsp;
<!--maxThread -->
<input type="radio" name="grade" value="2" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;线程数&nbsp;&nbsp;
<!--qps -->
<input type="radio" name="grade" value="3" checked ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'"
/>&nbsp;入口 QPS
<input type="radio" name="grade" value="3" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'"/>&nbsp;入口 QPS
<!--highestCpuUsage -->
<input type="radio" name="grade" value="4" checked ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;CPU 使用率&nbsp;&nbsp;

</div>
<div class="form-control highlight-border" ng-if="systemRuleDialog.type == 'add'" align="center">
<!--avgLoad -->
<input type="radio" name="grade" value="0" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;LOAD&nbsp;&nbsp;
<!--highestSystemLoad -->
<input type="radio" name="grade" value="0" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;系统LOAD&nbsp;&nbsp;
<!--avgRt -->
<input type="radio" name="grade" value="1" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;RT&nbsp;&nbsp;
<!--maxThread -->
<input type="radio" name="grade" value="2" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;线程数&nbsp;&nbsp;
<!--qps -->
<input type="radio" name="grade" value="3" checked ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'"
/>&nbsp;入口 QPS
<input type="radio" name="grade" value="3" ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'"/>&nbsp;入口 QPS
<!--highestCpuUsage -->
<input type="radio" name="grade" value="4" checked ng-model='currentRule.grade' ng-disabled="systemRuleDialog.type == 'edit'" />&nbsp;CPU 使用率&nbsp;&nbsp;

</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">阈值</label>
<div class="col-sm-9">
<input type='number' class="form-control highlight-border" ng-model='currentRule.avgLoad' placeholder="[0, ~)的小数" ng-if="currentRule.grade == 0"
/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.avgRt' placeholder="[0, ~)的整数" ng-if="currentRule.grade == 1"
/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.maxThread' placeholder="[0, ~)的整数" ng-if="currentRule.grade == 2"
/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.qps' placeholder="[0, ~)的小数" ng-if="currentRule.grade == 3"
/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.highestSystemLoad' placeholder="[0, ~)的正整数" ng-if="currentRule.grade == 0"/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.avgRt' placeholder="[0, ~)的正整数" ng-if="currentRule.grade == 1"/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.maxThread' placeholder="[0, ~)的正整数" ng-if="currentRule.grade == 2"/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.qps' placeholder="[0, ~)的正整数" ng-if="currentRule.grade == 3"/>
<input type='number' class="form-control highlight-border" ng-model='currentRule.highestCpuUsage' placeholder="[0, 1)的小数" ng-if="currentRule.grade == 4"/>
</div>
</div>
</form>
Expand Down
Loading

0 comments on commit 5e85965

Please sign in to comment.