Skip to content

Commit

Permalink
Remove graphing of loaded chunks/entities/tileentities from Metrics. …
Browse files Browse the repository at this point in the history
…There's no point in keeping this data
  • Loading branch information
LunNova committed Jan 18, 2017
1 parent bf70ac9 commit b12f0df
Showing 1 changed file with 3 additions and 262 deletions.
265 changes: 3 additions & 262 deletions src/main/java/nallar/tickprofiler/reporting/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;
import nallar.tickprofiler.Log;
import net.minecraft.world.World;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.FMLCommonHandler;
Expand Down Expand Up @@ -75,11 +71,6 @@ public class Metrics {
* The url used to report a server's status
*/
private static final String REPORT_URL = "/report/%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)
*/
Expand All @@ -89,11 +80,6 @@ public class Metrics {
*/
private final String modname;
private final String modversion;
/**
* All of the custom graphs to submit to metrics
*/
private final Set<Graph> graphs = Collections
.synchronizedSet(new HashSet<Graph>());
/**
* The metrics configuration file
*/
Expand Down Expand Up @@ -123,14 +109,8 @@ public Metrics(@NonNull final String modname, @NonNull final String modversion)
debug = configuration.get(Configuration.CATEGORY_GENERAL, "debug", false, "Set to true for verbose debug").getBoolean(false);
configuration.save();

if (!isOptOut()) {
Graph graph = createGraph("Perf");
graph.addPlotter(new TileEntityPlotter());
graph.addPlotter(new EntityPlotter());
graph.addPlotter(new ChunkPlotter());
if (start()) {
Log.info("Started TickProfiler mcstats.org metrics reporting. This can be disabled in PluginMetrics.cfg");
}
if (!isOptOut() && start()) {
Log.info("Started TickProfiler mcstats.org metrics reporting. This can be disabled in PluginMetrics.cfg");
}
}

Expand All @@ -144,21 +124,6 @@ private static File getConfigFile() {
return new File(Loader.instance().getConfigDir(), "PluginMetrics.cfg");
}

/**
* Check if mineshafter is present. If it is, we need to bypass it to send
* POST requests
*
* @return true if mineshafter is installed on the server
*/
private static boolean isMineshafterPresent() {
try {
Class.forName("mineshafter.MineServer");
return true;
} catch (Exception e) {
return false;
}
}

/**
* <p>
* Encode a key/value data pair to be used in a HTTP post request. This
Expand Down Expand Up @@ -189,31 +154,6 @@ private static String encode(final String text) {
return URLEncoder.encode(text, "UTF-8");
}

/**
* 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
*/
private Graph createGraph(@NonNull final String name) {
val graph = new Graph(name);
addGraph(graph);
return graph;
}

/**
* Add a Graph object to Metrics that represents data for the plugin that
* should be sent to the backend
*
* @param graph The name of the graph
*/
private void addGraph(@NonNull final Graph graph) {
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
Expand Down Expand Up @@ -326,46 +266,11 @@ private void postPlugin(final boolean isPing) {
encodeDataPair(data, "ping", "true");
}

// Acquire a lock on the graphs, which lets us make the assumption we
// also lock everything
// inside of the graph (e.g plotters)
synchronized (graphs) {
for (final Graph graph : graphs) {
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
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
// value of plotter.getValue()
final String value = Integer.toString(plotter.getValue());

// Add it to the http post data :)
encodeDataPair(data, key, value);
}
}
}

// Create the url
URL url = new URL(BASE_URL + String.format(REPORT_URL, encode(modname)));

// Connect to the website
URLConnection connection;

// Mineshafter creates a socks proxy, so we can safely bypass it
// It does not reroute POST requests so we need to go around it
if (isMineshafterPresent()) {
connection = url.openConnection(Proxy.NO_PROXY);
} else {
connection = url.openConnection();
}

URLConnection connection = url.openConnection();
connection.setDoOutput(true);

// Write the data
Expand All @@ -384,168 +289,4 @@ private void postPlugin(final boolean isPing) {
throw new IOException(response); // Throw the exception
}
}

/**
* Represents a custom graph on the website
*/
private 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<>();

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
*/
void addPlotter(final Plotter plotter) {
plotters.add(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
*/
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);
}
}

/**
* Interface used to collect custom data for a plugin
*/
abstract static class Plotter {
/**
* The plot's name
*/
private final String name;

/**
* Construct a plotter with a specific plot name
*
* @param name the name of the plotter to use, which will show up on the
* website
*/
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
*/
String getColumnName() {
return name;
}

@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();
}
}

private static class TileEntityPlotter extends Plotter {
TileEntityPlotter() {
super("TileEntities");
}

@Override
public int getValue() {
int i = 0;
for (World world : DimensionManager.getWorlds()) {
i += world.loadedTileEntityList.size();
}
return i;
}
}

private static class EntityPlotter extends Plotter {
EntityPlotter() {
super("Entities");
}

@Override
public int getValue() {
int i = 0;
for (World world : DimensionManager.getWorlds()) {
i += world.loadedEntityList.size();
}
return i;
}
}

private static class ChunkPlotter extends Plotter {
ChunkPlotter() {
super("Chunks");
}

@Override
public int getValue() {
int i = 0;
for (World world : DimensionManager.getWorlds()) {
val provider = world.getChunkProvider();
if (provider instanceof ChunkProviderServer)
i += ((ChunkProviderServer) provider).getLoadedChunkCount();
}
return i;
}
}
}

0 comments on commit b12f0df

Please sign in to comment.