1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.text.DecimalFormat ; 33 34 56 public class FetchData implements RrdDataSet { 57 private FetchRequest request; 58 private Archive matchingArchive; 59 private String [] dsNames; 60 private long[] timestamps; 61 private double[][] values; 62 63 FetchData(Archive matchingArchive, FetchRequest request) throws IOException { 64 this.matchingArchive = matchingArchive; 65 this.dsNames = request.getFilter(); 66 if(this.dsNames == null) { 67 this.dsNames = matchingArchive.getParentDb().getDsNames(); 68 } 69 this.request = request; 70 } 71 72 void setTimestamps(long[] timestamps) { 73 this.timestamps = timestamps; 74 } 75 76 void setValues(double[][] values) { 77 this.values = values; 78 } 79 80 85 public int getRowCount() { 86 return timestamps.length; 87 } 88 89 95 public int getColumnCount() { 96 return dsNames.length; 97 } 98 99 106 public FetchPoint getRow(int rowIndex) { 107 int numCols = getColumnCount(); 108 FetchPoint point = new FetchPoint(timestamps[rowIndex], getColumnCount()); 109 for(int dsIndex = 0; dsIndex < numCols; dsIndex++) { 110 point.setValue(dsIndex, values[dsIndex][rowIndex]); 111 } 112 return point; 113 } 114 115 120 public long[] getTimestamps() { 121 return timestamps; 122 } 123 124 128 public long getStep() { 129 return timestamps[1] - timestamps[0]; 130 } 131 132 139 public double[] getValues(int dsIndex) { 140 return values[dsIndex]; 141 } 142 143 149 public double[][] getValues() { 150 return values; 151 } 152 153 161 public double[] getValues(String dsName) throws RrdException { 162 for(int dsIndex = 0; dsIndex < getColumnCount(); dsIndex++) { 163 if(dsName.equals(dsNames[dsIndex])) { 164 return getValues(dsIndex); 165 } 166 } 167 throw new RrdException("Datasource [" + dsName + "] not found"); 168 } 169 170 174 public FetchRequest getRequest() { 175 return request; 176 } 177 178 182 public long getFirstTimestamp() { 183 return timestamps[0]; 184 } 185 186 190 public long getLastTimestamp() { 191 return timestamps[timestamps.length - 1]; 192 } 193 194 200 public Archive getMatchingArchive() { 201 return matchingArchive; 202 } 203 204 210 public String [] getDsNames() { 211 return dsNames; 212 } 213 214 219 public int getDsIndex(String dsName) { 220 for (int i = 0; i < dsNames.length; i++) 222 if ( dsNames[i].equals(dsName) ) 223 return i; 224 225 return -1; } 227 228 231 public void dump() { 232 for(int i = 0; i < getRowCount(); i++) { 233 System.out.println(getRow(i).dump()); 234 } 235 } 236 237 241 public String toString() { 242 final DecimalFormat df = new DecimalFormat ("+0.0000000000E00"); 243 StringBuffer buff = new StringBuffer (); 245 buff.append(padWithBlanks("", 10)); 246 buff.append(" "); 247 for(int i = 0; i < dsNames.length; i++) { 248 buff.append(padWithBlanks(dsNames[i], 18)); 249 } 250 buff.append("\n \n"); 251 for(int i = 0; i < timestamps.length; i++) { 252 buff.append(padWithBlanks("" + timestamps[i], 10)); 253 buff.append(":"); 254 for(int j = 0; j < dsNames.length; j++) { 255 double value = values[j][i]; 256 String valueStr = Double.isNaN(value)? "nan": df.format(value); 257 buff.append(padWithBlanks(valueStr, 18)); 258 } 259 buff.append("\n"); 260 } 261 return buff.toString(); 262 } 263 264 private static String padWithBlanks(String input, int width) { 265 StringBuffer buff = new StringBuffer (""); 266 int diff = width - input.length(); 267 while(diff-- > 0) { 268 buff.append(' '); 269 } 270 buff.append(input); 271 return buff.toString(); 272 } 273 274 283 public double getAggregate(String dsName, String consolFun) throws RrdException { 284 return getAggregate(dsName, consolFun, null); 285 } 286 287 302 public double getAggregate(String dsName, String consolFun, String rpnExpression) 303 throws RrdException { 304 if(consolFun.equals("MAX")) { 305 return getMax(dsName, rpnExpression); 306 } 307 else if(consolFun.equals("MIN")) { 308 return getMin(dsName, rpnExpression); 309 } 310 else if(consolFun.equals("LAST")) { 311 return getLast(dsName, rpnExpression); 312 } 313 else if(consolFun.equals("AVERAGE")) { 314 return getAverage(dsName, rpnExpression); 315 } 316 else { 317 throw new RrdException("Unsupported consolidation function [" + consolFun + "]"); 318 } 319 } 320 321 private double getMax(String dsName, String rpnExpression) throws RrdException { 322 RpnCalculator rpnCalculator = null; 323 if(rpnExpression != null) { 324 rpnCalculator = new RpnCalculator(rpnExpression); 325 } 326 double vals[] = getValues(dsName), max = Double.NaN; 327 for(int i = 0; i < vals.length - 1; i++) { 328 double value = vals[i + 1]; 329 if(rpnCalculator != null) { 330 rpnCalculator.setValue(value); 331 value = rpnCalculator.calculate(); 332 } 333 max = Util.max(max, value); 334 } 335 return max; 336 } 337 338 private double getMin(String dsName, String rpnExpression) throws RrdException { 339 RpnCalculator rpnCalculator = null; 340 if(rpnExpression != null) { 341 rpnCalculator = new RpnCalculator(rpnExpression); 342 } 343 double vals[] = getValues(dsName), min = Double.NaN; 344 for(int i = 0; i < vals.length - 1; i++) { 345 double value = vals[i + 1]; 346 if(rpnCalculator != null) { 347 rpnCalculator.setValue(value); 348 value = rpnCalculator.calculate(); 349 } 350 min = Util.min(min, value); 351 } 352 return min; 353 } 354 355 private double getLast(String dsName, String rpnExpression) throws RrdException { 356 RpnCalculator rpnCalculator = null; 357 if(rpnExpression != null) { 358 rpnCalculator = new RpnCalculator(rpnExpression); 359 } 360 double vals[] = getValues(dsName); 361 double value = vals[vals.length - 1]; 362 if(rpnCalculator != null) { 363 rpnCalculator.setValue(value); 364 value = rpnCalculator.calculate(); 365 } 366 return value; 367 } 368 369 private double getAverage(String dsName, String rpnExpression) throws RrdException { 370 RpnCalculator rpnCalculator = null; 371 if(rpnExpression != null) { 372 rpnCalculator = new RpnCalculator(rpnExpression); 373 } 374 double vals[] = getValues(dsName); 375 double totalVal = 0; 376 long totalSecs = 0; 377 for(int i = 0; i < vals.length - 1; i++) { 378 long t1 = Math.max(request.getFetchStart(), timestamps[i]); 379 long t2 = Math.min(request.getFetchEnd(), timestamps[i + 1]); 380 double value = vals[i + 1]; 381 if(rpnCalculator != null) { 382 rpnCalculator.setValue(value); 383 value = rpnCalculator.calculate(); 384 } 385 if(!Double.isNaN(value)) { 386 totalSecs += (t2 - t1); 387 totalVal += (t2 - t1) * value; 388 } 389 } 390 return totalSecs > 0? totalVal / totalSecs: Double.NaN; 391 } 392 393 398 public void exportXml(OutputStream outputStream) throws IOException { 399 XmlWriter writer = new XmlWriter(outputStream); 400 writer.startTag("fetch_data"); 401 writer.startTag("request"); 402 writer.writeTag("file", request.getParentDb().getPath()); 403 writer.writeComment(Util.getDate(request.getFetchStart())); 404 writer.writeTag("start", request.getFetchStart()); 405 writer.writeComment(Util.getDate(request.getFetchEnd())); 406 writer.writeTag("end", request.getFetchEnd()); 407 writer.writeTag("resolution", request.getResolution()); 408 writer.writeTag("cf", request.getConsolFun()); 409 writer.closeTag(); writer.startTag("datasources"); 411 for(int i = 0; i < dsNames.length; i++) { 412 writer.writeTag("name", dsNames[i]); 413 } 414 writer.closeTag(); writer.startTag("data"); 416 for(int i = 0; i < timestamps.length; i++) { 417 writer.startTag("row"); 418 writer.writeComment(Util.getDate(timestamps[i])); 419 writer.writeTag("timestamp", timestamps[i]); 420 writer.startTag("values"); 421 for(int j = 0; j < dsNames.length; j++) { 422 writer.writeTag("v", values[j][i]); 423 } 424 writer.closeTag(); writer.closeTag(); } 427 writer.closeTag(); writer.closeTag(); writer.flush(); 430 } 431 432 437 public void exportXml(String filepath) throws IOException { 438 OutputStream outputStream = null; 439 try { 440 outputStream = new FileOutputStream (filepath); 441 exportXml(outputStream); 442 } 443 finally { 444 if(outputStream != null) { 445 outputStream.close(); 446 } 447 } 448 } 449 450 455 public String exportXml() throws IOException { 456 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 457 exportXml(outputStream); 458 return outputStream.toString(); 459 } 460 } 461 | Popular Tags |