-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix jmx memory_pool unit and time unit error (#1273)
Co-authored-by: zhuoshangyi <zhuoshangyi@hntradesp.com>
- Loading branch information
Showing
9 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
collector/src/main/java/org/dromara/hertzbeat/collector/dispatch/unit/TimeLengthUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.dromara.hertzbeat.collector.dispatch.unit; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* the enum of time length | ||
* 时间长短的枚举类 | ||
* @author rbsrcy | ||
* | ||
*/ | ||
public enum TimeLengthUnit { | ||
/** | ||
* NANOSECONDS | ||
*/ | ||
NS("NS", 1), | ||
/** | ||
* MICROSECONDS | ||
*/ | ||
US("US", 1000), | ||
/** | ||
* MILLISECONDS | ||
*/ | ||
MS("MS", 1000_000), | ||
/** | ||
* SECONDS | ||
*/ | ||
S("S", 1000_000_000), | ||
/** | ||
* MINUTES | ||
*/ | ||
MIN("MIN", 60_000_000_000L), | ||
/** | ||
* HOURS | ||
*/ | ||
H("H", 3600_000_000_000L), | ||
/** | ||
* DAYS | ||
*/ | ||
D("D", 86_400_000_000_000L); | ||
|
||
private final String unit; | ||
private final long scale; | ||
|
||
private TimeLengthUnit(String unit, long scale) { | ||
this.unit = unit; | ||
this.scale = scale; | ||
} | ||
|
||
public String getUnit() { | ||
return unit; | ||
} | ||
|
||
public long getScale() { | ||
return scale; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...r/src/main/java/org/dromara/hertzbeat/collector/dispatch/unit/impl/TimeLengthConvert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.dromara.hertzbeat.collector.dispatch.unit.impl; | ||
|
||
import org.dromara.hertzbeat.collector.dispatch.unit.TimeLengthUnit; | ||
import org.dromara.hertzbeat.collector.dispatch.unit.UnitConvert; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
/** | ||
* the convert of time length | ||
* 时间长短转换 | ||
* @author rbsrcy | ||
* | ||
*/ | ||
@Component | ||
public final class TimeLengthConvert implements UnitConvert { | ||
|
||
|
||
@Override | ||
public String convert(String value, String originUnit, String newUnit) { | ||
if (value == null || "".equals(value)) { | ||
return null; | ||
} | ||
BigDecimal length = new BigDecimal(value); | ||
// 思路:value通过originUnit转换为纳秒,在转换为newUnit单位对应的值 | ||
for (TimeLengthUnit timeLengthUnit : TimeLengthUnit.values()) { | ||
if (timeLengthUnit.getUnit().equals(originUnit.toUpperCase())) { | ||
length = length.multiply(new BigDecimal(timeLengthUnit.getScale())); | ||
} | ||
if (timeLengthUnit.getUnit().equals(newUnit.toUpperCase())) { | ||
length = length.divide(new BigDecimal(timeLengthUnit.getScale()), 12, RoundingMode.HALF_UP); | ||
} | ||
} | ||
return length.setScale(4, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString(); | ||
} | ||
|
||
@Override | ||
public boolean checkUnit(String unit) { | ||
if (unit == null || "".equals(unit)) { | ||
return false; | ||
} | ||
for (TimeLengthUnit timeUnit : TimeLengthUnit.values()) { | ||
// 不区分大小写 | ||
if (timeUnit.getUnit().equals(unit.toUpperCase())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...or/src/test/java/org/dromara/hertzbeat/collector/dispatch/unit/TimeLengthConvertTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.dromara.hertzbeat.collector.dispatch.unit; | ||
|
||
import org.dromara.hertzbeat.collector.dispatch.unit.impl.TimeLengthConvert; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* Test case for {@link TimeLengthConvert} | ||
*/ | ||
class TimeLengthConvertTest { | ||
|
||
private TimeLengthConvert convert; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.convert = new TimeLengthConvert(); | ||
} | ||
|
||
/** | ||
* 测试纳秒转秒 | ||
*/ | ||
@Test | ||
void convertNs2Sec() { | ||
String result = convert.convert("1000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.S.getUnit()); | ||
assertEquals("1", result); | ||
} | ||
|
||
/** | ||
* 测试纳秒转毫秒 | ||
*/ | ||
@Test | ||
void convertNs2Ms() { | ||
String result = convert.convert("1000123450", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.MS.getUnit()); | ||
assertEquals("1000.1235", result); | ||
} | ||
|
||
/** | ||
* 测试纳秒转微秒 | ||
*/ | ||
@Test | ||
void convertNs2Us() { | ||
String result = convert.convert("1000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.US.getUnit()); | ||
assertEquals("1000000", result); | ||
} | ||
|
||
|
||
/** | ||
* 测试纳秒转天 | ||
*/ | ||
@Test | ||
void convertNs2Day() { | ||
String result = convert.convert("86400000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.D.getUnit()); | ||
assertEquals("1", result); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters