File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\TimeSeries.java.html

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