From e946c4ccdabc15ebb33c267703e6830d04bbbc3d Mon Sep 17 00:00:00 2001 From: pz2 Date: Sat, 13 Jul 2024 21:47:46 +0200 Subject: [PATCH] Added `HofundConnectionsTable` to allow printing HofundConnection to terminal during booting up. --- README.md | 31 +++++++ .../000002-print_connections_to_terminal.yml | 8 ++ .../java/dev/logchange/hofund/AsciiTable.java | 90 +++++++++++++++++++ .../connection/HofundConnectionsTable.java | 43 +++++++++ .../logchange/hofund/connection/Status.java | 14 ++- .../dev/logchange/hofund/AsciiTableTest.java | 61 +++++++++++++ .../ConnectionTabelAutoConfigure.java | 23 +++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + 8 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/000002-print_connections_to_terminal.yml create mode 100644 hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java create mode 100644 hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java create mode 100644 hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java create mode 100644 hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/ConnectionTabelAutoConfigure.java diff --git a/README.md b/README.md index f7f4d6b..a9d7eec 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,37 @@ If you don't want to test connection in some conditions, you can use `new Simple - PostgreSQL - Oracle +### 7. Connection Tabel + +This simple functionality allows to print connections status in logger during booting up! + +```txt ++----------+--------------+----------+----------------------------------------------+ +| TYPE | NAME | STATUS | URL | ++----------+--------------+----------+----------------------------------------------+ +| DATABASE | mydb | UP | jdbc:postgresql://localhost:5432/mydb | +| DATABASE | mydb2 | UP | jdbc:mysql://localhost:3306/mydb2 | +| DATABASE | orcl | DOWN | jdbc:oracle:thin:@localhost:1521:orcl | +| HTTP | external-api | UP | https://api.external-service.com | +| HTTP | internal-api | UP | https://api.internal-service.local | +| HTTP | public-API | INACTIVE | https://api.public-service.com | ++----------+--------------+----------+----------------------------------------------+ +``` + +You can achieve this by creating simple class: + +```java +@Slf4j +@Component +public class PrintHofundConnectionsTabel { + + @Autowired + public PrintHofundConnectionsTabel(HofundConnectionsTable connectionsTable) { + log.info(connectionsTable.print()); + } +} +``` + # Grafana Dashboards ## [hofund-node-graph.json](https://github.com/logchange/hofund/raw/master/grafana-dashboards/hofund-node-graph.json) diff --git a/changelog/unreleased/000002-print_connections_to_terminal.yml b/changelog/unreleased/000002-print_connections_to_terminal.yml new file mode 100644 index 0000000..1cc4cde --- /dev/null +++ b/changelog/unreleased/000002-print_connections_to_terminal.yml @@ -0,0 +1,8 @@ +title: Added `HofundConnectionsTable` to allow printing HofundConnection to terminal during booting up. +authors: + - name: Peter Zmilczak + nick: marwin1991 + url: https://github.com/marwin1991 +type: added #[added/changed/deprecated/removed/fixed/security/other] +merge_requests: + - 43 \ No newline at end of file diff --git a/hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java b/hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java new file mode 100644 index 0000000..dbc1472 --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java @@ -0,0 +1,90 @@ +package dev.logchange.hofund; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class AsciiTable { + + private final List headers; + private final List> rows; + + public AsciiTable(List headers) { + this.headers = headers; + this.rows = new ArrayList<>(); + } + + + public void addRow(String... columns) { + if (columns.length != headers.size()) { + throw new IllegalArgumentException("Number of columns must match number of headers"); + } + rows.add(Arrays.asList(columns)); + } + + public String printTable() { + StringBuilder table = new StringBuilder(); + int[] columnWidths = new int[headers.size()]; + + // Calculate the width of each column + for (int i = 0; i < headers.size(); i++) { + columnWidths[i] = headers.get(i).length(); + } + + for (List row : rows) { + for (int i = 0; i < row.size(); i++) { + columnWidths[i] = Math.max(columnWidths[i], row.get(i).length()); + } + } + + // Append the headers + table.append(printSeparator(columnWidths)); + table.append(printRow(headers, columnWidths)); + table.append(printSeparator(columnWidths)); + + // Append the rows + for (List row : rows) { + table.append(printRow(row, columnWidths)); + } + table.append(printSeparator(columnWidths)); + + return table.toString(); + } + + private String printRow(List row, int[] columnWidths) { + StringBuilder rowString = new StringBuilder(); + rowString.append("| "); + for (int i = 0; i < row.size(); i++) { + if (i == row.size() - 1) { + rowString.append(padRight(row.get(i), columnWidths[i])).append(" |"); + } else { + rowString.append(padRight(row.get(i), columnWidths[i])).append(" | "); + } + } + rowString.append("\n"); + return rowString.toString(); + } + + private String printSeparator(int[] columnWidths) { + StringBuilder separator = new StringBuilder(); + separator.append("+"); + for (int width : columnWidths) { + separator.append(repeat("-", width + 2)).append("+"); + } + separator.append("\n"); + return separator.toString(); + } + + private String padRight(String text, int length) { + return String.format("%-" + length + "s", text); + } + + private String repeat(String str, int times) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < times; i++) { + result.append(str); + } + return result.toString(); + } + +} diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java new file mode 100644 index 0000000..989e123 --- /dev/null +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java @@ -0,0 +1,43 @@ +package dev.logchange.hofund.connection; + +import dev.logchange.hofund.AsciiTable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class HofundConnectionsTable { + + private static final List HEADERS = Arrays.asList("TYPE", "NAME", "STATUS", "URL"); + + private final List connections; + + public HofundConnectionsTable(List connectionsProviders) { + List connections = new ArrayList<>(); + for (HofundConnectionsProvider connectionsProvider : connectionsProviders) { + connections.addAll(connectionsProvider.getConnections()); + } + + this.connections = connections; + } + + public String print() { + AsciiTable table = new AsciiTable(HEADERS); + + for (HofundConnection connection : connections) { + table.addRow( + connection.getType().name(), + connection.getTarget(), + connection.getFun().get().getStatus().getName(), + connection.getUrl() + ); + } + + return table.printTable(); + } + + @Override + public String toString() { + return print(); + } +} \ No newline at end of file diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/Status.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/Status.java index 0def46b..9abf2ec 100644 --- a/hofund-core/src/main/java/dev/logchange/hofund/connection/Status.java +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/Status.java @@ -10,13 +10,25 @@ public class Status { private static final double UP_VALUE = 1.0; public static final Status UP = new Status(UP_VALUE); + private static final double DOWN_VALUE = 0.0; public static final Status DOWN = new Status(DOWN_VALUE); private static final double INACTIVE_VALUE = -1.0; - public static final Status INACTIVE = new Status(INACTIVE_VALUE); private final double value; + public String getName() { + if (value == UP_VALUE) { + return "UP"; + } else if (value == DOWN_VALUE) { + return "DOWN"; + } else if (value == INACTIVE_VALUE) { + return "INACTIVE"; + } else { + return "UNKNOWN"; + } + } + } diff --git a/hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java b/hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java new file mode 100644 index 0000000..fbf14a9 --- /dev/null +++ b/hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java @@ -0,0 +1,61 @@ +package dev.logchange.hofund; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class AsciiTableTest { + + @Test + public void testEmptyTable() { + AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); + String expected = + "+------+------+--------+-----+\n" + + "| TYPE | NAME | STATUS | URL |\n" + + "+------+------+--------+-----+\n" + + "+------+------+--------+-----+\n"; + assertEquals(expected, tablePrinter.printTable()); + } + + @Test + public void testSingleRow() { + AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); + tablePrinter.addRow("Type1", "Name1", "Status1", "http://url1.com"); + String expected = + "+-------+-------+---------+-----------------+\n" + + "| TYPE | NAME | STATUS | URL |\n" + + "+-------+-------+---------+-----------------+\n" + + "| Type1 | Name1 | Status1 | http://url1.com |\n" + + "+-------+-------+---------+-----------------+\n"; + assertEquals(expected, tablePrinter.printTable()); + } + + @Test + public void testMultipleRows() { + AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); + tablePrinter.addRow("Type1", "Name1", "Status1", "http://url1.com"); + tablePrinter.addRow("Type2", "Name2", "Status2", "http://url2.com"); + tablePrinter.addRow("Type3", "Name3", "Status3", "http://url3.com"); + String expected = + "+-------+-------+---------+-----------------+\n" + + "| TYPE | NAME | STATUS | URL |\n" + + "+-------+-------+---------+-----------------+\n" + + "| Type1 | Name1 | Status1 | http://url1.com |\n" + + "| Type2 | Name2 | Status2 | http://url2.com |\n" + + "| Type3 | Name3 | Status3 | http://url3.com |\n" + + "+-------+-------+---------+-----------------+\n"; + assertEquals(expected, tablePrinter.printTable()); + } + + @Test + public void testAddRowWithInvalidColumnCount() { + AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + tablePrinter.addRow("Type1", "Name1", "Status1"); // Missing one column + }); + assertEquals("Number of columns must match number of headers", thrown.getMessage()); + } +} diff --git a/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/ConnectionTabelAutoConfigure.java b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/ConnectionTabelAutoConfigure.java new file mode 100644 index 0000000..1d4dd7b --- /dev/null +++ b/hofund-spring-boot-autoconfigure/src/main/java/dev/logchange/hofund/connection/springboot/autoconfigure/ConnectionTabelAutoConfigure.java @@ -0,0 +1,23 @@ +package dev.logchange.hofund.connection.springboot.autoconfigure; + +import dev.logchange.hofund.connection.HofundConnectionsTable; +import dev.logchange.hofund.connection.HofundConnectionsProvider; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@Slf4j +@Configuration(proxyBeanMethods = false) +public class ConnectionTabelAutoConfigure { + + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty(name = "hofund.connections-tabel", havingValue = "true", matchIfMissing = true) + public HofundConnectionsTable connectionsTable(List hofundConnectionsProviders) { + return new HofundConnectionsTable(hofundConnectionsProviders); + } +} diff --git a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 3161f6c..b0bcd22 100644 --- a/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/hofund-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -2,6 +2,7 @@ dev.logchange.hofund.info.springboot.autoconfigure.HofundInfoAutoConfiguration dev.logchange.hofund.git.springboot.autoconfigure.HofundGitInfoAutoConfiguration dev.logchange.hofund.git.springboot.autoconfigure.HofundDefaultGitInfoProperties dev.logchange.hofund.connection.springboot.autoconfigure.HofundConnectionAutoConfiguration +dev.logchange.hofund.connection.springboot.autoconfigure.ConnectionTabelAutoConfigure dev.logchange.hofund.graph.springboot.autoconfigure.HofundGraphAutoConfiguration dev.logchange.hofund.java.springboot.autoconfigure.HofundJavaInfoAutoConfiguration dev.logchange.hofund.os.springboot.autoconfigure.HofundOsInfoAutoConfiguration