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

📄 fetchdata.java

📁 jrobin,使用纯java实现的RRD数据库,使用RRD数据库来统计数据.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ============================================================
 * JRobin : Pure java implementation of RRDTool's functionality
 * ============================================================
 *
 * Project Info:  http://www.jrobin.org
 * Project Lead:  Sasa Markovic (saxon@jrobin.org);
 *
 * (C) Copyright 2003-2005, by Sasa Markovic.
 *
 * Developers:    Sasa Markovic (saxon@jrobin.org)
 *
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package org.jrobin.core;

import org.jrobin.data.Aggregates;
import org.jrobin.data.DataProcessor;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Class used to represent data fetched from the RRD.
 * Object of this class is created when the method
 * {@link FetchRequest#fetchData() fetchData()} is
 * called on a {@link FetchRequest FetchRequest} object.<p>
 * <p/>
 * Data returned from the RRD is, simply, just one big table filled with
 * timestamps and corresponding datasource values.
 * Use {@link #getRowCount() getRowCount()} method to count the number
 * of returned timestamps (table rows).<p>
 * <p/>
 * The first table column is filled with timestamps. Time intervals
 * between consecutive timestamps are guaranteed to be equal. Use
 * {@link #getTimestamps() getTimestamps()} method to get an array of
 * timestamps returned.<p>
 * <p/>
 * Remaining columns are filled with datasource values for the whole timestamp range,
 * on a column-per-datasource basis. Use {@link #getColumnCount() getColumnCount()} to find
 * the number of datasources and {@link #getValues(int) getValues(i)} method to obtain
 * all values for the i-th datasource. Returned datasource values correspond to
 * the values returned with {@link #getTimestamps() getTimestamps()} method.<p>
 */
public class FetchData implements ConsolFuns {
	// anything fuuny will do
	private static final String RPN_SOURCE_NAME = "WHERE THE SPEECHLES UNITE IN A SILENT ACCORD";

	private FetchRequest request;
	private String[] dsNames;
	private long[] timestamps;
	private double[][] values;

	private Archive matchingArchive;
	private long arcStep;
	private long arcEndTime;

	FetchData(Archive matchingArchive, FetchRequest request) throws IOException {
		this.matchingArchive = matchingArchive;
		this.arcStep = matchingArchive.getArcStep();
		this.arcEndTime = matchingArchive.getEndTime();
		this.dsNames = request.getFilter();
		if (this.dsNames == null) {
			this.dsNames = matchingArchive.getParentDb().getDsNames();
		}
		this.request = request;
	}

	void setTimestamps(long[] timestamps) {
		this.timestamps = timestamps;
	}

	void setValues(double[][] values) {
		this.values = values;
	}

	/**
	 * Returns the number of rows fetched from the corresponding RRD.
	 * Each row represents datasource values for the specific timestamp.
	 *
	 * @return Number of rows.
	 */
	public int getRowCount() {
		return timestamps.length;
	}

	/**
	 * Returns the number of columns fetched from the corresponding RRD.
	 * This number is always equal to the number of datasources defined
	 * in the RRD. Each column represents values of a single datasource.
	 *
	 * @return Number of columns (datasources).
	 */
	public int getColumnCount() {
		return dsNames.length;
	}

	/**
	 * Returns an array of timestamps covering the whole range specified in the
	 * {@link FetchRequest FetchReguest} object.
	 *
	 * @return Array of equidistant timestamps.
	 */
	public long[] getTimestamps() {
		return timestamps;
	}

	/**
	 * Returns the step with which this data was fetched.
	 *
	 * @return Step as long.
	 */
	public long getStep() {
		return timestamps[1] - timestamps[0];
	}

	/**
	 * Returns all archived values for a single datasource.
	 * Returned values correspond to timestamps
	 * returned with {@link #getTimestamps() getTimestamps()} method.
	 *
	 * @param dsIndex Datasource index.
	 * @return Array of single datasource values.
	 */
	public double[] getValues(int dsIndex) {
		return values[dsIndex];
	}

	/**
	 * Returns all archived values for all datasources.
	 * Returned values correspond to timestamps
	 * returned with {@link #getTimestamps() getTimestamps()} method.
	 *
	 * @return Two-dimensional aray of all datasource values.
	 */
	public double[][] getValues() {
		return values;
	}

	/**
	 * Returns all archived values for a single datasource.
	 * Returned values correspond to timestamps
	 * returned with {@link #getTimestamps() getTimestamps()} method.
	 *
	 * @param dsName Datasource name.
	 * @return Array of single datasource values.
	 * @throws RrdException Thrown if no matching datasource name is found.
	 */
	public double[] getValues(String dsName) throws RrdException {
		for (int dsIndex = 0; dsIndex < getColumnCount(); dsIndex++) {
			if (dsName.equals(dsNames[dsIndex])) {
				return getValues(dsIndex);
			}
		}
		throw new RrdException("Datasource [" + dsName + "] not found");
	}

	/**
	 * Returns a set of values created by applying 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 values for <code>(x+y)/2<code> use something like: <p>
	 * <code>getRpnValues("x,y,+,2,/");</code><p>
	 *
	 * @param rpnExpression RRDTool-like RPN expression
	 * @return Calculated values
	 * @throws RrdException Thrown if invalid RPN expression is supplied
	 */
	public double[] getRpnValues(String rpnExpression) throws RrdException {
		DataProcessor dataProcessor = createDataProcessor(rpnExpression);
		return dataProcessor.getValues(RPN_SOURCE_NAME);
	}

	/**
	 * Returns {@link FetchRequest FetchRequest} object used to create this FetchData object.
	 *
	 * @return Fetch request object.
	 */
	public FetchRequest getRequest() {
		return request;
	}

	/**
	 * Returns the first timestamp in this FetchData object.
	 *
	 * @return The smallest timestamp.
	 */
	public long getFirstTimestamp() {
		return timestamps[0];
	}

	/**
	 * Returns the last timestamp in this FecthData object.
	 *
	 * @return The biggest timestamp.
	 */
	public long getLastTimestamp() {
		return timestamps[timestamps.length - 1];
	}

	/**
	 * Returns Archive object which is determined to be the best match for the
	 * timestamps specified in the fetch request. All datasource values are obtained
	 * from round robin archives belonging to this archive.
	 *
	 * @return Matching archive.
	 */
	public Archive getMatchingArchive() {
		return matchingArchive;
	}

	/**
	 * Returns array of datasource names found in the corresponding RRD. If the request
	 * was filtered (data was fetched only for selected datasources), only datasources selected
	 * for fetching are returned.
	 *
	 * @return Array of datasource names.
	 */
	public String[] getDsNames() {
		return dsNames;
	}

	/**
	 * Retrieve the table index number of a datasource by name.  Names are case sensitive.
	 *
	 * @param dsName Name of the datasource for which to find the index.
	 * @return Index number of the datasources in the value table.
	 */
	public int getDsIndex(String dsName) {
		// Let's assume the table of dsNames is always small, so it is not necessary to use a hashmap for lookups
		for (int i = 0; i < dsNames.length; i++) {
			if (dsNames[i].equals(dsName)) {
				return i;
			}
		}
		return -1;		// Datasource not found !
	}

	/**
	 * 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();

⌨️ 快捷键说明

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