[manager,web-app] 监控列表,新增修改监控等编码
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.usthe.manager.controller;
|
||||
|
||||
import com.usthe.common.entity.dto.Message;
|
||||
import com.usthe.manager.pojo.entity.Monitor;
|
||||
import com.usthe.manager.service.MonitorService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* 监控管理批量API
|
||||
* @author tom
|
||||
* @date 2021/12/1 20:43
|
||||
*/
|
||||
@Api(tags = "监控列表API")
|
||||
@RestController
|
||||
@RequestMapping(path = "/monitors", produces = {APPLICATION_JSON_VALUE})
|
||||
public class MonitorsController {
|
||||
|
||||
@Autowired
|
||||
private MonitorService monitorService;
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation(value = "查询监控列表", notes = "根据查询过滤项获取监控信息列表")
|
||||
public ResponseEntity<Message<Page<Monitor>>> getMonitors(
|
||||
@ApiParam(value = "监控ID", example = "6565463543") @RequestParam(required = false) List<Long> ids,
|
||||
@ApiParam(value = "监控类型", example = "linux") @RequestParam(required = false) String app,
|
||||
@ApiParam(value = "监控名称,模糊查询", example = "linux-127.0.0.1") @RequestParam(required = false) String name,
|
||||
@ApiParam(value = "监控Host,模糊查询", example = "127.0.0.1") @RequestParam(required = false) String host,
|
||||
@ApiParam(value = "排序字段,默认id", example = "name") @RequestParam(defaultValue = "id") String sort,
|
||||
@ApiParam(value = "排序方式,asc:升序,desc:降序", example = "asc") @RequestParam(defaultValue = "asc") String order,
|
||||
@ApiParam(value = "列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
|
||||
@ApiParam(value = "列表分页数量", example = "10") @RequestParam(defaultValue = "8") int pageSize) {
|
||||
|
||||
Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
|
||||
Predicate predicate = criteriaBuilder.conjunction();
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
|
||||
for (long id : ids) {
|
||||
inPredicate.value(id);
|
||||
}
|
||||
predicate = criteriaBuilder.and(inPredicate);
|
||||
}
|
||||
if (app != null && !"".equals(app)) {
|
||||
Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app);
|
||||
predicate = criteriaBuilder.and(predicateApp);
|
||||
}
|
||||
if (name != null && !"".equals(name)) {
|
||||
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
|
||||
predicate = criteriaBuilder.and(predicateName);
|
||||
}
|
||||
if (host != null && !"".equals(host)) {
|
||||
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
|
||||
predicate = criteriaBuilder.and(predicateHost);
|
||||
}
|
||||
return predicate;
|
||||
};
|
||||
// 分页是必须的
|
||||
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
|
||||
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
|
||||
Page<Monitor> monitorPage = monitorService.getMonitors(specification, pageRequest);
|
||||
Message<Page<Monitor>> message = new Message<>(monitorPage);
|
||||
return ResponseEntity.ok(message);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
@ApiOperation(value = "批量删除监控", notes = "根据监控ID列表批量删除监控项")
|
||||
public ResponseEntity<Message<Void>> deleteMonitors(
|
||||
@ApiParam(value = "监控IDs", example = "6565463543") @RequestParam(required = false) List<Long> ids
|
||||
) {
|
||||
if (ids != null && !ids.isEmpty()) {
|
||||
monitorService.deleteMonitors(new HashSet<>(ids));
|
||||
}
|
||||
Message<Void> message = new Message<>();
|
||||
return ResponseEntity.ok(message);
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,30 @@ package com.usthe.manager.dao;
|
||||
|
||||
import com.usthe.manager.pojo.entity.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* AuthResources 数据库操作
|
||||
* @author tomsun28
|
||||
* @date 2021/11/14 11:24
|
||||
*/
|
||||
public interface MonitorDao extends JpaRepository<Monitor, Long> {
|
||||
public interface MonitorDao extends JpaRepository<Monitor, Long>, JpaSpecificationExecutor<Monitor> {
|
||||
|
||||
|
||||
/**
|
||||
* 根据监控ID列表删除监控
|
||||
* @param monitorIds 监控ID列表
|
||||
*/
|
||||
void deleteAllByIdIn(Set<Long> monitorIds);
|
||||
|
||||
/**
|
||||
* 根据监控ID列表查询监控
|
||||
* @param monitorIds 监控ID列表
|
||||
* @return 监控列表
|
||||
*/
|
||||
List<Monitor> findMonitorsByIdIn(Set<Long> monitorIds);
|
||||
|
||||
}
|
||||
|
||||
@@ -4,8 +4,12 @@ import com.usthe.manager.pojo.dto.MonitorDto;
|
||||
import com.usthe.manager.pojo.entity.Monitor;
|
||||
import com.usthe.manager.pojo.entity.Param;
|
||||
import com.usthe.manager.support.exception.MonitorDetectException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 监控管理服务
|
||||
@@ -54,6 +58,13 @@ public interface MonitorService {
|
||||
*/
|
||||
void deleteMonitor(long id) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* 批量删除监控
|
||||
* @param ids 监控ID
|
||||
* @throws RuntimeException 删除过程中异常抛出
|
||||
*/
|
||||
void deleteMonitors(Set<Long> ids) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* 获取监控信息
|
||||
* @param id 监控ID
|
||||
@@ -61,4 +72,12 @@ public interface MonitorService {
|
||||
* @throws RuntimeException 查询过程中异常抛出
|
||||
*/
|
||||
MonitorDto getMonitor(long id) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* 动态条件查询
|
||||
* @param specification 查询条件
|
||||
* @param pageRequest 分页参数
|
||||
* @return 查询结果
|
||||
*/
|
||||
Page<Monitor> getMonitors(Specification<Monitor> specification, PageRequest pageRequest);
|
||||
}
|
||||
|
||||
@@ -21,12 +21,16 @@ import com.usthe.manager.support.exception.MonitorDetectException;
|
||||
import com.usthe.scheduler.JobScheduling;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -166,10 +170,20 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
break;
|
||||
case "password":
|
||||
// 明文密码需加密传输存储
|
||||
String value = param.getValue();
|
||||
if (!AesUtil.isCiphertext(value)) {
|
||||
value = AesUtil.aesEncode(value);
|
||||
param.setValue(value);
|
||||
String passwordValue = param.getValue();
|
||||
if (!AesUtil.isCiphertext(passwordValue)) {
|
||||
passwordValue = AesUtil.aesEncode(passwordValue);
|
||||
param.setValue(passwordValue);
|
||||
}
|
||||
break;
|
||||
case "boolean":
|
||||
// boolean校验
|
||||
String booleanValue = param.getValue();
|
||||
try {
|
||||
Boolean.parseBoolean(booleanValue);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Params field " + field + " value "
|
||||
+ booleanValue + " is invalid boolean value.");
|
||||
}
|
||||
break;
|
||||
// todo 更多参数定义与实际值格式校验
|
||||
@@ -236,6 +250,19 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteMonitors(Set<Long> ids) throws RuntimeException {
|
||||
List<Monitor> monitors = monitorDao.findMonitorsByIdIn(ids);
|
||||
if (monitors != null) {
|
||||
monitorDao.deleteAll(monitors);
|
||||
paramDao.deleteParamsByMonitorIdIn(ids);
|
||||
for (Monitor monitor : monitors) {
|
||||
jobScheduling.cancelAsyncCollectJob(monitor.getJobId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public MonitorDto getMonitor(long id) throws RuntimeException {
|
||||
@@ -250,4 +277,9 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Monitor> getMonitors(Specification<Monitor> specification, PageRequest pageRequest) {
|
||||
return monitorDao.findAll(specification, pageRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ public class GlobalExceptionHandler {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[input argument not valid happen]-{}", errorMessage, e);
|
||||
}
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).build();
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).code(PARAM_INVALID).build();
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public class GlobalExceptionHandler {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.warn("[scheduler warning]-{}", errorMessage);
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).code(MONITOR_CONFLICT).build();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ public class GlobalExceptionHandler {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.warn("[database error happen]-{}", errorMessage, exception);
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).code(MONITOR_CONFLICT).build();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ public class GlobalExceptionHandler {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.error("[monitor]-[unknown error happen]-{}", errorMessage, exception);
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).code(MONITOR_CONFLICT).build();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,4 +32,4 @@ param:
|
||||
required: false
|
||||
# 当type为boolean时,前端用switch展示开关
|
||||
# 当type为radio单选框,checkbox复选框时,option表示可选项值列表
|
||||
option: Yes,No
|
||||
# option: Yes,No
|
||||
Reference in New Issue
Block a user