1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.collector.impl.persistence; |
23 |
|
|
24 |
|
import java.io.ByteArrayInputStream; |
25 |
|
import java.io.ByteArrayOutputStream; |
26 |
|
import java.io.IOException; |
27 |
|
import java.io.InputStream; |
28 |
|
import java.io.ObjectInputStream; |
29 |
|
import java.io.ObjectOutputStream; |
30 |
|
import java.lang.reflect.InvocationTargetException; |
31 |
|
import java.math.BigDecimal; |
32 |
|
import java.sql.PreparedStatement; |
33 |
|
import java.sql.ResultSet; |
34 |
|
import java.sql.SQLException; |
35 |
|
import java.sql.Timestamp; |
36 |
|
import java.util.ArrayList; |
37 |
|
import java.util.Collection; |
38 |
|
import java.util.Date; |
39 |
|
import java.util.HashMap; |
40 |
|
import java.util.Iterator; |
41 |
|
import java.util.List; |
42 |
|
import java.util.Map; |
43 |
|
|
44 |
|
import javax.sql.DataSource; |
45 |
|
|
46 |
|
import net.sf.infrared.base.model.AggregateExecutionTime; |
47 |
|
import net.sf.infrared.base.model.AggregateOperationTree; |
48 |
|
import net.sf.infrared.base.model.ApplicationStatistics; |
49 |
|
import net.sf.infrared.base.model.ExecutionContext; |
50 |
|
import net.sf.infrared.base.model.LayerTime; |
51 |
|
import net.sf.infrared.base.model.StatisticsSnapshot; |
52 |
|
import net.sf.infrared.base.util.LoggingFactory; |
53 |
|
import net.sf.infrared.base.util.Tree; |
54 |
|
import net.sf.infrared.collector.ApplicationStatisticsDao; |
55 |
|
import org.apache.log4j.Logger; |
56 |
|
import org.springframework.dao.DataAccessException; |
57 |
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
58 |
|
import org.springframework.jdbc.core.PreparedStatementSetter; |
59 |
|
import org.springframework.jdbc.core.ResultSetExtractor; |
60 |
|
import org.springframework.jdbc.core.support.JdbcDaoSupport; |
61 |
|
|
62 |
0 |
public class ApplicationStatisticsDaoImpl |
63 |
|
extends JdbcDaoSupport implements ApplicationStatisticsDao { |
64 |
|
|
65 |
|
private static final String SQL_INSERT_LAYER_TIME = |
66 |
|
"insert into LAYER_TIMES (APP_NAME, HOST_NAME, LAYER, DURATION, INSERT_TIME) " + |
67 |
|
"values(?, ?, ?, ?, ?)"; |
68 |
|
|
69 |
|
private static final String SQL_INSERT_AGGREGATE_EXECUTION_TIME = |
70 |
|
"insert into EXECUTION_TIMES(APP_NAME, HOST_NAME, LAYER, CLASS_NAME, NAME, EXEC_COUNT, " |
71 |
|
+ "TOT_INCLUSIVE_TIME, MAX_INCLUSIVE_TIME, MIN_INCLUSIVE_TIME, TOT_EXCLUSIVE_TIME, " |
72 |
|
+ "MAX_EXCLUSIVE_TIME, MIN_EXCLUSIVE_TIME, TIME_OF_FST_EXEC, TIME_OF_LST_EXEC, " |
73 |
|
+ "INCLUSIVE_FST_EXEC_TIME, INCLUSIVE_LST_EXEC_TIME, EXCLUSIVE_FST_EXEC_TIME, " |
74 |
|
+ "EXCLUSIVE_LST_EXEC_TIME, HIERARCHICAL_LAYER, INSERT_TIME) " |
75 |
|
+ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
76 |
|
|
77 |
|
private static final String SQL_INSERT_TREE = |
78 |
|
"insert into AGG_OPERATION_TREE(APP_NAME, HOST_NAME, TREE, INSERT_TIME) " |
79 |
|
+ "values(?, ?, ?, ?)"; |
80 |
|
|
81 |
|
private static final String SQL_FETCH_LAYER_TIME_BASE = |
82 |
|
"select * from LAYER_TIMES " + |
83 |
|
"WHERE ( INSERT_TIME > ? AND INSERT_TIME < ?) AND (APP_NAME IN ("; |
84 |
|
|
85 |
|
private static final String SQL_FETCH_AGGREGATE_EXECUTION_TIME = |
86 |
|
"select * from EXECUTION_TIMES " + |
87 |
|
"WHERE ( INSERT_TIME > ? AND INSERT_TIME < ?) AND (APP_NAME IN ("; |
88 |
|
|
89 |
|
private static final String SQL_FETCH_TREE = |
90 |
|
"select * from AGG_OPERATION_TREE " + |
91 |
|
"where ( INSERT_TIME > ? AND INSERT_TIME < ?) AND (APP_NAME IN ("; |
92 |
|
|
93 |
|
private static final int MAX_LENGTH_OF_NAME = 1000; |
94 |
|
|
95 |
3 |
private static final Logger log = LoggingFactory.getLogger(ApplicationStatisticsDaoImpl.class); |
96 |
|
|
97 |
|
private int argumentCount; |
98 |
|
|
99 |
|
private DataSource dataSource; |
100 |
|
|
101 |
4 |
public ApplicationStatisticsDaoImpl(DataSource ds) { |
102 |
4 |
setDataSource(ds); |
103 |
4 |
this.dataSource = ds; |
104 |
4 |
} |
105 |
|
|
106 |
1 |
public ApplicationStatisticsDaoImpl() { |
107 |
1 |
} |
108 |
|
|
109 |
|
public void saveStatistics(ApplicationStatistics[] stats) { |
110 |
0 |
for (int i = 0; i < stats.length; i++) { |
111 |
0 |
saveStatistics(stats[i]); |
112 |
|
} |
113 |
0 |
} |
114 |
|
|
115 |
|
public void saveStatistics(ApplicationStatistics stats) { |
116 |
2 |
if (stats == null) { |
117 |
1 |
throw new IllegalArgumentException("The ApplicationStatistics to be saved is null"); |
118 |
|
} |
119 |
1 |
if (log.isDebugEnabled()) { |
120 |
0 |
log.debug("Going to save " + stats + " to DB"); |
121 |
|
} |
122 |
|
|
123 |
1 |
synchronized (stats) { |
124 |
1 |
saveLayerTimes(stats); |
125 |
1 |
saveExecutionTimes(stats); |
126 |
1 |
saveAggregateOperationTree(stats); |
127 |
1 |
} |
128 |
|
|
129 |
1 |
if (log.isDebugEnabled()) { |
130 |
0 |
log.debug("Saved statistics " + stats + " to DB"); |
131 |
|
} |
132 |
1 |
} |
133 |
|
|
134 |
|
public StatisticsSnapshot fetchStatistics(Collection appNames, |
135 |
|
Collection instanceIds, Date fromDate, Date toDate) { |
136 |
2 |
if ( (appNames == null) || (appNames.isEmpty()) |
137 |
|
|| (instanceIds == null) || (instanceIds.isEmpty()) ) { |
138 |
1 |
return null; |
139 |
|
} |
140 |
1 |
Collection results = null; |
141 |
|
|
142 |
1 |
Object[] queryAndArgs = |
143 |
|
getQueryAndArgsForFetchingExecutionTimes(appNames, instanceIds, fromDate, toDate); |
144 |
1 |
results = getJdbcTemplate().queryForList((String) queryAndArgs[0], (Object[]) queryAndArgs[1]); |
145 |
1 |
Map executionTimes = getLayerToExecutionTimeMap(results); |
146 |
|
|
147 |
1 |
queryAndArgs = getQueryAndArgsForFetchingLayerTimes(appNames, instanceIds, fromDate, toDate); |
148 |
1 |
results = getJdbcTemplate().queryForList((String) queryAndArgs[0], (Object[]) queryAndArgs[1]); |
149 |
1 |
Map layerTimes = getLayerTimesMap(results); |
150 |
|
|
151 |
1 |
AggregateOperationTree aggOperTree = |
152 |
|
fetchAggregateOperationTree(appNames, instanceIds, fromDate, toDate); |
153 |
|
|
154 |
1 |
return StatisticsSnapshot.createSnapshot(appNames, instanceIds, layerTimes, |
155 |
|
executionTimes, aggOperTree, fromDate.getTime(), toDate.getTime()); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
public DataSource getDaoDataSource() { |
160 |
1 |
return this.dataSource; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
Object[] getQueryAndArgsForFetchingExecutionTimes(Collection appNames, |
168 |
|
Collection instanceIds, Date fromDate, Date toDate) { |
169 |
2 |
Object[] stringAndArray = getWhereStringAndArgArray(appNames, instanceIds, fromDate, toDate); |
170 |
2 |
String queryWhereClause = (String) stringAndArray[0]; |
171 |
2 |
Object[] argArray = (Object[]) stringAndArray[1]; |
172 |
|
|
173 |
2 |
return new Object[] { SQL_FETCH_AGGREGATE_EXECUTION_TIME + queryWhereClause, argArray }; |
174 |
|
} |
175 |
|
|
176 |
|
Object[] getQueryAndArgsForFetchingLayerTimes(Collection appNames, |
177 |
|
Collection instanceIds, Date fromDate, Date toDate) { |
178 |
1 |
Object[] stringAndArray = getWhereStringAndArgArray(appNames, instanceIds, fromDate, toDate); |
179 |
1 |
String queryWhereClause = (String) stringAndArray[0]; |
180 |
1 |
Object[] argArray = (Object[]) stringAndArray[1]; |
181 |
|
|
182 |
1 |
return new Object[] { SQL_FETCH_LAYER_TIME_BASE + queryWhereClause, argArray }; |
183 |
|
} |
184 |
|
|
185 |
|
Object[] getWhereStringAndArgArray(Collection appNames, |
186 |
|
Collection instanceIds, Date fromDate, Date toDate) { |
187 |
4 |
argumentCount = 1; |
188 |
4 |
Object[] argArray = getArgumentArray(fromDate, toDate, appNames.size() + instanceIds.size()); |
189 |
4 |
StringBuffer query = new StringBuffer(); |
190 |
|
|
191 |
4 |
Object[] results = processQueryAndArgArrayForCollection(argArray, query, appNames); |
192 |
4 |
query = (StringBuffer) results[0]; |
193 |
4 |
argArray = (Object[]) results[1]; |
194 |
4 |
query.append(") AND HOST_NAME IN ("); |
195 |
|
|
196 |
4 |
results = processQueryAndArgArrayForCollection(argArray, query, instanceIds); |
197 |
4 |
query = (StringBuffer) results[0]; |
198 |
4 |
argArray = (Object[]) results[1]; |
199 |
4 |
query.append(") )"); |
200 |
|
|
201 |
4 |
results[0] = query.toString(); |
202 |
4 |
results[1] = argArray; |
203 |
|
|
204 |
4 |
return results; |
205 |
|
} |
206 |
|
|
207 |
|
Object[] processQueryAndArgArrayForCollection(Object[] argArray, StringBuffer query, |
208 |
|
Collection appNames) { |
209 |
8 |
int length = 0; |
210 |
8 |
Object[] returnValues = new Object[2]; |
211 |
|
|
212 |
8 |
for (Iterator iter = appNames.iterator(); iter.hasNext();) { |
213 |
7 |
String element = (String) iter.next(); |
214 |
7 |
argArray[++argumentCount] = element; |
215 |
7 |
query.append("?, "); |
216 |
|
} |
217 |
8 |
length = query.length(); |
218 |
8 |
query.delete(length - 2, length - 1); |
219 |
8 |
returnValues[0] = query; |
220 |
8 |
returnValues[1] = argArray; |
221 |
|
|
222 |
8 |
return class="keyword">returnValues; |
223 |
|
} |
224 |
|
|
225 |
|
Map getLayerTimesMap(Collection results) { |
226 |
1 |
String layerName = null; |
227 |
1 |
LayerTime mapLayerTime = null; |
228 |
1 |
long duration = 0; |
229 |
1 |
Map layerTimesMap = new HashMap(); |
230 |
|
|
231 |
1 |
for (Iterator iter = results.iterator(); iter.hasNext();) { |
232 |
3 |
Map element = (Map) iter.next(); |
233 |
3 |
layerName = (String) element.get("LAYER"); |
234 |
|
|
235 |
3 |
if (layerTimesMap.get(layerName) == null) |
236 |
3 |
layerTimesMap.put(layerName, constructLayerTime(element)); |
237 |
|
else { |
238 |
0 |
mapLayerTime = (LayerTime) layerTimesMap.get(layerName); |
239 |
0 |
duration = ((BigDecimal) element.get("DURATION")).longValue(); |
240 |
0 |
mapLayerTime.addToTime(duration); |
241 |
|
} |
242 |
|
} |
243 |
1 |
return layerTimesMap; |
244 |
|
} |
245 |
|
|
246 |
|
Map getLayerToExecutionTimeMap(Collection results) { |
247 |
1 |
Map layerToAetsMap = new HashMap(); |
248 |
1 |
Class[] argumentsClass = new Class[] { String.class, String.class }; |
249 |
1 |
Object[] argumentObjects = new Object[2]; |
250 |
|
|
251 |
1 |
for (Iterator iter = results.iterator(); iter.hasNext();) { |
252 |
5 |
Map element = (Map) iter.next(); |
253 |
5 |
String name = (String) element.get("NAME"); |
254 |
5 |
String ctxLayer = (String) element.get("LAYER"); |
255 |
5 |
String className = (String) element.get("CLASS_NAME"); |
256 |
|
|
257 |
5 |
argumentObjects[0] = name; |
258 |
5 |
argumentObjects[1] = ctxLayer; |
259 |
5 |
ExecutionContext newContext = |
260 |
|
getNewExecContextFromName(className, argumentsClass, argumentObjects); |
261 |
|
|
262 |
5 |
if (newContext == null) { |
263 |
0 |
log.debug("Ignoring one row because we could not create the ExecutionContext for it"); |
264 |
0 |
continue; |
265 |
|
} |
266 |
5 |
String layerName = (String) element.get("HIERARCHICAL_LAYER"); |
267 |
|
|
268 |
|
|
269 |
5 |
if (layerName == null) { |
270 |
5 |
log.debug("Ignoring one row because there is no hierarchical layer info for it"); |
271 |
5 |
continue; |
272 |
|
} |
273 |
0 |
AggregateExecutionTime aet = getAggregateExecutionTime(element, newContext); |
274 |
0 |
List aetsInTheLayer = (List) layerToAetsMap.get(layerName); |
275 |
0 |
if (aetsInTheLayer == null) { |
276 |
0 |
aetsInTheLayer = new ArrayList(); |
277 |
0 |
layerToAetsMap.put(layerName, aetsInTheLayer); |
278 |
|
} |
279 |
0 |
aetsInTheLayer.add(aet); |
280 |
|
} |
281 |
|
|
282 |
1 |
return layerToAetsMap; |
283 |
|
} |
284 |
|
|
285 |
|
ExecutionContext getNewExecContextFromName(String className, |
286 |
|
Class[] argClasses, Object[] args) { |
287 |
|
|
288 |
5 |
ExecutionContext ctx = null; |
289 |
|
|
290 |
|
try { |
291 |
5 |
Class ctxClass = Class.forName(className); |
292 |
5 |
ctx = (ExecutionContext) ctxClass.getConstructor(argClasses).newInstance(args); |
293 |
0 |
} catch (NoSuchMethodException ex) { |
294 |
0 |
logger.error("The two arguments constructor for " + className + " doesn't exist", ex); |
295 |
0 |
return null; |
296 |
0 |
} catch (InstantiationException ex) { |
297 |
0 |
logger.error("The class" + className + "cannot be instantiated", ex); |
298 |
0 |
return null; |
299 |
0 |
} catch (InvocationTargetException ex) { |
300 |
0 |
logger.error("InvocationTargetException in class" + className, ex); |
301 |
0 |
return null; |
302 |
0 |
} catch (IllegalAccessException ex) { |
303 |
0 |
logger.error("IllegalAccessException on constructor of class" + className, ex); |
304 |
0 |
return null; |
305 |
0 |
} catch (ClassNotFoundException ex) { |
306 |
0 |
logger.error("ClassNotFoundException on class" + className, ex); |
307 |
0 |
return null; |
308 |
5 |
} |
309 |
|
|
310 |
5 |
return ctx; |
311 |
|
} |
312 |
|
|
313 |
|
AggregateExecutionTime getAggregateExecutionTime(Map element, ExecutionContext context) { |
314 |
0 |
long class="keyword">longVal = 0; |
315 |
0 |
AggregateExecutionTime aggExecTime = new AggregateExecutionTime(context); |
316 |
|
|
317 |
0 |
longVal = ((BigDecimal) element.get("EXEC_COUNT")).longValue(); |
318 |
0 |
aggExecTime.setExecutionCount((int) longVal); |
319 |
|
|
320 |
0 |
longVal = ((BigDecimal) element.get("EXCLUSIVE_FST_EXEC_TIME")).longValue(); |
321 |
0 |
aggExecTime.setExclusiveFirstExecutionTime(longVal); |
322 |
|
|
323 |
0 |
longVal = ((BigDecimal) element.get("EXCLUSIVE_LST_EXEC_TIME")).longValue(); |
324 |
0 |
aggExecTime.setExclusiveLastExecutionTime(longVal); |
325 |
|
|
326 |
0 |
longVal = ((BigDecimal) element.get("INCLUSIVE_FST_EXEC_TIME")).longValue(); |
327 |
0 |
aggExecTime.setInclusiveFirstExecutionTime(longVal); |
328 |
|
|
329 |
0 |
longVal = ((BigDecimal) element.get("INCLUSIVE_LST_EXEC_TIME")).longValue(); |
330 |
0 |
aggExecTime.setInclusiveLastExecutionTime(longVal); |
331 |
|
|
332 |
0 |
longVal = ((BigDecimal) element.get("MAX_EXCLUSIVE_TIME")).longValue(); |
333 |
0 |
aggExecTime.setMaxExclusiveTime(longVal); |
334 |
|
|
335 |
0 |
longVal = ((BigDecimal) element.get("MAX_INCLUSIVE_TIME")).longValue(); |
336 |
0 |
aggExecTime.setMaxInclusiveTime(longVal); |
337 |
|
|
338 |
0 |
longVal = ((BigDecimal) element.get("MIN_EXCLUSIVE_TIME")).longValue(); |
339 |
0 |
aggExecTime.setMinExclusiveTime(longVal); |
340 |
|
|
341 |
0 |
longVal = ((BigDecimal) element.get("MIN_INCLUSIVE_TIME")).longValue(); |
342 |
0 |
aggExecTime.setMinInclusiveTime(longVal); |
343 |
|
|
344 |
0 |
longVal = ((BigDecimal) element.get("TIME_OF_FST_EXEC")).longValue(); |
345 |
0 |
aggExecTime.setTimeOfFirstExecution(longVal); |
346 |
|
|
347 |
0 |
longVal = ((BigDecimal) element.get("TIME_OF_LST_EXEC")).longValue(); |
348 |
0 |
aggExecTime.setTimeOfLastExecution(longVal); |
349 |
|
|
350 |
0 |
longVal = ((BigDecimal) element.get("TOT_EXCLUSIVE_TIME")).longValue(); |
351 |
0 |
aggExecTime.setTotalExclusiveTime(longVal); |
352 |
|
|
353 |
0 |
longVal = ((BigDecimal) element.get("TOT_INCLUSIVE_TIME")).longValue(); |
354 |
0 |
aggExecTime.setTotalInclusiveTime(longVal); |
355 |
|
|
356 |
0 |
String layerName = ((String) element.get("HIERARCHICAL_LAYER")); |
357 |
0 |
aggExecTime.setLayerName(layerName); |
358 |
|
|
359 |
0 |
return aggExecTime; |
360 |
|
} |
361 |
|
|
362 |
|
LayerTime constructLayerTime(Map elements) { |
363 |
3 |
long duration = ((BigDecimal) elements.get("DURATION")).longValue(); |
364 |
|
|
365 |
3 |
LayerTime layerTime = new LayerTime((String) elements.get("LAYER")); |
366 |
3 |
layerTime.setTime(duration); |
367 |
|
|
368 |
3 |
return layerTime; |
369 |
|
} |
370 |
|
|
371 |
|
void saveExecutionTimes(final ApplicationStatistics stats) { |
372 |
1 |
final String appName = stats.getApplicationName(); |
373 |
1 |
final String instanceId = stats.getInstanceId(); |
374 |
|
|
375 |
1 |
final String[] layers = stats.getLayers(); |
376 |
4 |
for (int i = 0; i <layers.length; i++) { |
377 |
3 |
final AggregateExecutionTime[] aets = stats.getExecutionsInLayer(layers[i]); |
378 |
3 |
final String layerName = layers[i]; |
379 |
3 |
getJdbcTemplate().batchUpdate(SQL_INSERT_AGGREGATE_EXECUTION_TIME, |
380 |
|
new BatchPreparedStatementSetter() { |
381 |
|
public int getBatchSize() { |
382 |
|
return aets.length; |
383 |
|
} |
384 |
|
|
385 |
|
public void setValues(PreparedStatement ps, int j) throws SQLException { |
386 |
|
String name = aets[j].getContext().getName(); |
387 |
|
|
388 |
|
ps.setString(1, appName); |
389 |
|
ps.setString(2, instanceId); |
390 |
|
ps.setString(3, aets[j].getContext().getLayer()); |
391 |
|
ps.setString(4, aets[j].getContext().getClass().getName()); |
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
if(name!= null && name.length() > MAX_LENGTH_OF_NAME) { |
396 |
|
name = name.substring(0, MAX_LENGTH_OF_NAME - 1); |
397 |
|
log.debug("The name element : " + name + " of the " + aets[j].getContext() + |
398 |
|
" context exceeded its maximum allotted length of " + MAX_LENGTH_OF_NAME + |
399 |
|
" and shall be trimmed down to the permitted size before persisting to database."); |
400 |
|
} |
401 |
|
ps.setString(5, name); |
402 |
|
|
403 |
|
|
404 |
|
ps.setLong(6, aets[j].getExecutionCount()); |
405 |
|
ps.setLong(7, aets[j].getTotalInclusiveTime()); |
406 |
|
ps.setLong(8, aets[j].getMaxInclusiveTime()); |
407 |
|
ps.setLong(9, aets[j].getMinInclusiveTime()); |
408 |
|
ps.setLong(10, aets[j].getTotalExclusiveTime()); |
409 |
|
ps.setLong(11, aets[j].getMaxExclusiveTime()); |
410 |
|
ps.setLong(12, aets[j].getMinExclusiveTime()); |
411 |
|
ps.setLong(13, aets[j].getTimeOfFirstExecution()); |
412 |
|
ps.setLong(14, aets[j].getTimeOfLastExecution()); |
413 |
|
ps.setLong(15, aets[j].getInclusiveFirstExecutionTime()); |
414 |
|
ps.setLong(16, aets[j].getInclusiveLastExecutionTime()); |
415 |
|
ps.setLong(17, aets[j].getExclusiveFirstExecutionTime()); |
416 |
|
ps.setLong(18, aets[j].getExclusiveLastExecutionTime()); |
417 |
|
ps.setString(19, aets[j].getLayerName()); |
418 |
|
ps.setTimestamp(20, new Timestamp(System.currentTimeMillis())); |
419 |
|
} |
420 |
|
}); |
421 |
3 |
if(log.isDebugEnabled()) { |
422 |
0 |
log.debug("Scheduled batch update for saving " + aets.length + |
423 |
|
" executions times in layer " + layerName + " to DB; stats=" + stats); |
424 |
|
} |
425 |
|
} |
426 |
1 |
if (log.isDebugEnabled()) { |
427 |
0 |
log.debug("Saved all execution times of " + layers.length + |
428 |
|
" layers in " + stats + " to DB"); |
429 |
|
} |
430 |
1 |
} |
431 |
|
|
432 |
|
void saveLayerTimes(final ApplicationStatistics stats) { |
433 |
1 |
final String appName = stats.getApplicationName(); |
434 |
1 |
final String instanceId = stats.getInstanceId(); |
435 |
|
|
436 |
1 |
final String[] layers = stats.getLayers(); |
437 |
|
|
438 |
1 |
getJdbcTemplate().batchUpdate(SQL_INSERT_LAYER_TIME, new BatchPreparedStatementSetter() { |
439 |
|
public int getBatchSize() { |
440 |
|
return layers.length; |
441 |
|
} |
442 |
|
|
443 |
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
444 |
|
ps.setString(1, appName); |
445 |
|
ps.setString(2, instanceId); |
446 |
|
ps.setString(3, layers[i]); |
447 |
|
ps.setLong(4, stats.getTimeInLayer(layers[i])); |
448 |
|
ps.setTimestamp(5, new Timestamp(System.currentTimeMillis())); |
449 |
|
} |
450 |
|
}); |
451 |
1 |
if (log.isDebugEnabled()) { |
452 |
0 |
log.debug("Saved " + layers.length + " layer times in " + stats + " to DB"); |
453 |
|
} |
454 |
1 |
} |
455 |
|
|
456 |
|
void saveAggregateOperationTree(ApplicationStatistics stats) { |
457 |
1 |
String appName = stats.getApplicationName(); |
458 |
1 |
String instanceId = stats.getInstanceId(); |
459 |
1 |
AggregateOperationTree tree = stats.getTree(); |
460 |
1 |
if (tree == null || tree.getAggregateTree() == class="keyword">null) |
461 |
0 |
return; |
462 |
|
|
463 |
1 |
insertTree(appName, instanceId, tree.getAggregateTree()); |
464 |
1 |
if (log.isDebugEnabled()) { |
465 |
0 |
log.debug("Saved aggregate operation tree in " + stats + " to DB"); |
466 |
|
} |
467 |
1 |
} |
468 |
|
|
469 |
|
|
470 |
|
AggregateOperationTree fetchAggregateOperationTree(Collection appNames, |
471 |
|
Collection instanceIds, Date fromDate, Date toDate) { |
472 |
1 |
Object[] stringAndArray = getWhereStringAndArgArray(appNames, instanceIds, fromDate, toDate); |
473 |
1 |
String whereClause = (String) stringAndArray[0]; |
474 |
1 |
Object[] argArray = (Object[]) stringAndArray[1]; |
475 |
|
|
476 |
|
|
477 |
1 |
AggregateOperationTree aggOpTree = (AggregateOperationTree) getJdbcTemplate().query( |
478 |
|
SQL_FETCH_TREE + whereClause, argArray, new ResultSetExtractor() { |
479 |
|
public Object extractData(ResultSet rs) throws SQLException, DataAccessException { |
480 |
|
AggregateOperationTree newTree = null; |
481 |
|
AggregateOperationTree aggOpTree = new AggregateOperationTree(); |
482 |
|
while(rs.next()) { |
483 |
|
try { |
484 |
|
InputStream is = rs.getBinaryStream("TREE"); |
485 |
|
ObjectInputStream ois = new ObjectInputStream(is); |
486 |
|
Tree tree = (Tree) ois.readObject(); |
487 |
|
newTree = new AggregateOperationTree(); |
488 |
|
newTree.setAggregateTree(tree); |
489 |
|
aggOpTree.merge(newTree); |
490 |
|
}catch(IOException e) { |
491 |
|
e.printStackTrace(); |
492 |
|
}catch(ClassNotFoundException ex) { |
493 |
|
ex.printStackTrace(); |
494 |
|
} |
495 |
|
} |
496 |
|
return aggOpTree; |
497 |
|
} |
498 |
|
}); |
499 |
|
|
500 |
1 |
return aggOpTree; |
501 |
|
} |
502 |
|
|
503 |
|
Object[] getArgumentArray(Date fromDate, Date toDate, int size) { |
504 |
4 |
Timestamp fromTimestamp = new Timestamp(fromDate.getTime()); |
505 |
4 |
Timestamp toTimeStamp = null; |
506 |
4 |
if (toDate != null) { |
507 |
3 |
toTimeStamp = new Timestamp(toDate.getTime()); |
508 |
|
} else { |
509 |
1 |
toTimeStamp = new Timestamp(System.currentTimeMillis()); |
510 |
|
} |
511 |
4 |
Object[] argArray = new Object[size + 2]; |
512 |
4 |
argArray[0] = fromTimestamp; |
513 |
4 |
argArray[1] = toTimeStamp; |
514 |
4 |
return argArray; |
515 |
|
} |
516 |
|
|
517 |
|
private void insertTree (final String appName, class="keyword">final String hostName, class="keyword">final Tree tree) { |
518 |
1 |
byte[] byteArray = null; |
519 |
|
try{ |
520 |
1 |
ByteArrayOutputStream baos = serializeObject(tree); |
521 |
1 |
byteArray = baos.toByteArray(); |
522 |
0 |
}catch(IOException e) { |
523 |
0 |
log.error("IOException : Unable to serialize the Aggregate Operation Tree Object"); |
524 |
1 |
} |
525 |
1 |
final ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); |
526 |
1 |
getJdbcTemplate().update(SQL_INSERT_TREE, new PreparedStatementSetter() { |
527 |
|
public void setValues(PreparedStatement ps) throws SQLException { |
528 |
|
ps.setString(1, appName); |
529 |
|
ps.setString(2, hostName); |
530 |
|
ps.setBinaryStream(3, bais, bais.available()); |
531 |
|
ps.setTimestamp(4, new Timestamp(System.currentTimeMillis())); |
532 |
|
} |
533 |
|
}); |
534 |
1 |
} |
535 |
|
|
536 |
|
private ByteArrayOutputStream serializeObject(Object obj) throws IOException { |
537 |
1 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
538 |
1 |
if (null != obj) { |
539 |
1 |
ObjectOutputStream out = new ObjectOutputStream(baos); |
540 |
1 |
out.writeObject(obj); |
541 |
1 |
out.flush(); |
542 |
|
} |
543 |
1 |
return baos; |
544 |
|
} |
545 |
|
|
546 |
|
} |