From: Sasa M. <sa...@us...> - 2004-11-25 12:15:09
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10251/org/jrobin/core Modified Files: FetchData.java FetchRequest.java Util.java Log Message: Added org.java.data.DataProcessor class. This class should be used to get datasource values for graphing purposes. AVERAGEs and TOTALs returned by this class will not change when the graph is resized. RPN calculator is greatly improved (it's very fast). Index: FetchData.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/FetchData.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** FetchData.java 21 Nov 2004 13:25:38 -0000 1.13 --- FetchData.java 25 Nov 2004 12:14:53 -0000 1.14 *************** *** 26,37 **** package org.jrobin.core; - import org.jrobin.data.DataStats; - import org.jrobin.data.RpnCalculator; - import java.io.IOException; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.ByteArrayOutputStream; - import java.text.DecimalFormat; /** --- 26,33 ---- *************** *** 57,61 **** * the values returned with {@link #getTimestamps() getTimestamps()} method.<p> * - * Use {@link #getStats(String, String)} method to calculate aggregates for the fetched data<p> */ public class FetchData implements RrdDataSet, ConsolFuns { --- 53,56 ---- *************** *** 248,257 **** /** ! * Dumps the content of the whole FetchData object to stdout. Useful for debugging. */ ! public void dump() { ! for (int i = 0; i < getRowCount(); i++) { ! System.out.println(getRow(i).dump()); } } --- 243,260 ---- /** ! * Dumps the content of the whole FetchData object. Useful for debugging. */ ! public String dump() { ! StringBuffer buffer = new StringBuffer(""); ! for (int row = 0; row < getRowCount(); row++) { ! buffer.append(timestamps[row]); ! buffer.append(": "); ! for(int dsIndex = 0; dsIndex < getColumnCount(); dsIndex++) { ! buffer.append(Util.formatDouble(values[dsIndex][row], true)); ! buffer.append(" "); ! } ! buffer.append("\n"); } + return buffer.toString(); } *************** *** 262,266 **** */ public String toString() { - final DecimalFormat df = new DecimalFormat("+0.0000000000E00"); // print header row StringBuffer buff = new StringBuffer(); --- 265,268 ---- *************** *** 276,280 **** for (int j = 0; j < dsNames.length; j++) { double value = values[j][i]; ! String valueStr = Double.isNaN(value) ? "nan" : df.format(value); buff.append(padWithBlanks(valueStr, 18)); } --- 278,282 ---- for (int j = 0; j < dsNames.length; j++) { double value = values[j][i]; ! String valueStr = Double.isNaN(value) ? "nan" : Util.formatDouble(value); buff.append(padWithBlanks(valueStr, 18)); } *************** *** 299,324 **** * @param dsName Datasource name * @param consolFun Consolidation function to be applied to fetched datasource values. - * Valid consolidation functions are "MIN", "MAX", "LAST" and "AVERAGE" - * (these string constants are conveniently defined in the {@link ConsolFuns} class). - * @return MIN, MAX, LAST or AVERAGE value calculated from the fetched data - * for the given datasource name - * @throws RrdException Thrown if the given datasource name cannot be found in fetched data. - * @deprecated Use {@link #getStats(String) getStats(dsName)} method instead. - */ - public double getAggregate(String dsName, String consolFun) throws RrdException { - return getAggregate(dsName, consolFun, null); - } - - /** - * Returns aggregated value from the fetched data for a single datasource. - * Before applying aggrregation functions, specified RPN expression is applied to fetched - * data. For example, if you have a GAUGE datasource named 'feet' but you want to - * find the maximum fetched value in meters use something like:</p> - * <code>getAggregate("feet", ConsolFuns.MAX, "value,0.3048,*");</code> - * Note that 'value' in the RPN expression is a reserved word and stands for the - * original value (value fetched from the RRD)</p> - * - * @param dsName Datasource name - * @param consolFun Consolidation function to be applied to fetched datasource values. * Valid consolidation functions are "MIN", "MAX", "LAST", "FIRST", "AVERAGE" and "TOTAL" * (these string constants are conveniently defined in the {@link ConsolFuns} class) --- 301,304 ---- *************** *** 326,351 **** * for the given datasource name * @throws RrdException Thrown if the given datasource name cannot be found in fetched data. ! * @deprecated Use {@link #getStats(String, String) getStats(dsName, rpnExpression)} method instead. */ ! public double getAggregate(String dsName, String consolFun, String rpnExpression) ! throws RrdException { ! DataStats stats = getStats(dsName, rpnExpression); ! if (consolFun.equals(CF_MAX)) { ! return stats.getMax(); } ! else if (consolFun.equals(CF_MIN)) { ! return stats.getMin(); } ! else if (consolFun.equals(CF_LAST)) { ! return stats.getLast(); } ! else if (consolFun.equals(CF_FIRST)) { ! return stats.getFirst(); } ! else if (consolFun.equals(CF_AVERAGE)) { ! return stats.getAverage(); } ! else if (consolFun.equals(CF_TOTAL)) { ! return stats.getTotal(); } else { --- 306,330 ---- * for the given datasource name * @throws RrdException Thrown if the given datasource name cannot be found in fetched data. ! * @deprecated This method may calculate averages slightly different from values displayed in the ! * corresponding graph. Use {@link org.jrobin.data.DataProcessor DataAnalyzer} class instead. */ ! public double getAggregate(String dsName, String consolFun) throws RrdException { ! if(consolFun.equals(CF_MAX)) { ! return getMax(dsName); } ! else if(consolFun.equals(CF_MIN)) { ! return getMin(dsName); } ! else if(consolFun.equals(CF_LAST)) { ! return getLast(dsName); } ! else if(consolFun.equals(CF_FIRST)) { ! return getFirst(dsName); } ! else if(consolFun.equals(CF_AVERAGE)) { ! return getAverage(dsName); } ! else if(consolFun.equals(CF_TOTAL)) { ! return getTotal(dsName); } else { *************** *** 354,398 **** } ! /** ! * Returns all aggregated values from the fetched data for a single datasource. ! * ! * @param dsName Datasource name ! * @return Object containing MIN, MAX, LAST, FIRST, AVERAGE and TOTAL values calculated from the fetched data ! * for the given datasource name ! * @throws RrdException Thrown if the given datasource name cannot be found in fetched data. ! */ ! public DataStats getStats(String dsName) throws RrdException { ! return getStats(dsName, null); } ! /** ! * Returns all aggregated values from the fetched data for a single datasource. ! * Before applying aggrregation functions, specified RPN expression is applied to fetched ! * data. For example, if you have a GAUGE datasource named 'feet' but you want to ! * calculate aggregates in meters use something like:<p> ! * <pre> ! * getStats("feet", "value,0.3048,*"); ! * </pre> ! * Note that the placeholder 'value' in the RPN expression is a reserved word and stands for the ! * original value (value fetched from the RRD) ! * ! * @param dsName Datasource name ! * @return Object containing MIN, MAX, LAST, FIRST, AVERAGE and TOTAL values calculated from the fetched data ! * for the given datasource name and a given RPN expression ! * @throws RrdException Thrown if the given datasource name cannot be found in fetched data ! */ ! public DataStats getStats(String dsName, String rpnExpression) throws RrdException { ! double[] values = getValues(dsName), calculatedValues = values; ! if (rpnExpression != null) { ! calculatedValues = new double[values.length]; ! RpnCalculator rpnCalculator = new RpnCalculator(rpnExpression); ! for (int i = 0; i < values.length; i++) { ! rpnCalculator.setValue(values[i]); ! rpnCalculator.setTimestamp(timestamps[i]); ! calculatedValues[i] = rpnCalculator.calculate(); } } ! return new DataStats(timestamps, calculatedValues, ! request.getFetchStart(), request.getFetchEnd()); } --- 333,387 ---- } ! private double getMax(String dsName) throws RrdException { ! double values[] = getValues(dsName), max = Double.NaN; ! for(int i = 1; i < values.length; i++) { ! max = Util.max(max, values[i]); ! } ! return max; } ! private double getMin(String dsName) throws RrdException { ! double values[] = getValues(dsName), min = Double.NaN; ! for(int i = 1; i < values.length; i++) { ! min = Util.min(min, values[i]); ! } ! return min; ! } ! ! private double getLast(String dsName) throws RrdException { ! double values[] = getValues(dsName); ! return values[values.length - 1]; ! } ! ! private double getFirst(String dsName) throws RrdException { ! double values[] = getValues(dsName); ! return values[1]; ! } ! ! private double getAverage(String dsName) throws RrdException { ! double values[] = getValues(dsName), totalVal = 0D; ! long tStart = request.getFetchStart(), tEnd = request.getFetchEnd(), totalSecs = 0; ! for(int i = 1; i < values.length; i++) { ! long t1 = Math.max(tStart, timestamps[i - 1]), t2 = Math.min(tEnd, timestamps[i]); ! double value = values[i]; ! if(!Double.isNaN(value)) { ! totalSecs += (t2 - t1); ! totalVal += (t2 - t1) * value; } } ! return totalSecs > 0? totalVal / totalSecs: Double.NaN; ! } ! ! private double getTotal(String dsName) throws RrdException { ! double vals[] = getValues(dsName), totalVal = 0D; ! long tStart = request.getFetchStart(), tEnd = request.getFetchEnd(); ! for(int i = 1; i < vals.length; i++) { ! long t1 = Math.max(tStart, timestamps[i - 1]), t2 = Math.min(tEnd, timestamps[i]); ! double value = vals[i]; ! if(!Double.isNaN(value)) { ! totalVal += (t2 - t1) * value; ! } ! } ! return totalVal; } Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** Util.java 22 Nov 2004 13:34:51 -0000 1.28 --- Util.java 25 Nov 2004 12:14:53 -0000 1.29 *************** *** 142,145 **** --- 142,155 ---- /** + * Formats double as a string using exponential notation. Used for debugging + * throught the project. + * @param x value to be formatted + * @return string like "+1.234567E+02" + */ + public static String formatDouble(double x) { + return formatDouble(x, true); + } + + /** * Returns <code>Date</code> object for the given timestamp (in seconds, without * milliseconds) Index: FetchRequest.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/FetchRequest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FetchRequest.java 21 Jul 2004 08:27:40 -0000 1.7 --- FetchRequest.java 25 Nov 2004 12:14:53 -0000 1.8 *************** *** 27,30 **** --- 27,31 ---- import java.io.IOException; + import java.util.Set; /** *************** *** 72,75 **** --- 73,89 ---- /** * Sets request filter in order to fetch data only for + * the specified set of datasources (datasource names). + * If the filter is not set (or set to null), fetched data will + * containt values of all datasources defined in the corresponding RRD. + * To fetch data only from selected + * datasources, specify a set of datasource names as method argument. + * @param filter Set of datsource names to fetch data for. + */ + public void setFilter(Set filter) { + this.filter = (String[]) filter.toArray(new String[0]); + } + + /** + * Sets request filter in order to fetch data only for * a single datasource (datasource name). * If not set (or set to null), fetched data will |