📄 exportdata.java
字号:
Source src = getSource( dsName );
if( consolFun.equalsIgnoreCase("MAX") )
return src.getAggregate( Source.AGG_MAXIMUM );
else if ( consolFun.equalsIgnoreCase("MIN") )
return src.getAggregate( Source.AGG_MINIMUM );
else if ( consolFun.equalsIgnoreCase("LAST") )
return src.getAggregate( Source.AGG_LAST);
else if ( consolFun.equalsIgnoreCase("FIRST") )
return src.getAggregate( Source.AGG_FIRST );
else if ( consolFun.equalsIgnoreCase("TOTAL") )
return src.getAggregate( Source.AGG_TOTAL );
else if ( consolFun.equalsIgnoreCase("AVERAGE") )
return src.getAggregate( Source.AGG_AVERAGE );
else
throw new RrdException("Unsupported consolidation function [" + consolFun + "]");
}
/**
* <p>Calculate the chosen consolidation function <code>consolFun</code> over
* the <code>sourceName</code> and returns the result as a string using the
* specified <code>format</code>.</p>
*
* <p>In the format string there should be a
* <code>@n.d</code> marker (replace <code>n</code> with the total number of spaces the
* value should at minimum take up, and replace <code>d</code> with the desired number of decimals)
* in the place where the number should be printed. If an additional <code>@s</code> is
* found in the format, the value will be scaled and an appropriate SI magnitude
* unit will be printed in place of the <code>@s</code> marker. If you specify
* <code>@S</code> instead of <code>@s</code>, the value will be scaled with the scale
* factor used in the last gprint directive (uniform value scaling).</p>
*
* @param sourceName Source name
* @param consolFun Consolidation function to be used for calculation ("AVERAGE",
* "MIN", "MAX", "LAST" or "TOTAL" (since 1.3.1)
* @param format Format string. For example: "speed is @5.2 @sbits/sec@c",
* "temperature = @0 degrees"
* @throws RrdException Thrown in case of JRobin specific error
*/
public String print( String sourceName, String consolFun, String format ) throws RrdException {
return print( sourceName, consolFun, format, ValueFormatter.DEFAULT_BASE );
}
/**
* <p>Calculate the chosen consolidation function <code>consolFun</code> over
* the <code>sourceName</code> and returns the result as a string using the
* specified <code>format</code>.</p>
*
* <p>In the format string there should be a
* <code>@n.d</code> marker (replace <code>n</code> with the total number of spaces the
* value should at minimum take up, and replace <code>d</code> with the desired number of decimals)
* in the place where the number should be printed. If an additional <code>@s</code> is
* found in the format, the value will be scaled and an appropriate SI magnitude
* unit will be printed in place of the <code>@s</code> marker. If you specify
* <code>@S</code> instead of <code>@s</code>, the value will be scaled with the scale
* factor used in the last gprint directive (uniform value scaling).</p>
*
* @param sourceName Source name
* @param consolFun Consolidation function to be used for calculation ("AVERAGE",
* "MIN", "MAX", "LAST" or "TOTAL" (since 1.3.1)
* @param format Format string. For example: "speed is @5.2 @sbits/sec@c",
* "temperature = @0 degrees"
* @param base Base value used to calculate the appriopriate scaling SI magnitude.
* @throws RrdException Thrown in case of JRobin specific error
*/
public String print( String sourceName, String consolFun, String format, double base ) throws RrdException
{
double value = getAggregate( sourceName, consolFun );
if ( printer == null )
printer = new Print( base, ValueFormatter.NO_SCALE );
return printer.getFormattedString( value, format, base );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* Datasources found will be named d1, d2, ...
*
* @param xportXml String containing the XML result of an export.
*/
public void importXml( String xportXml ) throws RrdException, IOException {
importXml( xportXml, true );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* Datasources found will be named d1, d2, ...
*
* @param xmlFile File containing export XML dump.
*/
public void importXml( File xmlFile ) throws RrdException, IOException {
importXml( xmlFile, true );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* The name of the datasources found will depend on the 'useLegendNames' flag.
*
* @param xmlFile File containing export XML dump.
* @param useLegendNames True if the names for the datasources should be set to
* the legend values, false if they should be d1, d2, ...
*/
public void importXml( File xmlFile , boolean useLegendNames ) throws RrdException, IOException
{
Element root = Util.Xml.getRootElement( xmlFile );
importXml( root, useLegendNames, "d" );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* The name of the datasources found will be the prefix passed as parameter,
* followed by a number, making the name unique.
*
* @param xportXml String containing the XML result of an export.
* @param dsNamePrefix Prefix of the datasource names.
*/
public void importXml( String xportXml, String dsNamePrefix ) throws RrdException, IOException
{
Element root = Util.Xml.getRootElement( xportXml );
importXml( root, false, dsNamePrefix );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* The name of the datasources found will be the prefix passed as parameter,
* followed by a number, making the name unique.
*
* @param xmlFile File containing export XML dump.
* @param dsNamePrefix Prefix of the datasource names.
*/
public void importXml( File xmlFile, String dsNamePrefix ) throws RrdException, IOException
{
Element root = Util.Xml.getRootElement( xmlFile );
importXml( root, false, dsNamePrefix );
}
/**
* Imports a export XML string and maps it back to this ExportData object.
* The XML can be from either a JRobin or RRDtool export.
*
* The name of the datasources found will depend on the 'useLegendNames' flag.
*
* @param xportXml String containing the XML result of an export.
* @param useLegendNames True if the names for the datasources should be set to
* the legend values, false if they should be d1, d2, ...
*/
public void importXml( String xportXml, boolean useLegendNames ) throws RrdException, IOException
{
Element root = Util.Xml.getRootElement( xportXml );
importXml( root, useLegendNames, "d" );
}
/**
* Dumps fetch data to output stream in XML format.
*
* @param outputStream Output stream to dump dataset to
* @throws RrdException Thrown in case of JRobin specific error.
* @throws IOException Thrown in case of I/O error
*/
public void exportXml( OutputStream outputStream ) throws RrdException, IOException
{
PrintWriter pw = new PrintWriter( outputStream );
pw.write( exportXml() );
pw.flush();
}
/**
* Dumps dataset to file in XML format.
*
* @param filepath Path to destination file
* @throws RrdException Thrown in case of JRobin specific error.
* @throws IOException Thrown in case of I/O error
*/
public void exportXml( String filepath ) throws RrdException, IOException
{
FileWriter fw = new FileWriter( filepath );
fw.write( exportXml() );
fw.close();
}
/**
* Dumps the dataset to XML.
*
* @return XML string format of the dataset.
* @throws RrdException Thrown in case of JRobin specific error.
* @throws java.io.IOException Thrown in case of an I/O related error.
*/
public String exportXml() throws RrdException, IOException
{
StringBuffer xml = new StringBuffer( "<xport>\n" );
// Add metadata section
xml.append( "\t<meta>\n" );
xml.append( "\t\t<start>" + timestamps[0] + "</start>\n" );
xml.append( "\t\t<step>" + (timestamps[1] - timestamps[0]) + "</step>\n" );
xml.append( "\t\t<end>" + timestamps[arraySize - 1] + "</end>\n" );
xml.append( "\t\t<rows>" + arraySize + "</rows>\n" );
xml.append( "\t\t<columns>" + sources.length + "</columns>\n" );
xml.append( "\t\t<legend>\n" );
for ( int i = 0; i < sources.length; i++ )
xml.append( "\t\t\t<entry>" + getExportLegend( sources[i].getName() ) + "</entry>\n" );
xml.append( "\t\t</legend>\n" );
xml.append( "\t</meta>\n" );
// Add data section
xml.append( "\t<data>\n" );
for ( int i = 0; i < arraySize; i++ )
{
xml.append( "\t\t<row>" );
xml.append( "<t>" + timestamps[i] + "</t>" );
for ( int j = 0; j < sources.length; j++ )
xml.append( "<v>" + sources[ j ].get( i ) + "</v>" );
xml.append( "</row>\n" );
}
xml.append( "\t</data>\n" );
xml.append( "</xport>\n" );
return xml.toString();
}
// ================================================================
// -- Protected methods
// ================================================================
protected Source[] getSources() {
return sources;
}
// ================================================================
// -- Private methods
// ================================================================
private String getExportLegend( String name )
{
if ( !legends.containsKey(name) )
return "";
return (String) legends.get(name);
}
private Source getSource( String name ) throws RrdException
{
if ( !sourceByName.containsKey(name) )
throw new RrdException( "No such datasource: " + name );
return (Source) sourceByName.get(name);
}
private void importXml( Element root, boolean useLegendNames, String dsNamePrefix ) throws RrdException
{
Node meta = Util.Xml.getFirstChildNode( root, "meta" );
Node[] dataRows = Util.Xml.getChildNodes( Util.Xml.getFirstChildNode( root, "data" ), "row" );
sourceByName.clear();
legends.clear();
// -- Parse the metadata
int columns = Util.Xml.getChildValueAsInt( meta, "columns" );
long step = Util.Xml.getChildValueAsLong( meta, "step" );
String[] dsNames = new String[ columns ];
Node[] legendNodes = Util.Xml.getChildNodes( Util.Xml.getFirstChildNode( meta, "legend"), "entry" );
for ( int i = 0; i < legendNodes.length; i++ )
{
String legend = Util.Xml.getValue( legendNodes[i] );
if ( useLegendNames )
dsNames[i] = legend;
else
dsNames[i] = dsNamePrefix + (i + 1);
legends.put( dsNames[i], legend );
}
// -- Parse the data
timestamps = new long[ dataRows.length ];
sources = new Source[ columns ];
arraySize = timestamps.length;
for ( int i = 0; i < sources.length; i++ )
{
sources[i] = new Def( dsNames[i], arraySize, arraySize );
sources[i].setFetchedStep( step );
}
for ( int i = 0; i < dataRows.length; i++ )
{
timestamps[i] = Util.Xml.getChildValueAsLong( dataRows[i], "t" );
Node[] data = Util.Xml.getChildNodes( dataRows[i], "v" );
for ( int j = 0; j < data.length; j++ )
sources[j].set( i, timestamps[i], Util.Xml.getValueAsDouble(data[j]) );
}
// -- Set the datasource - name
for ( int i = 0; i < sources.length; i++ )
sourceByName.put( sources[i].getName(), sources[i] );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -