/hudson-core/src/main/java/hudson/model/TimeSeries.java

http://github.com/hudson/hudson · Java · 104 lines · 35 code · 10 blank · 59 comment · 0 complexity · 60bd1491ca1ec56159e3224017c080a0 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.model;
  25. import hudson.CopyOnWrite;
  26. import org.kohsuke.stapler.export.ExportedBean;
  27. import org.kohsuke.stapler.export.Exported;
  28. /**
  29. * Scalar value that changes over the time (such as load average, Q length, # of executors, etc.)
  30. *
  31. * <p>
  32. * This class computes <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">
  33. * the exponential moving average</a> from the raw data (to be supplied by {@link #update(float)}).
  34. *
  35. * @author Kohsuke Kawaguchi
  36. */
  37. @ExportedBean
  38. public final class TimeSeries {
  39. /**
  40. * Decay ratio. Normally 1-e for some small e.
  41. */
  42. private final float decay;
  43. /**
  44. * Historical exponential moving average data. Newer ones first.
  45. */
  46. @CopyOnWrite
  47. private volatile float[] history;
  48. /**
  49. * Maximum history size.
  50. */
  51. private final int historySize;
  52. public TimeSeries(float initialValue, float decay, int historySize) {
  53. this.history = new float[]{initialValue};
  54. this.decay = decay;
  55. this.historySize = historySize;
  56. }
  57. /**
  58. * Pushes a new data point.
  59. *
  60. * <p>
  61. * Exponential moving average is calculated, and the {@link #history} is updated.
  62. * This method needs to be called periodically and regularly, and it represents
  63. * the raw data stream.
  64. */
  65. public void update(float newData) {
  66. float data = history[0]*decay + newData*(1-decay);
  67. float[] r = new float[Math.min(history.length+1,historySize)];
  68. System.arraycopy(history,0,r,1,Math.min(history.length,r.length-1));
  69. r[0] = data;
  70. history = r;
  71. }
  72. /**
  73. * Gets the history data of the exponential moving average. The returned array should be treated
  74. * as read-only and immutable.
  75. *
  76. * @return
  77. * Always non-null, contains at least one entry.
  78. */
  79. @Exported
  80. public float[] getHistory() {
  81. return history;
  82. }
  83. /**
  84. * Gets the most up-to-date data point value. {@code getHistory[0]}.
  85. */
  86. @Exported
  87. public float getLatest() {
  88. return history[0];
  89. }
  90. @Override
  91. public String toString() {
  92. return Float.toString(history[0]);
  93. }
  94. }