1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.Util; 28 29 34 class Source 35 { 36 protected static final int AGG_MINIMUM = 0; 40 protected static final int AGG_MAXIMUM = 1; 41 protected static final int AGG_AVERAGE = 2; 42 protected static final int AGG_FIRST = 3; 43 protected static final int AGG_LAST = 4; 44 protected static final int AGG_TOTAL = 5; 45 46 protected static final String [] aggregates = { "MINIMUM", "MAXIMUM", "AVERAGE", "FIRST", "LAST", "TOTAL" }; 47 private String name; 48 49 protected int aggregatePoints; 50 protected double[] values; 51 52 private double min = Double.NaN; 53 private double max = Double.NaN; 54 private double lastValue = Double.NaN; 55 private double totalValue = 0; 56 private double nextValue = Double.POSITIVE_INFINITY; 57 58 protected long step = 0; 59 private long lastPreciseTime = 0; private long lastTime = 0; 61 private long totalTime = 0; 62 63 private int stPos = 0; 64 private int lastStPos = 0; 66 73 Source( String name ) 74 { 75 this.name = name; 76 } 77 78 79 89 void set( int pos, long time, double val ) 90 { 91 if ( pos > 0 && pos < aggregatePoints ) 94 aggregate( time, val ); 95 } 96 97 void setFetchedStep( long step ) { 98 this.step = step; 99 } 100 101 long getStep() { 102 return step; 103 } 104 105 111 double get( int pos ) 112 { 113 if ( pos < 0 ) 114 return Double.NaN; 115 if ( pos > values.length ) 116 return Double.NaN; 117 118 double val = values[pos]; 119 120 if ( Double.isInfinite(val) ) 121 { 122 if ( !Double.isInfinite(nextValue) && pos >= lastStPos ) 124 return nextValue; 125 126 lastStPos = pos; 127 128 for ( int i = pos + 1; i < values.length; i++ ) 130 { 131 if ( !Double.isInfinite(values[i]) ) 132 { 133 nextValue = values[i]; 134 135 return nextValue; 136 } 137 } 138 139 nextValue = Double.NaN; 141 142 return nextValue; 143 } 144 else 145 nextValue = Double.POSITIVE_INFINITY; 146 147 lastStPos = pos; 148 149 return values[pos]; 150 } 151 152 double get( long preciseTime, long[] reducedTimestamps ) 153 { 154 long t = Util.normalize( preciseTime, step ); 155 t = ( t < preciseTime ? t + step : t ); 156 157 if ( preciseTime < lastPreciseTime ) stPos = 0; 159 160 lastPreciseTime = preciseTime; 161 162 while ( stPos < reducedTimestamps.length - 1 ) 163 { 164 if ( reducedTimestamps[ stPos + 1 ] <= t ) 165 stPos++; 166 else 167 return get( stPos ); 168 } 169 170 if ( t <= reducedTimestamps[stPos] ) 171 return get( stPos ); 172 173 return Double.NaN; 174 } 175 176 184 double getAggregate( int aggType ) 185 { 186 switch ( aggType ) 187 { 188 case AGG_MINIMUM: 189 return min; 190 191 case AGG_MAXIMUM: 192 return max; 193 194 case AGG_AVERAGE: 195 if ( totalTime > 0 ) 196 return totalValue / totalTime; 197 break; 198 199 case AGG_FIRST: 200 if ( values != null && values.length > 0) 201 return values[0]; 202 break; 203 204 case AGG_LAST: 205 if ( values != null && values.length > 0) 206 return values[values.length - 1]; 207 break; 208 209 case AGG_TOTAL: 210 return totalValue; 211 } 212 213 return Double.NaN; 214 } 215 216 String getName() { 217 return name; 218 } 219 220 double[] getValues() { 221 return values; 222 } 223 224 long getSampleCount() { 225 return ( values != null ? values.length : 0 ); 226 } 227 228 229 237 private void aggregate( long time, double value ) 238 { 239 if ( Double.isInfinite(value) ) 240 return; 241 242 min = Util.min( min, value ); 243 max = Util.max( max, value ); 244 245 if ( !Double.isNaN(lastValue) && !Double.isNaN(value) ) 246 { 247 long timeDelta = time - lastTime; 248 249 totalValue += timeDelta * ( value + lastValue ) / 2.0; 250 totalTime += timeDelta; 251 } 252 253 lastTime = time; 254 lastValue = value; 255 } 256 } 257 | Popular Tags |