From af72986ae85c8b4b7d01338a5b85df1ebee47679 Mon Sep 17 00:00:00 2001 From: NeatMonster Date: Mon, 20 Aug 2012 20:09:23 +0200 Subject: [PATCH] Fixed Metrics graphs. --- .../neatmonster/nocheatplus/NoCheatPlus.java | 22 +-- .../nocheatplus/metrics/MetricsData.java | 129 +++++++++++++++--- .../nocheatplus/utilities/LagMeasureTask.java | 26 +--- 3 files changed, 131 insertions(+), 46 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/NoCheatPlus.java b/src/fr/neatmonster/nocheatplus/NoCheatPlus.java index 79697b8a..b4296844 100644 --- a/src/fr/neatmonster/nocheatplus/NoCheatPlus.java +++ b/src/fr/neatmonster/nocheatplus/NoCheatPlus.java @@ -28,6 +28,7 @@ import fr.neatmonster.nocheatplus.metrics.Metrics; import fr.neatmonster.nocheatplus.metrics.Metrics.Graph; import fr.neatmonster.nocheatplus.metrics.Metrics.Plotter; import fr.neatmonster.nocheatplus.metrics.MetricsData; +import fr.neatmonster.nocheatplus.metrics.MetricsData.TicksPlotter; import fr.neatmonster.nocheatplus.packets.PacketsWorkaround; import fr.neatmonster.nocheatplus.players.Permissions; import fr.neatmonster.nocheatplus.utilities.LagMeasureTask; @@ -119,32 +120,33 @@ public class NoCheatPlus extends JavaPlugin implements Listener { @Override public int getValue() { - return MetricsData.getChecked(type); + final int checked = MetricsData.getChecked(type); + MetricsData.resetChecked(type); + return checked; } }); checksFailed.addPlotter(new Plotter(type.name()) { @Override public int getValue() { - return MetricsData.getFailed(type); + final int failed = MetricsData.getFailed(type); + MetricsData.resetFailed(type); + return failed; } }); violationLevels.addPlotter(new Plotter(type.name()) { @Override public int getValue() { - return (int) MetricsData.getViolationLevel(type); + final int violationLevel = (int) MetricsData.getViolationLevel(type); + MetricsData.resetViolationLevel(type); + return violationLevel; } }); } final Graph serverTicks = metrics.createGraph("Server Ticks"); - serverTicks.addPlotter(new Plotter("" + LagMeasureTask.getAverageTicks()) { - - @Override - public int getValue() { - return 1; - } - }); + for (int ticks = 0; ticks < 21; ticks++) + serverTicks.addPlotter(new TicksPlotter(ticks)); metrics.start(); } catch (final Exception e) {} diff --git a/src/fr/neatmonster/nocheatplus/metrics/MetricsData.java b/src/fr/neatmonster/nocheatplus/metrics/MetricsData.java index cc4db5d4..965c1934 100644 --- a/src/fr/neatmonster/nocheatplus/metrics/MetricsData.java +++ b/src/fr/neatmonster/nocheatplus/metrics/MetricsData.java @@ -5,6 +5,7 @@ import java.util.Map; import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.ViolationData; +import fr.neatmonster.nocheatplus.metrics.Metrics.Plotter; /* * M"""""`'"""`YM dP oo M""""""'YMM dP @@ -20,28 +21,71 @@ import fr.neatmonster.nocheatplus.checks.ViolationData; */ public class MetricsData { + /** + * The ticks plotter. + */ + public static class TicksPlotter extends Plotter { + + /** The ticks. */ + private final int ticks; + + /** + * Instantiates a new ticks plotter. + * + * @param ticks + * the ticks + */ + public TicksPlotter(final int ticks) { + super("" + ticks); + this.ticks = ticks; + } + + /* (non-Javadoc) + * @see fr.neatmonster.nocheatplus.metrics.Metrics.Plotter#getValue() + */ + @Override + public int getValue() { + final int ticks = MetricsData.getServerTicks(this.ticks); + MetricsData.resetServerTicks(this.ticks); + return ticks; + } + } + /** The violation levels. */ private static final Map violationLevels = new HashMap(); - /** The checks fails. */ - private static final Map checksFails = new HashMap(); + /** The checks failed. */ + private static final Map checksFailed = new HashMap(); /** The events checked. */ private static final Map eventsChecked = new HashMap(); + /** The server ticks. */ + private static final Map serverTicks = new HashMap(); + /** - * Called when an event is checked. + * Adds the checked. * * @param type * the type */ public static void addChecked(final CheckType type) { if (type.getParent() != null) - eventsChecked.put(type, getChecked(type)); + eventsChecked.put(type, getChecked(type) + 1); } /** - * Called when a player fails a check. + * Adds the ticks. + * + * @param ticks + * the ticks + */ + public static void addTicks(final int ticks) { + serverTicks.put(ticks, serverTicks.get(ticks) + 1); + } + + /** + * Adds the violation. * * @param violationData * the violation data @@ -49,17 +93,17 @@ public class MetricsData { public static void addViolation(final ViolationData violationData) { final CheckType type = violationData.check.getType(); if (type.getParent() != null) { + checksFailed.put(type, getFailed(type) + 1); violationLevels.put(type, getViolationLevel(type) + violationData.addedVL); - checksFails.put(type, getFailed(type) + 1); } } /** - * Gets the number of event checked. + * Gets the checked. * * @param type * the type - * @return the number of event checked + * @return the checked */ public static int getChecked(final CheckType type) { if (type == CheckType.ALL) { @@ -69,27 +113,40 @@ public class MetricsData { return eventsChecked; } if (!eventsChecked.containsKey(type)) - eventsChecked.put(type, 0); + resetChecked(type); return eventsChecked.get(type); } /** - * Gets the number of failed checks. + * Gets the failed. * * @param type * the type - * @return the number of failed checks + * @return the failed */ public static int getFailed(final CheckType type) { if (type == CheckType.ALL) { int checkFails = 0; - for (final double value : checksFails.values()) + for (final double value : checksFailed.values()) checkFails += value; return checkFails; } - if (!checksFails.containsKey(type)) - checksFails.put(type, 0); - return checksFails.get(type); + if (!checksFailed.containsKey(type)) + resetFailed(type); + return checksFailed.get(type); + } + + /** + * Gets the server ticks. + * + * @param ticks + * the ticks + * @return the server ticks + */ + public static int getServerTicks(final int ticks) { + if (!serverTicks.containsKey(ticks)) + resetServerTicks(ticks); + return serverTicks.get(ticks); } /** @@ -107,7 +164,47 @@ public class MetricsData { return violationLevel; } if (!violationLevels.containsKey(type)) - violationLevels.put(type, 0D); + resetViolationLevel(type); return violationLevels.get(type); } + + /** + * Reset checked. + * + * @param type + * the type + */ + public static void resetChecked(final CheckType type) { + eventsChecked.put(type, 0); + } + + /** + * Reset failed. + * + * @param type + * the type + */ + public static void resetFailed(final CheckType type) { + checksFailed.put(type, 0); + } + + /** + * Reset server ticks. + * + * @param ticks + * the ticks + */ + public static void resetServerTicks(final int ticks) { + serverTicks.put(ticks, 0); + } + + /** + * Reset violation level. + * + * @param type + * the type + */ + public static void resetViolationLevel(final CheckType type) { + violationLevels.put(type, 0D); + } } diff --git a/src/fr/neatmonster/nocheatplus/utilities/LagMeasureTask.java b/src/fr/neatmonster/nocheatplus/utilities/LagMeasureTask.java index 9ab23826..621e4a61 100644 --- a/src/fr/neatmonster/nocheatplus/utilities/LagMeasureTask.java +++ b/src/fr/neatmonster/nocheatplus/utilities/LagMeasureTask.java @@ -5,6 +5,7 @@ import org.bukkit.Bukkit; import fr.neatmonster.nocheatplus.NoCheatPlus; import fr.neatmonster.nocheatplus.config.ConfPaths; import fr.neatmonster.nocheatplus.config.ConfigManager; +import fr.neatmonster.nocheatplus.metrics.MetricsData; /* * M""MMMMMMMM M"""""`'"""`YM @@ -46,18 +47,6 @@ public class LagMeasureTask implements Runnable { } } - /** - * Gets the average ticks. - * - * @return the average ticks - */ - public static int getAverageTicks() { - final int averageTicks = (int) (instance.totalTicks / instance.numberOfValues); - instance.totalTicks = 0L; - instance.numberOfValues = 0; - return averageTicks; - } - /** * Returns if checking must be skipped (lag). * @@ -87,15 +76,9 @@ public class LagMeasureTask implements Runnable { /** The lag measure task id. */ private int lagMeasureTaskId = -1; - /** The number of values. */ - private int numberOfValues = 0; - /** The skip check. */ private boolean skipCheck = false; - /** The total ticks. */ - private long totalTicks = 0L; - /* (non-Javadoc) * @see java.lang.Runnable#run() */ @@ -106,8 +89,11 @@ public class LagMeasureTask implements Runnable { // If the previous second took to long, skip checks during this second. skipCheck = lastInGameSecondDuration > 2000; - totalTicks += Math.round(20000D / lastInGameSecondDuration); - numberOfValues++; + // Metrics data. + int ticks = (int) Math.round(20000D / lastInGameSecondDuration); + if (ticks > 20) + ticks = 20; + MetricsData.addTicks(ticks); if (ConfigManager.getConfigFile().getBoolean(ConfPaths.LOGGING_DEBUG)) if (oldStatus != skipCheck && skipCheck)