Cleanup new metrics implementation. (#2286)

* Refactor metrics initialization into its own class.

* Simplify the creation of metrics.

* Clean up new metrics.

* Refactor out duplicate metrics code.
This commit is contained in:
Jeremy Wood 2020-06-13 00:51:18 -04:00 committed by GitHub
parent 268c4982c3
commit 04c65cc59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 39 deletions

View File

@ -32,7 +32,6 @@ import com.onarandombox.MultiverseCore.api.MVPlugin;
import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig;
import com.onarandombox.MultiverseCore.api.MultiverseMessaging; import com.onarandombox.MultiverseCore.api.MultiverseMessaging;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.commands.AnchorCommand; import com.onarandombox.MultiverseCore.commands.AnchorCommand;
import com.onarandombox.MultiverseCore.commands.CheckCommand; import com.onarandombox.MultiverseCore.commands.CheckCommand;
@ -94,6 +93,7 @@ import com.onarandombox.MultiverseCore.utils.MVMessaging;
import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.MVPermissions;
import com.onarandombox.MultiverseCore.utils.MVPlayerSession; import com.onarandombox.MultiverseCore.utils.MVPlayerSession;
import com.onarandombox.MultiverseCore.utils.MaterialConverter; import com.onarandombox.MultiverseCore.utils.MaterialConverter;
import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator;
import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety;
import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation;
import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter;
@ -103,8 +103,6 @@ import com.onarandombox.MultiverseCore.utils.WorldManager;
import com.pneumaticraft.commandhandler.CommandHandler; import com.pneumaticraft.commandhandler.CommandHandler;
import me.main__.util.SerializationConfig.NoSuchPropertyException; import me.main__.util.SerializationConfig.NoSuchPropertyException;
import me.main__.util.SerializationConfig.SerializationConfig; import me.main__.util.SerializationConfig.SerializationConfig;
import org.apache.commons.lang.StringUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -333,7 +331,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
getServer().getPluginManager().registerEvents(this.chatListener, this); getServer().getPluginManager().registerEvents(this.chatListener, this);
this.initializeBuscript(); this.initializeBuscript();
this.setupMetrics();
MetricsConfigurator.configureMetrics(this);
// Output a little snippet to show it's enabled. // Output a little snippet to show it's enabled.
Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors());
@ -353,41 +352,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
buscript.setScriptVariable("multiverse", this); buscript.setScriptVariable("multiverse", this);
} }
private void setupMetrics() {
try {
Metrics metrics = new Metrics(this, 7765);
metrics.addCustomChart(new Metrics.AdvancedPie("custom_generators", () -> {
Map<String, Integer> map = new HashMap<>();
for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) {
String gen = w.getGenerator() != null ? w.getGenerator() : "N/A";
map.putIfAbsent(gen, 0);
map.put(gen, map.get(gen) + 1);
}
return map;
}));
metrics.addCustomChart(new Metrics.AdvancedPie("environments", () -> {
Map<String, Integer> map = new HashMap<>();
for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) {
String env = w.getEnvironment().name().replace('_', ' ');
env = StringUtils.capitalize(env.toLowerCase());
map.putIfAbsent(env, 0);
map.put(env, map.get(env) + 1);
}
return map;
}));
// TODO: add Worlds vs Loaded Worlds once bStats adds support for multi-line charts
log(Level.FINE, "Metrics were set up!");
} catch (Exception e) {
log(Level.WARNING, "There was an issue while enabling metrics:");
e.printStackTrace();
}
}
private void initializeDestinationFactory() { private void initializeDestinationFactory() {
this.destFactory = new DestinationFactory(this); this.destFactory = new DestinationFactory(this);
this.destFactory.registerDestinationType(WorldDestination.class, ""); this.destFactory.registerDestinationType(WorldDestination.class, "");

View File

@ -0,0 +1,71 @@
package com.onarandombox.MultiverseCore.utils.metrics;
import java.util.Map;
import java.util.function.Consumer;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.apache.commons.lang.StringUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.World;
public class MetricsConfigurator {
private static final int PLUGIN_ID = 7765;
private static final String NO_GENERATOR_NAME = "N/A";
public static void configureMetrics(MultiverseCore plugin) {
MetricsConfigurator configurator = new MetricsConfigurator(plugin);
configurator.initMetrics();
}
private final MultiverseCore plugin;
private final Metrics metrics;
private MetricsConfigurator(MultiverseCore plugin) {
this.plugin = plugin;
this.metrics = new Metrics(plugin, PLUGIN_ID);
}
private void initMetrics() {
try {
addCustomGeneratorsMetric();
createEnvironmentsMetric();
Logging.fine("Metrics enabled.");
} catch (Exception e) {
Logging.warning("There was an issue while enabling metrics:");
e.printStackTrace();
}
}
private void addCustomGeneratorsMetric() {
addAdvancedPieMetric("custom_generators", map -> {
for (MultiverseWorld w : plugin.getMVWorldManager().getMVWorlds()) {
MetricsHelper.incrementCount(map, getGeneratorName(w));
}
});
}
private String getGeneratorName(MultiverseWorld world) {
return world.getGenerator() != null ? world.getGenerator() : NO_GENERATOR_NAME;
}
private void createEnvironmentsMetric() {
addAdvancedPieMetric("environments", map -> {
for (MultiverseWorld w : plugin.getMVWorldManager().getMVWorlds()) {
MetricsHelper.incrementCount(map, titleCaseEnv(w.getEnvironment()));
}
});
}
private String titleCaseEnv(World.Environment env) {
String envName = env.name().replaceAll("_+", " ");
return StringUtils.capitalize(envName.toLowerCase());
}
private void addAdvancedPieMetric(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
metrics.addCustomChart(MetricsHelper.createAdvancedPieChart(chartId, metricsFunc));
}
}

View File

@ -0,0 +1,26 @@
package com.onarandombox.MultiverseCore.utils.metrics;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.bstats.bukkit.Metrics;
enum MetricsHelper {
;
/**
* Adds one to the value in the given map with the given key. If the key does not exist in the map, it will be added with a value of 1.
*/
static void incrementCount(Map<String, Integer> map, String key) {
Integer count = map.getOrDefault(key, 0);
map.put(key, count + 1);
}
static Metrics.AdvancedPie createAdvancedPieChart(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
Map<String, Integer> map = new HashMap<>();
metricsFunc.accept(map);
return new Metrics.AdvancedPie(chartId, () -> map);
}
}