[monitor] 修改监控,探测监控,删除监控接口编码

This commit is contained in:
tomsun28
2021-11-15 16:36:49 +08:00
parent 0b7697a09f
commit 0aabfb8fb9
5 changed files with 114 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@Api(tags = "监控管理API")
@RestController
@RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE})
@Validated
public class MonitorController {
@Autowired

View File

@@ -18,4 +18,10 @@ public interface ParamDao extends JpaRepository<Param, Long> {
* @return 参数值列表
*/
List<Param> findParamsByMonitorId(long monitorId);
/**
* 根据监控ID删除与之关联的参数列表
* @param monitorId 监控ID
*/
void deleteParamsByMonitorId(long monitorId);
}

View File

@@ -2,6 +2,7 @@ package com.usthe.manager.service.impl;
import com.usthe.common.entity.job.Configmap;
import com.usthe.common.entity.job.Job;
import com.usthe.common.entity.message.CollectRep;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.util.SnowFlakeIdGenerator;
import com.usthe.manager.dao.MonitorDao;
@@ -32,6 +33,8 @@ import java.util.stream.Collectors;
@Slf4j
public class MonitorServiceImpl implements MonitorService {
private static final Long MONITOR_ID_TMP = 1000000000L;
@Autowired
private AppService appService;
@@ -47,7 +50,19 @@ public class MonitorServiceImpl implements MonitorService {
@Override
@Transactional(readOnly = true)
public void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException {
Long monitorId = monitor.getId();
if (monitorId == null || monitorId == 0) {
monitorId = MONITOR_ID_TMP;
}
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setMonitorId(monitorId);
appDefine.setCyclic(false);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param ->
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
CollectRep collectRep = jobScheduling.addSyncCollectJob(appDefine);
// 判断探测结果 失败则抛出探测异常
}
@Override
@@ -95,12 +110,57 @@ public class MonitorServiceImpl implements MonitorService {
@Override
public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
long monitorId = monitor.getId();
// 查判断monitorId对应的此监控是否存在
Optional<Monitor> queryOption = monitorDao.findById(monitorId);
if (!queryOption.isPresent()) {
throw new IllegalArgumentException("The Monitor " + monitorId + " not exists");
}
Monitor preMonitor = queryOption.get();
if (!preMonitor.getApp().equals(monitor.getApp()) || !preMonitor.getHost().equals(monitor.getHost())) {
// 监控的 类型和host不能修改
throw new IllegalArgumentException("Can not modify monitor's app or host");
}
// 构造采集任务Job实体
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setId(preMonitor.getJobId());
appDefine.setMonitorId(monitorId);
appDefine.setInterval(monitor.getIntervals());
appDefine.setCyclic(true);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param -> {
param.setMonitorId(monitorId);
param.setGmtCreate(null);
param.setGmtUpdate(null);
return new Configmap(param.getField(), param.getValue(), param.getType());
}).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
// 更新采集任务
jobScheduling.updateAsyncCollectJob(appDefine);
// 下发更新成功后刷库
try {
monitor.setJobId(preMonitor.getJobId());
monitor.setStatus(preMonitor.getStatus());
monitor.setGmtCreate(null);
monitor.setGmtUpdate(null);
monitorDao.save(monitor);
paramDao.saveAll(params);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new MonitorDatabaseException(e.getMessage());
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteMonitor(long id) throws RuntimeException {
Optional<Monitor> monitorOptional = monitorDao.findById(id);
if (monitorOptional.isPresent()) {
Monitor monitor = monitorOptional.get();
monitorDao.deleteById(id);
paramDao.deleteParamsByMonitorId(id);
jobScheduling.cancelAsyncCollectJob(monitor.getJobId());
}
}
@Override

View File

@@ -8,14 +8,18 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.lang.reflect.Field;
import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
import static com.usthe.common.util.CommonConstants.MONITOR_CONFLICT;
import static com.usthe.common.util.CommonConstants.PARAM_INVALID;
/**
* controller exception handler
@@ -26,6 +30,15 @@ import static com.usthe.common.util.CommonConstants.MONITOR_CONFLICT;
@Slf4j
public class GlobalExceptionHandler {
private static Field detailMessage;
static {
try {
detailMessage = Throwable.class.getDeclaredField("detailMessage");
detailMessage.setAccessible(true);
} catch (Exception e) {}
}
/**
* 处理探测失败
* @param exception 探测异常
@@ -50,6 +63,35 @@ public class GlobalExceptionHandler {
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
}
/**
* 处理参数错误的失败
* @param exception 参数异常
* @return response
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
ResponseEntity<Message<Void>> handleIllegalArgumentException(IllegalArgumentException exception) {
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(PARAM_INVALID).build();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
}
/**
* 处理请求参数错误的失败, 请求参数json映射body时出错
* @param exception 参数映射body异常
* @return response
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
ResponseEntity<Message<Void>> handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) {
try {
Message<Void> message = Message.<Void>builder().msg((String) detailMessage.get(exception)).code(PARAM_INVALID).build();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
} catch (Exception e) {
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(PARAM_INVALID).build();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
}
}
/**
* handler the exception thrown for data input verify