mirror of https://github.com/MilkBowl/Vault.git
Update Plugin Metrics to R7
This commit is contained in:
parent
95162907cf
commit
8dd7c475f9
|
@ -29,143 +29,129 @@ package net.milkbowl.vault;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
import net.milkbowl.vault.chat.Chat;
|
import net.milkbowl.vault.chat.Chat;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
/**
|
|
||||||
* Tooling to post to metrics.griefcraft.com
|
|
||||||
*/
|
|
||||||
public class Metrics {
|
public class Metrics {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The metrics revision number
|
* The current revision number
|
||||||
*/
|
*/
|
||||||
private final static int REVISION = 5;
|
private final static int REVISION = 7;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base url of the metrics domain
|
* The base url of the metrics domain
|
||||||
*/
|
*/
|
||||||
private static final String BASE_URL = "http://metrics.griefcraft.com";
|
private static final String BASE_URL = "http://report.mcstats.org";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The url used to report a server's status
|
* The url used to report a server's status
|
||||||
*/
|
*/
|
||||||
private static final String REPORT_URL = "/report/%s";
|
private static final String REPORT_URL = "/plugin/%s";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The separator to use for custom data. This MUST NOT change unless you are hosting your own
|
* Interval of time to ping (in minutes)
|
||||||
* version of metrics and want to change it.
|
|
||||||
*/
|
*/
|
||||||
private static final String CUSTOM_DATA_SEPARATOR = "~~";
|
private static final int PING_INTERVAL = 15;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The file where guid and opt out is stored in
|
* The plugin this metrics submits for
|
||||||
*/
|
*/
|
||||||
private static final String CONFIG_FILE = "plugins/PluginMetrics/config.yml";
|
private final Plugin plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interval of time to ping in minutes
|
* All of the custom graphs to submit to metrics
|
||||||
*/
|
*/
|
||||||
private final static int PING_INTERVAL = 10;
|
private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>());
|
||||||
|
|
||||||
/**
|
|
||||||
* A map of all of the graphs for each plugin
|
|
||||||
*/
|
|
||||||
private Map<Plugin, Set<Graph>> graphs = Collections.synchronizedMap(new HashMap<Plugin, Set<Graph>>());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A convenient map of the default Graph objects (used by addCustomData mainly)
|
|
||||||
*/
|
|
||||||
private Map<Plugin, Graph> defaultGraphs = Collections.synchronizedMap(new HashMap<Plugin, Graph>());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plugin configuration file
|
* The plugin configuration file
|
||||||
*/
|
*/
|
||||||
private final YamlConfiguration configuration;
|
private final YamlConfiguration configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The plugin configuration file
|
||||||
|
*/
|
||||||
|
private final File configurationFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unique server id
|
* Unique server id
|
||||||
*/
|
*/
|
||||||
private String guid;
|
private final String guid;
|
||||||
|
|
||||||
private final String pluginVersion;
|
/**
|
||||||
private final String authors;
|
* Debug mode
|
||||||
|
*/
|
||||||
|
private final boolean debug;
|
||||||
|
|
||||||
public Metrics(String version, String authors) throws IOException {
|
/**
|
||||||
this.pluginVersion = version;
|
* Lock for synchronization
|
||||||
this.authors = authors;
|
*/
|
||||||
|
private final Object optOutLock = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scheduled task
|
||||||
|
*/
|
||||||
|
private volatile BukkitTask task = null;
|
||||||
|
|
||||||
|
public Metrics(Plugin plugin) throws IOException {
|
||||||
|
if (plugin == null) {
|
||||||
|
throw new IllegalArgumentException("Plugin cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.plugin = plugin;
|
||||||
|
|
||||||
// load the config
|
// load the config
|
||||||
File file = new File(CONFIG_FILE);
|
configurationFile = getConfigFile();
|
||||||
configuration = YamlConfiguration.loadConfiguration(file);
|
configuration = YamlConfiguration.loadConfiguration(configurationFile);
|
||||||
|
|
||||||
// add some defaults
|
// add some defaults
|
||||||
configuration.addDefault("opt-out", false);
|
configuration.addDefault("opt-out", false);
|
||||||
configuration.addDefault("guid", UUID.randomUUID().toString());
|
configuration.addDefault("guid", UUID.randomUUID().toString());
|
||||||
|
configuration.addDefault("debug", false);
|
||||||
|
|
||||||
// Do we need to create the file?
|
// Do we need to create the file?
|
||||||
if (configuration.get("guid", null) == null) {
|
if (configuration.get("guid", null) == null) {
|
||||||
configuration.options().header("http://metrics.griefcraft.com").copyDefaults(true);
|
configuration.options().header("http://mcstats.org").copyDefaults(true);
|
||||||
configuration.save(file);
|
configuration.save(configurationFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the guid then
|
// Load the guid then
|
||||||
guid = configuration.getString("guid");
|
guid = configuration.getString("guid");
|
||||||
}
|
debug = configuration.getBoolean("debug", false);
|
||||||
/**
|
|
||||||
* 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 plugin
|
|
||||||
* @param type
|
|
||||||
* @param name
|
|
||||||
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
|
|
||||||
*/
|
|
||||||
public Graph createGraph(Plugin plugin, Graph.Type type, String name) {
|
|
||||||
if (plugin == null || type == null || name == null) {
|
|
||||||
throw new IllegalArgumentException("All arguments must not be null");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct the graph object
|
|
||||||
Graph graph = new Graph(type, name);
|
|
||||||
|
|
||||||
// Get the graphs for the plugin
|
|
||||||
Set<Graph> graphs = getOrCreateGraphs(plugin);
|
|
||||||
|
|
||||||
// Now we can add our graph
|
|
||||||
graphs.add(graph);
|
|
||||||
|
|
||||||
// and return back
|
|
||||||
return graph;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findCustomData(Vault plugin) {
|
public void findCustomData() {
|
||||||
// Create our Economy Graph and Add our Economy plotters
|
// Create our Economy Graph and Add our Economy plotters
|
||||||
Graph econGraph = createGraph(plugin, Graph.Type.Pie, "Economy");
|
Graph econGraph = createGraph("Economy");
|
||||||
RegisteredServiceProvider<Economy> rspEcon = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
|
RegisteredServiceProvider<Economy> rspEcon = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
|
||||||
Economy econ = null;
|
Economy econ = null;
|
||||||
if (rspEcon != null) {
|
if (rspEcon != null) {
|
||||||
|
@ -181,7 +167,7 @@ public class Metrics {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create our Permission Graph and Add our permission Plotters
|
// Create our Permission Graph and Add our permission Plotters
|
||||||
Graph permGraph = createGraph(plugin, Graph.Type.Pie, "Permission");
|
Graph permGraph = createGraph("Permission");
|
||||||
final String permName = Bukkit.getServer().getServicesManager().getRegistration(Permission.class).getProvider().getName();
|
final String permName = Bukkit.getServer().getServicesManager().getRegistration(Permission.class).getProvider().getName();
|
||||||
permGraph.addPlotter(new Metrics.Plotter(permName) {
|
permGraph.addPlotter(new Metrics.Plotter(permName) {
|
||||||
|
|
||||||
|
@ -192,7 +178,7 @@ public class Metrics {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create our Chat Graph and Add our chat Plotters
|
// Create our Chat Graph and Add our chat Plotters
|
||||||
Graph chatGraph = createGraph(plugin, Graph.Type.Pie, "Chat");
|
Graph chatGraph = createGraph("Chat");
|
||||||
RegisteredServiceProvider<Chat> rspChat = Bukkit.getServer().getServicesManager().getRegistration(Chat.class);
|
RegisteredServiceProvider<Chat> rspChat = Bukkit.getServer().getServicesManager().getRegistration(Chat.class);
|
||||||
Chat chat = null;
|
Chat chat = null;
|
||||||
if (rspChat != null) {
|
if (rspChat != null) {
|
||||||
|
@ -210,101 +196,277 @@ public class Metrics {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a custom data plotter for a given plugin
|
* 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 plugin
|
* @param name The name of the graph
|
||||||
* @param plotter
|
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
|
||||||
*/
|
*/
|
||||||
public synchronized void addCustomData(Plugin plugin, Plotter plotter) {
|
public Graph createGraph(final String name) {
|
||||||
// The default graph for the plugin
|
if (name == null) {
|
||||||
Graph graph = getOrCreateDefaultGraph(plugin);
|
throw new IllegalArgumentException("Graph name cannot be null");
|
||||||
|
}
|
||||||
|
|
||||||
// Add the plotter to the graph o/
|
// Construct the graph object
|
||||||
graph.addPlotter(plotter);
|
final Graph graph = new Graph(name);
|
||||||
|
|
||||||
// Ensure the default graph is included in the submitted graphs
|
// Now we can add our graph
|
||||||
getOrCreateGraphs(plugin).add(graph);
|
graphs.add(graph);
|
||||||
|
|
||||||
|
// and return back
|
||||||
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Begin measuring a plugin
|
* Add a Graph object to BukkitMetrics that represents data for the plugin that should be sent to the backend
|
||||||
*
|
*
|
||||||
* @param plugin
|
* @param graph The name of the graph
|
||||||
*/
|
*/
|
||||||
public void beginMeasuringPlugin(final Plugin plugin) throws IOException {
|
public void addGraph(final Graph graph) {
|
||||||
// Did we opt out?
|
if (graph == null) {
|
||||||
if (configuration.getBoolean("opt-out", false)) {
|
throw new IllegalArgumentException("Graph cannot be null");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ping the server in intervals
|
graphs.add(graph);
|
||||||
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
|
}
|
||||||
private boolean firstPost = true;
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
postPlugin(plugin, !firstPost);
|
|
||||||
firstPost = false;
|
|
||||||
} catch (IOException e) {
|
|
||||||
//Ignore exceptions - otherwise we freak server admins out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 0, PING_INTERVAL * 1200);
|
|
||||||
|
|
||||||
return;
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public boolean start() {
|
||||||
|
synchronized (optOutLock) {
|
||||||
|
// Did we opt out?
|
||||||
|
if (isOptOut()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is metrics already running?
|
||||||
|
if (task != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Begin hitting the server with glorious data
|
||||||
|
task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
|
||||||
|
|
||||||
|
private boolean firstPost = true;
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
// This has to be synchronized or it can collide with the disable method.
|
||||||
|
synchronized (optOutLock) {
|
||||||
|
// Disable Task, if it is running and the server owner decided to opt-out
|
||||||
|
if (isOptOut() && task != null) {
|
||||||
|
task.cancel();
|
||||||
|
task = null;
|
||||||
|
// Tell all plotters to stop gathering information.
|
||||||
|
for (Graph graph : graphs) {
|
||||||
|
graph.onOptOut();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We use the inverse of firstPost because if it is the first time we are posting,
|
||||||
|
// it is not a interval ping, so it evaluates to FALSE
|
||||||
|
// Each time thereafter it will evaluate to TRUE, i.e PING!
|
||||||
|
postPlugin(!firstPost);
|
||||||
|
|
||||||
|
// After the first post we set firstPost to false
|
||||||
|
// Each post thereafter will be a ping
|
||||||
|
firstPost = false;
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (debug) {
|
||||||
|
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 0, PING_INTERVAL * 1200);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has the server owner denied plugin metrics?
|
||||||
|
*
|
||||||
|
* @return true if metrics should be opted out of it
|
||||||
|
*/
|
||||||
|
public boolean isOptOut() {
|
||||||
|
synchronized (optOutLock) {
|
||||||
|
try {
|
||||||
|
// Reload the metrics file
|
||||||
|
configuration.load(getConfigFile());
|
||||||
|
} catch (IOException ex) {
|
||||||
|
if (debug) {
|
||||||
|
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (InvalidConfigurationException ex) {
|
||||||
|
if (debug) {
|
||||||
|
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return configuration.getBoolean("opt-out", false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
|
public void enable() throws IOException {
|
||||||
|
// This has to be synchronized or it can collide with the check in the task.
|
||||||
|
synchronized (optOutLock) {
|
||||||
|
// Check if the server owner has already set opt-out, if not, set it.
|
||||||
|
if (isOptOut()) {
|
||||||
|
configuration.set("opt-out", false);
|
||||||
|
configuration.save(configurationFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable Task, if it is not running
|
||||||
|
if (task == null) {
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task.
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
|
public void disable() throws IOException {
|
||||||
|
// This has to be synchronized or it can collide with the check in the task.
|
||||||
|
synchronized (optOutLock) {
|
||||||
|
// Check if the server owner has already set opt-out, if not, set it.
|
||||||
|
if (!isOptOut()) {
|
||||||
|
configuration.set("opt-out", true);
|
||||||
|
configuration.save(configurationFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable Task, if it is running
|
||||||
|
if (task != null) {
|
||||||
|
task.cancel();
|
||||||
|
task = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the File object of the config file that should be used to store data such as the GUID and opt-out status
|
||||||
|
*
|
||||||
|
* @return the File object for the config file
|
||||||
|
*/
|
||||||
|
public File getConfigFile() {
|
||||||
|
// I believe the easiest way to get the base folder (e.g craftbukkit set via -P) for plugins to use
|
||||||
|
// is to abuse the plugin object we already have
|
||||||
|
// plugin.getDataFolder() => base/plugins/PluginA/
|
||||||
|
// pluginsFolder => base/plugins/
|
||||||
|
// The base is not necessarily relative to the startup directory.
|
||||||
|
File pluginsFolder = plugin.getDataFolder().getParentFile();
|
||||||
|
|
||||||
|
// return => base/plugins/PluginMetrics/config.yml
|
||||||
|
return new File(new File(pluginsFolder, "PluginMetrics"), "config.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic method that posts a plugin to the metrics website
|
* Generic method that posts a plugin to the metrics website
|
||||||
*
|
|
||||||
* @param plugin
|
|
||||||
*/
|
*/
|
||||||
private void postPlugin(Plugin plugin, boolean isPing) throws IOException {
|
private void postPlugin(final boolean isPing) throws IOException {
|
||||||
|
// Server software specific section
|
||||||
|
PluginDescriptionFile description = plugin.getDescription();
|
||||||
|
String pluginName = description.getName();
|
||||||
|
boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled
|
||||||
|
String pluginVersion = description.getVersion();
|
||||||
|
String serverVersion = Bukkit.getVersion();
|
||||||
|
int playersOnline = Bukkit.getServer().getOnlinePlayers().length;
|
||||||
|
|
||||||
|
// END server software specific section -- all code below does not use any code outside of this class / Java
|
||||||
|
|
||||||
// Construct the post data
|
// Construct the post data
|
||||||
String data = encode("guid") + '=' + encode(guid)
|
StringBuilder json = new StringBuilder(1024);
|
||||||
+ encodeDataPair("authors", authors)
|
json.append('{');
|
||||||
+ encodeDataPair("version", pluginVersion)
|
|
||||||
+ encodeDataPair("server", Bukkit.getVersion())
|
// The plugin's description file containg all of the plugin data such as name, version, author, etc
|
||||||
+ encodeDataPair("players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length))
|
appendJSONPair(json, "guid", guid);
|
||||||
+ encodeDataPair("revision", String.valueOf(REVISION));
|
appendJSONPair(json, "plugin_version", pluginVersion);
|
||||||
|
appendJSONPair(json, "server_version", serverVersion);
|
||||||
|
appendJSONPair(json, "players_online", Integer.toString(playersOnline));
|
||||||
|
|
||||||
|
// New data as of R6
|
||||||
|
String osname = System.getProperty("os.name");
|
||||||
|
String osarch = System.getProperty("os.arch");
|
||||||
|
String osversion = System.getProperty("os.version");
|
||||||
|
String java_version = System.getProperty("java.version");
|
||||||
|
int coreCount = Runtime.getRuntime().availableProcessors();
|
||||||
|
|
||||||
|
// normalize os arch .. amd64 -> x86_64
|
||||||
|
if (osarch.equals("amd64")) {
|
||||||
|
osarch = "x86_64";
|
||||||
|
}
|
||||||
|
|
||||||
|
appendJSONPair(json, "osname", osname);
|
||||||
|
appendJSONPair(json, "osarch", osarch);
|
||||||
|
appendJSONPair(json, "osversion", osversion);
|
||||||
|
appendJSONPair(json, "cores", Integer.toString(coreCount));
|
||||||
|
appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0");
|
||||||
|
appendJSONPair(json, "java_version", java_version);
|
||||||
|
|
||||||
// If we're pinging, append it
|
// If we're pinging, append it
|
||||||
if (isPing) {
|
if (isPing) {
|
||||||
data += encodeDataPair("ping", "true");
|
appendJSONPair(json, "ping", "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add any custom data available for the plugin
|
if (graphs.size() > 0) {
|
||||||
Set<Graph> graphs = getOrCreateGraphs(plugin);
|
synchronized (graphs) {
|
||||||
|
json.append(',');
|
||||||
|
json.append('"');
|
||||||
|
json.append("graphs");
|
||||||
|
json.append('"');
|
||||||
|
json.append(':');
|
||||||
|
json.append('{');
|
||||||
|
|
||||||
// Acquire a lock on the graphs, which lets us make the assumption we also lock everything
|
boolean firstGraph = true;
|
||||||
// inside of the graph (e.g plotters)
|
|
||||||
synchronized(graphs) {
|
|
||||||
Iterator<Graph> iter = graphs.iterator();
|
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
final Iterator<Graph> iter = graphs.iterator();
|
||||||
Graph graph = iter.next();
|
|
||||||
|
|
||||||
// Because we have a lock on the graphs set already, it is reasonable to assume
|
while (iter.hasNext()) {
|
||||||
// that our lock transcends down to the individual plotters in the graphs also.
|
Graph graph = iter.next();
|
||||||
// Because our methods are private, no one but us can reasonably access this list
|
|
||||||
// without reflection so this is a safe assumption without adding more code.
|
|
||||||
for (Plotter plotter : graph.getPlotters()) {
|
|
||||||
// The key name to send to the metrics server
|
|
||||||
// The format is C-GRAPHNAME-PLOTTERNAME where separator - is defined at the top
|
|
||||||
// Legacy (R4) submitters use the format Custom%s, or CustomPLOTTERNAME
|
|
||||||
String key = String.format("C%s%s%s%s", CUSTOM_DATA_SEPARATOR, graph.getName(), CUSTOM_DATA_SEPARATOR, plotter.getColumnName());
|
|
||||||
|
|
||||||
// The value to send, which for the foreseeable future is just the string
|
StringBuilder graphJson = new StringBuilder();
|
||||||
// value of plotter.getValue()
|
graphJson.append('{');
|
||||||
String value = Integer.toString(plotter.getValue());
|
|
||||||
|
|
||||||
// Add it to the http post data :)
|
for (Plotter plotter : graph.getPlotters()) {
|
||||||
data += encodeDataPair(key, value);
|
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('}');
|
||||||
|
|
||||||
// Create the url
|
// Create the url
|
||||||
URL url = new URL(BASE_URL + String.format(REPORT_URL, plugin.getDescription().getName()));
|
URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName)));
|
||||||
|
|
||||||
// Connect to the website
|
// Connect to the website
|
||||||
URLConnection connection;
|
URLConnection connection;
|
||||||
|
@ -317,31 +479,53 @@ public class Metrics {
|
||||||
connection = url.openConnection();
|
connection = url.openConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
byte[] uncompressed = json.toString().getBytes();
|
||||||
|
byte[] compressed = gzip(json.toString());
|
||||||
|
|
||||||
|
// Headers
|
||||||
|
connection.addRequestProperty("User-Agent", "MCStats/" + REVISION);
|
||||||
|
connection.addRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.addRequestProperty("Content-Encoding", "gzip");
|
||||||
|
connection.addRequestProperty("Content-Length", Integer.toString(compressed.length));
|
||||||
|
connection.addRequestProperty("Accept", "application/json");
|
||||||
|
connection.addRequestProperty("Connection", "close");
|
||||||
|
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
System.out.println("[Metrics] Prepared request for " + pluginName + " uncompressed=" + uncompressed.length + " compressed=" + compressed.length);
|
||||||
|
}
|
||||||
|
|
||||||
// Write the data
|
// Write the data
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
|
OutputStream os = connection.getOutputStream();
|
||||||
writer.write(data);
|
os.write(compressed);
|
||||||
writer.flush();
|
os.flush();
|
||||||
|
|
||||||
// Now read the response
|
// Now read the response
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
String response = reader.readLine();
|
String response = reader.readLine();
|
||||||
|
|
||||||
// close resources
|
// close resources
|
||||||
writer.close();
|
os.close();
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
if (response == null || response.startsWith("ERR")) {
|
if (response == null || response.startsWith("ERR") || response.startsWith("7")) {
|
||||||
throw new IOException(response); //Throw the exception
|
if (response == null) {
|
||||||
|
response = "null";
|
||||||
|
} else if (response.startsWith("7")) {
|
||||||
|
response = response.substring(response.startsWith("7,") ? 2 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IOException(response);
|
||||||
} else {
|
} else {
|
||||||
// Is this the first update this hour?
|
// Is this the first update this hour?
|
||||||
if (response.contains("OK This is your first update this hour")) {
|
if (response.equals("1") || response.contains("This is your first update this hour")) {
|
||||||
synchronized (graphs) {
|
synchronized (graphs) {
|
||||||
Iterator<Graph> iter = graphs.iterator();
|
final Iterator<Graph> iter = graphs.iterator();
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
Graph graph = iter.next();
|
final Graph graph = iter.next();
|
||||||
|
|
||||||
for (Plotter plotter : graph.getPlotters()) {
|
for (Plotter plotter : graph.getPlotters()) {
|
||||||
plotter.reset();
|
plotter.reset();
|
||||||
|
@ -350,49 +534,37 @@ public class Metrics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//if (response.startsWith("OK")) - We should get "OK" followed by an optional description if everything goes right
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get or create the Set of graphs for a specific plugin
|
* GZip compress a string of bytes
|
||||||
*
|
*
|
||||||
* @param plugin
|
* @param input
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Set<Graph> getOrCreateGraphs(Plugin plugin) {
|
public static byte[] gzip(String input) {
|
||||||
Set<Graph> theGraphs = graphs.get(plugin);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
GZIPOutputStream gzos = null;
|
||||||
|
|
||||||
// Create the Set if it does not already exist
|
try {
|
||||||
if (theGraphs == null) {
|
gzos = new GZIPOutputStream(baos);
|
||||||
theGraphs = Collections.synchronizedSet(new HashSet<Graph>());
|
gzos.write(input.getBytes("UTF-8"));
|
||||||
graphs.put(plugin, theGraphs);
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (gzos != null) try {
|
||||||
|
gzos.close();
|
||||||
|
} catch (IOException ignore) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return theGraphs;
|
return baos.toByteArray();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the default graph for a plugin and if it does not exist, create one
|
|
||||||
*
|
|
||||||
* @param plugin
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Graph getOrCreateDefaultGraph(Plugin plugin) {
|
|
||||||
Graph graph = defaultGraphs.get(plugin);
|
|
||||||
|
|
||||||
// Not yet created :(
|
|
||||||
if (graph == null) {
|
|
||||||
graph = new Graph(Graph.Type.Line, "Default");
|
|
||||||
defaultGraphs.put(plugin, graph);
|
|
||||||
}
|
|
||||||
|
|
||||||
return graph;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if mineshafter is present. If it is, we need to bypass it to send POST requests
|
* Check if mineshafter is present. If it is, we need to bypass it to send POST requests
|
||||||
*
|
*
|
||||||
* @return
|
* @return true if mineshafter is installed on the server
|
||||||
*/
|
*/
|
||||||
private boolean isMineshafterPresent() {
|
private boolean isMineshafterPresent() {
|
||||||
try {
|
try {
|
||||||
|
@ -402,73 +574,103 @@ public class Metrics {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode a key/value data pair to be used in a HTTP post request. This INCLUDES a & so the first
|
* Appends a json encoded key/value pair to the given string builder.
|
||||||
* key/value pair MUST be included manually, e.g:
|
|
||||||
* <p>
|
|
||||||
* String httpData = encode("guid") + "=" + encode("1234") + encodeDataPair("authors") + "..";
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
|
* @param json
|
||||||
* @param key
|
* @param key
|
||||||
* @param value
|
* @param value
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
private static void appendJSONPair(StringBuilder json, String key, String value) throws UnsupportedEncodingException {
|
||||||
|
boolean isValueNumeric;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Double.parseDouble(value);
|
||||||
|
isValueNumeric = true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
isValueNumeric = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (json.charAt(json.length() - 1) != '{') {
|
||||||
|
json.append(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
json.append(escapeJSON(key));
|
||||||
|
json.append(':');
|
||||||
|
|
||||||
|
if (isValueNumeric) {
|
||||||
|
json.append(value);
|
||||||
|
} else {
|
||||||
|
json.append(escapeJSON(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape a string to create a valid JSON string
|
||||||
|
*
|
||||||
|
* @param text
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static String encodeDataPair(String key, String value) throws UnsupportedEncodingException {
|
private static String escapeJSON(String text) {
|
||||||
return "&" + encode(key) + "=" + encode(value);
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
builder.append('"');
|
||||||
|
for (int index = 0; index < text.length(); index++) {
|
||||||
|
char chr = text.charAt(index);
|
||||||
|
|
||||||
|
switch (chr) {
|
||||||
|
case '"':
|
||||||
|
case '\\':
|
||||||
|
builder.append('\\');
|
||||||
|
builder.append(chr);
|
||||||
|
break;
|
||||||
|
case '\b':
|
||||||
|
builder.append("\\b");
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
builder.append("\\t");
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
builder.append("\\n");
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
builder.append("\\r");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (chr < ' ') {
|
||||||
|
String t = "000" + Integer.toHexString(chr);
|
||||||
|
builder.append("\\u" + t.substring(t.length() - 4));
|
||||||
|
} else {
|
||||||
|
builder.append(chr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.append('"');
|
||||||
|
|
||||||
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode text as UTF-8
|
* Encode text as UTF-8
|
||||||
*
|
*
|
||||||
* @param text
|
* @param text the text to encode
|
||||||
* @return
|
* @return the encoded text, as UTF-8
|
||||||
*/
|
*/
|
||||||
private static String encode(String text) throws UnsupportedEncodingException {
|
private static String urlEncode(final String text) throws UnsupportedEncodingException {
|
||||||
return URLEncoder.encode(text, "UTF-8");
|
return URLEncoder.encode(text, "UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a custom graph on the website
|
* Represents a custom graph on the website
|
||||||
*/
|
*/
|
||||||
public static class Graph {
|
public static class Graph {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The graph's type that will be visible on the website
|
* The graph's name, alphanumeric and spaces only :) If it does not comply to the above when submitted, it is
|
||||||
*/
|
* rejected
|
||||||
public static enum Type {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple line graph which also includes a scrollable timeline viewer to view
|
|
||||||
* as little or as much of the data as possible.
|
|
||||||
*/
|
|
||||||
Line,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An area graph. This is the same as a line graph except the area under the curve is shaded
|
|
||||||
*/
|
|
||||||
Area,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A column graph, which is a graph where the data is represented by columns on the vertical axis,
|
|
||||||
* i.e they go up and down.
|
|
||||||
*/
|
|
||||||
Column,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A pie graph. The graph is generated by taking the data for the last hour and summing it
|
|
||||||
* together. Then the percentage for each plotter is calculated via round( (plot / total) * 100, 2 )
|
|
||||||
*/
|
|
||||||
Pie
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* What the graph should be plotted as
|
|
||||||
*/
|
|
||||||
private final Type type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
private final String name;
|
||||||
|
|
||||||
|
@ -477,15 +679,14 @@ public class Metrics {
|
||||||
*/
|
*/
|
||||||
private final Set<Plotter> plotters = new LinkedHashSet<Plotter>();
|
private final Set<Plotter> plotters = new LinkedHashSet<Plotter>();
|
||||||
|
|
||||||
private Graph(Type type, String name) {
|
private Graph(final String name) {
|
||||||
this.type = type;
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the graph's name
|
* Gets the graph's name
|
||||||
*
|
*
|
||||||
* @return name of graph
|
* @return the Graph's name
|
||||||
*/
|
*/
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
|
@ -494,24 +695,25 @@ public class Metrics {
|
||||||
/**
|
/**
|
||||||
* Add a plotter to the graph, which will be used to plot entries
|
* Add a plotter to the graph, which will be used to plot entries
|
||||||
*
|
*
|
||||||
* @param plotter
|
* @param plotter the plotter to add to the graph
|
||||||
*/
|
*/
|
||||||
public void addPlotter(Plotter plotter) {
|
public void addPlotter(final Plotter plotter) {
|
||||||
plotters.add(plotter);
|
plotters.add(plotter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a plotter from the graph
|
* Remove a plotter from the graph
|
||||||
*
|
*
|
||||||
* @param plotter
|
* @param plotter the plotter to remove from the graph
|
||||||
*/
|
*/
|
||||||
public void removePlotter(Plotter plotter) {
|
public void removePlotter(final Plotter plotter) {
|
||||||
plotters.remove(plotter);
|
plotters.remove(plotter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an <b>unmodifiable</b> set of the plotter objects in the graph
|
* Gets an <b>unmodifiable</b> set of the plotter objects in the graph
|
||||||
* @return returns a Set of Plotter's
|
*
|
||||||
|
* @return an unmodifiable {@link java.util.Set} of the plotter objects
|
||||||
*/
|
*/
|
||||||
public Set<Plotter> getPlotters() {
|
public Set<Plotter> getPlotters() {
|
||||||
return Collections.unmodifiableSet(plotters);
|
return Collections.unmodifiableSet(plotters);
|
||||||
|
@ -519,30 +721,37 @@ public class Metrics {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return (type.hashCode() * 17) ^ name.hashCode();
|
return name.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object) {
|
public boolean equals(final Object object) {
|
||||||
if (!(object instanceof Graph)) {
|
if (!(object instanceof Graph)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Graph graph = (Graph) object;
|
final Graph graph = (Graph) object;
|
||||||
return graph.type == type && graph.name.equals(name);
|
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
|
* Interface used to collect custom data for a plugin
|
||||||
*/
|
*/
|
||||||
public static abstract class Plotter {
|
public static abstract class Plotter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plot's name
|
* The plot's name
|
||||||
*/
|
*/
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Construct a plotter with the default plot name
|
* Construct a plotter with the default plot name
|
||||||
*/
|
*/
|
||||||
public Plotter() {
|
public Plotter() {
|
||||||
|
@ -552,12 +761,21 @@ public class Metrics {
|
||||||
/**
|
/**
|
||||||
* Construct a plotter with a specific plot name
|
* Construct a plotter with a specific plot name
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name the name of the plotter to use, which will show up on the website
|
||||||
*/
|
*/
|
||||||
public Plotter(String name) {
|
public Plotter(final String name) {
|
||||||
this.name = 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
|
* Get the column name for the plotted point
|
||||||
*
|
*
|
||||||
|
@ -567,18 +785,6 @@ public class Metrics {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current value for the plotted point
|
|
||||||
*
|
|
||||||
* @return returns value
|
|
||||||
*/
|
|
||||||
public abstract int getValue();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return getColumnName().hashCode() + getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called after the website graphs have been updated
|
* Called after the website graphs have been updated
|
||||||
*/
|
*/
|
||||||
|
@ -586,14 +792,18 @@ public class Metrics {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object) {
|
public int hashCode() {
|
||||||
|
return getColumnName().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object object) {
|
||||||
if (!(object instanceof Plotter)) {
|
if (!(object instanceof Plotter)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Plotter plotter = (Plotter) object;
|
final Plotter plotter = (Plotter) object;
|
||||||
return plotter.getColumnName().equals(getColumnName()) && plotter.getValue() == getValue();
|
return plotter.name.equals(name) && plotter.getValue() == getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,16 +148,9 @@ public class Vault extends JavaPlugin {
|
||||||
|
|
||||||
// Load up the Plugin metrics
|
// Load up the Plugin metrics
|
||||||
try {
|
try {
|
||||||
String authors = "";
|
metrics = new Metrics(this);
|
||||||
for (String author : this.getDescription().getAuthors()) {
|
metrics.findCustomData();
|
||||||
authors += author + ", ";
|
metrics.start();
|
||||||
}
|
|
||||||
if (!authors.isEmpty()) {
|
|
||||||
authors = authors.substring(0, authors.length() - 2);
|
|
||||||
}
|
|
||||||
metrics = new Metrics(getDescription().getVersion(), authors);
|
|
||||||
metrics.findCustomData(this);
|
|
||||||
metrics.beginMeasuringPlugin(this);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// ignore exception
|
// ignore exception
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue