KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > report > TableReportGenerator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.stats.report;
23
24 import org.jboss.varia.stats.TxReport;
25 import org.jboss.varia.stats.StatisticalItem;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 /**
32  *
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  * @version <tt>$Revision: 37459 $</tt>
35  */

36 public class TableReportGenerator
37    extends ReportGenerator
38 {
39    // ReportGenerator implementation
40

41    protected void content(String JavaDoc reportName, StringBuffer JavaDoc content) throws Exception JavaDoc
42    {
43       StringBuffer JavaDoc reportsTable = new StringBuffer JavaDoc();
44       reportsTable.append("<table><tr><th>Transaction started by</th><th>Total</th></tr>");
45
46       Map JavaDoc tables = new HashMap JavaDoc();
47       Map JavaDoc sqls = new HashMap JavaDoc();
48       int txTotal = 0;
49
50       Iterator JavaDoc reports = getReportsIterator();
51       while(reports.hasNext())
52       {
53          TxReport report = (TxReport) reports.next();
54          txTotal += report.getCount();
55          reportsTable.append("<tr><td>");
56
57          boolean selected = report.getName().equals(reportName);
58          if(!selected)
59          {
60             reportsTable.append("<a HREF='HtmlAdaptor?")
61                .append("action=invokeOpByName&name=")
62                .append(getServiceName())
63                .append("&methodName=generate&")
64                .append("argType=java.lang.String&arg0=")
65                .append(report.getName())
66                .append("'>");
67          }
68
69          reportsTable.append(report.getName());
70
71          if(!selected)
72          {
73             reportsTable.append("</a>");
74          }
75
76          reportsTable.append("</td><td>")
77             .append(report.getCount()).append("</td></tr>");
78
79          if(selected || reportName == null || reportName.trim().length() == 0)
80          {
81             generateReport(report, sqls, tables);
82          }
83       }
84
85       reportsTable.append("<tr><td>");
86
87       boolean select = reportName != null && reportName.trim().length() > 0;
88       if(select)
89       {
90          reportsTable.append("<a HREF='HtmlAdaptor?")
91             .append("action=invokeOpByName&name=")
92             .append(getServiceName())
93             .append("&methodName=generate&")
94             .append("argType=java.lang.String&arg0=")
95             .append("'>");
96       }
97
98       reportsTable.append("all transactions");
99
100       if(select)
101       {
102          reportsTable.append("</a>");
103       }
104
105       reportsTable.append("</td><td>").append(txTotal).append("</td></tr></table>");
106
107       StringBuffer JavaDoc tablesBuf = new StringBuffer JavaDoc();
108       tablesBuf.append(
109          "<table><tr><th>Table</th><th>selects</th><th>updates</th><th>inserts</th><th>deletes</th></tr>");
110       int totalSelects = 0;
111       int totalUpdates = 0;
112       int totalInserts = 0;
113       int totalDeletes = 0;
114       for(Iterator JavaDoc tableIter = tables.values().iterator(); tableIter.hasNext();)
115       {
116          TableStats table = (TableStats) tableIter.next();
117          tablesBuf.append("<tr><td>").append(table.name).append("</td><td>")
118             .append(table.selects).append("</td><td>")
119             .append(table.updates).append("</td><td>")
120             .append(table.inserts).append("</td><td>")
121             .append(table.deletes).append("</td></tr>");
122
123          totalSelects += table.selects;
124          totalUpdates += table.updates;
125          totalInserts += table.inserts;
126          totalDeletes += table.deletes;
127       }
128       tablesBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
129          .append(totalSelects).append("</font></td><td><font color='red'>")
130          .append(totalUpdates).append("</font></td><td><font color='red'>")
131          .append(totalInserts).append("</font></td><td><font color='red'>")
132          .append(totalDeletes).append("</font></td></tr>")
133          .append("</table>");
134
135       StringBuffer JavaDoc itemsTable = new StringBuffer JavaDoc();
136       itemsTable.append("<table><tr><th>SQL</th><th>Total</th></tr>");
137       int totalStmt = 0;
138       for(Iterator JavaDoc itemIter = sqls.values().iterator(); itemIter.hasNext();)
139       {
140          SqlStats sql = (SqlStats)itemIter.next();
141          itemsTable.append("<tr><td>").append(sql.sql)
142             .append("</td><td>").append(sql.total).append("</td></tr>");
143          totalStmt += sql.total;
144       }
145       itemsTable.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
146          .append(totalStmt).append("</font></td></tr></table>");
147
148       content.append("<table><tr valign='top'><td>")
149          .append(reportsTable)
150          .append("</td><td>").append(tablesBuf)
151          .append("</td><td>").append(itemsTable)
152          .append("</td></tr></table>");
153    }
154
155    // Private
156

157    private void generateReport(TxReport report, Map JavaDoc sqls, Map JavaDoc tables)
158    {
159       Map JavaDoc itemMap = (Map JavaDoc) report.getStats().get(TxReport.SqlStats.NAME);
160       if(itemMap != null)
161       {
162          for(Iterator JavaDoc items = itemMap.values().iterator(); items.hasNext();)
163          {
164             StatisticalItem item = (StatisticalItem) items.next();
165             String JavaDoc sql = item.getValue().toLowerCase();
166
167             SqlStats sqlStats = (SqlStats)sqls.get(sql);
168             if(sqlStats == null)
169             {
170                sqlStats = new SqlStats(sql);
171                sqls.put(sql, sqlStats);
172             }
173             sqlStats.total += item.getCount();
174
175             if(sql.startsWith("select "))
176             {
177                int fromStart = sql.indexOf("from ");
178                if(fromStart == -1)
179                {
180                   throw new IllegalStateException JavaDoc("FROM not found in: " + sql);
181                }
182
183                String JavaDoc table = sql.substring(fromStart + "from ".length());
184                int tableEnd = table.indexOf(' ');
185                if(tableEnd != -1)
186                {
187                   table = table.substring(0, tableEnd);
188                }
189
190                TableStats tableStats = (TableStats) tables.get(table);
191                if(tableStats == null)
192                {
193                   tableStats = new TableStats(table);
194                   tables.put(table, tableStats);
195                }
196                tableStats.selects += item.getCount();
197             }
198             else if(sql.startsWith("update "))
199             {
200                String JavaDoc table = sql.substring("update ".length());
201                int tableEnd = table.indexOf(' ');
202                if(tableEnd == -1)
203                {
204                   throw new IllegalStateException JavaDoc("Could not find end of the table name: " + sql);
205                }
206
207                table = table.substring(0, tableEnd);
208
209                TableStats tableStats = (TableStats) tables.get(table);
210                if(tableStats == null)
211                {
212                   tableStats = new TableStats(table);
213                   tables.put(table, tableStats);
214                }
215                tableStats.updates += item.getCount();
216             }
217             else if(sql.startsWith("insert into "))
218             {
219                String JavaDoc table = sql.substring("insert into ".length());
220                int tableEnd = table.indexOf('(');
221                if(tableEnd == -1)
222                {
223                   throw new IllegalStateException JavaDoc("Could not find end of the table name: " + sql);
224                }
225
226                table = table.substring(0, tableEnd).trim();
227                TableStats tableStats = (TableStats) tables.get(table);
228                if(tableStats == null)
229                {
230                   tableStats = new TableStats(table);
231                   tables.put(table, tableStats);
232                }
233                tableStats.inserts += item.getCount();
234             }
235             else if(sql.startsWith("delete from "))
236             {
237                String JavaDoc table = sql.substring("delete from ".length());
238                int tableEnd = table.indexOf(' ');
239                if(tableEnd == -1)
240                {
241                   throw new IllegalStateException JavaDoc("Could not find end of the table name: " + sql);
242                }
243
244                table = table.substring(0, tableEnd);
245                TableStats tableStats = (TableStats) tables.get(table);
246                if(tableStats == null)
247                {
248                   tableStats = new TableStats(table);
249                   tables.put(table, tableStats);
250                }
251                tableStats.deletes += item.getCount();
252             }
253             else
254             {
255                throw new IllegalStateException JavaDoc("Unrecognized sql statement: " + sql);
256             }
257          }
258       }
259    }
260
261    // Inner
262

263    private static class SqlStats
264    {
265       public final String JavaDoc sql;
266       public int total;
267
268       public SqlStats(String JavaDoc sql)
269       {
270          this.sql = sql;
271       }
272    }
273
274    private static class TableStats
275    {
276       public final String JavaDoc name;
277       public int selects;
278       public int updates;
279       public int inserts;
280       public int deletes;
281
282       public TableStats(String JavaDoc name)
283       {
284          this.name = name;
285       }
286
287       public boolean equals(Object JavaDoc o)
288       {
289          if(this == o) return true;
290          if(!(o instanceof TableStats)) return false;
291
292          final TableStats tableStats = (TableStats) o;
293
294          if(!name.equals(tableStats.name)) return false;
295
296          return true;
297       }
298
299       public int hashCode()
300       {
301          return name.hashCode();
302       }
303    }
304 }
305
Popular Tags