From b5797d209111b4255fd2991a31899075fd4f5988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=94=E6=B0=91=E5=B0=8F=E9=95=87?= <262610965@qq.com> Date: Mon, 7 Oct 2024 13:41:15 +0800 Subject: [PATCH] :whale: #376 i18n TimeRangeInOut --- .../core/flow/internal/TimeRangeInOut.java | 73 ++++++------------- .../game/action/skeleton/i18n/Bundle.java | 2 +- .../game/action/skeleton/i18n/MessageKey.java | 4 + .../src/main/resources/iohao.properties | 4 + .../src/main/resources/iohao_zh_CN.properties | 4 + .../skeleton/core/data/TestDataKit.java | 4 + .../flow/internal/TimeRangeInOutTest.java | 15 ++-- .../game/common/kit/time/CacheTimeKit.java | 27 ++++--- 8 files changed, 64 insertions(+), 69 deletions(-) diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOut.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOut.java index 5881c151d..3519a3d0a 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOut.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOut.java @@ -20,9 +20,11 @@ import com.iohao.game.action.skeleton.core.flow.ActionMethodInOut; import com.iohao.game.action.skeleton.core.flow.FlowContext; +import com.iohao.game.action.skeleton.i18n.Bundle; +import com.iohao.game.action.skeleton.i18n.MessageKey; import com.iohao.game.common.kit.CollKit; -import com.iohao.game.common.kit.concurrent.IntervalTaskListener; import com.iohao.game.common.kit.concurrent.TaskKit; +import com.iohao.game.common.kit.time.CacheTimeKit; import com.iohao.game.common.kit.time.FormatTimeKit; import lombok.Getter; import org.jctools.maps.NonBlockingHashMap; @@ -99,8 +101,8 @@ public final class TimeRangeInOut implements ActionMethodInOut { final TimeRangeDayRegion region = new TimeRangeDayRegion(); - - ChangeListener listener = new DefaultChangeListener(); + ChangeListener listener = new ChangeListener() { + }; /** * 设置监听器 @@ -108,11 +110,6 @@ public final class TimeRangeInOut implements ActionMethodInOut { * @param listener 监听器 */ public void setListener(ChangeListener listener) { - - if (this.listener instanceof DefaultChangeListener that) { - that.active = false; - } - this.listener = Objects.requireNonNull(listener); } @@ -219,6 +216,9 @@ public void increment(LocalTime localTime) { } } + /** action 调用次数 共 [%d] 次 */ + private static final String dayTitle = Bundle.getMessage(MessageKey.timeRangeInOutDayTitle); + @Override public String toString() { @@ -229,12 +229,12 @@ public String toString() { .toList(); if (CollKit.isEmpty(timeRangeHoursList)) { - // TimeRange - return localDateFormat + " action 调用次数暂无数据"; + // TimeRange,action 暂无数据 + return localDateFormat + " action no data"; } - StringBuilder builder = new StringBuilder(); - builder.append(localDateFormat).append(" action 调用次数 共 [").append(this.count).append("] 次"); + StringBuilder builder = new StringBuilder(localDateFormat); + builder.append(" ").append(dayTitle.formatted(this.count.sum())); for (TimeRangeHour timeRangeHour : timeRangeHoursList) { builder.append("\n\t").append(timeRangeHour); @@ -277,12 +277,12 @@ public int getHour() { return this.hourTime.getHour(); } + /** %d:00 共 %s 次; */ + private static final String hourTitle = Bundle.getMessage(MessageKey.timeRangeInOutHourTitle); + @Override public String toString() { - - String hourStr = String.format("%d:00 共 %s 次;" - , this.getHour() - , this.count); + String hourStr = hourTitle.formatted(this.getHour(), this.count.sum()); if (CollKit.isEmpty(this.minuteList)) { return hourStr; @@ -326,9 +326,12 @@ void increment() { this.count.increment(); } + /** [%d~%d分钟,%d 次] */ + private static final String minuteTitle = Bundle.getMessage(MessageKey.timeRangeInOutMinuteTitle); + @Override public String toString() { - return String.format("[%d~%d分钟 %s 次]", this.start, this.end, this.count); + return minuteTitle.formatted(this.start, this.end, this.count.sum()); } } @@ -354,7 +357,7 @@ default void callbackYesterday(TimeRangeDay timeRangeYesterday) { * @return LocalDate */ default LocalDate nowLocalDate() { - return LocalDate.now(); + return CacheTimeKit.nowLocalDate(); } /** @@ -363,7 +366,7 @@ default LocalDate nowLocalDate() { * @return LocalTime */ default LocalTime nowLocalTime() { - return LocalTime.now(); + return CacheTimeKit.nowLocalTime(); } /** @@ -409,36 +412,4 @@ default List createListenerTimeRangeMinuteList() { return Collections.emptyList(); } } - - private static class DefaultChangeListener implements ChangeListener { - LocalDate nowLocalDate = LocalDate.now(); - LocalTime nowLocalTime = LocalTime.now(); - volatile boolean active = true; - - DefaultChangeListener() { - TaskKit.runIntervalMinute(new IntervalTaskListener() { - @Override - public void onUpdate() { - // 1 minute update - nowLocalDate = LocalDate.now(); - nowLocalTime = LocalTime.now(); - } - - @Override - public boolean isActive() { - return active; - } - }, 1); - } - - @Override - public LocalDate nowLocalDate() { - return this.nowLocalDate; - } - - @Override - public LocalTime nowLocalTime() { - return this.nowLocalTime; - } - } } \ No newline at end of file diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/Bundle.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/Bundle.java index 7869109b9..3cf0f2784 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/Bundle.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/Bundle.java @@ -47,7 +47,7 @@ public String getMessage(String key) { return getBundle().getString(key); } - public String getMessage(String key, Object... args) { + String getMessage(String key, Object... args) { var keyPattern = getBundle().getString(key); return MessageFormat.format(keyPattern, args); } diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/MessageKey.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/MessageKey.java index b61e0015c..fde5696e0 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/MessageKey.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/i18n/MessageKey.java @@ -62,4 +62,8 @@ public interface MessageKey { /* see ThreadMonitorInOut.java */ String threadMonitorInOutThreadMonitor = "threadMonitorInOutThreadMonitor"; + /* see TimeRangeInOut.java */ + String timeRangeInOutDayTitle = "timeRangeInOutDayTitle"; + String timeRangeInOutHourTitle = "timeRangeInOutHourTitle"; + String timeRangeInOutMinuteTitle = "timeRangeInOutMinuteTitle"; } diff --git a/common/common-core/src/main/resources/iohao.properties b/common/common-core/src/main/resources/iohao.properties index a31318306..c5e5c9fb0 100644 --- a/common/common-core/src/main/resources/iohao.properties +++ b/common/common-core/src/main/resources/iohao.properties @@ -31,3 +31,7 @@ statActionInOutTimeRange=%s ms, execute[%d] statActionInOutStatAction=%s, execute[%s], error[%s], avgTime[%d], maxExecuteTime[%s], totalExecuteTime[%s] %s # see ThreadMonitorInOut.java threadMonitorInOutThreadMonitor=thread[%s], execute[%d], avgTime[%d]ms, remainTask[%d] +# see TimeRangeInOut.java +timeRangeInOutDayTitle=action execution times[%d] +timeRangeInOutHourTitle=%d:00 execute[%s]; +timeRangeInOutMinuteTitle=[%d~%d minutes, execute:%d] diff --git a/common/common-core/src/main/resources/iohao_zh_CN.properties b/common/common-core/src/main/resources/iohao_zh_CN.properties index 81d40a6ff..42fd5e30f 100644 --- a/common/common-core/src/main/resources/iohao_zh_CN.properties +++ b/common/common-core/src/main/resources/iohao_zh_CN.properties @@ -31,3 +31,7 @@ statActionInOutTimeRange=%s ms 的请求共 [%d] 个 statActionInOutStatAction=%s, 执行[%s]次, 异常[%s]次, 平均耗时[%d], 最大耗时[%s], 总耗时[%s] %s # see ThreadMonitorInOut.java threadMonitorInOutThreadMonitor=业务线程[%s],共执行了[%d]次业务,平均耗时[%d]ms, 剩余[%d]个任务未执行 +# see TimeRangeInOut.java +timeRangeInOutDayTitle=action 执行次数 共 [%d] 次 +timeRangeInOutHourTitle=%d:00 共 %s 次; +timeRangeInOutMinuteTitle=[%d~%d分钟,%d 次] diff --git a/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/data/TestDataKit.java b/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/data/TestDataKit.java index ccc14487e..3342f68b5 100644 --- a/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/data/TestDataKit.java +++ b/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/data/TestDataKit.java @@ -88,6 +88,10 @@ public BarSkeletonBuilder createBuilder() { return createBuilder(null); } + public FlowContext ofFlowContext(CmdInfo cmdInfo) { + return ofFlowContext(cmdInfo, null); + } + public FlowContext ofFlowContext(CmdInfo cmdInfo, Object data) { RequestMessage requestMessage = BarMessageKit.createRequestMessage(cmdInfo, data); diff --git a/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOutTest.java b/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOutTest.java index 6960dade6..3495e3564 100644 --- a/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOutTest.java +++ b/common/common-core/src/test/java/com/iohao/game/action/skeleton/core/flow/internal/TimeRangeInOutTest.java @@ -1,11 +1,11 @@ package com.iohao.game.action.skeleton.core.flow.internal; -import com.iohao.game.action.skeleton.core.BarMessageKit; import com.iohao.game.action.skeleton.core.CmdInfo; +import com.iohao.game.action.skeleton.core.data.TestDataKit; import com.iohao.game.action.skeleton.core.flow.FlowContext; -import com.iohao.game.action.skeleton.protocol.RequestMessage; import com.iohao.game.action.skeleton.toy.IoGameBanner; import com.iohao.game.common.kit.RandomKit; +import org.junit.Assert; import org.junit.Test; import java.time.LocalDate; @@ -21,13 +21,11 @@ public class TimeRangeInOutTest { @Test public void fuckOut() { + FlowContext flowContext = TestDataKit.ofFlowContext(CmdInfo.of(1, 1)); + TimeRangeInOut inOut = new TimeRangeInOut(); setListener(inOut); - RequestMessage requestMessage = BarMessageKit.createRequestMessage(CmdInfo.of(1, 1)); - FlowContext flowContext = new FlowContext(); - flowContext.setRequest(requestMessage); - TimeRangeInOut.TimeRangeDayRegion region = inOut.region; LocalDate localDate = LocalDate.now(); @@ -41,9 +39,10 @@ public void fuckOut() { count.increment(); } - print(inOut); + var timeRangeDay = region.getTimeRangeDay(localDate); + Assert.assertEquals(timeRangeDay.count().sum(), count.sum()); - IoGameBanner.println(count); + print(inOut); } private static void print(TimeRangeInOut inOut) { diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/time/CacheTimeKit.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/time/CacheTimeKit.java index 9564f8e7e..938e46d48 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/time/CacheTimeKit.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/time/CacheTimeKit.java @@ -19,10 +19,12 @@ package com.iohao.game.common.kit.time; import com.iohao.game.common.kit.concurrent.TaskKit; +import lombok.Getter; import lombok.experimental.UtilityClass; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.concurrent.TimeUnit; /** @@ -54,11 +56,13 @@ */ @UtilityClass public final class CacheTimeKit { - private boolean cache; + private volatile LocalTime localTime = LocalTime.now(); + private volatile LocalDate localDate = LocalDate.now(); + private volatile LocalDateTime localDateTime = LocalDateTime.now(); + private volatile long currentTimeMillis = System.currentTimeMillis(); - private volatile LocalDate localDate; - private volatile LocalDateTime localDateTime; - private volatile long currentTimeMillis; + @Getter + private boolean cache; /** * get LocalDate @@ -78,6 +82,15 @@ public LocalDateTime nowLocalDateTime() { return cache ? localDateTime : LocalDateTime.now(); } + /** + * get LocalTime + * + * @return LocalTime + */ + public LocalTime nowLocalTime() { + return cache ? localTime : LocalTime.now(); + } + /** * get currentTimeMillis * @@ -92,10 +105,6 @@ public long currentTimeMillis() { */ public void enableCache() { if (!cache) { - localDate = LocalDate.now(); - localDateTime = LocalDateTime.now(); - currentTimeMillis = System.currentTimeMillis(); - cache = true; TaskKit.runInterval(() -> { @@ -107,8 +116,8 @@ public void enableCache() { TaskKit.runInterval(() -> { // 每分钟更新一次当前时间 localDate = LocalDate.now(); + localTime = LocalTime.now(); }, 1, TimeUnit.MINUTES); - } } }