Skip to content

Commit

Permalink
bStatsを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Feb 19, 2024
1 parent 65fb4cd commit 1580543
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ file:
- `schemuploader.upload`: Permission to use the schem file upload feature
- `schemuploader.download`: Permission to use the schem file download feature

## About Statistics Data

BungeePteroPower collects anonymous statistical data using [bStats](https://bstats.org/).
You can find the statistics data [here](https://bstats.org/plugin/bukkit/SchemUploader/21061).

bStats is used to understand the usage of the plugin and help improve it.
To disable the collection of statistical data, please set `enabled` to `false` in `plugins/bStats/config.yml`
8 changes: 8 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ download:
- `schemuploader.upload`: schemファイルのアップロード機能を使用する権限
- `schemuploader.download`: schemファイルのダウンロード機能を使用する権限

## 統計データについて

BungeePteroPowerは、[bStats](https://bstats.org/)を使用して匿名の統計データを収集しています。
統計データは[こちら](https://bstats.org/plugin/bukkit/SchemUploader/21061)。

bStatsは、プラグインの使用状況を把握するために使用され、プラグインの改善に役立てられます。
統計データの収集を無効にするには、`plugins/bStats/config.yml`の `enabled` を `false` に設定してください。
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@
<version>2.8.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>
14 changes: 14 additions & 0 deletions src/main/java/com/kamesuta/schemuploader/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
// Send an error message if the upload fails
if (!result.success) {
sender.sendMessage(plugin.messages.error("upload_failed", result.error));

// Add record to statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.UPLOAD_FAILURE);

return;
}

Expand All @@ -101,6 +105,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, result.url))
.create()
);

// Add record to statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.UPLOAD_SUCCESS);
});

return true;
Expand Down Expand Up @@ -174,6 +181,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
} else {
sender.sendMessage(plugin.messages.error("download_failed", result.error));
}

// Add record to statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.DOWNLOAD_FAILURE);

return;
}

Expand All @@ -184,6 +195,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, url))
.create()
);

// Add record to statistics
plugin.statistics.actionCounter.increment(Statistics.ActionCounter.ActionType.DOWNLOAD_SUCCESS);
});

return true;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/kamesuta/schemuploader/SchemUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public final class SchemUploader extends JavaPlugin {
*/
public static SchemUploader plugin;

/**
* Statistics
*/
public Statistics statistics;

/**
* Fallback Translations
*/
Expand Down Expand Up @@ -68,6 +73,9 @@ public void onEnable() {

// Register commands
CommandListener.register();

// Statistics
statistics = new Statistics();
}

@Override
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/kamesuta/schemuploader/Statistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.kamesuta.schemuploader;

import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bstats.charts.SingleLineChart;

import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static com.kamesuta.schemuploader.SchemUploader.plugin;

/**
* Statistics of the plugin
*/
public class Statistics {
public final ActionCounter actionCounter = new ActionCounter();

/**
* Register bStats
*/
public void register() {
// Enable bStats
Metrics metrics = new Metrics(plugin, 21061);

// Config charts
metrics.addCustomChart(new SimplePie("uploadEnabled", () -> PluginConfig.uploadEnabled ? "enabled" : "disabled"));
metrics.addCustomChart(new SimplePie("downloadEnabled", () -> PluginConfig.downloadEnabled ? "enabled" : "disabled"));
metrics.addCustomChart(new SimplePie("downloadUrlRestrictionEnabled", () -> PluginConfig.downloadUrlRestrictionEnabled ? "enabled" : "disabled"));
metrics.addCustomChart(new SimplePie("language", () -> PluginConfig.language));

// The number of power actions performed
for (ActionCounter.ActionType actionType : ActionCounter.ActionType.values()) {
metrics.addCustomChart(new SingleLineChart(actionType.name, () -> actionCounter.collect(actionType)));
}
}

/**
* The counter for each action
*/
public static class ActionCounter {
private final Map<ActionType, AtomicInteger> countMap = new EnumMap<>(ActionType.class);

/**
* Increment the counter
*
* @param actionType type of action to get statistics for
*/
public void increment(ActionType actionType) {
getOrCreate(actionType).incrementAndGet();
}

/**
* Get the collected value and reset the counter
*
* @param actionType type of action to get statistics for
* @return The collected value
*/
public int collect(ActionType actionType) {
return getOrCreate(actionType).getAndSet(0);
}

/**
* Get or create the counter
*
* @param actionType type of action to get statistics for
* @return The counter
*/
private AtomicInteger getOrCreate(ActionType actionType) {
return countMap.computeIfAbsent(actionType, (k) -> new AtomicInteger());
}

/**
* The service to collect statistics
*/
public enum ActionType {
UPLOAD_SUCCESS("upload_success"),
DOWNLOAD_SUCCESS("download_success"),
UPLOAD_FAILURE("upload_failure"),
DOWNLOAD_FAILURE("download_failure"),
;

public final String name;

ActionType(String name) {
this.name = name;
}
}
}
}

0 comments on commit 1580543

Please sign in to comment.