001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.wso2.esb.statistics.persistence;
017:
018: import org.apache.synapse.statistics.Statistics;
019: import org.apache.synapse.statistics.StatisticsHolder;
020: import org.wso2.esb.ServiceBusConstants;
021: import org.wso2.esb.persistence.PersistenceManager;
022: import org.wso2.esb.persistence.dataobject.StatisticsDO;
023:
024: import java.util.List;
025:
026: /**
027: * The Util class to access STATISTICS table
028: */
029:
030: public class StatisticsDBUtils {
031:
032: private static final String SERVER_ID_ = "serverId";
033: private static final String CATEGORY_ = "category";
034: private static final String NAME_ = "name";
035: private static final String DIRECTION_ = "direction";
036:
037: private static StatisticsDO createStatisticsRecord(String serverId,
038: int statisticsCategory, String key, int direction,
039: Statistics statistics) {
040:
041: StatisticsDO statisticsDO = new StatisticsDO();
042: statisticsDO.setName(key);
043: statisticsDO.setDirection(direction);
044: statisticsDO.setCategory(statisticsCategory);
045: statisticsDO.setServerId(serverId);
046: statisticsDO.setMaxTime(statistics.getMaxProcessingTime());
047: statisticsDO.setMinTime(statistics.getMinProcessingTime());
048: statisticsDO.setFaultCount(statistics.getFaultCount());
049: statisticsDO.setTotalCount(statistics.getCount());
050: statisticsDO.setAvgTime((Math.round(statistics
051: .getAvgProcessingTime() * 100.0) / 100.0));
052: return statisticsDO;
053: }
054:
055: /**
056: * To insert a record to the statistics table
057: *
058: * @param pm
059: * @param serverId
060: * @param statisticsHolder
061: * @
062: */
063: public static void insertRecord(PersistenceManager pm,
064: String serverId, StatisticsHolder statisticsHolder) {
065:
066: String key = statisticsHolder.getKey();
067: int statisticsCategory = statisticsHolder
068: .getStatisticsCategory();
069: if (statisticsHolder.getInFlowStatistics() != null) {
070: pm.addStatisticsRecord(createStatisticsRecord(serverId,
071: statisticsCategory, key, 0, statisticsHolder
072: .getInFlowStatistics()));
073: }
074: if (statisticsHolder.getOutFlowStatistics() != null) {
075: pm.addStatisticsRecord(createStatisticsRecord(serverId,
076: statisticsCategory, key, 1, statisticsHolder
077: .getOutFlowStatistics()));
078: }
079: }
080:
081: /**
082: * To update a record of the statistics table
083: *
084: * @param pm
085: * @param serverId
086: * @param statisticsHolder
087: * @
088: */
089: public static void updateRecord(PersistenceManager pm,
090: String serverId, StatisticsHolder statisticsHolder) {
091:
092: String key = statisticsHolder.getKey();
093: int statisticsCategory = statisticsHolder
094: .getStatisticsCategory();
095: if (statisticsHolder.getInFlowStatistics() != null) {
096: updateRecord(pm, serverId, statisticsCategory, key, 0,
097: statisticsHolder.getInFlowStatistics());
098: }
099: if (statisticsHolder.getOutFlowStatistics() != null) {
100: updateRecord(pm, serverId, statisticsCategory, key, 1,
101: statisticsHolder.getOutFlowStatistics());
102: }
103: }
104:
105: /**
106: * A helper method to update a record of the statistics table
107: *
108: * @param pm
109: * @param serverId
110: * @param statisticsCategory
111: * @param key
112: * @param direction
113: * @param statistics
114: * @
115: */
116: private static void updateRecord(PersistenceManager pm,
117: String serverId, int statisticsCategory, String key,
118: int direction, Statistics statistics) {
119:
120: String queryString = "from "
121: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
122: + " where " + SERVER_ID_ + "= '" + serverId + "' and "
123: + CATEGORY_ + "=" + statisticsCategory + " and "
124: + NAME_ + "= '" + key + "' and " + DIRECTION_ + " = "
125: + direction + "";
126:
127: int currentTotalCount = statistics.getCount();
128: int currentFaultCount = statistics.getFaultCount();
129: long currentMaxTime = statistics.getMaxProcessingTime();
130: long currentMinTime = statistics.getMinProcessingTime();
131: double currentAvgTime = statistics.getAvgProcessingTime();
132:
133: StatisticsDO[] resultSet;
134: resultSet = pm.selectStatisticsRecords(queryString);
135: int previousTotalCount, previousFaultCount;
136: long previousMaxTime, previousMinTime;
137: double previousAvgTime;
138:
139: StatisticsDO previousDO;
140: if (resultSet != null && resultSet.length == 1) {
141: previousDO = resultSet[0];
142: previousTotalCount = (int) previousDO.getTotalCount();
143: previousFaultCount = (int) previousDO.getFaultCount();
144: previousMaxTime = previousDO.getMaxTime();
145: previousMinTime = previousDO.getMinTime();
146: previousAvgTime = previousDO.getAvgTime();
147: int updatedTotalCount, updatedFaultCount;
148: long updatedMaxTime, updatedMinTime;
149: double updatedAvgTime;
150: updatedTotalCount = (currentTotalCount + previousTotalCount);
151: if (updatedTotalCount != 0) {
152: updatedFaultCount = currentFaultCount
153: + previousFaultCount;
154: updatedAvgTime = (currentAvgTime * currentTotalCount + previousAvgTime
155: * previousTotalCount)
156: / updatedTotalCount;
157: if (currentMaxTime > previousMaxTime) {
158: updatedMaxTime = currentMaxTime;
159: } else {
160: updatedMaxTime = previousMaxTime;
161: }
162: if (currentMinTime < previousMinTime) {
163: updatedMinTime = currentMinTime;
164: } else {
165: updatedMinTime = previousMinTime;
166: }
167: previousDO.setFaultCount(updatedFaultCount);
168: previousDO.setTotalCount(updatedTotalCount);
169: previousDO.setMaxTime(updatedMaxTime);
170: previousDO.setMinTime(updatedMinTime);
171: previousDO.setAvgTime(Math
172: .round(updatedAvgTime * 100.0) / 100.0);
173: pm.updateStatisticsRecord(previousDO);
174: }
175:
176: } else if (resultSet == null || resultSet.length == 0) {
177: previousDO = createStatisticsRecord(serverId,
178: statisticsCategory, key, direction, statistics);
179: pm.addStatisticsRecord(previousDO);
180: }
181: }
182:
183: public static StatisticsDO[] getStatistics(PersistenceManager pm,
184: String serverid) {
185:
186: String queryString = "from "
187: + ServiceBusConstants.DBAccess.STATISTICS_DO
188: + " where " + SERVER_ID_ + " = '" + serverid + "'";
189: return pm.selectStatisticsRecords(queryString);
190: }
191:
192: public static StatisticsDO[] getStatistics(PersistenceManager pm,
193: String serverid, int direction) {
194:
195: String queryString = "from "
196: + ServiceBusConstants.DBAccess.STATISTICS_DO
197: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
198: + DIRECTION_ + " = " + direction + "";
199: return pm.selectStatisticsRecords(queryString);
200: }
201:
202: public static StatisticsDO[] getStatistics(PersistenceManager pm,
203: String serverid, int direction, int category) {
204:
205: String queryString = "from "
206: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
207: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
208: + CATEGORY_ + "= " + category + " and " + DIRECTION_
209: + " = " + direction + "";
210: return pm.selectStatisticsRecords(queryString);
211: }
212:
213: public static StatisticsDO[] getTotalStatisticsForServer(
214: PersistenceManager pm, String serverid) {
215:
216: String queryString = "from "
217: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
218: + " where " + SERVER_ID_ + " = '" + serverid + "'";
219: return pm.selectStatisticsRecords(queryString);
220: }
221:
222: public static StatisticsDO[] getStatistics(PersistenceManager pm,
223: String serverid, int category, String name) {
224:
225: String queryString = "from "
226: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
227: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
228: + CATEGORY_ + "= " + category + " and " + NAME_
229: + " = '" + name + "'";
230: return pm.selectStatisticsRecords(queryString);
231: }
232:
233: public static StatisticsDO getStatistics(PersistenceManager pm,
234: String serverid, int category, int direction, String name) {
235:
236: String queryString = "from "
237: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
238: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
239: + CATEGORY_ + "= " + category + " and " + NAME_
240: + " = '" + name + "'" + " and " + DIRECTION_ + " = "
241: + direction + "";
242: return (StatisticsDO) pm
243: .createAnewObjectFromStatistics(queryString);
244: }
245:
246: public static StatisticsDO[] getStatistics(PersistenceManager pm,
247: int category, String name) {
248: String queryString = "from "
249: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
250: + " where " + CATEGORY_ + "= " + category + " and "
251: + NAME_ + " = '" + name + "'";
252: return pm.selectStatisticsRecords(queryString);
253: }
254:
255: public static StatisticsDO[] getStatistics(PersistenceManager pm,
256: int category) {
257: String queryString = "from "
258: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
259: + " where " + CATEGORY_ + "= " + category + "";
260: return pm.selectStatisticsRecords(queryString);
261: }
262:
263: public static StatisticsDO[] getStatistics(PersistenceManager pm,
264: int category, int direction) {
265: String queryString = "from "
266: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
267: + " where " + CATEGORY_ + "= " + category + " and "
268: + DIRECTION_ + " = " + direction + " ";
269: return pm.selectStatisticsRecords(queryString);
270: }
271:
272: public static StatisticsDO[] getStatisticsForServer(
273: PersistenceManager pm, String serverid, int category) {
274:
275: String queryString = "from "
276: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
277: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
278: + CATEGORY_ + " = " + category + "";
279: return pm.selectStatisticsRecords(queryString);
280: }
281:
282: public static StatisticsDO[] getStatisticsForServer(
283: PersistenceManager pm, String serverid, int direction,
284: int category) {
285:
286: String queryString = "from "
287: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
288: + " where " + SERVER_ID_ + " = '" + serverid + "' and "
289: + CATEGORY_ + "= " + category + " and " + DIRECTION_
290: + " = " + direction + "";
291: return pm.selectStatisticsRecords(queryString);
292: }
293:
294: public static Object getSummaryStatisticsForServer(
295: PersistenceManager pm, String serverid, int direction,
296: int category) {
297:
298: String queryString = "select sum(totalCount), sum(faultCount), avg(maxTime), "
299: + "avg(minTime), avg(avgTime) from "
300: + ServiceBusConstants.DBAccess.STATISTICS_DO
301: + " "
302: + " where "
303: + SERVER_ID_
304: + " = '"
305: + serverid
306: + "' and "
307: + CATEGORY_
308: + "= "
309: + category
310: + " and "
311: + DIRECTION_
312: + " = " + direction + "";
313: return pm.createAnewObjectFromStatistics(queryString);
314: }
315:
316: public static List getTotalCountForServerS(PersistenceManager pm,
317: int category) {
318: String queryString = "select " + SERVER_ID_
319: + " ,sum(totalCount) from "
320: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
321: + " where " + CATEGORY_ + "= " + category
322: + " group by " + SERVER_ID_ + " ";
323:
324: return pm.selectCustomObjectList(queryString);
325: }
326:
327: public static Object getServersIds(PersistenceManager pm) {
328: String queryString = "select distinct " + SERVER_ID_
329: + " from "
330: + ServiceBusConstants.DBAccess.STATISTICS_DO + " ";
331: return pm.selectCustomObjectList(queryString);
332: }
333:
334: public static Object getUniqueNames(PersistenceManager pm,
335: int category) {
336: String queryString = "select distinct " + NAME_ + " from "
337: + ServiceBusConstants.DBAccess.STATISTICS_DO
338: + " where " + CATEGORY_ + "= " + category + " ";
339: return pm.selectCustomObjectList(queryString);
340: }
341:
342: public static List getTotalCountForCategory(PersistenceManager pm,
343: int category) {
344: String queryString = "select " + NAME_
345: + ",sum(totalCount) from "
346: + ServiceBusConstants.DBAccess.STATISTICS_DO + " "
347: + " where " + CATEGORY_ + "= " + category
348: + " group by " + NAME_ + " ";
349: return pm.selectCustomObjectList(queryString);
350: }
351:
352: public static int clearAll(PersistenceManager pm) {
353: String queryString = "DELETE "
354: + ServiceBusConstants.DBAccess.STATISTICS_DO;
355: return pm.clearAll(queryString);
356: }
357: }
|