|
| 1 | +package com.kamesuta.schemuploader; |
| 2 | + |
| 3 | +import org.bstats.bukkit.Metrics; |
| 4 | +import org.bstats.charts.SimplePie; |
| 5 | +import org.bstats.charts.SingleLineChart; |
| 6 | + |
| 7 | +import java.util.EnumMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | + |
| 11 | +import static com.kamesuta.schemuploader.SchemUploader.plugin; |
| 12 | + |
| 13 | +/** |
| 14 | + * Statistics of the plugin |
| 15 | + */ |
| 16 | +public class Statistics { |
| 17 | + public final ActionCounter actionCounter = new ActionCounter(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Register bStats |
| 21 | + */ |
| 22 | + public void register() { |
| 23 | + // Enable bStats |
| 24 | + Metrics metrics = new Metrics(plugin, 21061); |
| 25 | + |
| 26 | + // Config charts |
| 27 | + metrics.addCustomChart(new SimplePie("uploadEnabled", () -> PluginConfig.uploadEnabled ? "enabled" : "disabled")); |
| 28 | + metrics.addCustomChart(new SimplePie("downloadEnabled", () -> PluginConfig.downloadEnabled ? "enabled" : "disabled")); |
| 29 | + metrics.addCustomChart(new SimplePie("downloadUrlRestrictionEnabled", () -> PluginConfig.downloadUrlRestrictionEnabled ? "enabled" : "disabled")); |
| 30 | + metrics.addCustomChart(new SimplePie("language", () -> PluginConfig.language)); |
| 31 | + |
| 32 | + // The number of power actions performed |
| 33 | + for (ActionCounter.ActionType actionType : ActionCounter.ActionType.values()) { |
| 34 | + metrics.addCustomChart(new SingleLineChart(actionType.name, () -> actionCounter.collect(actionType))); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * The counter for each action |
| 40 | + */ |
| 41 | + public static class ActionCounter { |
| 42 | + private final Map<ActionType, AtomicInteger> countMap = new EnumMap<>(ActionType.class); |
| 43 | + |
| 44 | + /** |
| 45 | + * Increment the counter |
| 46 | + * |
| 47 | + * @param actionType type of action to get statistics for |
| 48 | + */ |
| 49 | + public void increment(ActionType actionType) { |
| 50 | + getOrCreate(actionType).incrementAndGet(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get the collected value and reset the counter |
| 55 | + * |
| 56 | + * @param actionType type of action to get statistics for |
| 57 | + * @return The collected value |
| 58 | + */ |
| 59 | + public int collect(ActionType actionType) { |
| 60 | + return getOrCreate(actionType).getAndSet(0); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get or create the counter |
| 65 | + * |
| 66 | + * @param actionType type of action to get statistics for |
| 67 | + * @return The counter |
| 68 | + */ |
| 69 | + private AtomicInteger getOrCreate(ActionType actionType) { |
| 70 | + return countMap.computeIfAbsent(actionType, (k) -> new AtomicInteger()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The service to collect statistics |
| 75 | + */ |
| 76 | + public enum ActionType { |
| 77 | + UPLOAD_SUCCESS("upload_success"), |
| 78 | + DOWNLOAD_SUCCESS("download_success"), |
| 79 | + UPLOAD_FAILURE("upload_failure"), |
| 80 | + DOWNLOAD_FAILURE("download_failure"), |
| 81 | + ; |
| 82 | + |
| 83 | + public final String name; |
| 84 | + |
| 85 | + ActionType(String name) { |
| 86 | + this.name = name; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments