1 25 package org.jrobin.graph; 26 27 import org.jrobin.core.RrdDataSet; 28 import org.jrobin.core.RrdException; 29 import org.jrobin.core.Util; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 33 import java.util.HashMap ; 34 import java.io.*; 35 36 41 public class ExportData implements RrdDataSet 42 { 43 private int arraySize; 47 private long[] timestamps; 48 49 private HashMap sourceByName, legends; 50 private Source[] sources; 51 52 private Print printer; 53 54 ExportData() 58 { 59 sourceByName = new HashMap (); 60 legends = new HashMap (); 61 } 62 63 ExportData( long[] timestamps, Source[] sources, HashMap legends ) 64 { 65 this.timestamps = timestamps; 66 this.sources = sources; 67 this.legends = legends; 68 this.arraySize = timestamps.length; 69 70 sourceByName = new HashMap ( sources.length ); 71 for ( int i = 0; i < sources.length; i++ ) 72 sourceByName.put( sources[i].getName(), sources[i] ); 73 } 74 75 82 public ExportData( String xportXml ) throws RrdException, IOException 83 { 84 this(); 85 86 importXml( xportXml ); 87 } 88 89 97 public ExportData( String xportXml, boolean useLegendNames ) throws RrdException, IOException 98 { 99 this(); 100 101 importXml( xportXml, useLegendNames ); 102 } 103 104 112 public ExportData( String xportXml, String dsNamePrefix ) throws RrdException, IOException 113 { 114 this(); 115 116 importXml( xportXml, dsNamePrefix ); 117 } 118 119 126 public ExportData( File xmlFile ) throws RrdException, IOException 127 { 128 this(); 129 130 importXml( xmlFile ); 131 } 132 133 141 public ExportData( File xmlFile, boolean useLegendNames ) throws RrdException, IOException 142 { 143 this(); 144 145 importXml( xmlFile, useLegendNames ); 146 } 147 148 156 public ExportData( File xmlFile, String dsNamePrefix ) throws RrdException, IOException 157 { 158 this(); 159 160 importXml( xmlFile, dsNamePrefix ); 161 } 162 163 164 172 public int getRowCount() { 173 return sources.length; 174 } 175 176 181 public int getColumnCount() { 182 return arraySize; 183 } 184 185 191 public long[] getTimestamps() 192 { 193 return timestamps; 194 } 195 196 201 public long getStep() 202 { 203 return timestamps[1] - timestamps[0]; 204 } 205 206 213 public double[] getValues( int dsIndex ) 214 { 215 return sources[dsIndex].getValues(); 216 } 217 218 224 public double[][] getValues() 225 { 226 double[][] values = new double[ sources.length ][ arraySize ]; 227 228 for ( int i = 0; i < sources.length; i++ ) 229 values[i] = sources[i].getValues(); 230 231 return values; 232 } 233 234 242 public double[] getValues( String dsName ) throws RrdException 243 { 244 Source src = getSource( dsName ); 245 246 return src.getValues(); 247 } 248 249 254 public long getFirstTimestamp() { 255 return timestamps[0]; 256 } 257 258 263 public long getLastTimestamp() { 264 return timestamps[ arraySize - 1 ]; 265 } 266 267 272 public String [] getDsNames() 273 { 274 String [] names = new String [ sources.length ]; 275 276 for ( int i = 0; i < sources.length; i++ ) 277 names[i] = sources[i].getName(); 278 279 return names; 280 } 281 282 290 public int getDsIndex( String dsName ) throws RrdException 291 { 292 for ( int i = 0; i < sources.length; i++ ) 293 if ( sources[i].getName().equals(dsName) ) 294 return i; 295 296 throw new RrdException( "No such datasource: " + dsName ); 297 } 298 299 308 public double getAggregate( String dsName, String consolFun ) throws RrdException 309 { 310 Source src = getSource( dsName ); 311 312 if( consolFun.equalsIgnoreCase("MAX") ) 313 return src.getAggregate( Source.AGG_MAXIMUM ); 314 else if ( consolFun.equalsIgnoreCase("MIN") ) 315 return src.getAggregate( Source.AGG_MINIMUM ); 316 else if ( consolFun.equalsIgnoreCase("LAST") ) 317 return src.getAggregate( Source.AGG_LAST); 318 else if ( consolFun.equalsIgnoreCase("FIRST") ) 319 return src.getAggregate( Source.AGG_FIRST ); 320 else if ( consolFun.equalsIgnoreCase("TOTAL") ) 321 return src.getAggregate( Source.AGG_TOTAL ); 322 else if ( consolFun.equalsIgnoreCase("AVERAGE") ) 323 return src.getAggregate( Source.AGG_AVERAGE ); 324 else 325 throw new RrdException("Unsupported consolidation function [" + consolFun + "]"); 326 } 327 328 349 public String print( String sourceName, String consolFun, String format ) throws RrdException { 350 return print( sourceName, consolFun, format, ValueFormatter.DEFAULT_BASE ); 351 } 352 353 375 public String print( String sourceName, String consolFun, String format, double base ) throws RrdException 376 { 377 double value = getAggregate( sourceName, consolFun ); 378 379 if ( printer == null ) 380 printer = new Print( base, ValueFormatter.NO_SCALE ); 381 382 return printer.getFormattedString( value, format, base ); 383 } 384 385 393 public void importXml( String xportXml ) throws RrdException, IOException { 394 importXml( xportXml, true ); 395 } 396 397 405 public void importXml( File xmlFile ) throws RrdException, IOException { 406 importXml( xmlFile, true ); 407 } 408 409 419 public void importXml( File xmlFile , boolean useLegendNames ) throws RrdException, IOException 420 { 421 Element root = Util.Xml.getRootElement( xmlFile ); 422 importXml( root, useLegendNames, "d" ); 423 } 424 425 435 public void importXml( String xportXml, String dsNamePrefix ) throws RrdException, IOException 436 { 437 Element root = Util.Xml.getRootElement( xportXml ); 438 importXml( root, false, dsNamePrefix ); 439 } 440 441 451 public void importXml( File xmlFile, String dsNamePrefix ) throws RrdException, IOException 452 { 453 Element root = Util.Xml.getRootElement( xmlFile ); 454 importXml( root, false, dsNamePrefix ); 455 } 456 457 467 public void importXml( String xportXml, boolean useLegendNames ) throws RrdException, IOException 468 { 469 Element root = Util.Xml.getRootElement( xportXml ); 470 importXml( root, useLegendNames, "d" ); 471 } 472 473 480 public void exportXml( OutputStream outputStream ) throws RrdException, IOException 481 { 482 PrintWriter pw = new PrintWriter( outputStream ); 483 pw.write( exportXml() ); 484 pw.flush(); 485 } 486 487 494 public void exportXml( String filepath ) throws RrdException, IOException 495 { 496 FileWriter fw = new FileWriter( filepath ); 497 fw.write( exportXml() ); 498 fw.close(); 499 } 500 501 508 public String exportXml() throws RrdException, IOException 509 { 510 StringBuffer xml = new StringBuffer ( "<xport>\n" ); 511 512 xml.append( "\t<meta>\n" ); 514 xml.append( "\t\t<start>" + timestamps[0] + "</start>\n" ); 515 xml.append( "\t\t<step>" + (timestamps[1] - timestamps[0]) + "</step>\n" ); 516 xml.append( "\t\t<end>" + timestamps[arraySize - 1] + "</end>\n" ); 517 xml.append( "\t\t<rows>" + arraySize + "</rows>\n" ); 518 xml.append( "\t\t<columns>" + sources.length + "</columns>\n" ); 519 xml.append( "\t\t<legend>\n" ); 520 for ( int i = 0; i < sources.length; i++ ) 521 xml.append( "\t\t\t<entry>" + getExportLegend( sources[i].getName() ) + "</entry>\n" ); 522 xml.append( "\t\t</legend>\n" ); 523 xml.append( "\t</meta>\n" ); 524 525 xml.append( "\t<data>\n" ); 527 528 for ( int i = 0; i < arraySize; i++ ) 529 { 530 xml.append( "\t\t<row>" ); 531 xml.append( "<t>" + timestamps[i] + "</t>" ); 532 for ( int j = 0; j < sources.length; j++ ) 533 xml.append( "<v>" + sources[ j ].get( i ) + "</v>" ); 534 xml.append( "</row>\n" ); 535 } 536 xml.append( "\t</data>\n" ); 537 538 xml.append( "</xport>\n" ); 539 540 return xml.toString(); 541 } 542 543 protected Source[] getSources() { 547 return sources; 548 } 549 550 private String getExportLegend( String name ) 554 { 555 if ( !legends.containsKey(name) ) 556 return ""; 557 558 return (String ) legends.get(name); 559 } 560 561 private Source getSource( String name ) throws RrdException 562 { 563 if ( !sourceByName.containsKey(name) ) 564 throw new RrdException( "No such datasource: " + name ); 565 566 return (Source) sourceByName.get(name); 567 } 568 569 private void importXml( Element root, boolean useLegendNames, String dsNamePrefix ) throws RrdException 570 { 571 Node meta = Util.Xml.getFirstChildNode( root, "meta" ); 572 Node [] dataRows = Util.Xml.getChildNodes( Util.Xml.getFirstChildNode( root, "data" ), "row" ); 573 574 sourceByName.clear(); 575 legends.clear(); 576 577 int columns = Util.Xml.getChildValueAsInt( meta, "columns" ); 579 long step = Util.Xml.getChildValueAsLong( meta, "step" ); 580 String [] dsNames = new String [ columns ]; 581 Node [] legendNodes = Util.Xml.getChildNodes( Util.Xml.getFirstChildNode( meta, "legend"), "entry" ); 582 for ( int i = 0; i < legendNodes.length; i++ ) 583 { 584 String legend = Util.Xml.getValue( legendNodes[i] ); 585 if ( useLegendNames ) 586 dsNames[i] = legend; 587 else 588 dsNames[i] = dsNamePrefix + (i + 1); 589 590 legends.put( dsNames[i], legend ); 591 } 592 593 timestamps = new long[ dataRows.length ]; 595 sources = new Source[ columns ]; 596 arraySize = timestamps.length; 597 598 for ( int i = 0; i < sources.length; i++ ) 599 { 600 sources[i] = new Def( dsNames[i], arraySize, arraySize ); 601 sources[i].setFetchedStep( step ); 602 } 603 604 for ( int i = 0; i < dataRows.length; i++ ) 605 { 606 timestamps[i] = Util.Xml.getChildValueAsLong( dataRows[i], "t" ); 607 Node [] data = Util.Xml.getChildNodes( dataRows[i], "v" ); 608 609 for ( int j = 0; j < data.length; j++ ) 610 sources[j].set( i, timestamps[i], Util.Xml.getValueAsDouble(data[j]) ); 611 } 612 613 for ( int i = 0; i < sources.length; i++ ) 615 sourceByName.put( sources[i].getName(), sources[i] ); 616 } 617 } 618 | Popular Tags |