Make status data nested, rename Heartbeat to Status

This commit is contained in:
Jonathan Shook 2024-01-11 00:40:49 -06:00
parent 325676fa6b
commit 933584e7ce
2 changed files with 40 additions and 24 deletions

View File

@ -26,6 +26,7 @@ public class HeartbeatRepresenter extends StandardRepresenter {
public HeartbeatRepresenter(DumpSettings settings) {
super(settings);
this.representers.put(NBInvokableState.class, new RepresentEnumToString());
this.representers.put(Status.class, new RepresentStatusToMap());
}
public class RepresentEnumToString implements RepresentToNode {
@ -40,4 +41,15 @@ public class HeartbeatRepresenter extends StandardRepresenter {
}
}
}
private class RepresentStatusToMap implements RepresentToNode {
@Override
public Node representData(Object data) {
if (data instanceof Status status) {
return represent(status.toMap());
} else {
throw new RuntimeException("Unable to represent object " + data.toString() + "\n(" + data.getClass().getCanonicalName() + ")");
}
}
}
}

View File

@ -22,32 +22,35 @@ import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.common.FlowStyle;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public record Heartbeat(
public record Status(
NBLabels labels,
NBInvokableState state,
long started_at,
long session_time_ns,
long started_epoch_ms,
long session_time_ms,
long heartbeat_interval_ms,
long heartbeat_epoch_ms
long heartbeat_epoch_ms,
List<Status> substatus
) {
public final static Dump dump = createDump();
private static Dump createDump() {
DumpSettings settings = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK).build();
return new Dump(settings, new HeartbeatRepresenter(settings));
}
public Heartbeat withHeartbeatDetails(long new_heartbeat_interval_ms, long new_heartbeat_ms_epoch) {
return new Heartbeat(
public Status withHeartbeatDetails(long new_heartbeat_interval_ms, long new_heartbeat_ms_epoch) {
return new Status(
labels,
state,
started_at,
session_time_ns,
started_epoch_ms,
session_time_ms,
new_heartbeat_interval_ms,
new_heartbeat_ms_epoch
new_heartbeat_ms_epoch,
substatus
);
}
@ -56,14 +59,15 @@ public record Heartbeat(
}
public Map<String, Object> toMap() {
return Map.of(
"labels", labels.asMap(),
"state", state,
"started_at_epochms", started_at,
"session_time_ns", session_time_ns,
"heartbeat_interval_ms", heartbeat_interval_ms,
"heartbeat_epoch_ms", heartbeat_epoch_ms
);
return new LinkedHashMap<>() {{
put("labels", labels.asMap());
put("state", state);
put("started_at_epochms", started_epoch_ms);
put("session_time_ms", session_time_ms);
put("heartbeat_interval_ms", heartbeat_interval_ms);
put("heartbeat_epoch_ms", heartbeat_epoch_ms);
put("substatus", substatus);
}};
}
@Override