[ agent ] [ status ] - accomplish agent status - 5
This commit is contained in:
@@ -30,6 +30,6 @@ public class AgentStatus {
|
||||
|
||||
List<NetworkInfo> networkInfo;
|
||||
|
||||
OperatingSystem osInfo;
|
||||
AgentSystemInfo osInfo;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package io.wdd.common.beans.status;
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -24,12 +30,18 @@ public class AgentSystemInfo {
|
||||
|
||||
public static AgentSystemInfo mapFromOHSISystem(OperatingSystem os) {
|
||||
|
||||
|
||||
return AgentSystemInfo.builder()
|
||||
.osInfo(String.valueOf(os.getVersionInfo()))
|
||||
.family(os.getFamily())
|
||||
.manufacturer(os.getManufacturer())
|
||||
.bootTime()
|
||||
.bootTime(TimeUtils.localDateTimeString(
|
||||
LocalDateTime.ofInstant(
|
||||
Instant.ofEpochMilli(os.getSystemBootTime()),
|
||||
ZoneId.of("UTC+8")
|
||||
)
|
||||
))
|
||||
.upTime(TimeUtils.toRelative(os.getSystemUptime()*1000))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
90
common/src/main/java/io/wdd/common/utils/TimeUtils.java
Normal file
90
common/src/main/java/io/wdd/common/utils/TimeUtils.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package io.wdd.common.utils;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimeUtils {
|
||||
|
||||
public static ByteBuffer currentTimeByteBuffer() {
|
||||
|
||||
byte[] timeBytes = LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
return ByteBuffer.wrap(timeBytes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return UTC+8 [ yyyy-MM-dd HH:mm:ss ] Time String
|
||||
*/
|
||||
public static String currentTimeString() {
|
||||
|
||||
return LocalDateTime.now(ZoneId.of("UTC+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
|
||||
public static String localDateTimeString(LocalDateTime time) {
|
||||
return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
|
||||
private static final Map<String, Long> times = new HashMap<>(16);
|
||||
|
||||
static {
|
||||
times.put("year", TimeUnit.DAYS.toMillis(365));
|
||||
times.put("month", TimeUnit.DAYS.toMillis(30));
|
||||
times.put("week", TimeUnit.DAYS.toMillis(7));
|
||||
times.put("day", TimeUnit.DAYS.toMillis(1));
|
||||
times.put("hour", TimeUnit.HOURS.toMillis(1));
|
||||
times.put("minute", TimeUnit.MINUTES.toMillis(1));
|
||||
times.put("second", TimeUnit.SECONDS.toMillis(1));
|
||||
}
|
||||
|
||||
public static String toRelative(long duration, int maxLevel) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
int level = 0;
|
||||
for (Map.Entry<String, Long> time : times.entrySet()){
|
||||
long timeDelta = duration / time.getValue();
|
||||
if (timeDelta > 0){
|
||||
res.append(timeDelta)
|
||||
.append(" ")
|
||||
.append(time.getKey())
|
||||
.append(timeDelta > 1 ? "s" : "")
|
||||
.append(", ");
|
||||
duration -= time.getValue() * timeDelta;
|
||||
level++;
|
||||
}
|
||||
if (level == maxLevel){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ("".equals(res.toString())) {
|
||||
return "0 seconds ago";
|
||||
} else {
|
||||
res.setLength(res.length() - 2);
|
||||
res.append(" ago");
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static String toRelative(long duration) {
|
||||
return toRelative(duration, times.size());
|
||||
}
|
||||
|
||||
public static String toRelative(Date start, Date end){
|
||||
assert start.after(end);
|
||||
return toRelative(end.getTime() - start.getTime());
|
||||
}
|
||||
|
||||
public static String toRelative(Date start, Date end, int level){
|
||||
assert start.after(end);
|
||||
return toRelative(end.getTime() - start.getTime(), level);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user