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

import java.sql.ResultSet;
import java.sql.Timestamp;

class LongDelta {
	public String name = null;
	public Long curValue = null;
	public Long lastValue = null;
	public Timestamp lastFetch = null;
	public Timestamp curFetch = null;
	
	public void update(ResultSet rset) throws Throwable{
		update(rset, false);
	}
	public void update(ResultSet rset, boolean convert) throws Throwable{
		rset.next();
		this.lastFetch = this.curFetch;
		this.curFetch = rset.getTimestamp(1);
		this.lastValue = this.curValue;
		if(convert)
			this.curValue = rset.getLong(2)/1000;
		else
			this.curValue = rset.getLong(2);
		rset.close(); 
	}
	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 - this.lastValue;
			double perMilli = delta/numMilliSeconds;
			return perMilli * 1000;
		} else {
			return 0;
		}
	}
	public long getCurrentValue() throws Throwable {
		if(this.curValue != null) return this.curValue;
		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;
	}
}