package org.jrobin.cmd;

import org.jrobin.core.RrdException;
import org.jrobin.core.RrdDb;
import org.jrobin.core.FetchRequest;
import org.jrobin.core.FetchData;

import java.io.IOException;

class RrdFetchCmd extends RrdToolCmd {
	static final String DEFAULT_START = "end-1day";
	static final String DEFAULT_END = "now";

	public RrdFetchCmd(RrdCmdScanner cmdScanner) {
		super(cmdScanner);
	}

	String getCmdType() {
		return "fetch";
	}

	Object execute() throws RrdException, IOException {
		// --start
		String startStr = cmdScanner.getOptionValue("s", "start", DEFAULT_START);
		TimeSpec spec1 = new TimeParser(startStr).parse();
		// --end
		String endStr = cmdScanner.getOptionValue("e", "end", DEFAULT_END);
		TimeSpec spec2 = new TimeParser(endStr).parse();
		long[] timestamps = TimeSpec.getTimestamps(spec1, spec2);
		// --resolution
		String resolutionStr = cmdScanner.getOptionValue("r", "resolution");
		long resolution = 1;
		if(resolutionStr != null) {
			resolution = parseLong(resolutionStr);
		}
		// other words
    	String[] tokens = cmdScanner.getRemainingWords();
		if(tokens.length != 3) {
			throw new RrdException("Invalid rrdfetch syntax");
		}
		String path = tokens[1];
		String consolFun = tokens[2];
		RrdDb rrdDb = getRrdDbReference(path);
		try {
			FetchRequest fetchRequest = rrdDb.createFetchRequest(
					consolFun, timestamps[0], timestamps[1], resolution);
			System.out.println(fetchRequest.dump());
			FetchData fetchData = fetchRequest.fetchData();
			println(fetchData.toString());
			return fetchData;
		}
		finally {
			releaseRrdDbReference(rrdDb);
		}
	}
}

@@ -1,8 +1,1185 @@
+/* ============================================================%0A * JRobin : Pure java implementation of RRDTool's functionality%0A * ============================================================%0A *%0A * Project Info:  http://www.jrobin.org%0A * Project Lead:  Sasa Markovic (saxon@jrobin.org);%0A *%0A * (C) Copyright 2003, by Sasa Markovic.%0A *%0A * Developers:    Sasa Markovic (saxon@jrobin.org)%0A *                Arne Vandamme (cobralord@jrobin.org)%0A *%0A * This library is free software; you can redistribute it and/or modify it under the terms%0A * of the GNU Lesser General Public License as published by the Free Software Foundation;%0A * either version 2.1 of the License, or (at your option) any later version.%0A *%0A * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;%0A * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.%0A * See the GNU Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public License along with this%0A * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,%0A * Boston, MA 02111-1307, USA.%0A */%0A%0A
 package 

add file header to RrdFetchCmd