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) { public HeartbeatRepresenter(DumpSettings settings) {
super(settings); super(settings);
this.representers.put(NBInvokableState.class, new RepresentEnumToString()); this.representers.put(NBInvokableState.class, new RepresentEnumToString());
this.representers.put(Status.class, new RepresentStatusToMap());
} }
public class RepresentEnumToString implements RepresentToNode { 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.api.DumpSettings;
import org.snakeyaml.engine.v2.common.FlowStyle; import org.snakeyaml.engine.v2.common.FlowStyle;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public record Heartbeat( public record Status(
NBLabels labels, NBLabels labels,
NBInvokableState state, NBInvokableState state,
long started_at, long started_epoch_ms,
long session_time_ns, long session_time_ms,
long heartbeat_interval_ms, long heartbeat_interval_ms,
long heartbeat_epoch_ms long heartbeat_epoch_ms,
List<Status> substatus
) { ) {
public final static Dump dump = createDump(); public final static Dump dump = createDump();
private static Dump createDump() { private static Dump createDump() {
DumpSettings settings = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK).build(); DumpSettings settings = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK).build();
return new Dump(settings, new HeartbeatRepresenter(settings)); return new Dump(settings, new HeartbeatRepresenter(settings));
} }
public Heartbeat withHeartbeatDetails(long new_heartbeat_interval_ms, long new_heartbeat_ms_epoch) { public Status withHeartbeatDetails(long new_heartbeat_interval_ms, long new_heartbeat_ms_epoch) {
return new Heartbeat( return new Status(
labels, labels,
state, state,
started_at, started_epoch_ms,
session_time_ns, session_time_ms,
new_heartbeat_interval_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() { public Map<String, Object> toMap() {
return Map.of( return new LinkedHashMap<>() {{
"labels", labels.asMap(), put("labels", labels.asMap());
"state", state, put("state", state);
"started_at_epochms", started_at, put("started_at_epochms", started_epoch_ms);
"session_time_ns", session_time_ns, put("session_time_ms", session_time_ms);
"heartbeat_interval_ms", heartbeat_interval_ms, put("heartbeat_interval_ms", heartbeat_interval_ms);
"heartbeat_epoch_ms", heartbeat_epoch_ms put("heartbeat_epoch_ms", heartbeat_epoch_ms);
); put("substatus", substatus);
}};
} }
@Override @Override