-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/io/github/thebusybiscuit/slimefun4/core/services/AnalyticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package io.github.thebusybiscuit.slimefun4.core.services; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import org.bukkit.plugin.Plugin; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
|
||
import io.github.thebusybiscuit.slimefun4.core.debug.Debug; | ||
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase; | ||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; | ||
|
||
/** | ||
* This class represents an analytics service that sends data | ||
* This data is used to analyse performance of this {@link Plugin}. | ||
* <p> | ||
* You can find more info in the README file of this Project on GitHub. <br> | ||
* | ||
* @author WalshyDev | ||
*/ | ||
public class AnalyticsService { | ||
|
||
private static final int VERSION = 1; | ||
private static final String API_URL = "https://sf-analytics.walshy.dev/ingest"; | ||
|
||
private final boolean enabled; | ||
private final HttpClient client = HttpClient.newHttpClient(); | ||
|
||
public AnalyticsService(Slimefun plugin) { | ||
this.enabled = plugin.getConfig().getBoolean("metrics.analytics"); | ||
|
||
if (enabled) { | ||
plugin.getLogger().info("Enabled Analytics Service"); | ||
|
||
// Send the timings data every minute | ||
plugin.getServer().getScheduler() | ||
.runTaskTimerAsynchronously(plugin, sendTimingsAnalytics(), 20 * 60, 20 * 60); | ||
} | ||
} | ||
|
||
// Timings we'll send the average every minute | ||
private Runnable sendTimingsAnalytics() { | ||
return () -> { | ||
double tickInterval = Slimefun.getTickerTask().getTickRate(); | ||
// This is currently used by bStats in a ranged way, we'll move this | ||
double totalTimings = Slimefun.getProfiler().getAndResetAverageNanosecondTimings(); | ||
double avgPerMachine = Slimefun.getProfiler().getAverageTimingsPerMachine(); | ||
|
||
if (totalTimings == 0 || avgPerMachine == 0) { | ||
Debug.log(TestCase.ANALYTICS, "Ignoring analytics data for server_timings as no data was found" | ||
+ " - total: " + totalTimings + ", avg: " + avgPerMachine); | ||
// Ignore if no data | ||
return; | ||
} | ||
|
||
send("server_timings", new double[]{ | ||
tickInterval, | ||
totalTimings, | ||
avgPerMachine | ||
}, null); | ||
}; | ||
} | ||
|
||
// Important: Keep the order of these doubles and blobs the same unless you increment the version number | ||
// If a value is no longer used, just send null or replace it with a new value - don't shift the order | ||
@ParametersAreNonnullByDefault | ||
private void send(String id, double[] doubles, String[] blobs) { | ||
// If not enabled, just ignore. | ||
if (!enabled) return; | ||
|
||
JsonObject object = new JsonObject(); | ||
object.addProperty("index", id); | ||
|
||
JsonArray doublesArray = new JsonArray(); | ||
doublesArray.add(VERSION); | ||
if (doubles != null) { | ||
for (double d : doubles) { | ||
doublesArray.add(d); | ||
} | ||
} | ||
object.add("doubles", doublesArray); | ||
|
||
JsonArray blobsArray = new JsonArray(); | ||
if (blobs != null) { | ||
for (String s : blobs) { | ||
blobsArray.add(s); | ||
} | ||
} | ||
object.add("blobs", blobsArray); | ||
|
||
Debug.log(TestCase.ANALYTICS, "Sending analytics data for " + id); | ||
Debug.log(TestCase.ANALYTICS, object.toString()); | ||
|
||
// Send async, we do not care about the result. If it fails, that's fine. | ||
client.sendAsync(HttpRequest.newBuilder() | ||
.uri(URI.create(API_URL)) | ||
.POST(HttpRequest.BodyPublishers.ofString(object.toString())) | ||
.build(), HttpResponse.BodyHandlers.discarding()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ talismans: | |
|
||
metrics: | ||
auto-update: true | ||
analytics: true | ||
|
||
research-ranks: | ||
- Chicken | ||
|