LongDelta.java
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 long 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
long numSeconds = (this.curFetch.getTime() - this.lastFetch.getTime()) / 1000;
long delta = this.curValue - this.lastValue;
return delta/numSeconds;
} else {
return 0;
}
}
public long getSeconds() throws Throwable {
if(this.lastFetch != null && this.curFetch != null) {
long numSeconds = (this.curFetch.getTime() - this.lastFetch.getTime()) / 1000;
return numSeconds;
}
return 0;
}
}