mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 20:51:48 +00:00
support query rule management (#853)
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ActionInfo {
|
||||||
|
|
||||||
|
private List<Object> out;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RuleInfo {
|
||||||
|
|
||||||
|
private Mode mode;
|
||||||
|
private List<Object> parameters;
|
||||||
|
|
||||||
|
public enum Mode {
|
||||||
|
/**
|
||||||
|
* BEFORE, some days ago
|
||||||
|
* RECENT, the last few days
|
||||||
|
* EXIST, there was some information
|
||||||
|
*/
|
||||||
|
BEFORE, RECENT, EXIST
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo.enums;
|
||||||
|
|
||||||
|
public enum QueryRuleType {
|
||||||
|
|
||||||
|
ADD_DATE,
|
||||||
|
ADD_SELECT
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class QueryRuleFilter {
|
||||||
|
|
||||||
|
private List<Integer> statusList;
|
||||||
|
|
||||||
|
private List<Long> ruleIds;
|
||||||
|
|
||||||
|
private List<Long> dataSetIds;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo.request;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.ActionInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.RuleInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.SchemaItem;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.enums.QueryRuleType;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
public class QueryRuleReq extends SchemaItem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dataSetID
|
||||||
|
*/
|
||||||
|
private Long dataSetId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则的优先级, 1-低,2-中,3-高
|
||||||
|
*/
|
||||||
|
private Integer priority = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则类型
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private QueryRuleType ruleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 具体规则信息
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private RuleInfo rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则输出信息
|
||||||
|
*/
|
||||||
|
private ActionInfo action;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展信息
|
||||||
|
*/
|
||||||
|
private Map<String, String> ext = new HashMap<>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.tencent.supersonic.headless.api.pojo.response;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.ActionInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.RuleInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.SchemaItem;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.enums.QueryRuleType;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class QueryRuleResp extends SchemaItem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dataSetID
|
||||||
|
*/
|
||||||
|
private Long dataSetId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则的优先级, 1-低,2-中,3-高
|
||||||
|
*/
|
||||||
|
private Integer priority = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则类型
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private QueryRuleType ruleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 具体规则信息
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private RuleInfo rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则输出信息
|
||||||
|
*/
|
||||||
|
private ActionInfo action;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展信息
|
||||||
|
*/
|
||||||
|
private Map<String, String> ext;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.persistence.dataobject;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("s2_query_rule")
|
||||||
|
public class QueryRuleDO {
|
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dataSetID
|
||||||
|
*/
|
||||||
|
private Long dataSetId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则的优先级, 0-系统默认规则
|
||||||
|
*/
|
||||||
|
private Integer priority;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则类型
|
||||||
|
*/
|
||||||
|
private String ruleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则业务名称
|
||||||
|
*/
|
||||||
|
private String bizName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 具体规则信息
|
||||||
|
*/
|
||||||
|
private String rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则输出信息
|
||||||
|
*/
|
||||||
|
private String action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态,0-正常,1-下线,2-删除
|
||||||
|
*/
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
private Date createdAt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*/
|
||||||
|
private String createdBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新时间
|
||||||
|
*/
|
||||||
|
private Date updatedAt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新人
|
||||||
|
*/
|
||||||
|
private String updatedBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展信息
|
||||||
|
*/
|
||||||
|
private String ext;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.persistence.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.dataobject.QueryRuleDO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface QueryRuleMapper extends BaseMapper<QueryRuleDO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.persistence.repository;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.dataobject.QueryRuleDO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface QueryRuleRepository {
|
||||||
|
|
||||||
|
Integer create(QueryRuleDO queryRuleDO);
|
||||||
|
|
||||||
|
Integer update(QueryRuleDO queryRuleDO);
|
||||||
|
|
||||||
|
QueryRuleDO getQueryRuleById(Long id);
|
||||||
|
|
||||||
|
List<QueryRuleDO> getQueryRules(QueryRuleFilter filter);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.persistence.repository.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.dataobject.QueryRuleDO;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.mapper.QueryRuleMapper;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.repository.QueryRuleRepository;
|
||||||
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class QueryRuleRepositoryImpl implements QueryRuleRepository {
|
||||||
|
|
||||||
|
private final QueryRuleMapper mapper;
|
||||||
|
|
||||||
|
public QueryRuleRepositoryImpl(QueryRuleMapper mapper) {
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer create(QueryRuleDO queryRuleDO) {
|
||||||
|
return mapper.insert(queryRuleDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer update(QueryRuleDO queryRuleDO) {
|
||||||
|
return mapper.updateById(queryRuleDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QueryRuleDO getQueryRuleById(Long id) {
|
||||||
|
return mapper.selectById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<QueryRuleDO> getQueryRules(QueryRuleFilter filter) {
|
||||||
|
QueryWrapper<QueryRuleDO> wrapper = new QueryWrapper<>();
|
||||||
|
if (CollectionUtils.isNotEmpty(filter.getRuleIds())) {
|
||||||
|
wrapper.lambda().in(QueryRuleDO::getId, filter.getRuleIds());
|
||||||
|
}
|
||||||
|
if (CollectionUtils.isNotEmpty(filter.getDataSetIds())) {
|
||||||
|
wrapper.lambda().in(QueryRuleDO::getDataSetId, filter.getDataSetIds());
|
||||||
|
}
|
||||||
|
if (CollectionUtils.isNotEmpty(filter.getStatusList())) {
|
||||||
|
wrapper.lambda().in(QueryRuleDO::getStatus, filter.getStatusList());
|
||||||
|
}
|
||||||
|
wrapper.lambda().gt(QueryRuleDO::getPriority, 0);
|
||||||
|
List<QueryRuleDO> queryRuleDOList = mapper.selectList(wrapper);
|
||||||
|
|
||||||
|
QueryWrapper<QueryRuleDO> wrapperSys = new QueryWrapper<>();
|
||||||
|
// 返回系统设置的规则
|
||||||
|
wrapperSys.or().eq("priority", 0L);
|
||||||
|
List<QueryRuleDO> queryRuleDOListSys = mapper.selectList(wrapperSys);
|
||||||
|
|
||||||
|
queryRuleDOList.addAll(queryRuleDOListSys);
|
||||||
|
return queryRuleDOList;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.rest;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
|
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleReq;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.QueryRuleResp;
|
||||||
|
import com.tencent.supersonic.headless.server.service.QueryRuleService;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/semantic/query/rule")
|
||||||
|
public class QueryRuleController {
|
||||||
|
|
||||||
|
private final QueryRuleService queryRuleService;
|
||||||
|
|
||||||
|
public QueryRuleController(QueryRuleService queryRuleService) {
|
||||||
|
this.queryRuleService = queryRuleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新建查询规则
|
||||||
|
*
|
||||||
|
* @param queryRuleReq
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@PostMapping("/create")
|
||||||
|
public QueryRuleResp create(@RequestBody @Validated QueryRuleReq queryRuleReq,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
User user = UserHolder.findUser(request, response);
|
||||||
|
return queryRuleService.addQueryRule(queryRuleReq, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑查询规则
|
||||||
|
*
|
||||||
|
* @param queryRuleReq
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@PostMapping("/update")
|
||||||
|
public QueryRuleResp update(@RequestBody @Validated QueryRuleReq queryRuleReq,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
User user = UserHolder.findUser(request, response);
|
||||||
|
return queryRuleService.updateQueryRule(queryRuleReq, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除查询规则
|
||||||
|
* @param id
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@DeleteMapping("delete/{id}")
|
||||||
|
public Boolean delete(@PathVariable("id") Long id,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
User user = UserHolder.findUser(request, response);
|
||||||
|
return queryRuleService.dropQueryRule(id, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询规则列表
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("query")
|
||||||
|
public List<QueryRuleResp> query(@RequestBody @Validated QueryRuleFilter queryRuleFilter,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
User user = UserHolder.findUser(request, response);
|
||||||
|
return queryRuleService.getQueryRuleList(queryRuleFilter, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.service;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleReq;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.QueryRuleResp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface QueryRuleService {
|
||||||
|
|
||||||
|
QueryRuleResp addQueryRule(QueryRuleReq queryRuleReq, User user);
|
||||||
|
|
||||||
|
QueryRuleResp updateQueryRule(QueryRuleReq queryRuleReq, User user);
|
||||||
|
|
||||||
|
Boolean dropQueryRule(Long id, User user);
|
||||||
|
|
||||||
|
QueryRuleResp getQueryRuleById(Long id, User user);
|
||||||
|
|
||||||
|
List<QueryRuleResp> getQueryRuleList(QueryRuleFilter queryRuleFilter, User user);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.service.impl;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
|
import com.tencent.supersonic.common.pojo.enums.StatusEnum;
|
||||||
|
import com.tencent.supersonic.common.util.BeanMapper;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleReq;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.DataSetResp;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.QueryRuleResp;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.dataobject.QueryRuleDO;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.repository.QueryRuleRepository;
|
||||||
|
import com.tencent.supersonic.headless.server.service.DataSetService;
|
||||||
|
import com.tencent.supersonic.headless.server.service.QueryRuleService;
|
||||||
|
import com.tencent.supersonic.headless.server.utils.QueryRuleConverter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class QueryRuleServiceImpl implements QueryRuleService {
|
||||||
|
|
||||||
|
private final QueryRuleRepository queryRuleRepository;
|
||||||
|
private final DataSetService dataSetService;
|
||||||
|
|
||||||
|
public QueryRuleServiceImpl(QueryRuleRepository queryRuleRepository, DataSetService dataSetService) {
|
||||||
|
this.queryRuleRepository = queryRuleRepository;
|
||||||
|
this.dataSetService = dataSetService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QueryRuleResp addQueryRule(QueryRuleReq queryRuleReq, User user) {
|
||||||
|
checkPermission(queryRuleReq, user);
|
||||||
|
QueryRuleDO queryRuleDO = QueryRuleConverter.convert2DO(queryRuleReq);
|
||||||
|
|
||||||
|
Date date = new Date();
|
||||||
|
queryRuleDO.setCreatedBy(user.getName());
|
||||||
|
queryRuleDO.setCreatedAt(date);
|
||||||
|
queryRuleDO.setUpdatedBy(user.getName());
|
||||||
|
queryRuleDO.setUpdatedAt(date);
|
||||||
|
queryRuleDO.setStatus(StatusEnum.ONLINE.getCode());
|
||||||
|
queryRuleDO.setId(null);
|
||||||
|
|
||||||
|
queryRuleRepository.create(queryRuleDO);
|
||||||
|
return getQueryRuleById(queryRuleDO.getId(), user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QueryRuleResp updateQueryRule(QueryRuleReq queryRuleReq, User user) {
|
||||||
|
checkPermission(queryRuleReq, user);
|
||||||
|
QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(queryRuleReq.getId());
|
||||||
|
QueryRuleDO queryRuleNew = QueryRuleConverter.convert2DO(queryRuleReq);
|
||||||
|
BeanMapper.mapper(queryRuleNew, queryRuleDO);
|
||||||
|
queryRuleDO.setUpdatedAt(new Date());
|
||||||
|
queryRuleDO.setUpdatedBy(user.getName());
|
||||||
|
queryRuleRepository.update(queryRuleDO);
|
||||||
|
return getQueryRuleById(queryRuleDO.getId(), user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean dropQueryRule(Long id, User user) {
|
||||||
|
QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(id);
|
||||||
|
checkPermission(queryRuleDO, user);
|
||||||
|
queryRuleDO.setStatus(StatusEnum.DELETED.getCode());
|
||||||
|
queryRuleRepository.update(queryRuleDO);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QueryRuleResp getQueryRuleById(Long id, User user) {
|
||||||
|
QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(id);
|
||||||
|
QueryRuleResp queryRuleResp = QueryRuleConverter.convert2Resp(queryRuleDO);
|
||||||
|
return queryRuleResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<QueryRuleResp> getQueryRuleList(QueryRuleFilter queryRuleFilter, User user) {
|
||||||
|
List<QueryRuleDO> queryRules = queryRuleRepository.getQueryRules(queryRuleFilter);
|
||||||
|
List<QueryRuleResp> queryRuleRespList = QueryRuleConverter.convert2RespList(queryRules);
|
||||||
|
return queryRuleRespList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkPermission(QueryRuleReq queryRuleReq, User user) {
|
||||||
|
String userName = user.getName();
|
||||||
|
if (Objects.nonNull(queryRuleReq.getDataSetId())) {
|
||||||
|
DataSetResp dataSet = dataSetService.getDataSet(queryRuleReq.getDataSetId());
|
||||||
|
if (dataSet.getAdmins().contains(userName) || dataSet.getCreatedBy().equalsIgnoreCase(userName)) {
|
||||||
|
log.debug(String.format("user:%s, queryRuleReq:%s", userName, queryRuleReq));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new RuntimeException("用户暂无权限变更数据集的规则, 请确认");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkPermission(QueryRuleDO queryRuleDO, User user) {
|
||||||
|
String userName = user.getName();
|
||||||
|
if (Objects.nonNull(queryRuleDO.getDataSetId())) {
|
||||||
|
DataSetResp dataSet = dataSetService.getDataSet(queryRuleDO.getDataSetId());
|
||||||
|
if (dataSet.getAdmins().contains(userName) || dataSet.getCreatedBy().equalsIgnoreCase(userName)) {
|
||||||
|
log.debug(String.format("user:%s, queryRuleDO:%s", userName, queryRuleDO));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new RuntimeException("用户暂无权限变更数据集的规则, 请确认");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.tencent.supersonic.headless.server.utils;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.common.util.JsonUtil;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.ActionInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.RuleInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.enums.QueryRuleType;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleReq;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.QueryRuleResp;
|
||||||
|
import com.tencent.supersonic.headless.server.persistence.dataobject.QueryRuleDO;
|
||||||
|
import org.apache.directory.api.util.Strings;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class QueryRuleConverter {
|
||||||
|
|
||||||
|
public static QueryRuleDO convert2DO(QueryRuleReq queryRuleReq) {
|
||||||
|
QueryRuleDO queryRuleDO = new QueryRuleDO();
|
||||||
|
BeanUtils.copyProperties(queryRuleReq, queryRuleDO);
|
||||||
|
queryRuleDO.setRuleType(queryRuleReq.getRuleType().name());
|
||||||
|
queryRuleDO.setRule(JsonUtil.toString(queryRuleReq.getRule()));
|
||||||
|
queryRuleDO.setAction(Objects.isNull(queryRuleReq.getAction()) ? "" :
|
||||||
|
JsonUtil.toString(queryRuleReq.getAction()));
|
||||||
|
queryRuleDO.setExt(JsonUtil.toString(queryRuleReq.getExt()));
|
||||||
|
|
||||||
|
return queryRuleDO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static QueryRuleResp convert2Resp(QueryRuleDO queryRuleDO) {
|
||||||
|
QueryRuleResp queryRuleResp = new QueryRuleResp();
|
||||||
|
BeanUtils.copyProperties(queryRuleDO, queryRuleResp);
|
||||||
|
queryRuleResp.setRuleType(QueryRuleType.valueOf(queryRuleDO.getRuleType()));
|
||||||
|
queryRuleResp.setRule(JsonUtil.toObject(queryRuleDO.getRule(), RuleInfo.class));
|
||||||
|
queryRuleResp.setAction(Strings.isEmpty(queryRuleDO.getAction()) ? new ActionInfo() :
|
||||||
|
JsonUtil.toObject(queryRuleDO.getAction(), ActionInfo.class));
|
||||||
|
queryRuleResp.setExt(JsonUtil.toMap(queryRuleDO.getExt(), String.class, String.class));
|
||||||
|
|
||||||
|
return queryRuleResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<QueryRuleResp> convert2RespList(List<QueryRuleDO> queryRules) {
|
||||||
|
List<QueryRuleResp> queryRuleRespList = new ArrayList<>();
|
||||||
|
queryRules.stream().forEach(queryRuleDO -> queryRuleRespList.add(convert2Resp(queryRuleDO)));
|
||||||
|
return queryRuleRespList;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -264,4 +264,25 @@ CREATE TABLE IF NOT EXISTS s2_tag(
|
|||||||
`updated_by` varchar(100) DEFAULT NULL ,
|
`updated_by` varchar(100) DEFAULT NULL ,
|
||||||
`ext` text DEFAULT NULL ,
|
`ext` text DEFAULT NULL ,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
--20240321
|
||||||
|
CREATE TABLE IF NOT EXISTS `s2_query_rule` (
|
||||||
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
|
`data_set_id` INT ,
|
||||||
|
`priority` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`rule_type` varchar(255) NOT NULL ,
|
||||||
|
`name` varchar(255) NOT NULL ,
|
||||||
|
`biz_name` varchar(255) NOT NULL ,
|
||||||
|
`description` varchar(500) DEFAULT NULL ,
|
||||||
|
`rule` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`action` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`status` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`created_at` TIMESTAMP NOT NULL ,
|
||||||
|
`created_by` varchar(100) NOT NULL ,
|
||||||
|
`updated_at` TIMESTAMP DEFAULT NULL ,
|
||||||
|
`updated_by` varchar(100) DEFAULT NULL ,
|
||||||
|
`ext` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
);
|
||||||
|
COMMENT ON TABLE s2_query_rule IS 'tag query rule table';
|
||||||
@@ -603,4 +603,24 @@ CREATE TABLE IF NOT EXISTS `s2_tag_object` (
|
|||||||
`ext` LONGVARCHAR DEFAULT NULL ,
|
`ext` LONGVARCHAR DEFAULT NULL ,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
);
|
);
|
||||||
COMMENT ON TABLE s2_tag IS 'tag object information';
|
COMMENT ON TABLE s2_tag IS 'tag object information';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `s2_query_rule` (
|
||||||
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
|
`data_set_id` INT ,
|
||||||
|
`priority` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`rule_type` varchar(255) NOT NULL ,
|
||||||
|
`name` varchar(255) NOT NULL ,
|
||||||
|
`biz_name` varchar(255) NOT NULL ,
|
||||||
|
`description` varchar(500) DEFAULT NULL ,
|
||||||
|
`rule` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`action` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`status` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`created_at` TIMESTAMP NOT NULL ,
|
||||||
|
`created_by` varchar(100) NOT NULL ,
|
||||||
|
`updated_at` TIMESTAMP DEFAULT NULL ,
|
||||||
|
`updated_by` varchar(100) DEFAULT NULL ,
|
||||||
|
`ext` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
);
|
||||||
|
COMMENT ON TABLE s2_query_rule IS 'tag query rule table';
|
||||||
@@ -526,4 +526,23 @@ CREATE TABLE IF NOT EXISTS `s2_tag_object`
|
|||||||
`ext` text DEFAULT NULL,
|
`ext` text DEFAULT NULL,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE = InnoDB
|
) ENGINE = InnoDB
|
||||||
DEFAULT CHARSET = utf8 COMMENT ='标签表对象';
|
DEFAULT CHARSET = utf8 COMMENT ='标签对象表';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `s2_query_rule` (
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
`data_set_id` bigint(20) ,
|
||||||
|
`priority` int(10) NOT NULL DEFAULT '1' ,
|
||||||
|
`rule_type` varchar(255) NOT NULL ,
|
||||||
|
`name` varchar(255) NOT NULL ,
|
||||||
|
`biz_name` varchar(255) NOT NULL ,
|
||||||
|
`description` varchar(500) DEFAULT NULL ,
|
||||||
|
`rule` text DEFAULT NULL ,
|
||||||
|
`action` text DEFAULT NULL ,
|
||||||
|
`status` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`created_at` datetime NOT NULL ,
|
||||||
|
`created_by` varchar(100) NOT NULL ,
|
||||||
|
`updated_at` datetime DEFAULT NULL ,
|
||||||
|
`updated_by` varchar(100) DEFAULT NULL ,
|
||||||
|
`ext` text DEFAULT NULL ,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='查询规则表';
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package com.tencent.supersonic.headless;
|
||||||
|
|
||||||
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.ActionInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.RuleInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.enums.QueryRuleType;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleFilter;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.request.QueryRuleReq;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.response.QueryRuleResp;
|
||||||
|
import com.tencent.supersonic.headless.server.service.QueryRuleService;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class QueryRuleTest extends BaseTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private QueryRuleService queryRuleService;
|
||||||
|
|
||||||
|
private User user = User.getFakeUser();
|
||||||
|
|
||||||
|
public QueryRuleReq addSystemRule() {
|
||||||
|
QueryRuleReq queryRuleReq = new QueryRuleReq();
|
||||||
|
queryRuleReq.setPriority(0);
|
||||||
|
queryRuleReq.setRuleType(QueryRuleType.ADD_DATE);
|
||||||
|
queryRuleReq.setName("全局默认时间设置");
|
||||||
|
queryRuleReq.setBizName("global date config");
|
||||||
|
RuleInfo rule = new RuleInfo();
|
||||||
|
rule.setMode(RuleInfo.Mode.BEFORE);
|
||||||
|
List parameters = new ArrayList(Arrays.asList(3));
|
||||||
|
rule.setParameters(parameters);
|
||||||
|
queryRuleReq.setRule(rule);
|
||||||
|
return queryRuleReq;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryRuleReq addUserRule1() {
|
||||||
|
QueryRuleReq queryRuleReq = new QueryRuleReq();
|
||||||
|
queryRuleReq.setDataSetId(2L);
|
||||||
|
queryRuleReq.setPriority(1);
|
||||||
|
queryRuleReq.setRuleType(QueryRuleType.ADD_DATE);
|
||||||
|
queryRuleReq.setName("规则_1");
|
||||||
|
queryRuleReq.setBizName("rule_1");
|
||||||
|
|
||||||
|
RuleInfo rule = new RuleInfo();
|
||||||
|
rule.setMode(RuleInfo.Mode.BEFORE);
|
||||||
|
List parameters = new ArrayList(Arrays.asList(4));
|
||||||
|
rule.setParameters(parameters);
|
||||||
|
queryRuleReq.setRule(rule);
|
||||||
|
return queryRuleReq;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryRuleReq addUserRule2() {
|
||||||
|
QueryRuleReq queryRuleReq = new QueryRuleReq();
|
||||||
|
queryRuleReq.setDataSetId(2L);
|
||||||
|
queryRuleReq.setPriority(1);
|
||||||
|
queryRuleReq.setRuleType(QueryRuleType.ADD_SELECT);
|
||||||
|
queryRuleReq.setName("规则_2");
|
||||||
|
queryRuleReq.setBizName("rule_2");
|
||||||
|
|
||||||
|
RuleInfo rule = new RuleInfo();
|
||||||
|
rule.setMode(RuleInfo.Mode.EXIST);
|
||||||
|
rule.setParameters(Arrays.asList("singer_id"));
|
||||||
|
queryRuleReq.setRule(rule);
|
||||||
|
|
||||||
|
ActionInfo action = new ActionInfo();
|
||||||
|
List parameters = new ArrayList(Arrays.asList("c1", "c2"));
|
||||||
|
action.setOut(parameters);
|
||||||
|
queryRuleReq.setAction(action);
|
||||||
|
return queryRuleReq;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddQueryRule() {
|
||||||
|
QueryRuleReq queryRuleReqSys = addSystemRule();
|
||||||
|
QueryRuleResp queryRuleResp = queryRuleService.addQueryRule(queryRuleReqSys, user);
|
||||||
|
QueryRuleResp queryRule = queryRuleService.getQueryRuleById(1L, user);
|
||||||
|
Assert.assertEquals(queryRuleResp.getPriority().intValue(), 0);
|
||||||
|
Assert.assertEquals(queryRule.getPriority().intValue(), 0);
|
||||||
|
|
||||||
|
QueryRuleReq queryRuleReq1 = addUserRule1();
|
||||||
|
QueryRuleResp queryRuleResp1 = queryRuleService.addQueryRule(queryRuleReq1, user);
|
||||||
|
queryRuleResp1.setName("规则_1_1");
|
||||||
|
QueryRuleReq queryRuleReq11 = new QueryRuleReq();
|
||||||
|
BeanUtils.copyProperties(queryRuleResp1, queryRuleReq11);
|
||||||
|
queryRuleService.updateQueryRule(queryRuleReq11, user);
|
||||||
|
|
||||||
|
QueryRuleReq queryRuleReq2 = addUserRule2();
|
||||||
|
queryRuleService.addQueryRule(queryRuleReq2, user);
|
||||||
|
|
||||||
|
QueryRuleFilter queryRuleFilter = new QueryRuleFilter();
|
||||||
|
List<QueryRuleResp> queryRuleList = queryRuleService.getQueryRuleList(queryRuleFilter, user);
|
||||||
|
queryRuleList.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -606,3 +606,23 @@ CREATE TABLE IF NOT EXISTS `s2_tag_object` (
|
|||||||
);
|
);
|
||||||
COMMENT ON TABLE s2_tag IS 'tag object information';
|
COMMENT ON TABLE s2_tag IS 'tag object information';
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `s2_query_rule` (
|
||||||
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
|
`data_set_id` INT ,
|
||||||
|
`priority` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`rule_type` varchar(255) NOT NULL ,
|
||||||
|
`name` varchar(255) NOT NULL ,
|
||||||
|
`biz_name` varchar(255) NOT NULL ,
|
||||||
|
`description` varchar(500) DEFAULT NULL ,
|
||||||
|
`rule` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`action` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
`status` INT NOT NULL DEFAULT '1' ,
|
||||||
|
`created_at` TIMESTAMP NOT NULL ,
|
||||||
|
`created_by` varchar(100) NOT NULL ,
|
||||||
|
`updated_at` TIMESTAMP DEFAULT NULL ,
|
||||||
|
`updated_by` varchar(100) DEFAULT NULL ,
|
||||||
|
`ext` LONGVARCHAR DEFAULT NULL ,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
);
|
||||||
|
COMMENT ON TABLE s2_query_rule IS 'tag query rule table';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user