From fcd9f285c64f90396aec460dec33eccbef80939e Mon Sep 17 00:00:00 2001 From: zl11357 Date: Fri, 23 Aug 2024 00:25:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=A4=A7=E6=95=B0?= =?UTF-8?q?=E5=80=BC=E6=8E=92=E5=BA=8F=E9=97=AE=E9=A2=98#58?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aggregation/bucket/MathBucket.java | 4 +- .../com/ly/ckibana/parser/ResultParser.java | 3 +- .../aggs/MathCategoryAggregation.java | 3 +- .../com/ly/ckibana/utils/NumberSortTest.java | 69 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/ly/ckibana/utils/NumberSortTest.java 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(); + } +}