⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fetchdata.java

📁 jrobin,使用纯java实现的RRD数据库,使用RRD数据库来统计数据.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}

	/**
	 * Returns string representing fetched data in a RRDTool-like form.
	 *
	 * @return Fetched data as a string in a rrdfetch-like output form.
	 */
	public String toString() {
		// print header row
		StringBuffer buff = new StringBuffer();
		buff.append(padWithBlanks("", 10));
		buff.append(" ");
		for (String dsName : dsNames) {
			buff.append(padWithBlanks(dsName, 18));
		}
		buff.append("\n \n");
		for (int i = 0; i < timestamps.length; i++) {
			buff.append(padWithBlanks("" + timestamps[i], 10));
			buff.append(":");
			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));
			}
			buff.append("\n");
		}
		return buff.toString();
	}

	private static String padWithBlanks(String input, int width) {
		StringBuffer buff = new StringBuffer("");
		int diff = width - input.length();
		while (diff-- > 0) {
			buff.append(' ');
		}
		buff.append(input);
		return buff.toString();
	}

	/**
	 * Returns single aggregated value from the fetched data for a single datasource.
	 *
	 * @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)
	 * @return MIN, MAX, LAST, FIRST, AVERAGE or TOTAL 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.
	 */
	public double getAggregate(String dsName, String consolFun) throws RrdException {
		DataProcessor dp = createDataProcessor(null);
		return dp.getAggregate(dsName, consolFun);
	}

	/**
	 * Returns aggregated value from the fetched data for a single datasource.
	 * Before applying aggregation functions, specified RPN expression is applied to fetched data.
	 * For example, if you have a gauge datasource named 'foots' but you want to find the maximum
	 * fetched value in meters use something like: <p>
	 * <code>getAggregate("foots", "MAX", "foots,0.3048,*");</code><p>
	 *
	 * @param dsName		Datasource name
	 * @param consolFun	 Consolidation function (MIN, MAX, LAST, FIRST, AVERAGE or TOTAL)
	 * @param rpnExpression RRDTool-like RPN expression
	 * @return Aggregated value
	 * @throws RrdException Thrown if the given datasource name cannot be found in fetched data, or if
	 *                      invalid RPN expression is supplied
	 * @throws IOException  Thrown in case of I/O error (unlikely to happen)
	 * @deprecated This method is preserved just for backward compatibility.
	 */
	public double getAggregate(String dsName, String consolFun, String rpnExpression)
			throws RrdException, IOException {
		// for backward compatibility
		rpnExpression = rpnExpression.replaceAll("value", dsName);
		return getRpnAggregate(rpnExpression, consolFun);
	}

	/**
	 * Returns aggregated value for a set of values calculated by applying an RPN expression to the
	 * fetched data. For example, if you have two datasources named <code>x</code> and <code>y</code>
	 * in this FetchData and you want to calculate MAX value of <code>(x+y)/2<code> use something like: <p>
	 * <code>getRpnAggregate("x,y,+,2,/", "MAX");</code><p>
	 *
	 * @param rpnExpression RRDTool-like RPN expression
	 * @param consolFun	 Consolidation function (MIN, MAX, LAST, FIRST, AVERAGE or TOTAL)
	 * @return Aggregated value
	 * @throws RrdException Thrown if invalid RPN expression is supplied
	 */
	public double getRpnAggregate(String rpnExpression, String consolFun) throws RrdException {
		DataProcessor dataProcessor = createDataProcessor(rpnExpression);
		return dataProcessor.getAggregate(RPN_SOURCE_NAME, consolFun);
	}

	/**
	 * Returns all aggregated values (MIN, MAX, LAST, FIRST, AVERAGE or TOTAL) calculated from the fetched data
	 * for a single datasource.
	 *
	 * @param dsName Datasource name.
	 * @return Simple object containing all aggregated values.
	 * @throws RrdException Thrown if the given datasource name cannot be found in the fetched data.
	 */
	public Aggregates getAggregates(String dsName) throws RrdException {
		DataProcessor dataProcessor = createDataProcessor(null);
		return dataProcessor.getAggregates(dsName);
	}

	/**
	 * Returns all aggregated values for a set of values calculated by applying an RPN expression to the
	 * fetched data. For example, if you have two datasources named <code>x</code> and <code>y</code>
	 * in this FetchData and you want to calculate MIN, MAX, LAST, FIRST, AVERAGE and TOTAL value
	 * of <code>(x+y)/2<code> use something like: <p>
	 * <code>getRpnAggregates("x,y,+,2,/");</code><p>
	 *
	 * @param rpnExpression RRDTool-like RPN expression
	 * @return Object containing all aggregated values
	 * @throws RrdException Thrown if invalid RPN expression is supplied
	 */
	public Aggregates getRpnAggregates(String rpnExpression) throws RrdException, IOException {
		DataProcessor dataProcessor = createDataProcessor(rpnExpression);
		return dataProcessor.getAggregates(RPN_SOURCE_NAME);
	}

	/**
	 * Used by ISPs which charge for bandwidth utilization on a "95th percentile" basis.<p>
	 * <p/>
	 * The 95th percentile is the highest source value left when the top 5% of a numerically sorted set
	 * of source data is discarded. It is used as a measure of the peak value used when one discounts
	 * a fair amount for transitory spikes. This makes it markedly different from the average.<p>
	 * <p/>
	 * Read more about this topic at:<p>
	 * <a href="http://www.red.net/support/resourcecentre/leasedline/percentile.php">Rednet</a> or<br>
	 * <a href="http://www.bytemark.co.uk/support/tech/95thpercentile.html">Bytemark</a>.
	 *
	 * @param dsName Datasource name
	 * @return 95th percentile of fetched source values
	 * @throws RrdException Thrown if invalid source name is supplied
	 */
	public double get95Percentile(String dsName) throws RrdException {
		DataProcessor dataProcessor = createDataProcessor(null);
		return dataProcessor.get95Percentile(dsName);
	}

	/**
	 * Same as {@link #get95Percentile(String)}, but for a set of values calculated with the given
	 * RPN expression.
	 *
	 * @param rpnExpression RRDTool-like RPN expression
	 * @return 95-percentile
	 * @throws RrdException Thrown if invalid RPN expression is supplied
	 */
	public double getRpn95Percentile(String rpnExpression) throws RrdException {
		DataProcessor dataProcessor = createDataProcessor(rpnExpression);
		return dataProcessor.get95Percentile(RPN_SOURCE_NAME);
	}

	/**
	 * Dumps fetch data to output stream in XML format.
	 *
	 * @param outputStream Output stream to dump fetch data to
	 * @throws IOException Thrown in case of I/O error
	 */
	public void exportXml(OutputStream outputStream) throws IOException {
		XmlWriter writer = new XmlWriter(outputStream);
		writer.startTag("fetch_data");
		writer.startTag("request");
		writer.writeTag("file", request.getParentDb().getPath());
		writer.writeComment(Util.getDate(request.getFetchStart()));
		writer.writeTag("start", request.getFetchStart());
		writer.writeComment(Util.getDate(request.getFetchEnd()));
		writer.writeTag("end", request.getFetchEnd());
		writer.writeTag("resolution", request.getResolution());
		writer.writeTag("cf", request.getConsolFun());
		writer.closeTag(); // request
		writer.startTag("datasources");
		for (String dsName : dsNames) {
			writer.writeTag("name", dsName);
		}
		writer.closeTag(); // datasources
		writer.startTag("data");
		for (int i = 0; i < timestamps.length; i++) {
			writer.startTag("row");
			writer.writeComment(Util.getDate(timestamps[i]));
			writer.writeTag("timestamp", timestamps[i]);
			writer.startTag("values");
			for (int j = 0; j < dsNames.length; j++) {
				writer.writeTag("v", values[j][i]);
			}
			writer.closeTag(); // values
			writer.closeTag(); // row
		}
		writer.closeTag(); // data
		writer.closeTag(); // fetch_data
		writer.flush();
	}

	/**
	 * Dumps fetch data to file in XML format.
	 *
	 * @param filepath Path to destination file
	 * @throws IOException Thrown in case of I/O error
	 */
	public void exportXml(String filepath) throws IOException {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(filepath);
			exportXml(outputStream);
		}
		finally {
			if (outputStream != null) {
				outputStream.close();
			}
		}
	}

	/**
	 * Dumps fetch data in XML format.
	 *
	 * @return String containing XML formatted fetch data
	 * @throws IOException Thrown in case of I/O error
	 */
	public String exportXml() throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		exportXml(outputStream);
		return outputStream.toString();
	}

	/**
	 * Returns the step of the corresponding RRA archive
	 *
	 * @return Archive step in seconds
	 */
	public long getArcStep() {
		return arcStep;
	}

	/**
	 * Returns the timestamp of the last populated slot in the corresponding RRA archive
	 *
	 * @return Timestamp in seconds
	 */
	public long getArcEndTime() {
		return arcEndTime;
	}

	private DataProcessor createDataProcessor(String rpnExpression) throws RrdException {
		DataProcessor dataProcessor = new DataProcessor(request.getFetchStart(), request.getFetchEnd());
		for (String dsName : dsNames) {
			dataProcessor.addDatasource(dsName, this);
		}
		if (rpnExpression != null) {
			dataProcessor.addDatasource(RPN_SOURCE_NAME, rpnExpression);
			try {
				dataProcessor.processData();
			}
			catch (IOException ioe) {
				// highly unlikely, since all datasources have already calculated values
				throw new RuntimeException("Impossible error: " + ioe);
			}
		}
		return dataProcessor;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -