From 6ea1567342cd7a66e671658dfbfa90ee46c676c3 Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Mon, 25 Apr 2022 19:09:37 +0200 Subject: [PATCH] make "online percentage/changes" warnings configurable --- .../service/NodeOnlineWarningsProvider.java | 22 +++- .../NodeOnlineWarningsProviderTest.java | 26 ++++- .../configuration/ConfigurationService.java | 10 ++ .../ConfigurationServiceTest.java | 107 +++++++++++++----- example-lnd-manageJ.conf | 2 + 5 files changed, 133 insertions(+), 34 deletions(-) diff --git a/backend/src/main/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProvider.java b/backend/src/main/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProvider.java index d9d02522..47c14f0c 100644 --- a/backend/src/main/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProvider.java +++ b/backend/src/main/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProvider.java @@ -1,5 +1,6 @@ package de.cotto.lndmanagej.service; +import de.cotto.lndmanagej.configuration.ConfigurationService; import de.cotto.lndmanagej.model.Pubkey; import de.cotto.lndmanagej.model.warnings.NodeOnlineChangesWarning; import de.cotto.lndmanagej.model.warnings.NodeOnlinePercentageWarning; @@ -13,15 +14,18 @@ import java.util.stream.Stream; @Component public class NodeOnlineWarningsProvider implements NodeWarningsProvider { - private static final int ONLINE_PERCENTAGE_THRESHOLD = 80; - private static final int ONLINE_CHANGES_THRESHOLD = 50; + private static final int DEFAULT_ONLINE_PERCENTAGE_THRESHOLD = 80; + private static final int DEFAULT_ONLINE_CHANGES_THRESHOLD = 50; private final OnlinePeersService onlinePeersService; + private final ConfigurationService configurationService; public NodeOnlineWarningsProvider( - OnlinePeersService onlinePeersService + OnlinePeersService onlinePeersService, + ConfigurationService configurationService ) { this.onlinePeersService = onlinePeersService; + this.configurationService = configurationService; } @Override @@ -36,7 +40,7 @@ public class NodeOnlineWarningsProvider implements NodeWarningsProvider { private Optional getOnlinePercentageWarning(Pubkey pubkey) { int onlinePercentage = onlinePeersService.getOnlinePercentage(pubkey); int daysForOnlinePercentage = onlinePeersService.getDaysForOnlinePercentage(); - if (onlinePercentage < ONLINE_PERCENTAGE_THRESHOLD) { + if (onlinePercentage < getOnlinePercentageThreshold()) { return Optional.of(new NodeOnlinePercentageWarning(onlinePercentage, daysForOnlinePercentage)); } return Optional.empty(); @@ -45,9 +49,17 @@ public class NodeOnlineWarningsProvider implements NodeWarningsProvider { private Optional getOnlineChangesWarning(Pubkey pubkey) { int changes = onlinePeersService.getChanges(pubkey); int daysForChanges = onlinePeersService.getDaysForChanges(); - if (changes > ONLINE_CHANGES_THRESHOLD) { + if (changes > getOnlineChangesThreshold()) { return Optional.of(new NodeOnlineChangesWarning(changes, daysForChanges)); } return Optional.empty(); } + + private int getOnlinePercentageThreshold() { + return configurationService.getOnlinePercentageThreshold().orElse(DEFAULT_ONLINE_PERCENTAGE_THRESHOLD); + } + + private int getOnlineChangesThreshold() { + return configurationService.getOnlineChangesThreshold().orElse(DEFAULT_ONLINE_CHANGES_THRESHOLD); + } } diff --git a/backend/src/test/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProviderTest.java b/backend/src/test/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProviderTest.java index 971eb1b5..a7d065c1 100644 --- a/backend/src/test/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProviderTest.java +++ b/backend/src/test/java/de/cotto/lndmanagej/service/NodeOnlineWarningsProviderTest.java @@ -1,5 +1,6 @@ package de.cotto.lndmanagej.service; +import de.cotto.lndmanagej.configuration.ConfigurationService; import de.cotto.lndmanagej.model.warnings.NodeOnlineChangesWarning; import de.cotto.lndmanagej.model.warnings.NodeOnlinePercentageWarning; import org.junit.jupiter.api.BeforeEach; @@ -9,6 +10,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Optional; + import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -21,6 +24,9 @@ class NodeOnlineWarningsProviderTest { @Mock private OnlinePeersService onlinePeersService; + @Mock + private ConfigurationService configurationService; + @BeforeEach void setUp() { when(onlinePeersService.getOnlinePercentage(PUBKEY)).thenReturn(80); @@ -35,6 +41,15 @@ class NodeOnlineWarningsProviderTest { .containsExactly(new NodeOnlinePercentageWarning(79, 456)); } + @Test + void getNodeWarnings_online_below_configured_threshold() { + when(configurationService.getOnlinePercentageThreshold()).thenReturn(Optional.of(99)); + when(onlinePeersService.getOnlinePercentage(PUBKEY)).thenReturn(98); + when(onlinePeersService.getDaysForOnlinePercentage()).thenReturn(456); + assertThat(warningsProvider.getNodeWarnings(PUBKEY)) + .containsExactly(new NodeOnlinePercentageWarning(98, 456)); + } + @Test void getNodeWarnings_online_changes_above_threshold() { when(onlinePeersService.getChanges(PUBKEY)).thenReturn(51); @@ -43,8 +58,17 @@ class NodeOnlineWarningsProviderTest { .containsExactly(new NodeOnlineChangesWarning(51, 123)); } + @Test + void getNodeWarnings_online_changes_above_configured_threshold() { + when(configurationService.getOnlineChangesThreshold()).thenReturn(Optional.of(30)); + when(onlinePeersService.getChanges(PUBKEY)).thenReturn(40); + when(onlinePeersService.getDaysForChanges()).thenReturn(123); + assertThat(warningsProvider.getNodeWarnings(PUBKEY)) + .containsExactly(new NodeOnlineChangesWarning(40, 123)); + } + @Test void getNodeWarnings_ok() { assertThat(warningsProvider.getNodeWarnings(PUBKEY)).isEmpty(); } -} \ No newline at end of file +} diff --git a/configuration/src/main/java/de/cotto/lndmanagej/configuration/ConfigurationService.java b/configuration/src/main/java/de/cotto/lndmanagej/configuration/ConfigurationService.java index 3801519d..0a975a7d 100644 --- a/configuration/src/main/java/de/cotto/lndmanagej/configuration/ConfigurationService.java +++ b/configuration/src/main/java/de/cotto/lndmanagej/configuration/ConfigurationService.java @@ -27,6 +27,8 @@ public class ConfigurationService { private static final String MAX_NUM_UPDATES = "max_num_updates"; private static final String NODE_FLOW_MINIMUM_DAYS_FOR_WARNING = "node_flow_minimum_days_for_warning"; private static final String NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER = "node_flow_maximum_days_to_consider"; + private static final String ONLINE_PERCENTAGE_THRESHOLD = "online_percentage_threshold"; + private static final String ONLINE_CHANGES_THRESHOLD = "online_changes_threshold"; private final IniFileReader iniFileReader; @@ -87,6 +89,14 @@ public class ConfigurationService { return getInteger(WARNINGS_SECTION, NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER); } + public Optional getOnlinePercentageThreshold() { + return getInteger(WARNINGS_SECTION, ONLINE_PERCENTAGE_THRESHOLD); + } + + public Optional getOnlineChangesThreshold() { + return getInteger(WARNINGS_SECTION, ONLINE_CHANGES_THRESHOLD); + } + private Optional getInteger(String sectionName, String configurationName) { Map> values = iniFileReader.getValues(sectionName); return values.getOrDefault(configurationName, Set.of()).stream() diff --git a/configuration/src/test/java/de/cotto/lndmanagej/configuration/ConfigurationServiceTest.java b/configuration/src/test/java/de/cotto/lndmanagej/configuration/ConfigurationServiceTest.java index a5eee0ff..1d262da6 100644 --- a/configuration/src/test/java/de/cotto/lndmanagej/configuration/ConfigurationServiceTest.java +++ b/configuration/src/test/java/de/cotto/lndmanagej/configuration/ConfigurationServiceTest.java @@ -7,7 +7,9 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import java.util.Map; +import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY; @@ -30,6 +32,8 @@ class ConfigurationServiceTest { private static final String MAX_NUM_UPDATES = "max_num_updates"; private static final String NODE_FLOW_MINIMUM_DAYS_FOR_WARNING = "node_flow_minimum_days_for_warning"; private static final String NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER = "node_flow_maximum_days_to_consider"; + private static final String ONLINE_PERCENTAGE_THRESHOLD = "online_percentage_threshold"; + private static final String ONLINE_CHANGES_THRESHOLD = "online_changes_threshold"; @InjectMocks private ConfigurationService configurationService; @@ -111,16 +115,18 @@ class ConfigurationServiceTest { @Test void getChannelBalanceFluctuationWarningLowerThreshold() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(CHANNEL_FLUCTUATION_LOWER_THRESHOLD, Set.of("1"))); - assertThat(configurationService.getChannelFluctuationWarningLowerThreshold()).contains(1); + assertValue( + configurationService::getChannelFluctuationWarningLowerThreshold, + CHANNEL_FLUCTUATION_LOWER_THRESHOLD + ); } @Test void getChannelBalanceFluctuationWarningLowerThreshold_not_integer() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(CHANNEL_FLUCTUATION_LOWER_THRESHOLD, Set.of("x"))); - assertThat(configurationService.getChannelFluctuationWarningLowerThreshold()).isEmpty(); + assertEmptyForNonIntegerValue( + configurationService::getChannelFluctuationWarningLowerThreshold, + CHANNEL_FLUCTUATION_LOWER_THRESHOLD + ); } @Test @@ -130,16 +136,18 @@ class ConfigurationServiceTest { @Test void getChannelBalanceFluctuationWarningUpperThreshold() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(CHANNEL_FLUCTUATION_UPPER_THRESHOLD, Set.of("99"))); - assertThat(configurationService.getChannelFluctuationWarningUpperThreshold()).contains(99); + assertValue( + configurationService::getChannelFluctuationWarningUpperThreshold, + CHANNEL_FLUCTUATION_UPPER_THRESHOLD + ); } @Test void getChannelBalanceFluctuationWarningUpperThreshold_not_integer() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(CHANNEL_FLUCTUATION_UPPER_THRESHOLD, Set.of("x"))); - assertThat(configurationService.getChannelFluctuationWarningUpperThreshold()).isEmpty(); + assertEmptyForNonIntegerValue( + configurationService::getChannelFluctuationWarningUpperThreshold, + CHANNEL_FLUCTUATION_UPPER_THRESHOLD + ); } @Test @@ -149,14 +157,12 @@ class ConfigurationServiceTest { @Test void getMaxNumUpdates() { - when(iniFileReader.getValues(WARNINGS_SECTION)).thenReturn(Map.of(MAX_NUM_UPDATES, Set.of("99"))); - assertThat(configurationService.getMaxNumUpdates()).contains(99); + assertValue(configurationService::getMaxNumUpdates, MAX_NUM_UPDATES); } @Test void getMaxNumUpdates_not_integer() { - when(iniFileReader.getValues(WARNINGS_SECTION)).thenReturn(Map.of(MAX_NUM_UPDATES, Set.of("x"))); - assertThat(configurationService.getMaxNumUpdates()).isEmpty(); + assertEmptyForNonIntegerValue(configurationService::getMaxNumUpdates, MAX_NUM_UPDATES); } @Test @@ -166,16 +172,15 @@ class ConfigurationServiceTest { @Test void getNodeFlowWarningMinimumDaysForWarning() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(NODE_FLOW_MINIMUM_DAYS_FOR_WARNING, Set.of("99"))); - assertThat(configurationService.getNodeFlowWarningMinimumDaysForWarning()).contains(99); + assertValue(configurationService::getNodeFlowWarningMinimumDaysForWarning, NODE_FLOW_MINIMUM_DAYS_FOR_WARNING); } @Test void getNodeFlowWarningMinimumDaysForWarning_not_integer() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(NODE_FLOW_MINIMUM_DAYS_FOR_WARNING, Set.of("x"))); - assertThat(configurationService.getNodeFlowWarningMinimumDaysForWarning()).isEmpty(); + assertEmptyForNonIntegerValue( + configurationService::getNodeFlowWarningMinimumDaysForWarning, + NODE_FLOW_MINIMUM_DAYS_FOR_WARNING + ); } @Test @@ -185,15 +190,61 @@ class ConfigurationServiceTest { @Test void getNodeFlowWarningMaximumDaysToConsider() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER, Set.of("99"))); - assertThat(configurationService.getNodeFlowWarningMaximumDaysToConsider()).contains(99); + assertValue(configurationService::getNodeFlowWarningMaximumDaysToConsider, NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER); } @Test void getNodeFlowWarningMaximumDaysToConsider_not_integer() { - when(iniFileReader.getValues(WARNINGS_SECTION)) - .thenReturn(Map.of(NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER, Set.of("x"))); - assertThat(configurationService.getNodeFlowWarningMaximumDaysToConsider()).isEmpty(); + assertEmptyForNonIntegerValue( + configurationService::getNodeFlowWarningMaximumDaysToConsider, + NODE_FLOW_MAXIMUM_DAYS_TO_CONSIDER + ); + } + + @Test + void getOnlinePercentageThreshold_defaults_to_empty() { + assertThat(configurationService.getOnlinePercentageThreshold()).isEmpty(); + } + + @Test + void getOnlinePercentageThreshold() { + assertValue(configurationService::getOnlinePercentageThreshold, ONLINE_PERCENTAGE_THRESHOLD); + } + + @Test + void getOnlinePercentageThreshold_not_integer() { + assertEmptyForNonIntegerValue( + configurationService::getOnlinePercentageThreshold, + ONLINE_PERCENTAGE_THRESHOLD + ); + } + + @Test + void getOnlineChangesThreshold_defaults_to_empty() { + assertThat(configurationService.getOnlineChangesThreshold()).isEmpty(); + } + + @Test + void getOnlineChangesThreshold() { + assertValue(configurationService::getOnlineChangesThreshold, ONLINE_CHANGES_THRESHOLD); + } + + @Test + void getOnlineChangesThreshold_not_integer() { + assertEmptyForNonIntegerValue( + configurationService::getOnlineChangesThreshold, + ONLINE_CHANGES_THRESHOLD + ); + } + + private void assertEmptyForNonIntegerValue(Supplier> supplier, String key) { + when(iniFileReader.getValues(WARNINGS_SECTION)).thenReturn(Map.of(key, Set.of("x"))); + assertThat(supplier.get()).isEmpty(); + } + + private void assertValue(Supplier> supplier, String key) { + int expectedValue = 42; + when(iniFileReader.getValues(WARNINGS_SECTION)).thenReturn(Map.of(key, Set.of(String.valueOf(expectedValue)))); + assertThat(supplier.get()).contains(expectedValue); } } diff --git a/example-lnd-manageJ.conf b/example-lnd-manageJ.conf index d833dea4..719996de 100644 --- a/example-lnd-manageJ.conf +++ b/example-lnd-manageJ.conf @@ -16,3 +16,5 @@ channel_fluctuation_upper_threshold=90 max_num_updates=100000 node_flow_minimum_days_for_warning=30 node_flow_maximum_days_to_consider=90 +online_percentage_threshold=80 +online_changes_threshold=50