[manager]保证监控名称的唯一性
This commit is contained in:
@@ -88,7 +88,7 @@ public class MonitorController {
|
||||
@PostMapping(path = "/detect")
|
||||
@ApiOperation(value = "探测监控", notes = "根据监控信息去对此监控进行可用性探测")
|
||||
public ResponseEntity<Message<Void>> detectMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
monitorService.validate(monitorDto, false);
|
||||
monitorService.validate(monitorDto, null);
|
||||
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
return ResponseEntity.ok(new Message<>("Detect success."));
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -39,6 +40,13 @@ public interface MonitorDao extends JpaRepository<Monitor, Long>, JpaSpecificati
|
||||
*/
|
||||
List<Monitor> findMonitorsByAppEquals(String app);
|
||||
|
||||
/**
|
||||
* 根据监控名称查询监控
|
||||
* @param name 监控名称
|
||||
* @return 监控列表
|
||||
*/
|
||||
Optional<Monitor> findMonitorByNameEquals(String name);
|
||||
|
||||
/**
|
||||
* 查询监控类别及其对应的监控数量
|
||||
* @return 监控类别与监控数量映射
|
||||
|
||||
@@ -43,7 +43,7 @@ public interface MonitorService {
|
||||
* @param isModify 是否是修改监控
|
||||
* @throws IllegalArgumentException 校验参数错误抛出
|
||||
*/
|
||||
void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException;
|
||||
void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* 修改更新监控
|
||||
|
||||
@@ -119,13 +119,29 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException {
|
||||
public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgumentException {
|
||||
// 请求监控参数与监控参数定义映射校验匹配
|
||||
Monitor monitor = monitorDto.getMonitor();
|
||||
Map<String, Param> paramMap = monitorDto.getParams()
|
||||
.stream()
|
||||
.peek(param -> param.setMonitorId(monitor.getId()))
|
||||
.collect(Collectors.toMap(Param::getField, param -> param));
|
||||
// 校验名称唯一性
|
||||
if (isModify != null) {
|
||||
Optional<Monitor> monitorOptional = monitorDao.findMonitorByNameEquals(monitor.getName());
|
||||
if (monitorOptional.isPresent()) {
|
||||
Monitor existMonitor = monitorOptional.get();
|
||||
if (isModify) {
|
||||
if (!existMonitor.getId().equals(monitor.getId())) {
|
||||
throw new IllegalArgumentException("监控名称不能重复!");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("监控名称不能重复!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 参数定义结构校验
|
||||
List<ParamDefine> paramDefines = appService.getAppParamDefines(monitorDto.getMonitor().getApp());
|
||||
if (paramDefines != null) {
|
||||
for (ParamDefine paramDefine : paramDefines) {
|
||||
|
||||
Reference in New Issue
Block a user