1 9 package org.jrobin.core.jrrd; 10 11 import java.io.IOException ; 12 import java.io.PrintStream ; 13 import java.text.*; 14 import java.util.*; 15 16 22 public class Archive { 23 24 RRDatabase db; 25 long offset; 26 long dataOffset; 27 long size; 28 ConsolidationFunctionType type; 29 int rowCount; 30 int pdpCount; 31 double xff; 32 ArrayList cdpStatusBlocks; 33 int currentRow; 34 35 private double[][] values; 36 37 Archive(RRDatabase db) throws IOException { 38 39 this.db = db; 40 41 RRDFile file = db.rrdFile; 42 43 offset = file.getFilePointer(); 44 type = 45 ConsolidationFunctionType.get(file.readString(Constants.CF_NAM_SIZE)); 46 rowCount = file.readInt(); 47 pdpCount = file.readInt(); 48 49 file.align(); 50 51 xff = file.readDouble(); 52 53 file.align(); 55 file.skipBytes(72); 56 57 size = file.getFilePointer() - offset; 58 } 59 60 65 public ConsolidationFunctionType getType() { 66 return type; 67 } 68 69 void loadCDPStatusBlocks(RRDFile file, int numBlocks) throws IOException { 70 71 cdpStatusBlocks = new ArrayList(); 72 73 for (int i = 0; i < numBlocks; i++) { 74 cdpStatusBlocks.add(new CDPStatusBlock(file)); 75 } 76 } 77 78 84 public CDPStatusBlock getCDPStatusBlock(int index) { 85 return (CDPStatusBlock) cdpStatusBlocks.get(index); 86 } 87 88 94 public Iterator getCDPStatusBlocks() { 95 return cdpStatusBlocks.iterator(); 96 } 97 98 void loadCurrentRow(RRDFile file) throws IOException { 99 currentRow = file.readInt(); 100 } 101 102 void loadData(RRDFile file, int dsCount) throws IOException { 103 104 dataOffset = file.getFilePointer(); 105 106 file.skipBytes(8 * rowCount * dsCount); 108 } 109 110 DataChunk loadData(DataChunk chunk) throws IOException { 111 112 Calendar end = Calendar.getInstance(); 113 Calendar start = (Calendar) end.clone(); 114 115 start.add(Calendar.DATE, -1); 116 117 loadData(chunk, start.getTime().getTime() / 1000, 118 end.getTime().getTime() / 1000); 119 return chunk; 120 } 121 122 void loadData(DataChunk chunk, long startTime, long endTime) 123 throws IOException { 124 125 long pointer; 126 127 if (chunk.start < 0) { 128 pointer = currentRow + 1; 129 } else { 130 pointer = currentRow + chunk.start + 1; 131 } 132 133 db.rrdFile.ras.seek(dataOffset + (pointer * 8)); 134 138 double[][] data = chunk.data; 139 140 143 int row = 0; 144 for (int i = chunk.start; i < rowCount - chunk.end; i++, row++) { 145 if (i < 0) { for (int ii = 0; ii < chunk.dsCount; ii++) { 147 data[row][ii] = Double.NaN; 148 } 149 } else if (i >= rowCount) { for (int ii = 0; ii < chunk.dsCount; ii++) { 151 data[row][ii] = Double.NaN; 152 } 153 } else { if (pointer >= rowCount) { 155 pointer -= rowCount; 156 157 db.rrdFile.ras.seek(dataOffset + (pointer * 8)); 158 } 159 160 for (int ii = 0; ii < chunk.dsCount; ii++) { 161 data[row][ii] = db.rrdFile.readDouble(); 162 } 163 164 pointer++; 165 } 166 } 167 } 168 169 void printInfo(PrintStream s, NumberFormat numberFormat, int index) { 170 171 StringBuffer sb = new StringBuffer ("rra["); 172 173 sb.append(index); 174 s.print(sb); 175 s.print("].cf = \""); 176 s.print(type); 177 s.println("\""); 178 s.print(sb); 179 s.print("].rows = "); 180 s.println(rowCount); 181 s.print(sb); 182 s.print("].pdp_per_row = "); 183 s.println(pdpCount); 184 s.print(sb); 185 s.print("].xff = "); 186 s.println(xff); 187 sb.append("].cdp_prep["); 188 189 int cdpIndex = 0; 190 191 for (Iterator i = cdpStatusBlocks.iterator(); i.hasNext();) { 192 CDPStatusBlock cdp = (CDPStatusBlock) i.next(); 193 194 s.print(sb); 195 s.print(cdpIndex); 196 s.print("].value = "); 197 198 double value = cdp.value; 199 200 s.println(Double.isNaN(value) 201 ? "NaN" 202 : numberFormat.format(value)); 203 s.print(sb); 204 s.print(cdpIndex++); 205 s.print("].unknown_datapoints = "); 206 s.println(cdp.unknownDatapoints); 207 } 208 } 209 210 void toXml(PrintStream s) { 211 212 try { 213 s.println("\t<rra>"); 214 s.print("\t\t<cf> "); 215 s.print(type); 216 s.println(" </cf>"); 217 s.print("\t\t<pdp_per_row> "); 218 s.print(pdpCount); 219 s.print(" </pdp_per_row> <!-- "); 220 s.print(db.header.pdpStep * pdpCount); 221 s.println(" seconds -->"); 222 s.print("\t\t<xff> "); 223 s.print(xff); 224 s.println(" </xff>"); 225 s.println(); 226 s.println("\t\t<cdp_prep>"); 227 228 for (int i = 0; i < cdpStatusBlocks.size(); i++) { 229 ((CDPStatusBlock) cdpStatusBlocks.get(i)).toXml(s); 230 } 231 232 s.println("\t\t</cdp_prep>"); 233 s.println("\t\t<database>"); 234 235 long timer = -(rowCount - 1); 236 int counter = 0; 237 int row = currentRow; 238 239 db.rrdFile.ras.seek(dataOffset + (row + 1) * 16); 240 241 long lastUpdate = db.lastUpdate.getTime() / 1000; 242 int pdpStep = db.header.pdpStep; 243 NumberFormat numberFormat = new DecimalFormat("0.0000000000E0"); 244 SimpleDateFormat dateFormat = 245 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 246 247 while (counter++ < rowCount) { 248 row++; 249 250 if (row == rowCount) { 251 row = 0; 252 253 db.rrdFile.ras.seek(dataOffset); 254 } 255 256 long now = (lastUpdate - lastUpdate % (pdpCount * pdpStep)) 257 + (timer * pdpCount * pdpStep); 258 259 timer++; 260 261 s.print("\t\t\t<!-- "); 262 s.print(dateFormat.format(new Date(now * 1000))); 263 s.print(" / "); 264 s.print(now); 265 s.print(" --> "); 266 267 for (int col = 0; col < db.header.dsCount; col++) { 268 s.print("<v> "); 269 270 double value = db.rrdFile.readDouble(); 271 272 if (Double.isNaN(value)) { 274 s.print("NaN"); 275 } else { 276 s.print(numberFormat.format(value)); 277 } 278 279 s.print(" </v>"); 280 } 281 282 s.println("</row>"); 283 } 284 285 s.println("\t\t</database>"); 286 s.println("\t</rra>"); 287 } catch (IOException e) { throw new RuntimeException (e.getMessage()); 289 } 290 } 291 292 public double[][] getValues() throws IOException { 293 if (values != null) { 294 return values; 295 } 296 values = new double[db.header.dsCount][rowCount]; 297 int row = currentRow; 298 db.rrdFile.ras.seek(dataOffset + (row + 1) * 16); 299 for (int counter = 0; counter < rowCount; counter++) { 300 row++; 301 if (row == rowCount) { 302 row = 0; 303 db.rrdFile.ras.seek(dataOffset); 304 } 305 for (int col = 0; col < db.header.dsCount; col++) { 306 double value = db.rrdFile.readDouble(); 307 values[col][counter] = value; 308 } 309 } 310 return values; 311 } 312 313 320 public int getPdpCount() { 321 return pdpCount; 322 } 323 324 329 public int getRowCount() { 330 return rowCount; 331 } 332 333 338 public double getXff() { 339 return xff; 340 } 341 342 347 public String toString() { 348 349 StringBuffer sb = new StringBuffer ("[Archive: OFFSET=0x"); 350 351 sb.append(Long.toHexString(offset)); 352 sb.append(", SIZE=0x"); 353 sb.append(Long.toHexString(size)); 354 sb.append(", type="); 355 sb.append(type); 356 sb.append(", rowCount="); 357 sb.append(rowCount); 358 sb.append(", pdpCount="); 359 sb.append(pdpCount); 360 sb.append(", xff="); 361 sb.append(xff); 362 sb.append(", currentRow="); 363 sb.append(currentRow); 364 sb.append("]"); 365 366 for (Iterator i = cdpStatusBlocks.iterator(); i.hasNext();) { 367 CDPStatusBlock cdp = (CDPStatusBlock) i.next(); 368 369 sb.append("\n\t\t"); 370 sb.append(cdp.toString()); 371 } 372 373 return sb.toString(); 374 } 375 } 376 | Popular Tags |