Search in sources :

Example 1 with HeartbeatData

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

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

the class CommandOpenPacketsTest method test.

@Test
void test() throws JsonProcessingException {
    PacketTestUtil.testJson(new ImmutableRequestCommandPacket("GEN OFF"), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableRequestFlagPacket(new FlagData("disable_automations", new TimeRangeActivePeriod(TimeRange.create(Instant.parse("2021-10-05T04:53:22.877307Z"), Instant.parse("2021-10-05T04:53:44.305146Z"))))), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableScheduleCommandPacket(new ScheduledCommandData(System.currentTimeMillis(), "GEN OFF", Collections.singleton(1)), UUID.randomUUID()), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableDeleteAlterPacket("my_document_id", new RevisionUpdateToken("46-9ab7d71841380a36e1ec9e367deae36e")), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableRequestHeartbeatPacket(new HeartbeatData("Hourly Mate Ping", "heartbeat.ping.mate.hourly", Duration.ofHours(1), Duration.ofMinutes(5)), UUID.randomUUID()), CommandOpenPacket.class, true);
}
Also used : FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) RevisionUpdateToken(me.retrodaredevil.solarthing.database.couchdb.RevisionUpdateToken) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData) Test(org.junit.jupiter.api.Test)

Example 3 with HeartbeatData

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

Example 4 with HeartbeatData

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

HeartbeatData (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData)4 Instant (java.time.Instant)2 HashMap (java.util.HashMap)2 Packet (me.retrodaredevil.solarthing.packets.Packet)2 HeartbeatPacket (me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket)2 HeartbeatIdentifier (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)2 HeartbeatNode (me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 RequestHeartbeatPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket)1 RevisionUpdateToken (me.retrodaredevil.solarthing.database.couchdb.RevisionUpdateToken)1 ExecutionReason (me.retrodaredevil.solarthing.reason.ExecutionReason)1 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)1 FlagData (me.retrodaredevil.solarthing.type.alter.flag.FlagData)1 TimeRangeActivePeriod (me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod)1 ScheduledCommandData (me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)1 ImmutableHeartbeatPacket (me.retrodaredevil.solarthing.type.event.feedback.ImmutableHeartbeatPacket)1 OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)1 Test (org.junit.jupiter.api.Test)1