private double getAverage(String dsName, String rpnExpression) throws RrdException {
		RpnCalculator rpnCalculator = null;
		if(rpnExpression != null) {
			rpnCalculator = new RpnCalculator(rpnExpression);
		}
		double vals[] = getValues(dsName);
		double totalVal = 0;
		long totalSecs = 0;
		for(int i = 0; i < vals.length - 1; i++) {
			long t1 = Math.max(request.getFetchStart(), timestamps[i]);
			long t2 = Math.min(request.getFetchEnd(), timestamps[i + 1]);
			double value = vals[i + 1];
			if(rpnCalculator != null) {
				rpnCalculator.setValue(value);
				value = rpnCalculator.calculate();
			}
			if(!Double.isNaN(value)) {
                totalSecs += (t2 - t1);
				totalVal += (t2 - t1) * value;
			}
		}
		return totalSecs > 0? totalVal / totalSecs: Double.NaN;
	}