1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.FetchData; 28 import org.jrobin.core.RrdException; 29 import org.jrobin.core.Util; 30 31 36 class ValueExtractor 37 { 38 private String [] varNames; 43 private int reduceFactor = 1; 44 45 private int[] tPos; 46 private long[] steps; 47 private long[][] timestamps; 48 49 private double[][][] dsValues; 50 51 52 60 ValueExtractor( String [] names, FetchData[] values, int reduceFactor ) 61 { 62 this.varNames = names; 63 this.reduceFactor = reduceFactor; 64 65 tPos = new int[values.length]; 67 steps = new long[values.length]; 68 timestamps = new long[values.length][]; 69 70 dsValues = new double[values.length][][]; 71 72 for (int i = 0; i < timestamps.length; i++) 73 { 74 if ( values[i] != null ) 75 { 76 timestamps[i] = values[i].getTimestamps(); 77 dsValues[i] = values[i].getValues(); 78 79 if ( timestamps[i].length >= 2 ) 80 steps[i] = (timestamps[i][1] - timestamps[i][0]); 81 } 82 } 83 } 84 85 86 int prepareSources( Source[] sources, int offset ) 90 { 91 int tblPos = offset; 92 93 for ( int i = 0; i < dsValues.length; i++ ) 94 { 95 if ( dsValues[i] != null ) 96 { 97 for (int x = 0; x < dsValues[i].length; x++) 98 { 99 sources[tblPos++].setFetchedStep( steps[i] ); 100 } 101 } 102 } 103 104 return tblPos; 105 } 106 107 117 int extract( long timestamp, Source[] sources, int row, int offset ) throws RrdException 118 { 119 int tblPos = offset; 120 121 for ( int i = 0; i < dsValues.length; i++ ) 122 { 123 if ( dsValues[i] == null ) 124 continue; 125 126 int tIndex = tPos[i]; 127 128 double[] nValue = new double[ dsValues[i].length ]; 129 int[] vValue = new int[ nValue.length ]; 131 for ( int j = 0; j < nValue.length; j++ ) 132 nValue[j] = Double.NaN; 133 134 int j; 136 137 for ( j = 0; j < reduceFactor && timestamps[i][tIndex] <= timestamp; j++ ) 138 { 139 for (int x = 0; x < dsValues[i].length; x++) 140 { 141 if ( Double.isNaN(dsValues[i][x][tIndex]) ) 142 continue; 143 144 vValue[x]++; 145 146 if ( Double.isNaN(nValue[x]) ) 147 nValue[x] = dsValues[i][x][tIndex]; 148 else 149 { 150 switch ( i ) 151 { 152 case FetchSource.AVG: 153 nValue[x] += dsValues[i][x][tIndex]; 154 break; 155 case FetchSource.MAX: 156 nValue[x] = Util.max( nValue[x], dsValues[i][x][tIndex] ); 157 break; 158 case FetchSource.MIN: 159 nValue[x] = Util.min( nValue[x], dsValues[i][x][tIndex] ); 160 break; 161 case FetchSource.LAST: 162 nValue[x] = dsValues[i][x][tIndex]; 163 break; 164 } 165 } 166 } 167 168 tIndex++; 169 } 170 171 if ( j == 0 && row > 0 ) 173 { 174 sources[tblPos++].set( row, timestamp, Double.POSITIVE_INFINITY ); 175 } 176 else 177 { 178 for (int x = 0; x < dsValues[i].length; x++) 180 { 181 if ( i == FetchSource.AVG ) 182 nValue[x] /= vValue[x]; 183 184 sources[tblPos++].set( row, timestamp, nValue[x] ); 185 } 186 } 187 188 tPos[i] = tIndex; 189 } 190 191 return tblPos; 192 } 193 194 String [] getNames() { 195 return varNames; 196 } 197 } 198 | Popular Tags |