Skip to content

Commit

Permalink
Merge pull request #43 from logchange/print-tabel
Browse files Browse the repository at this point in the history
Added `HofundConnectionsTable` to allow printing HofundConnection to …
  • Loading branch information
marwin1991 authored Jul 14, 2024
2 parents 17b2a75 + da9d6ab commit b7e3479
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 1 deletion.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions changelog/unreleased/000002-print_connections_to_terminal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]
issues:
- 26
merge_requests:
- 43
90 changes: 90 additions & 0 deletions hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java
Original file line number Diff line number Diff line change
@@ -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<String> headers;
private final List<List<String>> rows;

public AsciiTable(List<String> 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<String> 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<String> row : rows) {
table.append(printRow(row, columnWidths));
}
table.append(printSeparator(columnWidths));

return table.toString();
}

private String printRow(List<String> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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<String> HEADERS = Arrays.asList("TYPE", "NAME", "STATUS", "URL");

private final List<HofundConnection> connections;

public HofundConnectionsTable(List<HofundConnectionsProvider> connectionsProviders) {
List<HofundConnection> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

}
61 changes: 61 additions & 0 deletions hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<HofundConnectionsProvider> hofundConnectionsProviders) {
return new HofundConnectionsTable(hofundConnectionsProviders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7e3479

Please sign in to comment.