Skip to content

Commit 4bd799d

Browse files
committed
cleanup
1 parent 2c6cdf9 commit 4bd799d

4 files changed

Lines changed: 57 additions & 11 deletions

File tree

src/main/java/com/samourai/javaserver/utils/ServerUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.samourai.javaserver.utils;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import java.lang.invoke.MethodHandles;
5+
import java.util.Collection;
6+
import java.util.Comparator;
7+
import java.util.stream.Collectors;
48
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
610
import org.slf4j.event.Level;
@@ -11,6 +15,8 @@ public class ServerUtils {
1115
public static final String PROFILE_TEST = "test";
1216
public static final String PROFILE_DEFAULT = "default";
1317

18+
private static final ObjectMapper objectMapper = new ObjectMapper();
19+
1420
private static ServerUtils instance;
1521

1622
public static ServerUtils getInstance() {
@@ -30,4 +36,32 @@ public String obfuscateString(String str, int offset) {
3036
}
3137
return str.substring(0, offset) + "***" + str.substring(str.length() - offset, str.length());
3238
}
39+
40+
public String toJsonString(Object o) {
41+
try {
42+
return objectMapper.writeValueAsString(o);
43+
} catch (Exception e) {
44+
log.error("", e);
45+
}
46+
return null;
47+
}
48+
49+
public Collection<Thread> getThreads() {
50+
ThreadGroup tg = Thread.currentThread().getThreadGroup();
51+
Collection<Thread> threads =
52+
Thread.getAllStackTraces()
53+
.keySet()
54+
.stream()
55+
.filter(
56+
(t) -> {
57+
return t.getThreadGroup() == tg;
58+
})
59+
.sorted(
60+
Comparator.comparing(
61+
(o) -> {
62+
return o.getName().toLowerCase();
63+
}))
64+
.collect(Collectors.toList());
65+
return threads;
66+
}
3367
}

src/main/java/com/samourai/javaserver/web/models/ConfigTemplateModel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.samourai.javaserver.web.models;
22

3+
import com.samourai.javaserver.utils.ServerUtils;
34
import java.util.Map;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.web.ServerProperties;
47
import org.springframework.ui.Model;
58

69
public class ConfigTemplateModel extends DashboardTemplateModel {
710
public Map<String, String> configInfo;
11+
@Autowired protected ServerProperties serverProperties;
812

913
public ConfigTemplateModel(String pageTitle, String logoTitle, Map<String, String> configInfo) {
1014
super(pageTitle, logoTitle);
@@ -13,6 +17,7 @@ public ConfigTemplateModel(String pageTitle, String logoTitle, Map<String, Strin
1317

1418
public void apply(Model model) {
1519
super.apply(model);
20+
configInfo.put("serverProperties", ServerUtils.getInstance().toJsonString(serverProperties));
1621
model.addAttribute("configModel", this);
1722
}
1823
}

src/main/java/com/samourai/javaserver/web/models/SystemTemplateModel.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
package com.samourai.javaserver.web.models;
22

3+
import com.samourai.javaserver.utils.ServerUtils;
34
import java.util.Collection;
4-
import java.util.Comparator;
5-
import java.util.stream.Collectors;
5+
import java.util.Map;
66
import org.springframework.ui.Model;
77

88
public class SystemTemplateModel extends DashboardTemplateModel {
99
public Collection<Thread> threads;
1010
public long memUsed;
1111
public long memTotal;
1212
public long startupTime;
13+
public Map<String, String> metrics;
1314

14-
public SystemTemplateModel(String pageTitle, String logoTitle) {
15+
public SystemTemplateModel(String pageTitle, String logoTitle, Map<String, String> metrics) {
1516
super(pageTitle, logoTitle);
1617

17-
ThreadGroup tg = Thread.currentThread().getThreadGroup();
18-
this.threads =
19-
Thread.getAllStackTraces()
20-
.keySet()
21-
.stream()
22-
.filter(t -> t.getThreadGroup() == tg)
23-
.sorted(Comparator.comparing(o -> o.getName().toLowerCase()))
24-
.collect(Collectors.toList());
18+
this.threads = ServerUtils.getInstance().getThreads();
2519
if (false) { // template usage
2620
Thread t = threads.iterator().next();
2721
t.getName();
@@ -34,6 +28,8 @@ public SystemTemplateModel(String pageTitle, String logoTitle) {
3428
long free = rt.freeMemory();
3529
memUsed = bytesToMB(total - free);
3630
memTotal = bytesToMB(total);
31+
32+
this.metrics = metrics;
3733
}
3834

3935
public void setStartupTime(long startupTime) {

src/main/resources/templates/decorators/system.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,31 @@ <h1 class="h2">System</h1>
1414

1515
<div>Startup: <strong><span th:text="${#dates.format(systemModel.startupTime, 'dd-MMM-yyyy HH:mm:ss')}"/></strong></div>
1616
<div>Mem use: <strong><span th:text="${systemModel.memUsed}"/>M</strong>/<span th:text="${systemModel.memTotal}"/>M, <strong th:text="${systemModel.threads.size()}"/> threads running.</div>
17+
<table class="table table-sm system">
18+
<tr th:each="entry : *{systemModel.metrics}">
19+
<div><span th:text="${entry.key}"/>: <strong th:text="${entry.value}"></strong></div>
20+
</tr>
21+
</table>
1722
<div class="table-responsive">
1823
<table class="table table-sm system">
1924
<thead>
2025
<tr>
2126
<th scope="col"></th>
2227
<th scope="col">Thread</th>
2328
<th scope="col">Status</th>
29+
<th scope="col">Stacktrace</th>
2430
</tr>
2531
</thead>
2632
<tbody>
2733
<tr th:each="thread,iter : ${systemModel.threads}">
2834
<td class="threadIndex"><span th:text="${iter.index+1}"/></td>
2935
<td class="threadName"><span th:text="${thread.getName()}"/></td>
3036
<td class="threadState"><span th:text="${thread.getState()}"/></td>
37+
<td class="threadStacktrace">
38+
<div th:each="line : ${thread.getStackTrace()}">
39+
<div th:text="${line.toString()}"/>
40+
</div>
41+
</td>
3142
</tr>
3243
</tbody>
3344
</table>

0 commit comments

Comments
 (0)