(improvement)(headless) Refactor MetricDrillDownChecker code (#1356)

Co-authored-by: lxwcodemonkey
This commit is contained in:
LXW
2024-07-05 17:56:17 +08:00
committed by GitHub
parent 2ddf0ad41a
commit 7c86e2b3db
3 changed files with 23 additions and 18 deletions

View File

@@ -43,6 +43,7 @@ import com.tencent.supersonic.headless.core.utils.ComponentFactory;
import com.tencent.supersonic.headless.server.annotation.S2DataPermission;
import com.tencent.supersonic.headless.server.facade.service.SemanticLayerService;
import com.tencent.supersonic.headless.server.manager.SemanticSchemaManager;
import com.tencent.supersonic.headless.server.utils.MetricDrillDownChecker;
import com.tencent.supersonic.headless.server.utils.QueryReqConverter;
import com.tencent.supersonic.headless.server.utils.QueryUtils;
import com.tencent.supersonic.headless.server.utils.StatUtils;
@@ -77,6 +78,8 @@ public class S2SemanticLayerService implements SemanticLayerService {
private final SchemaService schemaService;
private final SemanticTranslator semanticTranslator;
private final MetricDrillDownChecker metricDrillDownChecker;
public S2SemanticLayerService(
StatUtils statUtils,
QueryUtils queryUtils,
@@ -84,7 +87,8 @@ public class S2SemanticLayerService implements SemanticLayerService {
SemanticSchemaManager semanticSchemaManager,
DataSetService dataSetService,
SchemaService schemaService,
SemanticTranslator semanticTranslator) {
SemanticTranslator semanticTranslator,
MetricDrillDownChecker metricDrillDownChecker) {
this.statUtils = statUtils;
this.queryUtils = queryUtils;
this.queryReqConverter = queryReqConverter;
@@ -92,6 +96,7 @@ public class S2SemanticLayerService implements SemanticLayerService {
this.dataSetService = dataSetService;
this.schemaService = schemaService;
this.semanticTranslator = semanticTranslator;
this.metricDrillDownChecker = metricDrillDownChecker;
}
public DataSetSchema getDataSetSchema(Long id) {
@@ -263,15 +268,17 @@ public class S2SemanticLayerService implements SemanticLayerService {
return querySqlReq;
}
private SemanticQueryResp query(QueryStatement queryStatement) throws Exception {
private SemanticQueryResp query(QueryStatement queryStatement) {
SemanticQueryResp semanticQueryResp = null;
try {
//1 translate
if (!queryStatement.isTranslated()) {
semanticTranslator.translate(queryStatement);
}
//2. query pre-check
queryPreCheck(queryStatement);
//2 execute
//3 execute
for (QueryExecutor queryExecutor : ComponentFactory.getQueryExecutors()) {
if (queryExecutor.accept(queryStatement)) {
semanticQueryResp = queryExecutor.execute(queryStatement);
@@ -286,6 +293,12 @@ public class S2SemanticLayerService implements SemanticLayerService {
}
}
private void queryPreCheck(QueryStatement queryStatement) {
//Check whether the dimensions of the metric drill-down are correct temporarily,
//add the abstraction of a validator later.
metricDrillDownChecker.checkQuery(queryStatement);
}
public EntityInfo getEntityInfo(SemanticParseInfo parseInfo, DataSetSchema dataSetSchema, User user) {
if (parseInfo != null && parseInfo.getDataSetId() != null && parseInfo.getDataSetId() > 0) {
EntityInfo entityInfo = getEntityBasicInfo(dataSetSchema);

View File

@@ -1,9 +1,9 @@
package com.tencent.supersonic.headless.server.aspect;
package com.tencent.supersonic.headless.server.utils;
import com.google.common.collect.Lists;
import com.tencent.supersonic.common.jsqlparser.SqlSelectHelper;
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
import com.tencent.supersonic.common.pojo.exception.InvalidArgumentException;
import com.tencent.supersonic.common.jsqlparser.SqlSelectHelper;
import com.tencent.supersonic.headless.api.pojo.DrillDownDimension;
import com.tencent.supersonic.headless.api.pojo.response.DimSchemaResp;
import com.tencent.supersonic.headless.api.pojo.response.DimensionResp;
@@ -14,9 +14,6 @@ import com.tencent.supersonic.headless.core.pojo.QueryStatement;
import com.tencent.supersonic.headless.server.web.service.MetricService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -25,7 +22,6 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Aspect
@Component
@Slf4j
public class MetricDrillDownChecker {
@@ -33,15 +29,10 @@ public class MetricDrillDownChecker {
@Autowired
private MetricService metricService;
@Around("execution(* com.tencent.supersonic.headless.core.translator.DefaultSemanticTranslator.parse(..))")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] objects = joinPoint.getArgs();
QueryStatement queryStatement = (QueryStatement) objects[0];
if (queryStatement.getDataSetQueryParam() == null) {
return joinPoint.proceed();
}
checkQuery(queryStatement.getSemanticSchemaResp(), queryStatement.getDataSetQueryParam().getSql());
return joinPoint.proceed();
public void checkQuery(QueryStatement queryStatement) {
SemanticSchemaResp semanticSchemaResp = queryStatement.getSemanticSchemaResp();
String sql = queryStatement.getDataSetQueryParam().getSql();
checkQuery(semanticSchemaResp, sql);
}
public void checkQuery(SemanticSchemaResp semanticSchemaResp, String sql) {

View File

@@ -7,6 +7,7 @@ import com.tencent.supersonic.headless.api.pojo.response.DimSchemaResp;
import com.tencent.supersonic.headless.api.pojo.response.MetricSchemaResp;
import com.tencent.supersonic.headless.api.pojo.response.SemanticSchemaResp;
import com.tencent.supersonic.headless.server.utils.DataUtils;
import com.tencent.supersonic.headless.server.utils.MetricDrillDownChecker;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.util.List;