|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.datavines.metric.plugin; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | + |
| 21 | +import io.datavines.common.config.CheckResult; |
| 22 | +import io.datavines.common.config.ConfigChecker; |
| 23 | +import io.datavines.common.entity.ExecuteSql; |
| 24 | +import io.datavines.common.enums.DataVinesDataType; |
| 25 | +import io.datavines.common.utils.StringUtils; |
| 26 | +import io.datavines.metric.api.ConfigItem; |
| 27 | +import io.datavines.metric.api.MetricDimension; |
| 28 | +import io.datavines.metric.api.MetricType; |
| 29 | +import io.datavines.metric.api.SqlMetric; |
| 30 | + |
| 31 | +import static io.datavines.common.CommonConstants.TABLE; |
| 32 | +import static io.datavines.common.ConfigConstants.*; |
| 33 | + |
| 34 | +public class CustomCountSql implements SqlMetric { |
| 35 | + |
| 36 | + private final Set<String> requiredOptions = new HashSet<>(); |
| 37 | + |
| 38 | + private final HashMap<String, ConfigItem> configMap = new HashMap<>(); |
| 39 | + |
| 40 | + private String invalidateItemsSql = null; |
| 41 | + |
| 42 | + public CustomCountSql() { |
| 43 | + configMap.put(INVALIDATE_ITEMS_SQL, new ConfigItem(INVALIDATE_ITEMS_SQL, "错误数据SQL", INVALIDATE_ITEMS_SQL)); |
| 44 | + |
| 45 | + requiredOptions.add(INVALIDATE_ITEMS_SQL); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getName() { |
| 50 | + return "custom_count_sql"; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getZhName() { |
| 55 | + return "自定义统计SQL"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public MetricDimension getDimension() { |
| 60 | + return MetricDimension.ACCURACY; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public MetricType getType() { |
| 65 | + return MetricType.SINGLE_TABLE; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isInvalidateItemsCanOutput() { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public CheckResult validateConfig(Map<String, Object> config) { |
| 75 | + CheckResult basicCheck = ConfigChecker.checkConfig(config, requiredOptions); |
| 76 | + if (!basicCheck.isSuccess()) { |
| 77 | + return basicCheck; |
| 78 | + } |
| 79 | + |
| 80 | + Object sqlValue = config.get(INVALIDATE_ITEMS_SQL); |
| 81 | + if (sqlValue == null || StringUtils.isEmpty(String.valueOf(sqlValue).trim())) { |
| 82 | + return new CheckResult(false, INVALIDATE_ITEMS_SQL + " cannot be empty"); |
| 83 | + } |
| 84 | + |
| 85 | + return new CheckResult(true, ""); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void prepare(Map<String, String> config) { |
| 90 | + if (config.containsKey(INVALIDATE_ITEMS_SQL) && StringUtils.isNotEmpty(config.get(INVALIDATE_ITEMS_SQL))) { |
| 91 | + this.invalidateItemsSql = config.get(INVALIDATE_ITEMS_SQL); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Map<String, ConfigItem> getConfigMap() { |
| 97 | + return configMap; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public ExecuteSql getInvalidateItems(Map<String, String> inputParameter) { |
| 102 | + if (StringUtils.isEmpty(invalidateItemsSql)) { |
| 103 | + throw new IllegalStateException("invalidate_items_sql is not configured or empty"); |
| 104 | + } |
| 105 | + |
| 106 | + String uniqueKey = inputParameter.get(METRIC_UNIQUE_KEY); |
| 107 | + if (StringUtils.isEmpty(uniqueKey)) { |
| 108 | + throw new IllegalStateException("metric_unique_key is missing in input parameters"); |
| 109 | + } |
| 110 | + |
| 111 | + ExecuteSql executeSql = new ExecuteSql(); |
| 112 | + executeSql.setResultTable("invalidate_items_" + uniqueKey); |
| 113 | + executeSql.setSql(invalidateItemsSql); |
| 114 | + executeSql.setErrorOutput(true); |
| 115 | + return executeSql; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public ExecuteSql getActualValue(Map<String, String> inputParameter) { |
| 120 | + if (StringUtils.isEmpty(invalidateItemsSql)) { |
| 121 | + throw new IllegalStateException("invalidate_items_sql is not configured or empty"); |
| 122 | + } |
| 123 | + |
| 124 | + String uniqueKey = inputParameter.get(METRIC_UNIQUE_KEY); |
| 125 | + if (StringUtils.isEmpty(uniqueKey)) { |
| 126 | + throw new IllegalStateException("metric_unique_key is missing in input parameters"); |
| 127 | + } |
| 128 | + |
| 129 | + inputParameter.put(ACTUAL_TABLE, inputParameter.get(TABLE)); |
| 130 | + |
| 131 | + String actualAggregateSql = "select count(1) as actual_value_" + uniqueKey + " from ${invalidate_items_table}"; |
| 132 | + |
| 133 | + return new ExecuteSql(actualAggregateSql, "invalidate_count_" + uniqueKey); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public ExecuteSql getDirectActualValue(Map<String, String> inputParameter) { |
| 138 | + if (StringUtils.isEmpty(invalidateItemsSql)) { |
| 139 | + throw new IllegalStateException("invalidate_items_sql is not configured or empty"); |
| 140 | + } |
| 141 | + |
| 142 | + String uniqueKey = inputParameter.get(METRIC_UNIQUE_KEY); |
| 143 | + if (StringUtils.isEmpty(uniqueKey)) { |
| 144 | + throw new IllegalStateException("metric_unique_key is missing in input parameters"); |
| 145 | + } |
| 146 | + |
| 147 | + String actualAggregateSql = "select count(1) as actual_value_" + uniqueKey |
| 148 | + + " from ( " + invalidateItemsSql + " ) t"; |
| 149 | + |
| 150 | + return new ExecuteSql(actualAggregateSql, "invalidate_count_" + uniqueKey); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public List<DataVinesDataType> suitableType() { |
| 155 | + return Collections.emptyList(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public boolean isCustomSql() { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public String getTableDiscoverySql(Map<String, String> inputParameter) { |
| 165 | + return inputParameter.get(INVALIDATE_ITEMS_SQL); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void setTableDiscoverySql(Map<String, String> inputParameter, String sql) { |
| 170 | + inputParameter.put(INVALIDATE_ITEMS_SQL, sql); |
| 171 | + this.invalidateItemsSql = sql; |
| 172 | + } |
| 173 | +} |
0 commit comments