forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboard: code and frontend style refinement for alibaba#927 and ali…
…baba#869 Signed-off-by: Eric Zhao <[email protected]>
- Loading branch information
Showing
8 changed files
with
101 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
} | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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); | ||
|
@@ -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"); | ||
|
@@ -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); | ||
|
@@ -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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.