Skip to content

Commit ad15529

Browse files
committed
Add test constructor and improve logging in BukkitMetrics
1 parent b807779 commit ad15529

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

bukkit/src/main/java/org/faststats/BukkitMetrics.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.gson.stream.JsonWriter;
88
import org.bukkit.plugin.Plugin;
99
import org.faststats.chart.Chart;
10+
import org.jetbrains.annotations.TestOnly;
1011
import org.jspecify.annotations.NullMarked;
1112
import org.jspecify.annotations.Nullable;
1213

@@ -18,6 +19,7 @@
1819
import java.nio.file.StandardOpenOption;
1920
import java.util.Optional;
2021
import java.util.UUID;
22+
import java.util.concurrent.TimeUnit;
2123
import java.util.function.Supplier;
2224
import java.util.logging.Level;
2325

@@ -54,6 +56,30 @@ public BukkitMetrics(Plugin plugin, String token) throws IOException {
5456
startSubmitting();
5557
}
5658

59+
@TestOnly
60+
@SuppressWarnings("deprecation")
61+
public BukkitMetrics(Plugin plugin, String token, UUID serverId, boolean enabled, boolean debug) {
62+
this.serverId = serverId;
63+
this.enabled = enabled;
64+
this.debug = debug;
65+
66+
this.plugin = plugin;
67+
this.onlineMode = checkOnlineMode();
68+
69+
this.token = token;
70+
71+
addChart(Chart.pie("online_mode", () -> String.valueOf(onlineMode)));
72+
addChart(Chart.pie("plugin_version", () -> plugin.getDescription().getVersion()));
73+
addChart(Chart.pie("server_type", () -> plugin.getServer().getName()));
74+
addChart(Chart.pie("minecraft_version", () -> plugin.getServer().getMinecraftVersion()));
75+
addChart(Chart.line("player_count", () -> plugin.getServer().getOnlinePlayers().size()));
76+
startSubmitting();
77+
}
78+
79+
protected void startSubmitting() {
80+
startSubmitting(0, 30, TimeUnit.MINUTES);
81+
}
82+
5783
private static Optional<JsonObject> readOrCreate(Path path) throws IOException {
5884
if (Files.isRegularFile(path)) {
5985
return Optional.of(read(path));
@@ -101,14 +127,6 @@ private boolean isProxyOnlineMode() {
101127
return settings.getBoolean("bungeecord") && proxies.getBoolean("bungee-cord.online-mode");
102128
}
103129

104-
private <T> Optional<T> tryOrEmpty(Supplier<@Nullable T> supplier) {
105-
try {
106-
return Optional.ofNullable(supplier.get());
107-
} catch (NoSuchMethodError | Exception e) {
108-
return Optional.empty();
109-
}
110-
}
111-
112130
@Override
113131
protected UUID getServerId() {
114132
return serverId;
@@ -121,16 +139,29 @@ protected boolean isEnabled() {
121139

122140
@Override
123141
protected void error(String message, Throwable throwable) {
124-
if (debug) plugin.getLogger().log(Level.SEVERE, message, throwable);
142+
if (!debug) return;
143+
var msg = "[" + BukkitMetrics.class.getName() + "]: " + message;
144+
plugin.getLogger().log(Level.SEVERE, msg, throwable);
125145
}
126146

127147
@Override
128148
protected void debug(String message) {
129-
if (debug) plugin.getLogger().log(Level.INFO, message);
149+
if (!debug) return;
150+
var msg = "[" + BukkitMetrics.class.getName() + "]: " + message;
151+
plugin.getLogger().log(Level.INFO, msg);
130152
}
131153

132154
@Override
133155
public String getToken() {
134156
return token;
135157
}
158+
159+
private <T> Optional<T> tryOrEmpty(Supplier<@Nullable T> supplier) {
160+
try {
161+
return Optional.ofNullable(supplier.get());
162+
} catch (NoSuchMethodError | Exception e) {
163+
error("Failed to call supplier", e);
164+
return Optional.empty();
165+
}
166+
}
136167
}

core/src/main/java/org/faststats/SimpleMetrics.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.luben.zstd.Zstd;
44
import com.google.gson.JsonObject;
55
import org.faststats.chart.Chart;
6+
import org.jspecify.annotations.Nullable;
67

78
import java.io.IOException;
89
import java.net.URI;
@@ -21,19 +22,22 @@
2122
public abstract class SimpleMetrics implements Metrics {
2223
private final HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(3)).build();
2324
private final Set<Chart<?>> charts = new HashSet<>();
25+
private @Nullable ScheduledExecutorService executor = null;
2426

25-
private final ScheduledExecutorService executor;
27+
protected void startSubmitting(int initialDelay, int period, TimeUnit unit) {
28+
if (!isEnabled()) {
29+
debug("Metrics disabled, not starting submission");
30+
return;
31+
}
2632

27-
public SimpleMetrics() {
2833
this.executor = Executors.newSingleThreadScheduledExecutor(runnable -> {
2934
var thread = new Thread(runnable, "metrics-submitter");
3035
thread.setDaemon(true);
3136
return thread;
3237
});
33-
}
3438

35-
protected void startSubmitting() {
36-
if (isEnabled()) executor.scheduleAtFixedRate(this::submitData, 0, 30, TimeUnit.MINUTES);
39+
debug("Starting metrics submission");
40+
executor.scheduleAtFixedRate(this::submitData, initialDelay, period, unit);
3741
}
3842

3943
@Override
@@ -98,6 +102,9 @@ protected String getURL() {
98102
protected abstract void debug(String message);
99103

100104
public void shutdown() {
105+
debug("Shutting down metrics");
106+
if (executor == null) return;
101107
executor.shutdown();
108+
executor = null;
102109
}
103110
}

0 commit comments

Comments
 (0)