review: 重构代码.

This commit is contained in:
lijiahang
2023-11-09 16:10:58 +08:00
parent 0ee6ebe207
commit 6fc6c61d48
22 changed files with 366 additions and 50 deletions

View File

@@ -29,17 +29,9 @@ Authorization: {{token}}
### 查询主机
POST {{baseUrl}}/asset/host/list-all
Content-Type: application/json
GET {{baseUrl}}/asset/host/list
Authorization: {{token}}
{
"id": "",
"name": "",
"code": "",
"address": ""
}
### 分页查询主机
POST {{baseUrl}}/asset/host/query

View File

@@ -45,6 +45,8 @@ public class HostController {
@Resource
private HostConfigService hostConfigService;
// fixme host_config 设置为缓存
@OperatorLog(HostOperatorType.CREATE)
@PostMapping("/create")
@Operation(summary = "创建主机")
@@ -79,11 +81,11 @@ public class HostController {
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/list-all")
@GetMapping("/list")
@Operation(summary = "查询主机")
@PreAuthorize("@ss.hasPermission('asset:host:query')")
public List<HostVO> getHostListAll(@Validated @RequestBody HostQueryRequest request) {
return hostService.getHostList(request);
public List<HostVO> getHostList() {
return hostService.getHostListByCache();
}
@IgnoreLog(IgnoreLogMode.RET)

View File

@@ -0,0 +1,143 @@
// package com.orion.ops.module.asset.controller;
//
// import com.orion.lang.define.wrapper.DataGrid;
// import com.orion.ops.framework.biz.operator.log.core.annotation.OperatorLog;
// import com.orion.ops.framework.common.validator.group.Page;
// import com.orion.ops.framework.log.core.annotation.IgnoreLog;
// import com.orion.ops.framework.log.core.enums.IgnoreLogMode;
// import com.orion.ops.framework.web.core.annotation.RestWrapper;
// import com.orion.ops.module.asset.define.operator.HostOperatorType;
// import com.orion.ops.module.asset.entity.request.host.*;
// import com.orion.ops.module.asset.entity.vo.HostConfigVO;
// import com.orion.ops.module.asset.entity.vo.HostVO;
// import com.orion.ops.module.asset.service.HostConfigService;
// import com.orion.ops.module.asset.service.HostService;
// import io.swagger.v3.oas.annotations.Operation;
// import io.swagger.v3.oas.annotations.Parameter;
// import io.swagger.v3.oas.annotations.tags.Tag;
// import lombok.extern.slf4j.Slf4j;
// import org.springframework.security.access.prepost.PreAuthorize;
// import org.springframework.validation.annotation.Validated;
// import org.springframework.web.bind.annotation.*;
//
// import javax.annotation.Resource;
// import java.util.List;
//
// /**
// * 主机 api
// *
// * @author Jiahang Li
// * @version 1.0.0
// * @since 2023-9-11 14:16
// */
// @Tag(name = "asset - 主机服务")
// @Slf4j
// @Validated
// @RestWrapper
// @RestController
// @RequestMapping("/asset/host-group")
// @SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
// public class HostGroupController {
//
// @Resource
// private HostService hostService;
//
// @Resource
// private HostConfigService hostConfigService;
//
// @OperatorLog(HostOperatorType.CREATE)
// @PostMapping("/create")
// @Operation(summary = "创建主机")
// @PreAuthorize("@ss.hasPermission('asset:host:create')")
// public Long createHost(@Validated @RequestBody HostCreateRequest request) {
// return hostService.createHost(request);
// }
//
// @OperatorLog(HostOperatorType.UPDATE)
// @PutMapping("/update")
// @Operation(summary = "通过 id 更新主机")
// @PreAuthorize("@ss.hasPermission('asset:host:update')")
// public Integer updateHost(@Validated @RequestBody HostUpdateRequest request) {
// return hostService.updateHostById(request);
// }
//
// @IgnoreLog(IgnoreLogMode.RET)
// @GetMapping("/get")
// @Operation(summary = "通过 id 查询主机")
// @Parameter(name = "id", description = "id", required = true)
// @Parameter(name = "extra", description = "是否查询额外信息")
// @Parameter(name = "config", description = "是否查询配置信息")
// @PreAuthorize("@ss.hasPermission('asset:host:query')")
// public HostVO getHost(@RequestParam("id") Long id,
// @RequestParam(name = "extra", required = false) boolean extra,
// @RequestParam(name = "config", required = false) boolean config) {
// HostQueryRequest request = new HostQueryRequest();
// request.setId(id);
// request.setExtra(extra);
// request.setConfig(config);
// return hostService.getHostById(request);
// }
//
// @IgnoreLog(IgnoreLogMode.RET)
// @PostMapping("/list")
// @Operation(summary = "查询主机")
// @PreAuthorize("@ss.hasPermission('asset:host:query')")
// public List<HostVO> getHostList() {
// return hostService.getHostListByCache();
// }
//
// @IgnoreLog(IgnoreLogMode.RET)
// @PostMapping("/query")
// @Operation(summary = "分页查询主机")
// @PreAuthorize("@ss.hasPermission('asset:host:query')")
// public DataGrid<HostVO> getHostPage(@Validated(Page.class) @RequestBody HostQueryRequest request) {
// return hostService.getHostPage(request);
// }
//
// @OperatorLog(HostOperatorType.DELETE)
// @DeleteMapping("/delete")
// @Operation(summary = "通过 id 删除主机")
// @Parameter(name = "id", description = "id", required = true)
// @PreAuthorize("@ss.hasPermission('asset:host:delete')")
// public Integer deleteHost(@RequestParam("id") Long id) {
// return hostService.deleteHostById(id);
// }
//
// @IgnoreLog(IgnoreLogMode.RET)
// @GetMapping("/get-config")
// @Operation(summary = "查询主机配置")
// @Parameter(name = "hostId", description = "hostId", required = true)
// @Parameter(name = "type", description = "配置类型", required = true)
// @PreAuthorize("@ss.hasPermission('asset:host:query')")
// public HostConfigVO getHostConfig(@RequestParam("hostId") Long hostId,
// @RequestParam(name = "type") String type) {
// return hostConfigService.getHostConfig(hostId, type);
// }
//
// @IgnoreLog(IgnoreLogMode.RET)
// @GetMapping("/get-config-all")
// @Operation(summary = "查询主机配置 - 全部")
// @Parameter(name = "hostId", description = "hostId", required = true)
// @PreAuthorize("@ss.hasPermission('asset:host:query')")
// public List<HostConfigVO> getHostConfig(@RequestParam("hostId") Long hostId) {
// return hostConfigService.getHostConfig(hostId);
// }
//
// @OperatorLog(HostOperatorType.UPDATE_CONFIG)
// @PutMapping("/update-config")
// @Operation(summary = "更新主机配置")
// @PreAuthorize("@ss.hasPermission('asset:host:update-config')")
// public Integer updateHostConfig(@Validated @RequestBody HostConfigUpdateRequest request) {
// return hostConfigService.updateHostConfig(request);
// }
//
// @OperatorLog(HostOperatorType.UPDATE_CONFIG_STATUS)
// @PutMapping("/update-config-status")
// @Operation(summary = "更新主机配置状态")
// @PreAuthorize("@ss.hasPermission('asset:host:update-config')")
// public Integer updateHostConfigStatus(@Validated @RequestBody HostConfigUpdateStatusRequest request) {
// return hostConfigService.updateHostConfigStatus(request);
// }
//
// }
//

View File

@@ -62,7 +62,7 @@ public class HostKeyController {
@GetMapping("/get")
@Operation(summary = "查询主机秘钥详情")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasAnyPermission('asset:host-key:detail', 'asset:host-key:update')")
@PreAuthorize("@ss.hasAnyPermission('asset:host-key:query-detail', 'asset:host-key:update')")
public HostKeyVO getHostKey(@RequestParam("id") Long id) {
return hostKeyService.getHostKeyById(id);
}

View File

@@ -1,6 +1,7 @@
package com.orion.ops.module.asset.convert;
import com.orion.ops.module.asset.entity.domain.HostDO;
import com.orion.ops.module.asset.entity.dto.HostCacheDTO;
import com.orion.ops.module.asset.entity.request.host.HostCreateRequest;
import com.orion.ops.module.asset.entity.request.host.HostQueryRequest;
import com.orion.ops.module.asset.entity.request.host.HostUpdateRequest;
@@ -8,8 +9,6 @@ import com.orion.ops.module.asset.entity.vo.HostVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 主机 内部对象转换器
*
@@ -30,6 +29,8 @@ public interface HostConvert {
HostVO to(HostDO domain);
List<HostVO> to(List<HostDO> list);
HostVO to(HostCacheDTO cache);
HostCacheDTO toCache(HostDO domain);
}

View File

@@ -2,6 +2,7 @@ package com.orion.ops.module.asset.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.ops.module.asset.entity.dto.HostCacheDTO;
import com.orion.ops.module.asset.entity.dto.HostIdentityCacheDTO;
import com.orion.ops.module.asset.entity.dto.HostKeyCacheDTO;
@@ -16,18 +17,25 @@ import java.util.concurrent.TimeUnit;
*/
public interface HostCacheKeyDefine {
CacheKeyDefine HOST_INFO = new CacheKeyBuilder()
.key("host:info:list")
.desc("主机列表")
.type(HostCacheDTO.class)
.timeout(1, TimeUnit.DAYS)
.build();
CacheKeyDefine HOST_KEY = new CacheKeyBuilder()
.key("host:key:list")
.desc("主机秘钥列表")
.type(HostKeyCacheDTO.class)
.timeout(1, TimeUnit.HOURS)
.timeout(1, TimeUnit.DAYS)
.build();
CacheKeyDefine HOST_IDENTITY = new CacheKeyBuilder()
.key("host:identity:list")
.desc("主机身份列表")
.type(HostIdentityCacheDTO.class)
.timeout(1, TimeUnit.HOURS)
.timeout(1, TimeUnit.DAYS)
.build();
}

View File

@@ -0,0 +1,37 @@
package com.orion.ops.module.asset.entity.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 主机 缓存对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-9-11 14:16
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "HostCacheDTO", description = "主机 缓存对象")
public class HostCacheDTO implements Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "主机名称")
private String name;
@Schema(description = "主机编码")
private String code;
@Schema(description = "主机地址")
private String address;
}

View File

@@ -44,10 +44,9 @@ public interface HostService {
/**
* 查询主机
*
* @param request request
* @return rows
*/
List<HostVO> getHostList(HostQueryRequest request);
List<HostVO> getHostListByCache();
/**
* 分页查询主机

View File

@@ -7,15 +7,19 @@ import com.orion.lang.utils.Booleans;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.asset.convert.HostConfigConvert;
import com.orion.ops.module.asset.convert.HostConvert;
import com.orion.ops.module.asset.dao.HostConfigDAO;
import com.orion.ops.module.asset.dao.HostDAO;
import com.orion.ops.module.asset.define.cache.HostCacheKeyDefine;
import com.orion.ops.module.asset.entity.domain.HostConfigDO;
import com.orion.ops.module.asset.entity.domain.HostDO;
import com.orion.ops.module.asset.entity.dto.HostCacheDTO;
import com.orion.ops.module.asset.entity.request.host.HostCreateRequest;
import com.orion.ops.module.asset.entity.request.host.HostQueryRequest;
import com.orion.ops.module.asset.entity.request.host.HostUpdateRequest;
@@ -51,6 +55,8 @@ import java.util.stream.Collectors;
@Service
public class HostServiceImpl implements HostService {
// FIXME 这里的收藏删除
private static final ThreadLocal<List<Long>> FAVORITE_HOLDER = new ThreadLocal<>();
@Resource
@@ -87,6 +93,8 @@ public class HostServiceImpl implements HostService {
}
// 创建配置
hostConfigService.initHostConfig(id);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO);
return id;
}
@@ -105,6 +113,8 @@ public class HostServiceImpl implements HostService {
// 更新
int effect = hostDAO.updateById(updateRecord);
log.info("HostService-updateHostById effect: {}", effect);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO);
// 更新 tag
tagRelApi.setTagRel(TagTypeEnum.HOST, id, request.getTags());
return effect;
@@ -123,21 +133,27 @@ public class HostServiceImpl implements HostService {
}
@Override
public List<HostVO> getHostList(HostQueryRequest request) {
try {
// 条件
LambdaQueryWrapper<HostDO> wrapper = this.buildQueryWrapper(request);
if (wrapper == null) {
return Lists.empty();
public List<HostVO> getHostListByCache() {
// 查询缓存
List<HostCacheDTO> list = RedisMaps.valuesJson(HostCacheKeyDefine.HOST_INFO);
if (list.isEmpty()) {
// 查询数据库
list = hostDAO.of().list(HostConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(HostCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 查询
List<HostVO> hosts = hostDAO.of(wrapper).list(HostConvert.MAPPER::to);
// 查询拓展信息
this.setExtraInfo(request, hosts);
return hosts;
} finally {
FAVORITE_HOLDER.remove();
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_INFO.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_INFO);
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(HostConvert.MAPPER::to)
.collect(Collectors.toList());
}
@Override
@@ -171,6 +187,8 @@ public class HostServiceImpl implements HostService {
// 删除
int effect = hostDAO.deleteById(id);
log.info("HostService-deleteHostById effect: {}", effect);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO, id);
// 删除配置
hostConfigDAO.deleteByHostId(id);
// 删除 tag 引用