Update Plugin Metrics to R7

This commit is contained in:
Tyler Blair 2013-06-29 10:47:05 -03:00 committed by KHobbits
parent 0e43233e46
commit cb052e690c

View file

@ -23,6 +23,7 @@ package com.earth2me.essentials.metrics;
* The views and conclusions contained in the software and documentation are those of the authors and contributors and * The views and conclusions contained in the software and documentation are those of the authors and contributors and
* should not be interpreted as representing official policies, either expressed or implied, of anybody else. * should not be interpreted as representing official policies, either expressed or implied, of anybody else.
*/ */
import java.io.*; import java.io.*;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
@ -30,6 +31,7 @@ import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -37,71 +39,64 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
/**
* <p> The metrics class obtains data about a plugin and submits statistics about it to the metrics backend. </p> <p>
* Public methods provided by this class: </p>
* <code>
* Graph createGraph(String name); <br/>
* void addCustomData(Metrics.Plotter plotter); <br/>
* void start(); <br/>
* </code>
*/
public class Metrics public class Metrics
{ {
/** /**
* The current revision number * The current revision number
*/ */
private final static int REVISION = 6; 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.essentials3.net"; private static final String BASE_URL = "http://report-metrics.essentials3.net";
/** /**
* 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 version of metrics and
* want to change it.
*/
private static final String CUSTOM_DATA_SEPARATOR = "~~";
/** /**
* Interval of time to ping (in minutes) * Interval of time to ping (in minutes)
*/ */
private static final int PING_INTERVAL = 10; private static final int PING_INTERVAL = 15;
/** /**
* The plugin this metrics submits for * The plugin this metrics submits for
*/ */
private final Plugin plugin; private final Plugin plugin;
/** /**
* All of the custom graphs to submit to metrics * All of the custom graphs to submit to metrics
*/ */
private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>()); private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>());
/**
* The default graph, used for addCustomData when you don't want a specific graph
*/
private final Graph defaultGraph = new Graph("Default");
/** /**
* The plugin configuration file * The plugin configuration file
*/ */
private final YamlConfiguration configuration; private final YamlConfiguration configuration;
/** /**
* The plugin configuration file * The plugin configuration file
*/ */
private final File configurationFile; private final File configurationFile;
/** /**
* Unique server id * Unique server id
*/ */
private final String guid; private final String guid;
/** /**
* Debug mode * Debug mode
*/ */
private final boolean debug; private final boolean debug;
/** /**
* Lock for synchronization * Lock for synchronization
*/ */
private final Object optOutLock = new Object(); private final Object optOutLock = new Object();
/** /**
* The scheduled task * The scheduled task
*/ */
@ -162,22 +157,18 @@ public class Metrics
} }
/** /**
* Adds a custom data plotter to the default graph * Add a Graph object to BukkitMetrics that represents data for the plugin that should be sent to the backend
* *
* @param plotter The plotter to use to plot custom data * @param graph The name of the graph
*/ */
public void addCustomData(final Plotter plotter) public void addGraph(final Graph graph)
{ {
if (plotter == null) if (graph == null)
{ {
throw new IllegalArgumentException("Plotter cannot be null"); throw new IllegalArgumentException("Graph cannot be null");
} }
// Add the plotter to the graph o/ graphs.add(graph);
defaultGraph.addPlotter(plotter);
// Ensure the default graph is included in the submitted graphs
graphs.add(defaultGraph);
} }
/** /**
@ -195,6 +186,12 @@ public class Metrics
return; return;
} }
// Is metrics already running?
if (task != null)
{
return;
}
// Begin hitting the server with glorious data // Begin hitting the server with glorious data
task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable()
{ {
@ -278,7 +275,7 @@ public class Metrics
/** /**
* Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.
* *
* @throws IOException * @throws java.io.IOException
*/ */
public void enable() throws IOException public void enable() throws IOException
{ {
@ -303,7 +300,7 @@ public class Metrics
/** /**
* Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task. * Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task.
* *
* @throws IOException * @throws java.io.IOException
*/ */
public void disable() throws IOException public void disable() throws IOException
{ {
@ -360,14 +357,14 @@ public class Metrics
// END server software specific section -- all code below does not use any code outside of this class / Java // 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
final StringBuilder data = new StringBuilder(); StringBuilder json = new StringBuilder(1024);
json.append('{');
// The plugin's description file containg all of the plugin data such as name, version, author, etc // The plugin's description file containg all of the plugin data such as name, version, author, etc
data.append(encode("guid")).append('=').append(encode(guid)); appendJSONPair(json, "guid", guid);
encodeDataPair(data, "version", pluginVersion); appendJSONPair(json, "plugin_version", pluginVersion);
encodeDataPair(data, "server", serverVersion); appendJSONPair(json, "server_version", serverVersion);
encodeDataPair(data, "players", Integer.toString(playersOnline)); appendJSONPair(json, "players_online", Integer.toString(playersOnline));
encodeDataPair(data, "revision", String.valueOf(REVISION));
// New data as of R6 // New data as of R6
String osname = System.getProperty("os.name"); String osname = System.getProperty("os.name");
@ -382,48 +379,69 @@ public class Metrics
osarch = "x86_64"; osarch = "x86_64";
} }
encodeDataPair(data, "osname", osname); appendJSONPair(json, "osname", osname);
encodeDataPair(data, "osarch", osarch); appendJSONPair(json, "osarch", osarch);
encodeDataPair(data, "osversion", osversion); appendJSONPair(json, "osversion", osversion);
encodeDataPair(data, "cores", Integer.toString(coreCount)); appendJSONPair(json, "cores", Integer.toString(coreCount));
encodeDataPair(data, "online-mode", Boolean.toString(onlineMode)); appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0");
encodeDataPair(data, "java_version", java_version); appendJSONPair(json, "java_version", java_version);
// If we're pinging, append it // If we're pinging, append it
if (isPing) if (isPing)
{ {
encodeDataPair(data, "ping", "true"); appendJSONPair(json, "ping", "1");
} }
// Acquire a lock on the graphs, which lets us make the assumption we also lock everything if (graphs.size() > 0)
// inside of the graph (e.g plotters) {
synchronized (graphs) synchronized (graphs)
{ {
json.append(',');
json.append('"');
json.append("graphs");
json.append('"');
json.append(':');
json.append('{');
boolean firstGraph = true;
final Iterator<Graph> iter = graphs.iterator(); final Iterator<Graph> iter = graphs.iterator();
while (iter.hasNext()) while (iter.hasNext())
{ {
final Graph graph = iter.next(); Graph graph = iter.next();
StringBuilder graphJson = new StringBuilder();
graphJson.append('{');
for (Plotter plotter : graph.getPlotters()) for (Plotter plotter : graph.getPlotters())
{ {
// The key name to send to the metrics server appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue()));
// The format is C-GRAPHNAME-PLOTTERNAME where separator - is defined at the top }
// Legacy (R4) submitters use the format Custom%s, or CustomPLOTTERNAME
final 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 graphJson.append('}');
// value of plotter.getValue()
final String value = Integer.toString(plotter.getValue());
// Add it to the http post data :) if (!firstGraph)
encodeDataPair(data, key, value); {
} 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, encode(pluginName))); URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName)));
// Connect to the website // Connect to the website
URLConnection connection; URLConnection connection;
@ -439,29 +457,55 @@ 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
final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); OutputStream os = connection.getOutputStream();
writer.write(data.toString()); os.write(compressed);
writer.flush(); os.flush();
// Now read the response // Now read the response
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final 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)
{ {
@ -481,6 +525,43 @@ public class Metrics
} }
} }
/**
* GZip compress a string of bytes
*
* @param input
* @return
*/
public static byte[] gzip(String input)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try
{
gzos = new GZIPOutputStream(baos);
gzos.write(input.getBytes("UTF-8"));
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (gzos != null)
{
try
{
gzos.close();
}
catch (IOException ignore)
{
}
}
}
return baos.toByteArray();
}
/** /**
* 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
* *
@ -500,21 +581,95 @@ public class Metrics
} }
/** /**
* <p>Encode a key/value data pair to be used in a HTTP post request. This INCLUDES a & so the first key/value pair * Appends a json encoded key/value pair to the given string builder.
* MUST be included manually, e.g:</p>
* <code>
* StringBuffer data = new StringBuffer();
* data.append(encode("guid")).append('=').append(encode(guid));
* encodeDataPair(data, "version", description.getVersion());
* </code>
* *
* @param buffer the stringbuilder to append the data pair onto * @param json
* @param key the key value * @param key
* @param value the value * @param value
* @throws UnsupportedEncodingException
*/ */
private static void encodeDataPair(final StringBuilder buffer, final String key, final String value) throws UnsupportedEncodingException private static void appendJSONPair(StringBuilder json, String key, String value) throws UnsupportedEncodingException
{ {
buffer.append('&').append(encode(key)).append('=').append(encode(value)); 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
*/
private static String escapeJSON(String text)
{
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();
} }
/** /**
@ -523,7 +678,7 @@ public class Metrics
* @param text the text to encode * @param text the text to encode
* @return the encoded text, as UTF-8 * @return the encoded text, as UTF-8
*/ */
private static String encode(final 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");
} }
@ -582,7 +737,7 @@ public class Metrics
/** /**
* 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 an unmodifiable {@link Set} of the plotter objects * @return an unmodifiable {@link java.util.Set} of the plotter objects
*/ */
public Set<Plotter> getPlotters() public Set<Plotter> getPlotters()
{ {
@ -608,7 +763,7 @@ public class Metrics
} }
/** /**
* Called when the server owner decides to opt-out of Metrics while the server is running. * Called when the server owner decides to opt-out of BukkitMetrics while the server is running.
*/ */
protected void onOptOut() protected void onOptOut()
{ {