Skip to content

Commit

Permalink
🐳 #376 i18n TimeRangeInOut
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Oct 7, 2024
1 parent 3645940 commit b5797d2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,20 +101,15 @@
public final class TimeRangeInOut implements ActionMethodInOut {

final TimeRangeDayRegion region = new TimeRangeDayRegion();

ChangeListener listener = new DefaultChangeListener();
ChangeListener listener = new ChangeListener() {
};

/**
* 设置监听器
*
* @param listener 监听器
*/
public void setListener(ChangeListener listener) {

if (this.listener instanceof DefaultChangeListener that) {
that.active = false;
}

this.listener = Objects.requireNonNull(listener);
}

Expand Down Expand Up @@ -219,6 +216,9 @@ public void increment(LocalTime localTime) {
}
}

/** action 调用次数 共 [%d] 次 */
private static final String dayTitle = Bundle.getMessage(MessageKey.timeRangeInOutDayTitle);

@Override
public String toString() {

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -354,7 +357,7 @@ default void callbackYesterday(TimeRangeDay timeRangeYesterday) {
* @return LocalDate
*/
default LocalDate nowLocalDate() {
return LocalDate.now();
return CacheTimeKit.nowLocalDate();
}

/**
Expand All @@ -363,7 +366,7 @@ default LocalDate nowLocalDate() {
* @return LocalTime
*/
default LocalTime nowLocalTime() {
return LocalTime.now();
return CacheTimeKit.nowLocalTime();
}

/**
Expand Down Expand Up @@ -409,36 +412,4 @@ default List<TimeRangeMinute> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
4 changes: 4 additions & 0 deletions common/common-core/src/main/resources/iohao.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 4 additions & 0 deletions common/common-core/src/main/resources/iohao_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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 次]
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
*
Expand All @@ -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(() -> {
Expand All @@ -107,8 +116,8 @@ public void enableCache() {
TaskKit.runInterval(() -> {
// 每分钟更新一次当前时间
localDate = LocalDate.now();
localTime = LocalTime.now();
}, 1, TimeUnit.MINUTES);

}
}
}

0 comments on commit b5797d2

Please sign in to comment.