use of com.usthe.manager.pojo.dto.MonitorDto in project hertzbeat by dromara.
the class MonitorServiceImpl method validate.
@Override
@Transactional(readOnly = true)
public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgumentException {
// The request monitoring parameter matches the monitoring parameter definition mapping check
// 请求监控参数与监控参数定义映射校验匹配
Monitor monitor = monitorDto.getMonitor();
monitor.setHost(monitor.getHost().trim());
monitor.setName(monitor.getName().trim());
Map<String, Param> paramMap = monitorDto.getParams().stream().peek(param -> {
param.setMonitorId(monitor.getId());
String value = param.getValue() == null ? null : param.getValue().trim();
param.setValue(value);
}).collect(Collectors.toMap(Param::getField, param -> param));
// Check name uniqueness 校验名称唯一性
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("监控名称不能重复!");
}
}
}
// todo 校验标签
if (monitor.getTags() != null) {
monitor.setTags(monitor.getTags().stream().distinct().collect(Collectors.toList()));
}
// Parameter definition structure verification 参数定义结构校验
List<ParamDefine> paramDefines = appService.getAppParamDefines(monitorDto.getMonitor().getApp());
if (paramDefines != null) {
for (ParamDefine paramDefine : paramDefines) {
String field = paramDefine.getField();
Param param = paramMap.get(field);
if (paramDefine.isRequired() && (param == null || param.getValue() == null)) {
throw new IllegalArgumentException("Params field " + field + " is required.");
}
if (param != null && param.getValue() != null && !"".equals(param.getValue())) {
switch(paramDefine.getType()) {
case "number":
double doubleValue;
try {
doubleValue = Double.parseDouble(param.getValue());
} catch (Exception e) {
throw new IllegalArgumentException("Params field " + field + " type " + paramDefine.getType() + " is invalid.");
}
if (paramDefine.getRange() != null) {
if (!IntervalExpressionUtil.validNumberIntervalExpress(doubleValue, paramDefine.getRange())) {
throw new IllegalArgumentException("Params field " + field + " type " + paramDefine.getType() + " over range " + paramDefine.getRange());
}
}
param.setType(CommonConstants.PARAM_TYPE_NUMBER);
break;
case "textarea":
case "text":
Short limit = paramDefine.getLimit();
if (limit != null) {
if (param.getValue() != null && param.getValue().length() > limit) {
throw new IllegalArgumentException("Params field " + field + " type " + paramDefine.getType() + " over limit " + limit);
}
}
break;
case "host":
String hostValue = param.getValue();
if (!IpDomainUtil.validateIpDomain(hostValue)) {
throw new IllegalArgumentException("Params field " + field + " value " + hostValue + " is invalid host value.");
}
break;
case "password":
// The plaintext password needs to be encrypted for transmission and storage
// 明文密码需加密传输存储
String passwordValue = param.getValue();
if (!AesUtil.isCiphertext(passwordValue)) {
passwordValue = AesUtil.aesEncode(passwordValue);
param.setValue(passwordValue);
}
param.setType(CommonConstants.PARAM_TYPE_PASSWORD);
break;
case "boolean":
// boolean check
String booleanValue = param.getValue();
try {
Boolean.parseBoolean(booleanValue);
} catch (Exception e) {
throw new IllegalArgumentException("Params field " + field + " value " + booleanValue + " is invalid boolean value.");
}
break;
case "radio":
// radio single value check radio单选值校验
List<ParamDefine.Option> options = paramDefine.getOptions();
boolean invalid = true;
if (options != null) {
for (ParamDefine.Option option : options) {
if (param.getValue().equalsIgnoreCase(option.getValue())) {
invalid = false;
break;
}
}
}
if (invalid) {
throw new IllegalArgumentException("Params field " + field + " value " + param.getValue() + " is invalid option value");
}
break;
case "checkbox":
// todo checkbox校验
break;
case "key-value":
// todo key-value校验
break;
// 更多参数定义与实际值格式校验
default:
throw new IllegalArgumentException("ParamDefine type " + paramDefine.getType() + " is invalid.");
}
}
}
}
}
use of com.usthe.manager.pojo.dto.MonitorDto in project hertzbeat by dromara.
the class MonitorServiceImpl method getMonitorDto.
@Override
@Transactional(readOnly = true)
public MonitorDto getMonitorDto(long id) throws RuntimeException {
Optional<Monitor> monitorOptional = monitorDao.findById(id);
if (monitorOptional.isPresent()) {
Monitor monitor = monitorOptional.get();
MonitorDto monitorDto = new MonitorDto();
monitorDto.setMonitor(monitor);
List<Param> params = paramDao.findParamsByMonitorId(id);
monitorDto.setParams(params);
Job job = appService.getAppDefine(monitor.getApp());
List<String> metrics = job.getMetrics().stream().map(Metrics::getName).collect(Collectors.toList());
monitorDto.setMetrics(metrics);
return monitorDto;
} else {
return null;
}
}
use of com.usthe.manager.pojo.dto.MonitorDto in project hertzbeat by dromara.
the class MonitorController method getMonitor.
@GetMapping(path = "/{id}")
@ApiOperation(value = "Obtain monitoring information based on monitoring ID", notes = "根据监控ID获取监控信息")
public ResponseEntity<Message<MonitorDto>> getMonitor(@ApiParam(value = "监控ID", example = "6565463543") @PathVariable("id") final long id) {
// Get monitoring information
// 获取监控信息
MonitorDto monitorDto = monitorService.getMonitorDto(id);
Message.MessageBuilder<MonitorDto> messageBuilder = Message.builder();
if (monitorDto == null) {
messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("Monitor not exist.");
} else {
messageBuilder.data(monitorDto);
}
return ResponseEntity.ok(messageBuilder.build());
}
Aggregations