MINOR: DI Simplify Formulas (#17382)

* MINOR: DI Similify Formulas

* checkstyle
This commit is contained in:
Mayur Singal 2024-08-13 17:42:46 +05:30 committed by GitHub
parent feab12422b
commit ac2264e9c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 45 additions and 13 deletions

View File

@ -21,7 +21,7 @@ public class DataInsightSystemChartRepository extends EntityRepository<DataInsig
public static final String DI_SEARCH_INDEX = "di-data-assets-*"; public static final String DI_SEARCH_INDEX = "di-data-assets-*";
public static final String FORMULA_FUNC_REGEX = public static final String FORMULA_FUNC_REGEX =
"\\b(count|sum|min|max|avg)+\\(k='([^']*)',?\\s*(q='([^']*)')?\\)?"; "\\b(count|sum|min|max|avg)+\\((k='([^']*)')?,?\\s*(q='([^']*)')?\\)?";
public static final String NUMERIC_VALIDATION_REGEX = "[\\d\\.+-\\/\\*\\(\\)\s]+"; public static final String NUMERIC_VALIDATION_REGEX = "[\\d\\.+-\\/\\*\\(\\)\s]+";

View File

@ -70,23 +70,28 @@ public interface ElasticSearchDynamicChartAggregatorInterface {
FormulaHolder holder = new FormulaHolder(); FormulaHolder holder = new FormulaHolder();
holder.setFormula(matcher.group()); holder.setFormula(matcher.group());
holder.setFunction(Function.valueOf(matcher.group(1).toUpperCase())); holder.setFunction(Function.valueOf(matcher.group(1).toUpperCase()));
holder.setField(matcher.group(2)); String field;
if (matcher.group(3) != null) {
field = matcher.group(3);
} else {
field = "id.keyword";
}
ValuesSourceAggregationBuilder subAgg = ValuesSourceAggregationBuilder subAgg =
getSubAggregationsByFunction( getSubAggregationsByFunction(
Function.valueOf(matcher.group(1).toUpperCase()), matcher.group(2), index); Function.valueOf(matcher.group(1).toUpperCase()), field, index);
if (matcher.group(4) != null) { if (matcher.group(5) != null) {
QueryBuilder queryBuilder; QueryBuilder queryBuilder;
if (filter != null) { if (filter != null) {
queryBuilder = queryBuilder =
QueryBuilders.boolQuery() QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(matcher.group(4))) .must(QueryBuilders.queryStringQuery(matcher.group(5)))
.must(filter); .must(filter);
} else { } else {
queryBuilder = QueryBuilders.queryStringQuery(matcher.group(4)); queryBuilder = QueryBuilders.queryStringQuery(matcher.group(5));
} }
dateHistogramAggregationBuilder.subAggregation( dateHistogramAggregationBuilder.subAggregation(
AggregationBuilders.filter("filer" + index, queryBuilder).subAggregation(subAgg)); AggregationBuilders.filter("filer" + index, queryBuilder).subAggregation(subAgg));
holder.setQuery(matcher.group(4)); holder.setQuery(matcher.group(5));
} else { } else {
if (filter != null) { if (filter != null) {
dateHistogramAggregationBuilder.subAggregation( dateHistogramAggregationBuilder.subAggregation(

View File

@ -70,23 +70,28 @@ public interface OpenSearchDynamicChartAggregatorInterface {
FormulaHolder holder = new FormulaHolder(); FormulaHolder holder = new FormulaHolder();
holder.setFormula(matcher.group()); holder.setFormula(matcher.group());
holder.setFunction(Function.valueOf(matcher.group(1).toUpperCase())); holder.setFunction(Function.valueOf(matcher.group(1).toUpperCase()));
holder.setField(matcher.group(2)); String field;
if (matcher.group(3) != null) {
field = matcher.group(3);
} else {
field = "id.keyword";
}
ValuesSourceAggregationBuilder subAgg = ValuesSourceAggregationBuilder subAgg =
getSubAggregationsByFunction( getSubAggregationsByFunction(
Function.valueOf(matcher.group(1).toUpperCase()), matcher.group(2), index); Function.valueOf(matcher.group(1).toUpperCase()), field, index);
if (matcher.group(4) != null) { if (matcher.group(5) != null) {
QueryBuilder queryBuilder; QueryBuilder queryBuilder;
if (filter != null) { if (filter != null) {
queryBuilder = queryBuilder =
QueryBuilders.boolQuery() QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(matcher.group(4))) .must(QueryBuilders.queryStringQuery(matcher.group(5)))
.must(filter); .must(filter);
} else { } else {
queryBuilder = QueryBuilders.queryStringQuery(matcher.group(4)); queryBuilder = QueryBuilders.queryStringQuery(matcher.group(5));
} }
dateHistogramAggregationBuilder.subAggregation( dateHistogramAggregationBuilder.subAggregation(
AggregationBuilders.filter("filer" + index, queryBuilder).subAggregation(subAgg)); AggregationBuilders.filter("filer" + index, queryBuilder).subAggregation(subAgg));
holder.setQuery(matcher.group(4)); holder.setQuery(matcher.group(5));
} else { } else {
if (filter != null) { if (filter != null) {
dateHistogramAggregationBuilder.subAggregation( dateHistogramAggregationBuilder.subAggregation(

View File

@ -130,6 +130,11 @@ public class ElasticSearchDynamicChartAggregatorTest extends OpenMetadataApplica
summaryCard.put("formula", "count(k='id.keyword')"); summaryCard.put("formula", "count(k='id.keyword')");
assertTrue(compareRequest(cardString, summaryCard)); assertTrue(compareRequest(cardString, summaryCard));
Map<String, Object> summaryCard1 = new LinkedHashMap<>();
summaryCard1.put("type", "SummaryCard");
summaryCard1.put("formula", "count()");
assertTrue(compareRequest(cardString, summaryCard1));
String lineString = String lineString =
"{\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}"; "{\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}";
Map<String, Object> lineChart = new LinkedHashMap<>(); Map<String, Object> lineChart = new LinkedHashMap<>();
@ -169,6 +174,12 @@ public class ElasticSearchDynamicChartAggregatorTest extends OpenMetadataApplica
lineChart4.put("groupBy", "entityType.keyword"); lineChart4.put("groupBy", "entityType.keyword");
assertTrue(compareRequest(lineString4, lineChart4)); assertTrue(compareRequest(lineString4, lineChart4));
Map<String, Object> lineChart41 = new LinkedHashMap<>();
lineChart41.put("type", "LineChart");
lineChart41.put("formula", "count(q='hasDescription: 1')+count(q='owner.name.keyword: *')");
lineChart41.put("groupBy", "entityType.keyword");
assertTrue(compareRequest(lineString4, lineChart41));
String lineString5 = String lineString5 =
"{\"size\":0,\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"0\":{\"terms\":{\"field\":\"entityType.keyword\",\"size\":20,\"min_doc_count\":1,\"shard_min_doc_count\":0,\"show_term_doc_count_error\":false,\"order\":[{\"_count\":\"desc\"},{\"_key\":\"asc\"}]},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"filer0\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"hasDescription: 1\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}},\"filer1\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"owner.name.keyword: *\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword1\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}}}}}"; "{\"size\":0,\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"0\":{\"terms\":{\"field\":\"entityType.keyword\",\"size\":20,\"min_doc_count\":1,\"shard_min_doc_count\":0,\"show_term_doc_count_error\":false,\"order\":[{\"_count\":\"desc\"},{\"_key\":\"asc\"}]},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"filer0\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"hasDescription: 1\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}},\"filer1\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"owner.name.keyword: *\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword1\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}}}}}";
Map<String, Object> lineChart5 = new LinkedHashMap<>(); Map<String, Object> lineChart5 = new LinkedHashMap<>();

View File

@ -129,6 +129,11 @@ public class OpenSearchDynamicChartAggregatorTest extends OpenMetadataApplicatio
summaryCard.put("formula", "count(k='id.keyword')"); summaryCard.put("formula", "count(k='id.keyword')");
assertTrue(compareRequest(cardString, summaryCard)); assertTrue(compareRequest(cardString, summaryCard));
Map<String, Object> summaryCard1 = new LinkedHashMap<>();
summaryCard1.put("type", "SummaryCard");
summaryCard1.put("formula", "count()");
assertTrue(compareRequest(cardString, summaryCard1));
String lineString = String lineString =
"{\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}"; "{\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}";
Map<String, Object> lineChart = new LinkedHashMap<>(); Map<String, Object> lineChart = new LinkedHashMap<>();
@ -168,6 +173,12 @@ public class OpenSearchDynamicChartAggregatorTest extends OpenMetadataApplicatio
lineChart4.put("groupBy", "entityType.keyword"); lineChart4.put("groupBy", "entityType.keyword");
assertTrue(compareRequest(lineString4, lineChart4)); assertTrue(compareRequest(lineString4, lineChart4));
Map<String, Object> lineChart41 = new LinkedHashMap<>();
lineChart41.put("type", "LineChart");
lineChart41.put("formula", "count(q='hasDescription: 1')+count(q='owner.name.keyword: *')");
lineChart41.put("groupBy", "entityType.keyword");
assertTrue(compareRequest(lineString4, lineChart41));
String lineString5 = String lineString5 =
"{\"size\":0,\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"0\":{\"terms\":{\"field\":\"entityType.keyword\",\"size\":20,\"min_doc_count\":1,\"shard_min_doc_count\":0,\"show_term_doc_count_error\":false,\"order\":[{\"_count\":\"desc\"},{\"_key\":\"asc\"}]},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"filer0\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"hasDescription: 1\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}},\"filer1\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"owner.name.keyword: *\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword1\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}}}}}"; "{\"size\":0,\"query\":{\"range\":{\"@timestamp\":{\"from\":\"2024-07-16T03:54:31Z\",\"to\":\"2024-07-23T01:34:31Z\",\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"0\":{\"terms\":{\"field\":\"entityType.keyword\",\"size\":20,\"min_doc_count\":1,\"shard_min_doc_count\":0,\"show_term_doc_count_error\":false,\"order\":[{\"_count\":\"desc\"},{\"_key\":\"asc\"}]},\"aggregations\":{\"1\":{\"date_histogram\":{\"field\":\"@timestamp\",\"calendar_interval\":\"1d\",\"offset\":0,\"order\":{\"_key\":\"asc\"},\"keyed\":false,\"min_doc_count\":0},\"aggregations\":{\"filer0\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"hasDescription: 1\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword0\":{\"value_count\":{\"field\":\"id.keyword\"}}}},\"filer1\":{\"filter\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"owner.name.keyword: *\",\"fields\":[],\"type\":\"best_fields\",\"default_operator\":\"or\",\"max_determinized_states\":10000,\"enable_position_increments\":true,\"fuzziness\":\"AUTO\",\"fuzzy_prefix_length\":0,\"fuzzy_max_expansions\":50,\"phrase_slop\":0,\"escape\":false,\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"term\":{\"owners.displayName.keyword\":{\"value\":\"admin\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"aggregations\":{\"id.keyword1\":{\"value_count\":{\"field\":\"id.keyword\"}}}}}}}}}}";
Map<String, Object> lineChart5 = new LinkedHashMap<>(); Map<String, Object> lineChart5 = new LinkedHashMap<>();