1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans.stats.collectors;
12
13 import jakarta.inject.Inject;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.TreeMap;
18
19 import org.jfree.data.xy.XYDataItem;
20
21 import psiprobe.Utils;
22 import psiprobe.beans.stats.listeners.StatsCollectionEvent;
23 import psiprobe.beans.stats.listeners.StatsCollectionListener;
24 import psiprobe.model.stats.StatsCollection;
25
26
27
28
29 public abstract class AbstractStatsCollectorBean {
30
31
32 @Inject
33 private StatsCollection statsCollection;
34
35
36 private int maxSeries = 240;
37
38
39 private List<StatsCollectionListener> listeners;
40
41
42 private final Map<String, Long> previousData = new TreeMap<>();
43
44
45 private final Map<String, Entry> previousData2D = new TreeMap<>();
46
47
48
49
50
51
52 public StatsCollection getStatsCollection() {
53 return statsCollection;
54 }
55
56
57
58
59
60
61 public void setStatsCollection(StatsCollection statsCollection) {
62 this.statsCollection = statsCollection;
63 }
64
65
66
67
68
69
70 public int getMaxSeries() {
71 return maxSeries;
72 }
73
74
75
76
77
78
79 public void setMaxSeries(int maxSeries) {
80 this.maxSeries = maxSeries;
81 }
82
83
84
85
86
87
88 public List<StatsCollectionListener> getListeners() {
89 return listeners;
90 }
91
92
93
94
95
96
97 public void setListeners(List<StatsCollectionListener> listeners) {
98 this.listeners = listeners;
99 }
100
101
102
103
104
105
106 public abstract void collect() throws Exception;
107
108
109
110
111
112
113
114
115
116
117
118 protected long buildDeltaStats(String name, long value) throws InterruptedException {
119 return buildDeltaStats(name, value, System.currentTimeMillis());
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 protected long buildDeltaStats(String name, long value, long time) throws InterruptedException {
134 long delta = 0;
135 if (statsCollection != null) {
136 long previousValue = Utils.toLong(previousData.get(name), 0);
137 delta = value - previousValue;
138 delta = delta > 0 ? delta : 0;
139 buildAbsoluteStats(name, delta, time);
140 previousData.put(name, value);
141 }
142 return delta;
143 }
144
145
146
147
148
149
150
151
152
153 protected void buildAbsoluteStats(String name, long value) throws InterruptedException {
154 buildAbsoluteStats(name, value, System.currentTimeMillis());
155 }
156
157
158
159
160
161
162
163
164
165
166 protected void buildAbsoluteStats(String name, long value, long time)
167 throws InterruptedException {
168
169 List<XYDataItem> stats = statsCollection.getStats(name);
170 if (stats == null) {
171 statsCollection.newStats(name, maxSeries);
172 } else {
173 XYDataItem data = new XYDataItem(time, value);
174 statsCollection.lockForUpdate();
175 try {
176 stats.add(data);
177 houseKeepStats(stats);
178 } finally {
179 statsCollection.releaseLock();
180 }
181 if (listeners != null) {
182 StatsCollectionEvent event = new StatsCollectionEvent(name, data);
183 for (StatsCollectionListener listener : listeners) {
184 if (listener.isEnabled()) {
185 listener.statsCollected(event);
186 }
187 }
188 }
189 }
190 }
191
192
193
194
195 private static class Entry {
196
197
198 long time;
199
200
201 long value;
202
203
204
205
206 public Entry() {
207
208 }
209
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 protected void buildTimePercentageStats(String name, long value, long time)
234 throws InterruptedException {
235
236 Entry entry = previousData2D.get(name);
237 if (entry == null) {
238 entry = new Entry();
239 entry.value = value;
240 entry.time = time;
241 previousData2D.put(name, entry);
242 } else {
243 double valueDelta = (double) value - entry.value;
244 double timeDelta = (double) time - entry.time;
245 double statValue = valueDelta * 100 / timeDelta;
246 statsCollection.lockForUpdate();
247 try {
248 List<XYDataItem> stats = statsCollection.getStats(name);
249 if (stats == null) {
250 stats = statsCollection.newStats(name, maxSeries);
251 }
252 stats.add(stats.size(), new XYDataItem(time, statValue));
253 houseKeepStats(stats);
254 } finally {
255 statsCollection.releaseLock();
256 }
257 }
258 }
259
260
261
262
263
264
265 protected void resetStats(String name) {
266 statsCollection.resetStats(name);
267 }
268
269
270
271
272
273
274 private void houseKeepStats(List<XYDataItem> stats) {
275 while (stats.size() > maxSeries) {
276 stats.remove(0);
277 }
278 }
279 }