mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 03:58:14 +00:00
[improvement][headless]Refactor translator module to make code logic cleaner and more readable.
This commit is contained in:
@@ -8,5 +8,5 @@ import java.util.List;
|
||||
@Data
|
||||
public class MetricDefineByMeasureParams extends MetricDefineParams {
|
||||
|
||||
private List<MeasureParam> measures = Lists.newArrayList();
|
||||
private List<Measure> measures = Lists.newArrayList();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.tencent.supersonic.headless.api.pojo.enums;
|
||||
|
||||
import com.tencent.supersonic.headless.api.pojo.MeasureParam;
|
||||
import com.tencent.supersonic.headless.api.pojo.Measure;
|
||||
import com.tencent.supersonic.headless.api.pojo.MetricDefineByMeasureParams;
|
||||
|
||||
import java.util.List;
|
||||
@@ -18,11 +18,6 @@ public enum MetricType {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Boolean isDerived(String src) {
|
||||
MetricType metricType = of(src);
|
||||
return Objects.nonNull(metricType) && metricType.equals(DERIVED);
|
||||
}
|
||||
|
||||
public static Boolean isDerived(MetricDefineType metricDefineType,
|
||||
MetricDefineByMeasureParams typeParams) {
|
||||
if (MetricDefineType.METRIC.equals(metricDefineType)) {
|
||||
@@ -32,7 +27,7 @@ public enum MetricType {
|
||||
return true;
|
||||
}
|
||||
if (MetricDefineType.MEASURE.equals(metricDefineType)) {
|
||||
List<MeasureParam> measures = typeParams.getMeasures();
|
||||
List<Measure> measures = typeParams.getMeasures();
|
||||
if (measures.size() > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,12 +3,19 @@ package com.tencent.supersonic.headless.api.pojo.response;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@ToString(callSuper = true)
|
||||
public class DimSchemaResp extends DimensionResp {
|
||||
|
||||
private Long useCnt = 0L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return super.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,9 @@ package com.tencent.supersonic.headless.api.pojo.response;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.supersonic.common.pojo.DataFormat;
|
||||
import com.tencent.supersonic.headless.api.pojo.DrillDownDimension;
|
||||
import com.tencent.supersonic.headless.api.pojo.MetricDefineByFieldParams;
|
||||
import com.tencent.supersonic.headless.api.pojo.MetricDefineByMeasureParams;
|
||||
import com.tencent.supersonic.headless.api.pojo.MetricDefineByMetricParams;
|
||||
import com.tencent.supersonic.headless.api.pojo.RelateDimension;
|
||||
import com.tencent.supersonic.headless.api.pojo.SchemaItem;
|
||||
import com.tencent.supersonic.headless.api.pojo.*;
|
||||
import com.tencent.supersonic.headless.api.pojo.enums.MetricDefineType;
|
||||
import com.tencent.supersonic.headless.api.pojo.enums.MetricType;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
@@ -69,6 +65,19 @@ public class MetricResp extends SchemaItem {
|
||||
|
||||
private boolean containsPartitionDimensions;
|
||||
|
||||
public void setMetricDefinition(MetricDefineType type, MetricDefineParams params) {
|
||||
if (MetricDefineType.MEASURE.equals(type)) {
|
||||
assert params instanceof MetricDefineByMeasureParams;
|
||||
metricDefineByMeasureParams = (MetricDefineByMeasureParams) params;
|
||||
} else if (MetricDefineType.FIELD.equals(type)) {
|
||||
assert params instanceof MetricDefineByFieldParams;
|
||||
metricDefineByFieldParams = (MetricDefineByFieldParams) params;
|
||||
} else if (MetricDefineType.METRIC.equals(type)) {
|
||||
assert params instanceof MetricDefineByMetricParams;
|
||||
metricDefineByMetricParams = (MetricDefineByMetricParams) params;
|
||||
}
|
||||
}
|
||||
|
||||
public void setClassifications(String tag) {
|
||||
if (StringUtils.isBlank(tag)) {
|
||||
classifications = Lists.newArrayList();
|
||||
@@ -105,4 +114,8 @@ public class MetricResp extends SchemaItem {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public boolean isDerived() {
|
||||
return MetricType.isDerived(metricDefineType, metricDefineByMeasureParams);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,26 @@
|
||||
package com.tencent.supersonic.headless.api.pojo.response;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
@ToString(callSuper = true)
|
||||
public class MetricSchemaResp extends MetricResp {
|
||||
|
||||
private Long useCnt = 0L;
|
||||
private Set<String> fields = Sets.newHashSet();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return super.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,14 +3,20 @@ package com.tencent.supersonic.headless.api.pojo.response;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.supersonic.common.pojo.ModelRela;
|
||||
import com.tencent.supersonic.common.pojo.enums.QueryType;
|
||||
import com.tencent.supersonic.headless.api.pojo.SchemaItem;
|
||||
import com.tencent.supersonic.headless.api.pojo.enums.SchemaType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@@ -39,6 +45,14 @@ public class SemanticSchemaResp {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public List<MetricSchemaResp> getMetrics(List<String> bizNames) {
|
||||
Map<String, MetricSchemaResp> metricLowerToNameMap = metrics.stream().collect(
|
||||
Collectors.toMap(entry -> entry.getBizName().toLowerCase(), entry -> entry));
|
||||
return bizNames.stream().map(String::toLowerCase)
|
||||
.filter(entry -> metricLowerToNameMap.containsKey(entry))
|
||||
.map(entry -> metricLowerToNameMap.get(entry)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public DimSchemaResp getDimension(String bizName) {
|
||||
return dimensions.stream()
|
||||
.filter(dimension -> bizName.equalsIgnoreCase(dimension.getBizName())).findFirst()
|
||||
@@ -50,6 +64,14 @@ public class SemanticSchemaResp {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public List<DimSchemaResp> getDimensions(List<String> bizNames) {
|
||||
Map<String, DimSchemaResp> dimLowerToNameMap = dimensions.stream().collect(
|
||||
Collectors.toMap(entry -> entry.getBizName().toLowerCase(), entry -> entry));
|
||||
return bizNames.stream().map(String::toLowerCase)
|
||||
.filter(entry -> dimLowerToNameMap.containsKey(entry))
|
||||
.map(entry -> dimLowerToNameMap.get(entry)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Set<String> getNameFromBizNames(Set<String> bizNames) {
|
||||
Set<String> names = new HashSet<>();
|
||||
for (String bizName : bizNames) {
|
||||
@@ -65,4 +87,32 @@ public class SemanticSchemaResp {
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public Map<String, String> getNameToBizNameMap() {
|
||||
// support fieldName and field alias to bizName
|
||||
Map<String, String> dimensionResults = dimensions.stream().flatMap(
|
||||
entry -> getPairStream(entry.getAlias(), entry.getName(), entry.getBizName()))
|
||||
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight, (k1, k2) -> k1));
|
||||
|
||||
Map<String, String> metricResults = metrics.stream().flatMap(
|
||||
entry -> getPairStream(entry.getAlias(), entry.getName(), entry.getBizName()))
|
||||
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight, (k1, k2) -> k1));
|
||||
|
||||
dimensionResults.putAll(metricResults);
|
||||
return dimensionResults;
|
||||
}
|
||||
|
||||
private Stream<Pair<String, String>> getPairStream(String aliasStr, String name,
|
||||
String bizName) {
|
||||
Set<Pair<String, String>> elements = new HashSet<>();
|
||||
elements.add(Pair.of(name, bizName));
|
||||
if (StringUtils.isNotBlank(aliasStr)) {
|
||||
List<String> aliasList = SchemaItem.getAliasList(aliasStr);
|
||||
for (String alias : aliasList) {
|
||||
elements.add(Pair.of(alias, bizName));
|
||||
}
|
||||
}
|
||||
return elements.stream();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user