PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 24package hudson.model;
 25
 26import hudson.CopyOnWrite;
 27import org.kohsuke.stapler.export.ExportedBean;
 28import org.kohsuke.stapler.export.Exported;
 29
 30/**
 31 * Scalar value that changes over the time (such as load average, Q length, # of executors, etc.)
 32 *
 33 * <p>
 34 * This class computes <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">
 35 * the exponential moving average</a> from the raw data (to be supplied by {@link #update(float)}).
 36 *
 37 * @author Kohsuke Kawaguchi
 38 */
 39@ExportedBean
 40public final class TimeSeries {
 41    /**
 42     * Decay ratio. Normally 1-e for some small e.
 43     */
 44    private final float decay;
 45
 46    /**
 47     * Historical exponential moving average data. Newer ones first.
 48     */
 49    @CopyOnWrite
 50    private volatile float[] history;
 51
 52    /**
 53     * Maximum history size.
 54     */
 55    private final int historySize;
 56
 57    public TimeSeries(float initialValue, float decay, int historySize) {
 58        this.history = new float[]{initialValue};
 59        this.decay = decay;
 60        this.historySize = historySize;
 61    }
 62
 63    /**
 64     * Pushes a new data point.
 65     *
 66     * <p>
 67     * Exponential moving average is calculated, and the {@link #history} is updated.
 68     * This method needs to be called periodically and regularly, and it represents
 69     * the raw data stream.
 70     */
 71    public void update(float newData) {
 72        float data = history[0]*decay + newData*(1-decay);
 73
 74        float[] r = new float[Math.min(history.length+1,historySize)];
 75        System.arraycopy(history,0,r,1,Math.min(history.length,r.length-1));
 76        r[0] = data;
 77        history = r;
 78    }
 79
 80    /**
 81     * Gets the history data of the exponential moving average. The returned array should be treated
 82     * as read-only and immutable.
 83     *
 84     * @return
 85     *      Always non-null, contains at least one entry.
 86     */
 87    @Exported
 88    public float[] getHistory() {
 89        return history;
 90    }
 91
 92    /**
 93     * Gets the most up-to-date data point value. {@code getHistory[0]}.
 94     */
 95    @Exported
 96    public float getLatest() {
 97        return history[0];
 98    }
 99
100    @Override
101    public String toString() {
102        return Float.toString(history[0]);
103    }
104}