Use metrics-lite, use global PluginMetrics config to control disable

This commit is contained in:
vemacs 2015-07-05 09:26:36 -06:00
parent 7fb263e75c
commit cf9c9f7073
9 changed files with 28 additions and 513 deletions

View file

@ -18,9 +18,7 @@
package com.earth2me.essentials;
import com.earth2me.essentials.commands.*;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.metrics.MetricsListener;
import com.earth2me.essentials.metrics.MetricsStarter;
import com.earth2me.essentials.metrics.MetricsLite;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.signs.SignBlockListener;
@ -91,7 +89,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient UserMap userMap;
private transient ExecuteTimer execTimer;
private transient I18n i18n;
private transient Metrics metrics;
private transient MetricsLite metrics;
private transient EssentialsTimer timer;
private final transient List<String> vanishedPlayers = new ArrayList<>();
private transient Method oldGetOnlinePlayers;
@ -206,12 +204,16 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
Economy.setEss(this);
execTimer.mark("RegHandler");
final MetricsStarter metricsStarter = new MetricsStarter(this);
if (metricsStarter.getStart() != null && metricsStarter.getStart()) {
runTaskLaterAsynchronously(metricsStarter, 1);
} else if (metricsStarter.getStart() != null) {
final MetricsListener metricsListener = new MetricsListener(this, metricsStarter);
pm.registerEvents(metricsListener, this);
if (!metrics.isOptOut()) {
try {
getLogger().info("Starting Metrics. Opt-out using the global PluginMetrics config.");
metrics = new MetricsLite(this);
metrics.start();
} catch (IOException e) {
// Failed to submit the stats :-(
}
} else {
getLogger().info("Metrics disabled per PluginMetrics config.");
}
final String timeroutput = execTimer.end();
@ -493,12 +495,12 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
}
@Override
public Metrics getMetrics() {
public MetricsLite getMetrics() {
return metrics;
}
@Override
public void setMetrics(Metrics metrics) {
public void setMetrics(MetricsLite metrics) {
this.metrics = metrics;
}

View file

@ -368,7 +368,6 @@ public class EssentialsUpgrade {
if (doneFile.getBoolean("warnMetrics", false)) {
return;
}
ess.getSettings().setMetricsEnabled(false);
doneFile.setProperty("warnMetrics", true);
doneFile.save();
}

View file

@ -3,7 +3,7 @@ package com.earth2me.essentials;
import com.earth2me.essentials.api.IItemDb;
import com.earth2me.essentials.api.IJails;
import com.earth2me.essentials.api.IWarps;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.metrics.MetricsLite;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import net.ess3.nms.SpawnerProvider;
@ -86,9 +86,9 @@ public interface IEssentials extends Plugin {
UserMap getUserMap();
Metrics getMetrics();
MetricsLite getMetrics();
void setMetrics(Metrics metrics);
void setMetrics(MetricsLite metrics);
EssentialsTimer getTimer();

View file

@ -182,10 +182,6 @@ public interface ISettings extends IConf {
long getTpaAcceptCancellation();
boolean isMetricsEnabled();
void setMetricsEnabled(boolean metricsEnabled);
long getTeleportInvulnerability();
boolean isTeleportInvulnerability();

View file

@ -940,16 +940,6 @@ public class Settings implements net.ess3.api.ISettings {
return config.getLong("tpa-accept-cancellation", 120);
}
@Override
public boolean isMetricsEnabled() {
return metricsEnabled;
}
@Override
public void setMetricsEnabled(boolean metricsEnabled) {
this.metricsEnabled = metricsEnabled;
}
private long teleportInvulnerabilityTime;
private long _getTeleportInvulnerability() {

View file

@ -4,7 +4,7 @@ import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.EssentialsUpgrade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.UserMap;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.metrics.MetricsLite;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.FloatUtil;
import com.earth2me.essentials.utils.NumberUtil;
@ -176,7 +176,7 @@ public class Commandessentials extends EssentialsCommand {
}
private void run_optout(final Server server, final CommandSource sender, final String command, final String args[]) {
final Metrics metrics = ess.getMetrics();
final MetricsLite metrics = ess.getMetrics();
try {
sender.sendMessage("Essentials collects simple metrics to highlight which features to concentrate work on in the future.");
if (metrics.isOptOut()) {

View file

@ -1,34 +0,0 @@
package com.earth2me.essentials.metrics;
import com.earth2me.essentials.User;
import net.ess3.api.IEssentials;
import org.bukkit.Server;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.logging.Level;
public class MetricsListener implements Listener {
private final transient IEssentials ess;
private final transient MetricsStarter starter;
public MetricsListener(final IEssentials parent, final MetricsStarter starter) {
this.ess = parent;
this.starter = starter;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(final PlayerJoinEvent event) {
final User player = ess.getUser(event.getPlayer());
if (!ess.getSettings().isMetricsEnabled() && (player.isAuthorized("essentials.essentials") || player.isAuthorized("bukkit.broadcast.admin"))) {
player.sendMessage("PluginMetrics collects minimal statistic data, starting in about 5 minutes.");
player.sendMessage("To opt out, disabling metrics for all plugins, run /essentials opt-out");
ess.getLogger().log(Level.INFO, "[Metrics] Admin join - Starting 5 minute opt-out period.");
ess.getSettings().setMetricsEnabled(true);
ess.runTaskLaterAsynchronously(starter, 5 * 1200);
}
}
}

View file

@ -25,6 +25,7 @@
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.earth2me.essentials.metrics;
import org.bukkit.Bukkit;
@ -49,16 +50,11 @@ import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
public class Metrics {
public class MetricsLite {
/**
* The current revision number
@ -78,18 +74,13 @@ public class Metrics {
/**
* Interval of time to ping (in minutes)
*/
private static final int PING_INTERVAL = 15;
private final static int PING_INTERVAL = 15;
/**
* The plugin this metrics submits for
*/
private final Plugin plugin;
/**
* All of the custom graphs to submit to metrics
*/
private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>());
/**
* The plugin configuration file
*/
@ -116,11 +107,11 @@ public class Metrics {
private final Object optOutLock = new Object();
/**
* The scheduled task
* Id of the scheduled task
*/
private volatile BukkitTask task = null;
public Metrics(final Plugin plugin) throws IOException {
public MetricsLite(Plugin plugin) throws IOException {
if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null");
}
@ -148,44 +139,9 @@ public class Metrics {
}
/**
* Construct and create a Graph that can be used to separate specific plotters to their own graphs on the metrics
* website. Plotters can be added to the graph object returned.
*
* @param name The name of the graph
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
*/
public Graph createGraph(final String name) {
if (name == null) {
throw new IllegalArgumentException("Graph name cannot be null");
}
// Construct the graph object
final Graph graph = new Graph(name);
// Now we can add our graph
graphs.add(graph);
// and return back
return graph;
}
/**
* Add a Graph object to BukkitMetrics that represents data for the plugin that should be sent to the backend
*
* @param graph The name of the graph
*/
public void addGraph(final Graph graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph cannot be null");
}
graphs.add(graph);
}
/**
* Start measuring statistics. This will immediately create an async repeating task as the plugin and send the
* initial data to the metrics backend, and then after that it will post in increments of PING_INTERVAL * 1200
* ticks.
* Start measuring statistics. This will immediately create an async repeating task as the plugin and send
* the initial data to the metrics backend, and then after that it will post in increments of
* PING_INTERVAL * 1200 ticks.
*
* @return True if statistics measuring is running, otherwise false.
*/
@ -214,10 +170,6 @@ public class Metrics {
if (isOptOut() && task != null) {
task.cancel();
task = null;
// Tell all plotters to stop gathering information.
for (Graph graph : graphs) {
graph.onOptOut();
}
}
}
@ -351,7 +303,7 @@ public class Metrics {
/**
* Generic method that posts a plugin to the metrics website
*/
private void postPlugin(final boolean isPing) throws IOException {
private void postPlugin(boolean isPing) throws IOException {
// Server software specific section
PluginDescriptionFile description = plugin.getDescription();
String pluginName = "EssentialsX";
@ -396,46 +348,6 @@ public class Metrics {
appendJSONPair(json, "ping", "1");
}
if (graphs.size() > 0) {
synchronized (graphs) {
json.append(',');
json.append('"');
json.append("graphs");
json.append('"');
json.append(':');
json.append('{');
boolean firstGraph = true;
final Iterator<Graph> iter = graphs.iterator();
while (iter.hasNext()) {
Graph graph = iter.next();
StringBuilder graphJson = new StringBuilder();
graphJson.append('{');
for (Plotter plotter : graph.getPlotters()) {
appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue()));
}
graphJson.append('}');
if (!firstGraph) {
json.append(',');
}
json.append(escapeJSON(graph.getName()));
json.append(':');
json.append(graphJson);
firstGraph = false;
}
json.append('}');
}
}
// close json
json.append('}');
@ -492,21 +404,6 @@ public class Metrics {
}
throw new IOException(response);
} else {
// Is this the first update this hour?
if (response.equals("1") || response.contains("This is your first update this hour")) {
synchronized (graphs) {
final Iterator<Graph> iter = graphs.iterator();
while (iter.hasNext()) {
final Graph graph = iter.next();
for (Plotter plotter : graph.getPlotters()) {
plotter.reset();
}
}
}
}
}
}
@ -639,147 +536,4 @@ public class Metrics {
return URLEncoder.encode(text, "UTF-8");
}
/**
* Represents a custom graph on the website
*/
public static class Graph {
/**
* The graph's name, alphanumeric and spaces only :) If it does not comply to the above when submitted, it is
* rejected
*/
private final String name;
/**
* The set of plotters that are contained within this graph
*/
private final Set<Plotter> plotters = new LinkedHashSet<Plotter>();
private Graph(final String name) {
this.name = name;
}
/**
* Gets the graph's name
*
* @return the Graph's name
*/
public String getName() {
return name;
}
/**
* Add a plotter to the graph, which will be used to plot entries
*
* @param plotter the plotter to add to the graph
*/
public void addPlotter(final Plotter plotter) {
plotters.add(plotter);
}
/**
* Remove a plotter from the graph
*
* @param plotter the plotter to remove from the graph
*/
public void removePlotter(final Plotter plotter) {
plotters.remove(plotter);
}
/**
* Gets an <b>unmodifiable</b> set of the plotter objects in the graph
*
* @return an unmodifiable {@link java.util.Set} of the plotter objects
*/
public Set<Plotter> getPlotters() {
return Collections.unmodifiableSet(plotters);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(final Object object) {
if (!(object instanceof Graph)) {
return false;
}
final Graph graph = (Graph) object;
return graph.name.equals(name);
}
/**
* Called when the server owner decides to opt-out of BukkitMetrics while the server is running.
*/
protected void onOptOut() {
}
}
/**
* Interface used to collect custom data for a plugin
*/
public static abstract class Plotter {
/**
* The plot's name
*/
private final String name;
/**
* Construct a plotter with the default plot name
*/
public Plotter() {
this("Default");
}
/**
* Construct a plotter with a specific plot name
*
* @param name the name of the plotter to use, which will show up on the website
*/
public Plotter(final String name) {
this.name = name;
}
/**
* Get the current value for the plotted point. Since this function defers to an external function it may or may
* not return immediately thus cannot be guaranteed to be thread friendly or safe. This function can be called
* from any thread so care should be taken when accessing resources that need to be synchronized.
*
* @return the current value for the point to be plotted.
*/
public abstract int getValue();
/**
* Get the column name for the plotted point
*
* @return the plotted point's column name
*/
public String getColumnName() {
return name;
}
/**
* Called after the website graphs have been updated
*/
public void reset() {
}
@Override
public int hashCode() {
return getColumnName().hashCode();
}
@Override
public boolean equals(final Object object) {
if (!(object instanceof Plotter)) {
return false;
}
final Plotter plotter = (Plotter) object;
return plotter.name.equals(name) && plotter.getValue() == getValue();
}
}
}

View file

@ -1,192 +0,0 @@
package com.earth2me.essentials.metrics;
import com.earth2me.essentials.metrics.Metrics.Graph;
import com.earth2me.essentials.metrics.Metrics.Plotter;
import com.earth2me.essentials.register.payment.Method;
import com.earth2me.essentials.register.payment.methods.VaultEco;
import com.earth2me.essentials.signs.EssentialsSign;
import net.ess3.api.IEssentials;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Locale;
import java.util.logging.Level;
public class MetricsStarter implements Runnable {
private final IEssentials ess;
private transient Boolean start;
private enum Modules {
Essentials,
EssentialsAntiBuild,
EssentialsAntiCheat,
EssentialsChat,
EssentialsSpawn,
EssentialsProtect,
EssentialsGeoIP,
EssentialsXMPP
}
public MetricsStarter(final IEssentials plugin) {
ess = plugin;
try {
final Metrics metrics = new Metrics(ess);
ess.setMetrics(metrics);
if (!metrics.isOptOut()) {
if (ess.getSettings().isMetricsEnabled()) {
start = true;
} else {
ess.getLogger().info("This plugin collects minimal statistic data and sends it to http://metrics.essentials3.net.");
ess.getLogger().info("You can opt out, disabling metrics for all plugins, by running /essentials opt-out");
ess.getLogger().info("This will start 5 minutes after the first admin/op joins.");
start = false;
}
return;
}
} catch (Exception ex) {
metricsError(ex);
}
}
@Override
public void run() {
try {
final Metrics metrics = ess.getMetrics();
final Graph moduleGraph = metrics.createGraph("Modules Used");
for (Modules module : Modules.values()) {
final String moduleName = module.toString();
if (ess.getServer().getPluginManager().isPluginEnabled(moduleName)) {
moduleGraph.addPlotter(new SimplePlotter(moduleName));
}
}
final Graph localeGraph = metrics.createGraph("Locale");
localeGraph.addPlotter(new SimplePlotter(ess.getI18n().getCurrentLocale().getDisplayLanguage(Locale.ENGLISH)));
final Graph featureGraph = metrics.createGraph("Features");
featureGraph.addPlotter(new Plotter("Unique Accounts") {
@Override
public int getValue() {
return ess.getUserMap().getUniqueUsers();
}
});
featureGraph.addPlotter(new Plotter("Jails") {
@Override
public int getValue() {
return ess.getJails().getCount();
}
});
featureGraph.addPlotter(new Plotter("Kits") {
@Override
public int getValue() {
ConfigurationSection kits = ess.getSettings().getKits();
if (kits == null) {
return 0;
}
return kits.getKeys(false).size();
}
});
featureGraph.addPlotter(new Plotter("Warps") {
@Override
public int getValue() {
return ess.getWarps().getCount();
}
});
final Graph enabledGraph = metrics.createGraph("EnabledFeatures");
enabledGraph.addPlotter(new SimplePlotter("Total"));
final String BKcommand = ess.getSettings().getBackupCommand();
if (BKcommand != null && !"".equals(BKcommand)) {
enabledGraph.addPlotter(new SimplePlotter("Backup"));
}
if (ess.getJails().getCount() > 0) {
enabledGraph.addPlotter(new SimplePlotter("Jails"));
}
if (ess.getSettings().getKits() != null && ess.getSettings().getKits().getKeys(false).size() > 0) {
enabledGraph.addPlotter(new SimplePlotter("Kits"));
}
if (ess.getWarps().getCount() > 0) {
enabledGraph.addPlotter(new SimplePlotter("Warps"));
}
if (ess.getSettings().getTeleportCooldown() > 0) {
enabledGraph.addPlotter(new SimplePlotter("TeleportCooldown"));
}
if (ess.getSettings().getTeleportDelay() > 0) {
enabledGraph.addPlotter(new SimplePlotter("TeleportDelay"));
}
if (!ess.getSettings().areSignsDisabled()) {
enabledGraph.addPlotter(new SimplePlotter("Signs"));
}
if (ess.getSettings().getAutoAfk() > 0) {
enabledGraph.addPlotter(new SimplePlotter("AutoAFK"));
}
if (ess.getSettings().changePlayerListName()) {
enabledGraph.addPlotter(new SimplePlotter("PlayerListName"));
}
if (ess.getSettings().getOperatorColor() != null) {
enabledGraph.addPlotter(new SimplePlotter("OpColour"));
}
if (ess.getSettings().changeDisplayName()) {
enabledGraph.addPlotter(new SimplePlotter("DisplayName"));
}
if (ess.getSettings().getChatRadius() >= 1) {
enabledGraph.addPlotter(new SimplePlotter("LocalChat"));
}
final Graph depGraph = metrics.createGraph("Dependencies");
final Method method = ess.getPaymentMethod().getMethod();
if (method != null) {
String version;
if (method instanceof VaultEco) {
version = ((VaultEco) method).getEconomy();
} else {
version = method.getVersion();
final int dashPosition = version.indexOf('-');
if (dashPosition > 0) {
version = version.substring(0, dashPosition);
}
}
depGraph.addPlotter(new SimplePlotter(method.getName() + " " + version));
}
depGraph.addPlotter(new SimplePlotter(ess.getPermissionsHandler().getName()));
final Graph signGraph = metrics.createGraph("Signs");
for (EssentialsSign sign : ess.getSettings().enabledSigns()) {
signGraph.addPlotter(new SimplePlotter(sign.getName()));
}
metrics.start();
} catch (Exception ex) {
metricsError(ex);
}
}
public void metricsError(final Exception ex) {
if (ess.getSettings().isDebug()) {
ess.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage(), ex);
} else {
ess.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
}
}
public Boolean getStart() {
return start;
}
private class SimplePlotter extends Plotter {
public SimplePlotter(final String name) {
super(name);
}
@Override
public int getValue() {
return 1;
}
}
}