Search in sources :

Example 1 with SystemRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity in project pig by pig-mesh.

the class SystemController method apiUpdateIfNotNull.

@GetMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    SystemRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    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.setHighestCpuUsage(highestCpuUsage);
    }
    if (avgRt != null) {
        if (avgRt < 0) {
            return Result.ofFail(-1, "avgRt must >= 0");
        }
        entity.setAvgRt(avgRt);
    }
    if (maxThread != null) {
        if (maxThread < 0) {
            return Result.ofFail(-1, "maxThread must >= 0");
        }
        entity.setMaxThread(maxThread);
    }
    if (qps != null) {
        if (qps < 0) {
            return Result.ofFail(-1, "qps must >= 0");
        }
        entity.setQps(qps);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish system rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 2 with SystemRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity in project spring-boot-student by wyh-spring-ecosystem-student.

the class SystemController method apiAdd.

@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
    if (checkResult != null) {
        return checkResult;
    }
    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 && 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) {
        entity.setHighestSystemLoad(highestSystemLoad);
    } else {
        entity.setHighestSystemLoad(-1D);
    }
    if (null != highestCpuUsage) {
        entity.setHighestCpuUsage(highestCpuUsage);
    } else {
        entity.setHighestCpuUsage(-1D);
    }
    if (avgRt != null) {
        entity.setAvgRt(avgRt);
    } else {
        entity.setAvgRt(-1L);
    }
    if (maxThread != null) {
        entity.setMaxThread(maxThread);
    } else {
        entity.setMaxThread(-1L);
    }
    if (qps != null) {
        entity.setQps(qps);
    } else {
        entity.setQps(-1D);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Add SystemRule error", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.warn("Publish system rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 3 with SystemRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity in project XHuiCloud by sindaZeng.

the class SystemController method apiAdd.

@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
    if (checkResult != null) {
        return checkResult;
    }
    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 && 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) {
        entity.setHighestSystemLoad(highestSystemLoad);
    } else {
        entity.setHighestSystemLoad(-1D);
    }
    if (null != highestCpuUsage) {
        entity.setHighestCpuUsage(highestCpuUsage);
    } else {
        entity.setHighestCpuUsage(-1D);
    }
    if (avgRt != null) {
        entity.setAvgRt(avgRt);
    } else {
        entity.setAvgRt(-1L);
    }
    if (maxThread != null) {
        entity.setMaxThread(maxThread);
    } else {
        entity.setMaxThread(-1L);
    }
    if (qps != null) {
        entity.setQps(qps);
    } else {
        entity.setQps(-1D);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Add SystemRule error", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.warn("Publish system rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 4 with SystemRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity in project RuoYi-Cloud-Plus by JavaLionLi.

the class SystemController method apiAdd.

@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
    if (checkResult != null) {
        return checkResult;
    }
    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 && 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) {
        entity.setHighestSystemLoad(highestSystemLoad);
    } else {
        entity.setHighestSystemLoad(-1D);
    }
    if (null != highestCpuUsage) {
        entity.setHighestCpuUsage(highestCpuUsage);
    } else {
        entity.setHighestCpuUsage(-1D);
    }
    if (avgRt != null) {
        entity.setAvgRt(avgRt);
    } else {
        entity.setAvgRt(-1L);
    }
    if (maxThread != null) {
        entity.setMaxThread(maxThread);
    } else {
        entity.setMaxThread(-1L);
    }
    if (qps != null) {
        entity.setQps(qps);
    } else {
        entity.setQps(-1D);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Add SystemRule error", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.warn("Publish system rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 5 with SystemRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity in project RuoYi-Cloud-Plus by JavaLionLi.

the class SystemController method apiUpdateIfNotNull.

@GetMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    SystemRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    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.setHighestCpuUsage(highestCpuUsage);
    }
    if (avgRt != null) {
        if (avgRt < 0) {
            return Result.ofFail(-1, "avgRt must >= 0");
        }
        entity.setAvgRt(avgRt);
    }
    if (maxThread != null) {
        if (maxThread < 0) {
            return Result.ofFail(-1, "maxThread must >= 0");
        }
        entity.setMaxThread(maxThread);
    }
    if (qps != null) {
        if (qps < 0) {
            return Result.ofFail(-1, "qps must >= 0");
        }
        entity.setQps(qps);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish system rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)10 SystemRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity)10 Date (java.util.Date)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5