LongDelta.java 2.97 KB
package se.lil.om;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.Timestamp;

class LongDelta {
	public String name = null;
	public BigDecimal curValue = null;
	public BigDecimal lastValue = null;
	public Timestamp lastFetch = null;
	public Timestamp curFetch = null;
	boolean urProt = false; // Under-run protection
	boolean lastUpdCausedUR = false;
	public static BigDecimal bd1000 = new BigDecimal(1000);
	
	public LongDelta() {};
	public LongDelta(boolean UnderrunProtection) { this.urProt = UnderrunProtection; }
	
	public void update(ResultSet rset) throws Throwable{
		update(rset, 2, false, true);
	}
	public void update(ResultSet rset, boolean convert) throws Throwable{
		update(rset, 2, convert, true);
	}
	public void update(ResultSet rset, int pos, boolean convert) throws Throwable{
		update(rset, pos, convert, true);
	}
	public void update(ResultSet rset, int pos, boolean convert, boolean close) throws Throwable{
		rset.next();
		update(rset.getBigDecimal(pos), rset.getTimestamp(1), convert);
		if(close) rset.close(); 
	}
	
	public void update(BigDecimal value, Timestamp timestamp) {
		update(value, timestamp, false);
	}
	
	public void update(BigDecimal value, Timestamp timestamp, boolean convert) {
		if(convert) value = value.divide(bd1000, BigDecimal.ROUND_HALF_UP);
		if(urProt) {
			if(this.curValue != null && this.curValue.compareTo(value) > 0) {
				//This would be a negative delta. Ignore this data
				//System.out.println("Underrunprotection on " + name + " New Val: " + newval + " Old Val: " + this.curValue);
				lastUpdCausedUR = true; 
				return;
			}
		}
		this.lastFetch = this.curFetch;
		this.curFetch = timestamp;
		this.lastValue = this.curValue;
		this.curValue = value;
		lastUpdCausedUR = false;
	}
	
	public boolean didLastUnderRun() {
		return lastUpdCausedUR;
	}
	
	public double getPerSecondValue() throws Throwable {
		if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
			// We have values, calculate the number of gets per second
			double numMilliSeconds = this.curFetch.getTime() - this.lastFetch.getTime();
			double delta = this.curValue.subtract(this.lastValue).doubleValue();
			double perMilli = delta/numMilliSeconds;
			return perMilli * 1000;
		} else {
			return 0;
		}
	}
	public long getCurrentValue() throws Throwable {
		if(this.curValue != null) return this.curValue.longValue();
		else return 0;
	}
	public double getSeconds() throws Throwable {
		if(this.lastFetch != null && this.curFetch != null) {
			double numSeconds = this.curFetch.getTime() - this.lastFetch.getTime();
			return numSeconds/1000;
		}
		return 0;
	}
	public long getMilliSeconds() throws Throwable {
		if(this.lastFetch != null && this.curFetch != null) {
			return this.curFetch.getTime() - this.lastFetch.getTime();
		}
		return 0;
	}
	public long getDelta() throws Throwable {
		if(this.lastValue != null && this.curValue != null) {
			return this.curValue.subtract(this.lastValue).longValue();
		}
		return 0;
	}
}