Search in sources :

Example 1 with HeartbeatNode

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);
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HashMap(java.util.HashMap) HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) Instant(java.time.Instant) ArrayList(java.util.ArrayList) HeartbeatNode(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode) HeartbeatIdentifier(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)

Example 2 with HeartbeatNode

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);
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HeartbeatNode(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode) HeartbeatIdentifier(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)

Example 3 with HeartbeatNode

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());
        }
    }
}
Also used : HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) HeartbeatNode(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode) Map(java.util.Map) HashMap(java.util.HashMap) HeartbeatIdentifier(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)

Aggregations

HeartbeatIdentifier (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)3 HeartbeatNode (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode)3 HashMap (java.util.HashMap)2 Packet (me.retrodaredevil.solarthing.packets.Packet)2 HeartbeatData (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData)2 HeartbeatPacket (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket)2 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1