Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohann0617 committed Dec 30, 2024
1 parent 54fe81a commit bd93417
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void createInstance(CreateInstanceParams params) {
.rootPassword(params.getRootPassword())
.build();
addTask(CommonUtils.CREATE_TASK_PREFIX + taskId, () ->
execCreate(sysUserDTO, sysService,instanceService, createTaskService),
execCreate(sysUserDTO, sysService, instanceService, createTaskService),
0, params.getInterval(), TimeUnit.SECONDS);
String beginCreateMsg = String.format(CommonUtils.BEGIN_CREATE_MESSAGE_TEMPLATE,
ociUser.getUsername(),
Expand Down Expand Up @@ -273,6 +273,8 @@ public Page<CreateTaskRsp> createTaskPage(CreateTaskPageParams params) {
list.parallelStream().forEach(x -> {
Long counts = (Long) TEMP_MAP.get(CommonUtils.CREATE_COUNTS_PREFIX + x.getId());
x.setCounts(counts == null ? "0" : String.valueOf(counts));
x.setOcpus(Double.valueOf(x.getOcpus()).longValue() + "");
x.setMemory(Double.valueOf(x.getMemory()).longValue() + "");
});
return CommonUtils.buildPage(list, params.getPageSize(), params.getCurrentPage(), total);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/yohann/ocihelper/task/OciTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ public void dailyBroadcastTask() {
if (ociCreateTaskList.isEmpty()) {
return "无";
}
String template = "[%s] [%s] [%s] [%s核/%sGB/%sGB] [%s台] [%s次]";
String template = "[%s] [%s] [%s] [%s核/%sGB/%sGB] [%s台] [%s] [%s次]";
return ociCreateTaskList.parallelStream().map(x -> {
OciUser ociUser = userService.getById(x.getUserId());
Long counts = (Long) TEMP_MAP.get(CommonUtils.CREATE_COUNTS_PREFIX + x.getId());
return String.format(template, ociUser.getUsername(), ociUser.getOciRegion(), x.getArchitecture(),
x.getOcpus(), x.getMemory(), x.getDisk(),
x.getCreateNumbers(), counts == null ? "0" : counts);
x.getOcpus().longValue(), x.getMemory().longValue(), x.getDisk(), x.getCreateNumbers(),
CommonUtils.getTimeDifference(x.getCreateTime()), counts == null ? "0" : counts);
}).collect(Collectors.joining("\n"));
});

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/yohann/ocihelper/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -421,6 +422,29 @@ public static boolean isIpInCidrList(String ip, List<String> cidrList) {
return false;
}

/**
* 计算时间差并返回格式化字符串
*
* @param startTime 开始时间 (LocalDateTime)
* @return 格式化的时间差字符串 "xx天xx小时xx分钟xx秒"
*/
public static String getTimeDifference(LocalDateTime startTime) {
// 获取当前时间
LocalDateTime now = LocalDateTime.now();

// 计算时间差
Duration duration = Duration.between(startTime, now);

// 转换为天、小时、分钟、秒
long days = duration.toDays();
long hours = duration.toHours() % 24;
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() % 60;

// 格式化结果
return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
}

public static String getPwdShell(String passwd) {
return "#cloud-config\n" +
"ssh_pwauth: yes\n" +
Expand Down

0 comments on commit bd93417

Please sign in to comment.