Skip to content

Commit b12adec

Browse files
committed
Rename Chart to Metric and move to data package
1 parent 195c9e3 commit b12adec

File tree

26 files changed

+297
-297
lines changed

26 files changed

+297
-297
lines changed

bukkit/example-plugin/src/main/java/com/example/ExamplePlugin.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import dev.faststats.bukkit.BukkitMetrics;
44
import dev.faststats.core.ErrorTracker;
5-
import dev.faststats.core.chart.Chart;
5+
import dev.faststats.core.data.Metric;
66
import org.bukkit.plugin.java.JavaPlugin;
77

88
import java.net.URI;
@@ -20,15 +20,15 @@ public class ExamplePlugin extends JavaPlugin {
2020
private final BukkitMetrics metrics = BukkitMetrics.factory()
2121
.url(URI.create("https://metrics.example.com/v1/collect")) // For self-hosted metrics servers only
2222

23-
// Custom example charts
23+
// Custom example metrics
2424
// For this to work you have to create a corresponding data source in your project settings first
25-
.addChart(Chart.number("example_chart", () -> 42))
26-
.addChart(Chart.number("game_count", gameCount::get))
27-
.addChart(Chart.string("example_string", () -> "Hello, World!"))
28-
.addChart(Chart.bool("example_boolean", () -> true))
29-
.addChart(Chart.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"}))
30-
.addChart(Chart.numberArray("example_number_array", () -> new Number[]{1, 2, 3}))
31-
.addChart(Chart.booleanArray("example_boolean_array", () -> new Boolean[]{true, false}))
25+
.addMetric(Metric.number("example_metric", () -> 42))
26+
.addMetric(Metric.number("game_count", gameCount::get))
27+
.addMetric(Metric.string("example_string", () -> "Hello, World!"))
28+
.addMetric(Metric.bool("example_boolean", () -> true))
29+
.addMetric(Metric.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"}))
30+
.addMetric(Metric.numberArray("example_number_array", () -> new Number[]{1, 2, 3}))
31+
.addMetric(Metric.booleanArray("example_boolean_array", () -> new Boolean[]{true, false}))
3232

3333
// Attach an error tracker
3434
// This must be enabled in the project settings
@@ -59,7 +59,7 @@ public void doSomethingWrong() {
5959
CONTEXT_UNAWARE_ERROR_TRACKER.trackError(e);
6060
}
6161
}
62-
62+
6363
public void startGame() {
6464
gameCount.incrementAndGet();
6565
}

bukkit/src/main/java/dev/faststats/bukkit/BukkitMetricsImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private BukkitMetricsImpl(final Factory factory, final Plugin plugin, final Path
2626
super(factory, config);
2727

2828
this.plugin = plugin;
29-
var server = plugin.getServer();
29+
final var server = plugin.getServer();
3030

3131
this.pluginVersion = tryOrEmpty(() -> plugin.getPluginMeta().getVersion())
3232
.orElseGet(() -> plugin.getDescription().getVersion());
@@ -43,15 +43,15 @@ Plugin plugin() {
4343
}
4444

4545
private boolean checkOnlineMode() {
46-
var server = plugin.getServer();
46+
final var server = plugin.getServer();
4747
return tryOrEmpty(() -> server.getServerConfig().isProxyOnlineMode())
4848
.or(() -> tryOrEmpty(this::isProxyOnlineMode))
4949
.orElseGet(server::getOnlineMode);
5050
}
5151

5252
@SuppressWarnings("removal")
5353
private boolean isProxyOnlineMode() {
54-
var server = plugin.getServer();
54+
final var server = plugin.getServer();
5555
final var proxies = server.spigot().getPaperConfig().getConfigurationSection("proxies");
5656
if (proxies == null) return false;
5757

@@ -64,12 +64,12 @@ private boolean isProxyOnlineMode() {
6464
}
6565

6666
@Override
67-
protected void appendDefaultData(final JsonObject charts) {
68-
charts.addProperty("minecraft_version", minecraftVersion);
69-
charts.addProperty("online_mode", checkOnlineMode());
70-
charts.addProperty("player_count", getPlayerCount());
71-
charts.addProperty("plugin_version", pluginVersion);
72-
charts.addProperty("server_type", serverType);
67+
protected void appendDefaultData(final JsonObject metrics) {
68+
metrics.addProperty("minecraft_version", minecraftVersion);
69+
metrics.addProperty("online_mode", checkOnlineMode());
70+
metrics.addProperty("player_count", getPlayerCount());
71+
metrics.addProperty("plugin_version", pluginVersion);
72+
metrics.addProperty("server_type", serverType);
7373
}
7474

7575
private int getPlayerCount() {

bungeecord/example-plugin/src/main/java/com/example/ExamplePlugin.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dev.faststats.bungee.BungeeMetrics;
44
import dev.faststats.core.ErrorTracker;
55
import dev.faststats.core.Metrics;
6-
import dev.faststats.core.chart.Chart;
6+
import dev.faststats.core.data.Metric;
77
import net.md_5.bungee.api.plugin.Plugin;
88

99
import java.net.URI;
@@ -18,14 +18,14 @@ public class ExamplePlugin extends Plugin {
1818
private final Metrics metrics = BungeeMetrics.factory()
1919
.url(URI.create("https://metrics.example.com/v1/collect")) // For self-hosted metrics servers only
2020

21-
// Custom example charts
21+
// Custom example metrics
2222
// For this to work you have to create a corresponding data source in your project settings first
23-
.addChart(Chart.number("example_chart", () -> 42))
24-
.addChart(Chart.string("example_string", () -> "Hello, World!"))
25-
.addChart(Chart.bool("example_boolean", () -> true))
26-
.addChart(Chart.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"}))
27-
.addChart(Chart.numberArray("example_number_array", () -> new Number[]{1, 2, 3}))
28-
.addChart(Chart.booleanArray("example_boolean_array", () -> new Boolean[]{true, false}))
23+
.addMetric(Metric.number("example_metric", () -> 42))
24+
.addMetric(Metric.string("example_string", () -> "Hello, World!"))
25+
.addMetric(Metric.bool("example_boolean", () -> true))
26+
.addMetric(Metric.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"}))
27+
.addMetric(Metric.numberArray("example_number_array", () -> new Number[]{1, 2, 3}))
28+
.addMetric(Metric.booleanArray("example_boolean_array", () -> new Boolean[]{true, false}))
2929

3030
// Attach an error tracker
3131
// This must be enabled in the project settings

bungeecord/src/main/java/dev/faststats/bungee/BungeeMetricsImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ private BungeeMetricsImpl(final Factory factory, final Plugin plugin, final Path
3131
}
3232

3333
@Override
34-
protected void appendDefaultData(final JsonObject charts) {
35-
charts.addProperty("online_mode", server.getConfig().isOnlineMode());
36-
charts.addProperty("player_count", server.getOnlineCount());
37-
charts.addProperty("plugin_version", plugin.getDescription().getVersion());
38-
charts.addProperty("proxy_version", server.getVersion());
39-
charts.addProperty("server_type", server.getName());
34+
protected void appendDefaultData(final JsonObject metrics) {
35+
metrics.addProperty("online_mode", server.getConfig().isOnlineMode());
36+
metrics.addProperty("player_count", server.getOnlineCount());
37+
metrics.addProperty("plugin_version", plugin.getDescription().getVersion());
38+
metrics.addProperty("proxy_version", server.getVersion());
39+
metrics.addProperty("server_type", server.getName());
4040
}
4141

4242
@Override

core/src/main/java/dev/faststats/core/ErrorTracker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static ErrorTracker contextUnaware() {
173173
* @since 0.14.0
174174
*/
175175
@Contract(pure = true)
176-
static boolean isSameLoader(ClassLoader loader, Throwable error) {
176+
static boolean isSameLoader(final ClassLoader loader, final Throwable error) {
177177
return ErrorHelper.isSameLoader(loader, error);
178178
}
179179
}

core/src/main/java/dev/faststats/core/Metrics.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.faststats.core;
22

3-
import dev.faststats.core.chart.Chart;
3+
import dev.faststats.core.data.Metric;
44
import org.jetbrains.annotations.Async;
55
import org.jetbrains.annotations.Contract;
66

@@ -71,17 +71,17 @@ default void ready() {
7171
*/
7272
interface Factory<T, F extends Factory<T, F>> {
7373
/**
74-
* Adds a chart to the metrics submission.
74+
* Adds a metric to the metrics submission.
7575
* <p>
76-
* If {@link Config#additionalMetrics()} is disabled, the chart will not be submitted.
76+
* If {@link Config#additionalMetrics()} is disabled, the metric will not be submitted.
7777
*
78-
* @param chart the chart to add
78+
* @param metric the metric to add
7979
* @return the metrics factory
80-
* @throws IllegalArgumentException if the chart is already added
81-
* @since 0.1.0
80+
* @throws IllegalArgumentException if the metric is already added
81+
* @since 0.16.0
8282
*/
8383
@Contract(mutates = "this")
84-
F addChart(Chart<?> chart) throws IllegalArgumentException;
84+
F addMetric(Metric<?> metric) throws IllegalArgumentException;
8585

8686
/**
8787
* Sets the flush callback for this metrics instance.

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.faststats.core;
22

33
import com.google.gson.JsonObject;
4-
import dev.faststats.core.chart.Chart;
4+
import dev.faststats.core.data.Metric;
55
import org.jetbrains.annotations.Async;
66
import org.jetbrains.annotations.Contract;
77
import org.jetbrains.annotations.MustBeInvokedByOverriders;
@@ -41,7 +41,7 @@ public abstract class SimpleMetrics implements Metrics {
4141
.build();
4242
private @Nullable ScheduledExecutorService executor = null;
4343

44-
private final Set<Chart<?>> charts;
44+
private final Set<Metric<?>> metrics;
4545
private final Config config;
4646
private final @Token String token;
4747
private final @Nullable ErrorTracker tracker;
@@ -55,7 +55,7 @@ protected SimpleMetrics(final Factory<?, ?> factory, final Config config) throws
5555
if (factory.token == null) throw new IllegalStateException("Token must be specified");
5656

5757
this.config = config;
58-
this.charts = config.additionalMetrics ? Set.copyOf(factory.charts) : Set.of();
58+
this.metrics = config.additionalMetrics ? Set.copyOf(factory.metrics) : Set.of();
5959
this.debug = factory.debug || Boolean.getBoolean("faststats.debug") || config.debug();
6060
this.token = factory.token;
6161
this.tracker = config.errorTracking ? factory.tracker : null;
@@ -71,7 +71,7 @@ protected SimpleMetrics(final Factory<?, ?> factory, final Path config) throws I
7171
@VisibleForTesting
7272
protected SimpleMetrics(
7373
final Config config,
74-
final Set<Chart<?>> charts,
74+
final Set<Metric<?>> metrics,
7575
@Token final String token,
7676
@Nullable final ErrorTracker tracker,
7777
@Nullable final Runnable flush,
@@ -82,7 +82,7 @@ protected SimpleMetrics(
8282
throw new IllegalArgumentException("Invalid token '" + token + "', must match '" + Token.PATTERN + "'");
8383
}
8484

85-
this.charts = config.additionalMetrics ? Set.copyOf(charts) : Set.of();
85+
this.metrics = config.additionalMetrics ? Set.copyOf(metrics) : Set.of();
8686
this.config = config;
8787
this.debug = debug;
8888
this.token = token;
@@ -235,32 +235,32 @@ private boolean submitNow() throws IOException {
235235

236236
protected JsonObject createData() {
237237
final var data = new JsonObject();
238-
final var charts = new JsonObject();
238+
final var metrics = new JsonObject();
239239

240-
charts.addProperty("java_version", javaVersion);
241-
charts.addProperty("os_arch", osArch);
242-
charts.addProperty("os_name", osName);
243-
charts.addProperty("os_version", osVersion);
244-
charts.addProperty("core_count", coreCount);
240+
metrics.addProperty("java_version", javaVersion);
241+
metrics.addProperty("os_arch", osArch);
242+
metrics.addProperty("os_name", osName);
243+
metrics.addProperty("os_version", osVersion);
244+
metrics.addProperty("core_count", coreCount);
245245

246-
this.charts.forEach(chart -> {
246+
this.metrics.forEach(metric -> {
247247
try {
248-
chart.getData().ifPresent(chartData -> charts.add(chart.getId(), chartData));
248+
metric.getData().ifPresent(element -> metrics.add(metric.getId(), element));
249249
} catch (final Throwable t) {
250-
error("Failed to build chart data: " + chart.getId(), t);
250+
error("Failed to build metric data: " + metric.getId(), t);
251251
getErrorTracker().ifPresent(tracker -> tracker.trackError(t));
252252
}
253253
});
254254

255255
try {
256-
appendDefaultData(charts);
256+
appendDefaultData(metrics);
257257
} catch (final Throwable t) {
258258
error("Failed to append default data", t);
259259
getErrorTracker().ifPresent(tracker -> tracker.trackError(t));
260260
}
261261

262262
data.addProperty("identifier", config.serverId().toString());
263-
data.add("data", charts);
263+
data.add("data", metrics);
264264

265265
getErrorTracker().map(SimpleErrorTracker.class::cast)
266266
.map(SimpleErrorTracker::getData)
@@ -285,7 +285,7 @@ public Metrics.Config getConfig() {
285285
}
286286

287287
@Contract(mutates = "param1")
288-
protected abstract void appendDefaultData(JsonObject charts);
288+
protected abstract void appendDefaultData(JsonObject metrics);
289289

290290
protected void error(final String message, @Nullable final Throwable throwable) {
291291
if (debug) printError("[" + getClass().getName() + "]: " + message, throwable);
@@ -322,7 +322,7 @@ public void shutdown() {
322322
}
323323

324324
public abstract static class Factory<T, F extends Metrics.Factory<T, F>> implements Metrics.Factory<T, F> {
325-
private final Set<Chart<?>> charts = new HashSet<>(0);
325+
private final Set<Metric<?>> metrics = new HashSet<>(0);
326326
private URI url = URI.create("https://metrics.faststats.dev/v1/collect");
327327
private @Nullable ErrorTracker tracker;
328328
private @Nullable Runnable flush;
@@ -331,8 +331,8 @@ public abstract static class Factory<T, F extends Metrics.Factory<T, F>> impleme
331331

332332
@Override
333333
@SuppressWarnings("unchecked")
334-
public F addChart(final Chart<?> chart) throws IllegalArgumentException {
335-
if (!charts.add(chart)) throw new IllegalArgumentException("Chart already added: " + chart.getId());
334+
public F addMetric(final Metric<?> metric) throws IllegalArgumentException {
335+
if (!metrics.add(metric)) throw new IllegalArgumentException("Metric already added: " + metric.getId());
336336
return (F) this;
337337
}
338338

0 commit comments

Comments
 (0)