001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.varia.stats.report;
023:
024: import org.jboss.varia.stats.TxReport;
025: import org.jboss.varia.stats.StatisticalItem;
026:
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.HashMap;
030:
031: /**
032: *
033: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
034: * @version <tt>$Revision: 57210 $</tt>
035: */
036: public class TableReportGenerator extends ReportGenerator {
037: // ReportGenerator implementation
038:
039: protected void content(String reportName, StringBuffer content)
040: throws Exception {
041: StringBuffer reportsTable = new StringBuffer();
042: reportsTable
043: .append("<table><tr><th>Transaction started by</th><th>Total</th></tr>");
044:
045: Map tables = new HashMap();
046: Map sqls = new HashMap();
047: int txTotal = 0;
048:
049: Iterator reports = getReportsIterator();
050: while (reports.hasNext()) {
051: TxReport report = (TxReport) reports.next();
052: txTotal += report.getCount();
053: reportsTable.append("<tr><td>");
054:
055: boolean selected = report.getName().equals(reportName);
056: if (!selected) {
057: reportsTable.append("<a href='HtmlAdaptor?").append(
058: "action=invokeOpByName&name=").append(
059: getServiceName()).append(
060: "&methodName=generate&").append(
061: "argType=java.lang.String&arg0=").append(
062: report.getName()).append("'>");
063: }
064:
065: reportsTable.append(report.getName());
066:
067: if (!selected) {
068: reportsTable.append("</a>");
069: }
070:
071: reportsTable.append("</td><td>").append(report.getCount())
072: .append("</td></tr>");
073:
074: if (selected || reportName == null
075: || reportName.trim().length() == 0) {
076: generateReport(report, sqls, tables);
077: }
078: }
079:
080: reportsTable.append("<tr><td>");
081:
082: boolean select = reportName != null
083: && reportName.trim().length() > 0;
084: if (select) {
085: reportsTable.append("<a href='HtmlAdaptor?").append(
086: "action=invokeOpByName&name=").append(
087: getServiceName()).append("&methodName=generate&")
088: .append("argType=java.lang.String&arg0=").append(
089: "'>");
090: }
091:
092: reportsTable.append("all transactions");
093:
094: if (select) {
095: reportsTable.append("</a>");
096: }
097:
098: reportsTable.append("</td><td>").append(txTotal).append(
099: "</td></tr></table>");
100:
101: StringBuffer tablesBuf = new StringBuffer();
102: tablesBuf
103: .append("<table><tr><th>Table</th><th>selects</th><th>updates</th><th>inserts</th><th>deletes</th></tr>");
104: int totalSelects = 0;
105: int totalUpdates = 0;
106: int totalInserts = 0;
107: int totalDeletes = 0;
108: for (Iterator tableIter = tables.values().iterator(); tableIter
109: .hasNext();) {
110: TableStats table = (TableStats) tableIter.next();
111: tablesBuf.append("<tr><td>").append(table.name).append(
112: "</td><td>").append(table.selects).append(
113: "</td><td>").append(table.updates).append(
114: "</td><td>").append(table.inserts).append(
115: "</td><td>").append(table.deletes).append(
116: "</td></tr>");
117:
118: totalSelects += table.selects;
119: totalUpdates += table.updates;
120: totalInserts += table.inserts;
121: totalDeletes += table.deletes;
122: }
123: tablesBuf
124: .append(
125: "<tr><td><font color='red'>total</font></td><td><font color='red'>")
126: .append(totalSelects).append(
127: "</font></td><td><font color='red'>").append(
128: totalUpdates).append(
129: "</font></td><td><font color='red'>").append(
130: totalInserts).append(
131: "</font></td><td><font color='red'>").append(
132: totalDeletes).append("</font></td></tr>")
133: .append("</table>");
134:
135: StringBuffer itemsTable = new StringBuffer();
136: itemsTable.append("<table><tr><th>SQL</th><th>Total</th></tr>");
137: int totalStmt = 0;
138: for (Iterator itemIter = sqls.values().iterator(); itemIter
139: .hasNext();) {
140: SqlStats sql = (SqlStats) itemIter.next();
141: itemsTable.append("<tr><td>").append(sql.sql).append(
142: "</td><td>").append(sql.total).append("</td></tr>");
143: totalStmt += sql.total;
144: }
145: itemsTable
146: .append(
147: "<tr><td><font color='red'>total</font></td><td><font color='red'>")
148: .append(totalStmt).append("</font></td></tr></table>");
149:
150: content.append("<table><tr valign='top'><td>").append(
151: reportsTable).append("</td><td>").append(tablesBuf)
152: .append("</td><td>").append(itemsTable).append(
153: "</td></tr></table>");
154: }
155:
156: // Private
157:
158: private void generateReport(TxReport report, Map sqls, Map tables) {
159: Map itemMap = (Map) report.getStats().get(
160: TxReport.SqlStats.NAME);
161: if (itemMap != null) {
162: for (Iterator items = itemMap.values().iterator(); items
163: .hasNext();) {
164: StatisticalItem item = (StatisticalItem) items.next();
165: String sql = item.getValue().toLowerCase();
166:
167: SqlStats sqlStats = (SqlStats) sqls.get(sql);
168: if (sqlStats == null) {
169: sqlStats = new SqlStats(sql);
170: sqls.put(sql, sqlStats);
171: }
172: sqlStats.total += item.getCount();
173:
174: if (sql.startsWith("select ")) {
175: int fromStart = sql.indexOf("from ");
176: if (fromStart == -1) {
177: throw new IllegalStateException(
178: "FROM not found in: " + sql);
179: }
180:
181: String table = sql.substring(fromStart
182: + "from ".length());
183: int tableEnd = table.indexOf(' ');
184: if (tableEnd != -1) {
185: table = table.substring(0, tableEnd);
186: }
187:
188: TableStats tableStats = (TableStats) tables
189: .get(table);
190: if (tableStats == null) {
191: tableStats = new TableStats(table);
192: tables.put(table, tableStats);
193: }
194: tableStats.selects += item.getCount();
195: } else if (sql.startsWith("update ")) {
196: String table = sql.substring("update ".length());
197: int tableEnd = table.indexOf(' ');
198: if (tableEnd == -1) {
199: throw new IllegalStateException(
200: "Could not find end of the table name: "
201: + sql);
202: }
203:
204: table = table.substring(0, tableEnd);
205:
206: TableStats tableStats = (TableStats) tables
207: .get(table);
208: if (tableStats == null) {
209: tableStats = new TableStats(table);
210: tables.put(table, tableStats);
211: }
212: tableStats.updates += item.getCount();
213: } else if (sql.startsWith("insert into ")) {
214: String table = sql.substring("insert into "
215: .length());
216: int tableEnd = table.indexOf('(');
217: if (tableEnd == -1) {
218: throw new IllegalStateException(
219: "Could not find end of the table name: "
220: + sql);
221: }
222:
223: table = table.substring(0, tableEnd).trim();
224: TableStats tableStats = (TableStats) tables
225: .get(table);
226: if (tableStats == null) {
227: tableStats = new TableStats(table);
228: tables.put(table, tableStats);
229: }
230: tableStats.inserts += item.getCount();
231: } else if (sql.startsWith("delete from ")) {
232: String table = sql.substring("delete from "
233: .length());
234: int tableEnd = table.indexOf(' ');
235: if (tableEnd == -1) {
236: throw new IllegalStateException(
237: "Could not find end of the table name: "
238: + sql);
239: }
240:
241: table = table.substring(0, tableEnd);
242: TableStats tableStats = (TableStats) tables
243: .get(table);
244: if (tableStats == null) {
245: tableStats = new TableStats(table);
246: tables.put(table, tableStats);
247: }
248: tableStats.deletes += item.getCount();
249: } else {
250: throw new IllegalStateException(
251: "Unrecognized sql statement: " + sql);
252: }
253: }
254: }
255: }
256:
257: // Inner
258:
259: private static class SqlStats {
260: public final String sql;
261: public int total;
262:
263: public SqlStats(String sql) {
264: this .sql = sql;
265: }
266: }
267:
268: private static class TableStats {
269: public final String name;
270: public int selects;
271: public int updates;
272: public int inserts;
273: public int deletes;
274:
275: public TableStats(String name) {
276: this .name = name;
277: }
278:
279: public boolean equals(Object o) {
280: if (this == o)
281: return true;
282: if (!(o instanceof TableStats))
283: return false;
284:
285: final TableStats tableStats = (TableStats) o;
286:
287: if (!name.equals(tableStats.name))
288: return false;
289:
290: return true;
291: }
292:
293: public int hashCode() {
294: return name.hashCode();
295: }
296: }
297: }
|