From 8a4ba2a66cbfee0768628ebafb244b65a5908772 Mon Sep 17 00:00:00 2001 From: "main()" Date: Thu, 12 Jul 2012 17:23:11 +0200 Subject: [PATCH] Metrics! --- pom.xml | 18 ++++ .../MultiverseCore/MultiverseCore.java | 93 +++++++++++++++++++ .../test/utils/TestInstanceCreator.java | 7 ++ 3 files changed, 118 insertions(+) diff --git a/pom.xml b/pom.xml index c4a693eb..b2bd76e1 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,10 @@ Bukkit Official http://repo.bukkit.org/content/repositories/public + + mcstats + http://repo.mcstats.org/content/repositories/snapshots + @@ -152,6 +156,7 @@ me.main__.util:SerializationConfig com.fernferret.allpay:AllPay com.pneumaticraft.commandhandler:CommandHandler + org.mcstats:metrics @@ -167,6 +172,10 @@ com.pneumaticraft.commandhandler com.pneumaticraft.commandhandler.multiverse + + org.mcstats + org.mcstats.multiverse + @@ -221,6 +230,15 @@ compile + + + org.mcstats + metrics + 1.0-SNAPSHOT + jar + compile + + junit diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index cb079ff8..3597af0e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -75,6 +75,7 @@ import com.pneumaticraft.commandhandler.CommandHandler; import me.main__.util.SerializationConfig.SerializationConfig; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.World.Environment; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.Configuration; @@ -84,6 +85,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.mcstats.Metrics; import java.io.File; import java.io.IOException; @@ -91,10 +93,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -303,6 +307,95 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.log(Level.INFO, "Spout integration enabled."); } */ + + this.setupMetrics(); + } + + /** + * Plotter for Environment-Values. + */ + private static final class EnvironmentPlotter extends Metrics.Plotter { + private MultiverseCore core; + private final Environment env; + + public EnvironmentPlotter(MultiverseCore core, Environment env) { + super(envToString(env)); + this.core = core; + this.env = env; + } + + private static String envToString(Environment env) { + return new StringBuilder().append(env.name().toUpperCase().charAt(0)) + .append(env.name().toLowerCase().substring(1)).toString(); + } + + @Override + public int getValue() { + int count = 0; + for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) + if (w.getEnvironment() == env) + count++; + System.out.println(String.format("Tracking %d worlds of type %s", count, env)); + return count; + } + } + + /** + * Plotter for Generator-Values. + */ + private static final class GeneratorPlotter extends Metrics.Plotter { + private MultiverseCore core; + private final String gen; + + public GeneratorPlotter(MultiverseCore core, String gen) { + super(gen); + this.core = core; + this.gen = gen; + } + + @Override + public int getValue() { + int count = 0; + for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) + if (w.getGenerator().equals(gen)) + count++; + System.out.println(String.format("Tracking %d worlds of type %s", count, gen)); + return count; + } + } + + private void setupMetrics() { + try { + Metrics m = new Metrics(this); + + + Metrics.Graph envGraph = m.createGraph("worlds_by_env"); + for (Environment env : Environment.values()) + envGraph.addPlotter(new EnvironmentPlotter(this, env)); + + + m.addCustomData(new Metrics.Plotter("world_count") { + @Override + public int getValue() { + return getMVWorldManager().getMVWorlds().size(); + } + }); + + Set gens = new HashSet(); + for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) + gens.add(w.getGenerator()); + gens.remove(null); + gens.remove("null"); + Metrics.Graph genGraph = m.createGraph("custom_gens"); + for (String gen : gens) + genGraph.addPlotter(new GeneratorPlotter(this, gen)); + + m.start(); + System.out.println("Metrics have run!"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } private void initializeDestinationFactory() { diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java index 552ee7ed..9d1a7995 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -30,6 +30,7 @@ import org.bukkit.scheduler.BukkitScheduler; import org.mockito.Matchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.powermock.api.easymock.PowerMock; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.MockGateway; @@ -60,6 +61,12 @@ public class TestInstanceCreator { MockGateway.MOCK_STANDARD_METHODS = false; core = PowerMockito.spy(new MultiverseCore()); + PowerMockito.doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + return null; // don't run metrics in tests + } + }).when(core, "setupMetrics"); // Let's let all MV files go to bin/test doReturn(pluginDirectory).when(core).getDataFolder();