Skip to content

Commit

Permalink
fix:修复大数值排序问题#58
Browse files Browse the repository at this point in the history
  • Loading branch information
zl11357 committed Aug 22, 2024
1 parent 69b684d commit fcd9f28
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/main/java/com/ly/ckibana/parser/ResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -293,7 +294,7 @@ private List<Bucket> sortTermsAggBySubAgg(Aggregation aggregation, List<Bucket>
//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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -61,7 +62,7 @@ public List<SqlConverter> 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;
}
}
69 changes: 69 additions & 0 deletions src/test/java/com/ly/ckibana/utils/NumberSortTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit fcd9f28

Please sign in to comment.