use of me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode in project solarthing by wildmountainfarms.
the class HeartbeatCommandChatBotHandler method displayHeartbeats.
// I really don't care at the moment. Extracting a method will make this less readable IMO
@SuppressWarnings("DuplicatedCode")
private void displayHeartbeats(MessageSender messageSender) {
Map<HeartbeatIdentifier, HeartbeatNode> map = new HashMap<>();
eventDatabaseCacheManager.access(eventDatabaseCache -> {
eventDatabaseCache.createCachedPacketsInRangeStream(TimeRange.createAfter(Instant.now().minus(Duration.ofHours(3))), false).map(// We are accessing new packets, so they should have source ID and fragment ID
PacketGroups::parseToInstancePacketGroupRequireNoDefaults).forEach(instancePacketGroup -> {
long dateMillis = instancePacketGroup.getDateMillis();
for (Packet packet : instancePacketGroup.getPackets()) {
if (packet instanceof HeartbeatPacket) {
HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), instancePacketGroup.getFragmentId());
HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
map.put(identifier, node);
}
}
});
});
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> activeHeartbeats = new ArrayList<>();
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> expectedHeartbeats = new ArrayList<>();
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> deadHeartbeats = new ArrayList<>();
Instant now = Instant.now();
map.entrySet().stream().sorted(// iterate over oldest first
Comparator.comparingLong(entry -> entry.getValue().getDateMillis())).forEach(entry -> {
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).compareTo(now) > 0) {
activeHeartbeats.add(entry);
} else if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).plus(data.getBufferDuration()).compareTo(now) > 0) {
expectedHeartbeats.add(entry);
} else {
deadHeartbeats.add(entry);
}
});
StringBuilder builder = new StringBuilder();
if (!activeHeartbeats.isEmpty()) {
builder.append("Active:\n");
activeHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
if (!expectedHeartbeats.isEmpty()) {
builder.append("Expected:\n");
expectedHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
if (!deadHeartbeats.isEmpty()) {
builder.append("Dead:\n");
deadHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
String message = builder.toString();
if (message.isEmpty()) {
messageSender.sendMessage("No heartbeats");
} else {
messageSender.sendMessage(message);
}
}
use of me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode in project solarthing by wildmountainfarms.
the class NoHeartbeatEvent method runForEvent.
@Override
public void runForEvent(MessageSender sender, InstancePacketGroup packetGroup) {
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof HeartbeatPacket) {
HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
long dateMillis = packetGroup.getDateMillis();
int fragmentId = packetGroup.getFragmentId();
HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), fragmentId);
HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
map.put(identifier, node);
}
}
}
use of me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode in project solarthing by wildmountainfarms.
the class NoHeartbeatEvent method checkForAlerts.
private void checkForAlerts(MessageSender messageSender) {
long now = System.currentTimeMillis();
for (Iterator<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<HeartbeatIdentifier, HeartbeatNode> entry = it.next();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
long latestNextHeartbeat = entry.getValue().getDateMillis() + data.getExpectedDurationToNextHeartbeat().toMillis() + data.getBufferDuration().toMillis();
if (now > latestNextHeartbeat) {
it.remove();
alertFor(messageSender, entry.getValue());
}
}
}
Aggregations