Collector.java
2.8 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package se.lil.om;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
class Collector {
ArrayList<LongDelta> list = new ArrayList<LongDelta>();
String name = "";
Boolean debugLog = false;
public Collector(String name) {
this.name = name;
}
public void update(ResultSet rset) throws Throwable {
String timeStamp = null;
BufferedWriter writer = null;
if(debugLog) {
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
writer = new BufferedWriter(new FileWriter("Collector_" + name + "_" + timeStamp + ".txt"));
}
while(rset.next()) {
// Add try catch for issue #5
Timestamp ts = rset.getTimestamp(1);
String name = rset.getString(2);
BigDecimal value;
try {
value = rset.getBigDecimal(3);
}
catch (java.sql.SQLException e) {
value = BigDecimal.ZERO;
}
updateValue(ts, name, value);
if(writer != null) {
writer.write(ts + ":" + name + ":" + value);
writer.newLine();
}
}
if(writer != null) {
writer.close();
}
rset.close();
}
public void updateValue(Timestamp timestamp, String name, BigDecimal value) {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(list.get(x).matches(name)) {
myDelta = list.get(x);
}
}
if(myDelta != null) {
myDelta.update(value, timestamp);
} else {
myDelta = new LongDelta(name);
myDelta.update(value, timestamp);
list.add(myDelta);
}
}
public long getCurrentValue(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(list.get(x).matches(name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getCurrentValue();
else
return 0;
}
// public long getDelta(String name) throws Throwable {
// LongDelta myDelta = null;
// for(int x = 0; x<list.size() && myDelta == null; x++) {
// if(list.get(x).matches(name)) {
// myDelta = list.get(x);
// }
// }
// if(myDelta != null)
// return myDelta.getDelta();
// else
// return 0;
// }
public double getPerSecValue(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(list.get(x).matches(name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getPerSecondValue();
else
return 0;
}
public double getSeconds(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<list.size() && myDelta == null; x++) {
if(list.get(x).matches(name)) {
myDelta = list.get(x);
}
}
if(myDelta != null)
return myDelta.getSeconds();
else
return 0;
}
}