Search in sources :

Example 1 with HeartbeatPacket

use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket 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 HeartbeatPacket

use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket 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 HeartbeatPacket

use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket in project solarthing by wildmountainfarms.

the class RequestHeartbeatReceiver method receivePacketGroup.

@Override
public void receivePacketGroup(String sender, TargetPacketGroup packetGroup) {
    Instant now = Instant.now();
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof RequestHeartbeatPacket) {
            RequestHeartbeatPacket requestHeartbeatPacket = (RequestHeartbeatPacket) packet;
            HeartbeatData data = requestHeartbeatPacket.getData();
            OpenSource openSource = new OpenSource(sender, packetGroup.getDateMillis(), requestHeartbeatPacket, requestHeartbeatPacket.getUniqueString());
            ExecutionReason executionReason = new OpenSourceExecutionReason(openSource);
            HeartbeatPacket heartbeatPacket = new ImmutableHeartbeatPacket(data, executionReason);
            eventPacketListReceiverHandler.uploadSimple(now, Arrays.asList(heartbeatPacket));
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) RequestHeartbeatPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket) ImmutableHeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableHeartbeatPacket) RequestHeartbeatPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket) HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) Instant(java.time.Instant) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) RequestHeartbeatPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket) ImmutableHeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableHeartbeatPacket) ExecutionReason(me.retrodaredevil.solarthing.reason.ExecutionReason) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) ImmutableHeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableHeartbeatPacket)

Aggregations

Packet (me.retrodaredevil.solarthing.packets.Packet)3 HeartbeatPacket (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket)3 Instant (java.time.Instant)2 HeartbeatData (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData)2 HeartbeatIdentifier (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)2 HeartbeatNode (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RequestHeartbeatPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket)1 ExecutionReason (me.retrodaredevil.solarthing.reason.ExecutionReason)1 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)1 ImmutableHeartbeatPacket (me.retrodaredevil.solarthing.type.event.feedback.ImmutableHeartbeatPacket)1 OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)1