You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigStats.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.debug.commands;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.addons.debug.Debug;
  25. import com.dmdirc.addons.debug.DebugCommand;
  26. import com.dmdirc.commandparser.CommandArguments;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.config.ConfigManager;
  29. import java.io.Serializable;
  30. import java.util.Comparator;
  31. import java.util.Map;
  32. import java.util.Map.Entry;
  33. import java.util.SortedSet;
  34. import java.util.TreeSet;
  35. /**
  36. * Outputs usage statistics for config settings.
  37. */
  38. public class ConfigStats extends DebugCommand {
  39. /**
  40. * Creates a new instance of the command.
  41. *
  42. * @param command Parent command
  43. */
  44. public ConfigStats(final Debug command) {
  45. super(command);
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public String getName() {
  50. return "configstats";
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public String getUsage() {
  55. return "[+<X>|<Y>|<regex>] - show config usage stats, optionally "
  56. + "filtering to the top X entries, entries with at least Y "
  57. + "usages, or entries matching the specified regex";
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public void execute(final FrameContainer<?> origin,
  62. final CommandArguments args, final CommandContext context) {
  63. if (args.getArguments().length == 2) {
  64. if (args.getArguments()[1].startsWith("+")) {
  65. int arg;
  66. try {
  67. arg = Integer.parseInt(args.getArguments()[1].substring(1));
  68. } catch (NumberFormatException e) {
  69. arg = Integer.MAX_VALUE;
  70. }
  71. doConfigStatsTop(origin, args.isSilent(), arg);
  72. } else if (args.getArguments()[1].matches("^[0-9]+$")) {
  73. int arg;
  74. try {
  75. arg = Integer.parseInt(args.getArguments()[1]);
  76. } catch (NumberFormatException e) {
  77. arg = -1;
  78. }
  79. doConfigStatsCutOff(origin, args.isSilent(), arg);
  80. } else {
  81. doConfigStatsOption(origin, args.isSilent(),
  82. args.getArguments()[1]);
  83. }
  84. } else {
  85. doConfigStatsCutOff(origin, args.isSilent(), -1);
  86. }
  87. }
  88. /**
  89. * Shows stats related to the config system, showing any options matching
  90. * the regex.
  91. *
  92. * @param origin The window this command was executed in
  93. * @param isSilent Whether this command has been silenced or not
  94. * @param regex Regex to match options against
  95. */
  96. private void doConfigStatsOption(final FrameContainer<?> origin,
  97. final boolean isSilent, final String regex) {
  98. final SortedSet<Entry<String, Integer>> sortedStats = getSortedStats();
  99. boolean found = false;
  100. for (Map.Entry<String, Integer> entry : sortedStats) {
  101. if (entry.getKey().matches(regex)) {
  102. sendLine(origin, isSilent, FORMAT_OUTPUT,
  103. entry.getKey() + " - " + entry.getValue());
  104. found = true;
  105. }
  106. }
  107. if (!found) {
  108. sendLine(origin, isSilent, FORMAT_ERROR,
  109. "Unable to locate option.");
  110. }
  111. }
  112. /**
  113. * Shows stats related to the config system, listing the top X number of
  114. * options.
  115. *
  116. * @param origin The window this command was executed in
  117. * @param isSilent Whether this command has been silenced or not
  118. * @param top Top number of entries to show
  119. */
  120. private void doConfigStatsTop(final FrameContainer<?> origin,
  121. final boolean isSilent, final int top) {
  122. final SortedSet<Entry<String, Integer>> sortedStats = getSortedStats();
  123. int i = 0;
  124. for (Map.Entry<String, Integer> entry : sortedStats) {
  125. if (i == top) {
  126. break;
  127. }
  128. i++;
  129. sendLine(origin, isSilent, FORMAT_OUTPUT,
  130. entry.getKey() + " - " + entry.getValue());
  131. }
  132. }
  133. /**
  134. * Shows stats related to the config system, lists all values with number
  135. * of usages over the specified value.
  136. *
  137. * @param origin The window this command was executed in
  138. * @param isSilent Whether this command has been silenced or not
  139. * @param cutoff Cut off value for stats
  140. */
  141. private void doConfigStatsCutOff(final FrameContainer<?> origin,
  142. final boolean isSilent, final int cutoff) {
  143. final SortedSet<Entry<String, Integer>> sortedStats = getSortedStats();
  144. for (Map.Entry<String, Integer> entry : sortedStats) {
  145. if (entry.getValue() <= cutoff) {
  146. break;
  147. }
  148. sendLine(origin, isSilent, FORMAT_OUTPUT,
  149. entry.getKey() + " - " + entry.getValue());
  150. }
  151. }
  152. /**
  153. * Gets a sorted Set of config options and the number of times they have
  154. * been called.
  155. *
  156. * @return Sorted set of config options and usages
  157. */
  158. private static SortedSet<Entry<String, Integer>> getSortedStats() {
  159. final SortedSet<Entry<String, Integer>> sortedStats =
  160. new TreeSet<Entry<String, Integer>>(new ValueComparator());
  161. sortedStats.addAll(ConfigManager.getStats().entrySet());
  162. return sortedStats;
  163. }
  164. /** Reverse value comparator for a map entry. */
  165. private static class ValueComparator implements
  166. Comparator<Entry<String, Integer>>, Serializable {
  167. /**
  168. * A version number for this class. It should be changed whenever the
  169. * class structure is changed (or anything else that would prevent
  170. * serialized objects being unserialized with the new class).
  171. */
  172. private static final long serialVersionUID = 1;
  173. /** {@inheritDoc} */
  174. @Override
  175. public int compare(final Entry<String, Integer> o1,
  176. final Entry<String, Integer> o2) {
  177. int returnValue = o1.getValue().compareTo(o2.getValue()) * -1;
  178. if (returnValue == 0) {
  179. returnValue = o1.getKey().compareToIgnoreCase(o2.getKey());
  180. }
  181. return returnValue;
  182. }
  183. }
  184. }