Skip to content

Commit 5e62121

Browse files
committed
Refactor SimpleMetrics HTTP request and data creation
1 parent fe2637c commit 5e62121

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.faststats;
22

3+
import com.google.gson.FormattingStyle;
34
import com.google.gson.JsonObject;
45
import com.google.gson.JsonParser;
56
import com.google.gson.stream.JsonReader;
@@ -48,8 +49,8 @@ public BukkitMetrics(Plugin plugin, String token) throws IOException {
4849
addChart(Chart.pie("online_mode", () -> String.valueOf(onlineMode)));
4950
addChart(Chart.pie("plugin_version", () -> plugin.getDescription().getVersion()));
5051
addChart(Chart.pie("server_type", () -> plugin.getServer().getName()));
51-
addChart(Chart.pie("server_version", () -> plugin.getServer().getMinecraftVersion()));
52-
addChart(Chart.line("player_amount", () -> plugin.getServer().getOnlinePlayers().size()));
52+
addChart(Chart.pie("minecraft_version", () -> plugin.getServer().getMinecraftVersion()));
53+
addChart(Chart.line("player_count", () -> plugin.getServer().getOnlinePlayers().size())); // todo: rename in backend
5354
startSubmitting();
5455
}
5556

@@ -63,8 +64,10 @@ private static Optional<JsonObject> readOrCreate(Path path) throws IOException {
6364
}
6465

6566
private static void create(Path path) throws IOException {
67+
Files.createDirectories(path.getParent());
6668
try (var out = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW);
6769
var writer = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
70+
writer.setFormattingStyle(FormattingStyle.PRETTY);
6871
writer.beginObject();
6972
writer.name("serverId").value(UUID.randomUUID().toString());
7073
writer.name("enabled").value(true);

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
public abstract class SimpleMetrics implements Metrics {
22-
private final HttpClient httpClient = HttpClient.newBuilder()
23-
.connectTimeout(Duration.ofSeconds(3))
24-
.build();
22+
private final HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(3)).build();
2523
private final Set<Chart<?>> charts = new HashSet<>();
2624

2725
private final ScheduledExecutorService executor;
@@ -47,42 +45,35 @@ protected void submitData() {
4745
try {
4846
var bytes = createData().toString().getBytes(StandardCharsets.UTF_8);
4947
var compressed = Zstd.compress(bytes, 6);
50-
var request = HttpRequest.newBuilder()
51-
.POST(HttpRequest.BodyPublishers.ofByteArray(compressed))
52-
.header("Content-Encoding", "zstd")
53-
.header("Content-Type", "application/octet-stream")
54-
.header("Authorization", "Bearer " + getToken())
55-
.header("User-Agent", "fastStats Metrics")
56-
.timeout(Duration.ofSeconds(3))
57-
.uri(URI.create(getURL()))
58-
.build();
48+
var request = HttpRequest.newBuilder().POST(HttpRequest.BodyPublishers.ofByteArray(compressed)).header("content-encoding", "zstd").header("content-type", "application/octet-stream").header("authorization", "Bearer " + getToken()).header("user-agent", "fastStats Metrics").timeout(Duration.ofSeconds(3)).uri(URI.create(getURL())).build();
5949
httpClient.send(request, HttpResponse.BodyHandlers.discarding());
50+
// todo: add debugs
6051
} catch (IOException | InterruptedException e) {
52+
// todo: shorten connection errors
6153
error("Failed to submit metrics", e);
6254
}
6355
}
6456

6557
protected JsonObject createData() {
6658
var data = new JsonObject();
67-
data.addProperty("serverIdentifier", getServerId().toString());
68-
data.addProperty("token", getToken());
59+
var charts = new JsonObject();
6960

70-
data.addProperty("javaVersion", System.getProperty("java.version"));
71-
data.addProperty("locale", System.getProperty("user.language"));
72-
data.addProperty("osArch", System.getProperty("os.arch"));
73-
data.addProperty("osName", System.getProperty("os.name"));
74-
data.addProperty("osVersion", System.getProperty("os.version"));
75-
data.addProperty("processors", Runtime.getRuntime().availableProcessors());
61+
charts.addProperty("java_version", System.getProperty("java.version"));
62+
charts.addProperty("os_arch", System.getProperty("os.arch")); // todo: rename in backend
63+
charts.addProperty("os_name", System.getProperty("os.name")); // todo: rename in backend
64+
charts.addProperty("os_version", System.getProperty("os.version")); // todo: rename in backend
65+
charts.addProperty("core_count", Runtime.getRuntime().availableProcessors());
7666

77-
var charts = new JsonObject();
7867
this.charts.forEach(chart -> {
7968
try {
8069
chart.getData().ifPresent(chartData -> charts.add(chart.getId(), chartData));
8170
} catch (Exception e) {
8271
error("Failed to build chart data: " + chart.getId(), e);
8372
}
8473
});
85-
if (!charts.isEmpty()) data.add("charts", charts);
74+
75+
data.addProperty("server_id", getServerId().toString());
76+
data.add("data", charts);
8677
return data;
8778
}
8879

0 commit comments

Comments
 (0)