diff --git a/src/main/java/com/ly/ckibana/model/compute/aggregation/bucket/MathBucket.java b/src/main/java/com/ly/ckibana/model/compute/aggregation/bucket/MathBucket.java index 07cf036..ad6cf10 100644 --- a/src/main/java/com/ly/ckibana/model/compute/aggregation/bucket/MathBucket.java +++ b/src/main/java/com/ly/ckibana/model/compute/aggregation/bucket/MathBucket.java @@ -18,9 +18,11 @@ import lombok.Data; import lombok.EqualsAndHashCode; +import java.math.BigDecimal; + @EqualsAndHashCode(callSuper = true) @Data public class MathBucket extends Bucket { - private double value; + private BigDecimal value; } diff --git a/src/main/java/com/ly/ckibana/parser/ResultParser.java b/src/main/java/com/ly/ckibana/parser/ResultParser.java index 551f7df..e561328 100644 --- a/src/main/java/com/ly/ckibana/parser/ResultParser.java +++ b/src/main/java/com/ly/ckibana/parser/ResultParser.java @@ -36,6 +36,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -293,7 +294,7 @@ private List sortTermsAggBySubAgg(Aggregation aggregation, List //math类bucket只有一个bucket MathBucket m1 = (MathBucket) s1.getBuckets().get(0); MathBucket m2 = (MathBucket) s2.getBuckets().get(0); - int compareValue = (int) (m1.getValue() - m2.getValue()); + int compareValue = (m1.getValue().compareTo(m2.getValue())); return orderValue.equalsIgnoreCase(SortType.ASC.name()) ? compareValue : (-compareValue); }).collect(Collectors.toList()); } diff --git a/src/main/java/com/ly/ckibana/strategy/aggs/MathCategoryAggregation.java b/src/main/java/com/ly/ckibana/strategy/aggs/MathCategoryAggregation.java index 95452d3..c4db5d5 100644 --- a/src/main/java/com/ly/ckibana/strategy/aggs/MathCategoryAggregation.java +++ b/src/main/java/com/ly/ckibana/strategy/aggs/MathCategoryAggregation.java @@ -28,6 +28,7 @@ import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -61,7 +62,7 @@ public List buildMathCategorySelectSql(String mathKey) { public MathBucket buildResultBucket(JSONObject obj) { MathBucket result = new MathBucket(); result.setKey(queryFieldName()); - result.setValue(obj.getDoubleValue(queryFieldName())); + result.setValue(new BigDecimal(obj.getString(queryFieldName()))); return result; } } diff --git a/src/test/java/com/ly/ckibana/utils/NumberSortTest.java b/src/test/java/com/ly/ckibana/utils/NumberSortTest.java new file mode 100644 index 0000000..1d13f69 --- /dev/null +++ b/src/test/java/com/ly/ckibana/utils/NumberSortTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023 LY.com All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.ly.ckibana.utils; + +import com.alibaba.fastjson2.JSONObject; +import com.ly.ckibana.model.enums.SortType; +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * mathBucket值排序转换 + * + * @author zl11357 + * @since 2023/10/19 22:17 + */ +public class NumberSortTest { + /** + * 大值排序测试 + */ + @Test + public void superLongNumberTest() { + Long bigValue = 523412341234L; + Long littleValue = 423412341234L; + JSONObject row = new JSONObject(); + row.put("v1", bigValue.toString()); + row.put("v2", littleValue.toString()); + Assert.assertTrue("升序测试",sort(row, SortType.ASC).equals(Arrays.asList(littleValue, bigValue).toString())); + Assert.assertTrue("降序测试", sort(row, SortType.DESC).equals(Arrays.asList(bigValue, littleValue).toString())); + } + + /** + * 小值排序测试 + */ + @Test + public void superIntNumberTest() { + Integer bigValue = 20; + Integer littleValue = 10; + JSONObject row = new JSONObject(); + row.put("v1", bigValue.toString()); + row.put("v2", littleValue.toString()); + Assert.assertTrue("升序测试",sort(row, SortType.ASC).equals(Arrays.asList(littleValue, bigValue).toString())); + Assert.assertTrue("降序测试", sort(row, SortType.DESC).equals(Arrays.asList(bigValue, littleValue).toString())); + } + + private String sort(JSONObject row, SortType sortType) { + return Arrays.asList(new BigDecimal(row.getString("v1")), new BigDecimal(row.getString("v2"))).stream() + .sorted((o1, o2) -> { + int compareValue = (o1.compareTo(o2)); + return sortType.equals(SortType.ASC) ? compareValue : -compareValue; + }).collect(Collectors.toList()).toString(); + } +}