PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jboss-5.1.0/varia/src/main/org/jboss/varia/stats/report/TableReportGenerator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 304 lines | 235 code | 40 blank | 29 comment | 47 complexity | 7b097348675b6eeca60500671227aaa9 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a 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. import org.jboss.varia.stats.TxReport;
  24. import org.jboss.varia.stats.StatisticalItem;
  25. import java.util.Iterator;
  26. import java.util.Map;
  27. import java.util.HashMap;
  28. /**
  29. *
  30. * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
  31. * @version <tt>$Revision: 81038 $</tt>
  32. */
  33. public class TableReportGenerator
  34. extends ReportGenerator
  35. {
  36. // ReportGenerator implementation
  37. protected void content(String reportName, StringBuffer content) throws Exception
  38. {
  39. StringBuffer reportsTable = new StringBuffer();
  40. reportsTable.append("<table><tr><th>Transaction started by</th><th>Total</th></tr>");
  41. Map tables = new HashMap();
  42. Map sqls = new HashMap();
  43. int txTotal = 0;
  44. Iterator reports = getReportsIterator();
  45. while(reports.hasNext())
  46. {
  47. TxReport report = (TxReport) reports.next();
  48. txTotal += report.getCount();
  49. reportsTable.append("<tr><td>");
  50. boolean selected = report.getName().equals(reportName);
  51. if(!selected)
  52. {
  53. reportsTable.append("<a href='HtmlAdaptor?")
  54. .append("action=invokeOpByName&name=")
  55. .append(getServiceName())
  56. .append("&methodName=generate&")
  57. .append("argType=java.lang.String&arg0=")
  58. .append(report.getName())
  59. .append("'>");
  60. }
  61. reportsTable.append(report.getName());
  62. if(!selected)
  63. {
  64. reportsTable.append("</a>");
  65. }
  66. reportsTable.append("</td><td>")
  67. .append(report.getCount()).append("</td></tr>");
  68. if(selected || reportName == null || reportName.trim().length() == 0)
  69. {
  70. generateReport(report, sqls, tables);
  71. }
  72. }
  73. reportsTable.append("<tr><td>");
  74. boolean select = reportName != null && reportName.trim().length() > 0;
  75. if(select)
  76. {
  77. reportsTable.append("<a href='HtmlAdaptor?")
  78. .append("action=invokeOpByName&name=")
  79. .append(getServiceName())
  80. .append("&methodName=generate&")
  81. .append("argType=java.lang.String&arg0=")
  82. .append("'>");
  83. }
  84. reportsTable.append("all transactions");
  85. if(select)
  86. {
  87. reportsTable.append("</a>");
  88. }
  89. reportsTable.append("</td><td>").append(txTotal).append("</td></tr></table>");
  90. StringBuffer tablesBuf = new StringBuffer();
  91. tablesBuf.append(
  92. "<table><tr><th>Table</th><th>selects</th><th>updates</th><th>inserts</th><th>deletes</th></tr>");
  93. int totalSelects = 0;
  94. int totalUpdates = 0;
  95. int totalInserts = 0;
  96. int totalDeletes = 0;
  97. for(Iterator tableIter = tables.values().iterator(); tableIter.hasNext();)
  98. {
  99. TableStats table = (TableStats) tableIter.next();
  100. tablesBuf.append("<tr><td>").append(table.name).append("</td><td>")
  101. .append(table.selects).append("</td><td>")
  102. .append(table.updates).append("</td><td>")
  103. .append(table.inserts).append("</td><td>")
  104. .append(table.deletes).append("</td></tr>");
  105. totalSelects += table.selects;
  106. totalUpdates += table.updates;
  107. totalInserts += table.inserts;
  108. totalDeletes += table.deletes;
  109. }
  110. tablesBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
  111. .append(totalSelects).append("</font></td><td><font color='red'>")
  112. .append(totalUpdates).append("</font></td><td><font color='red'>")
  113. .append(totalInserts).append("</font></td><td><font color='red'>")
  114. .append(totalDeletes).append("</font></td></tr>")
  115. .append("</table>");
  116. StringBuffer itemsTable = new StringBuffer();
  117. itemsTable.append("<table><tr><th>SQL</th><th>Total</th></tr>");
  118. int totalStmt = 0;
  119. for(Iterator itemIter = sqls.values().iterator(); itemIter.hasNext();)
  120. {
  121. SqlStats sql = (SqlStats)itemIter.next();
  122. itemsTable.append("<tr><td>").append(sql.sql)
  123. .append("</td><td>").append(sql.total).append("</td></tr>");
  124. totalStmt += sql.total;
  125. }
  126. itemsTable.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
  127. .append(totalStmt).append("</font></td></tr></table>");
  128. content.append("<table><tr valign='top'><td>")
  129. .append(reportsTable)
  130. .append("</td><td>").append(tablesBuf)
  131. .append("</td><td>").append(itemsTable)
  132. .append("</td></tr></table>");
  133. }
  134. // Private
  135. private void generateReport(TxReport report, Map sqls, Map tables)
  136. {
  137. Map itemMap = (Map) report.getStats().get(TxReport.SqlStats.NAME);
  138. if(itemMap != null)
  139. {
  140. for(Iterator items = itemMap.values().iterator(); items.hasNext();)
  141. {
  142. StatisticalItem item = (StatisticalItem) items.next();
  143. String sql = item.getValue().toLowerCase();
  144. SqlStats sqlStats = (SqlStats)sqls.get(sql);
  145. if(sqlStats == null)
  146. {
  147. sqlStats = new SqlStats(sql);
  148. sqls.put(sql, sqlStats);
  149. }
  150. sqlStats.total += item.getCount();
  151. if(sql.startsWith("select "))
  152. {
  153. int fromStart = sql.indexOf("from ");
  154. if(fromStart == -1)
  155. {
  156. throw new IllegalStateException("FROM not found in: " + sql);
  157. }
  158. String table = sql.substring(fromStart + "from ".length());
  159. int tableEnd = table.indexOf(' ');
  160. if(tableEnd != -1)
  161. {
  162. table = table.substring(0, tableEnd);
  163. }
  164. TableStats tableStats = (TableStats) tables.get(table);
  165. if(tableStats == null)
  166. {
  167. tableStats = new TableStats(table);
  168. tables.put(table, tableStats);
  169. }
  170. tableStats.selects += item.getCount();
  171. }
  172. else if(sql.startsWith("update "))
  173. {
  174. String table = sql.substring("update ".length());
  175. int tableEnd = table.indexOf(' ');
  176. if(tableEnd == -1)
  177. {
  178. throw new IllegalStateException("Could not find end of the table name: " + sql);
  179. }
  180. table = table.substring(0, tableEnd);
  181. TableStats tableStats = (TableStats) tables.get(table);
  182. if(tableStats == null)
  183. {
  184. tableStats = new TableStats(table);
  185. tables.put(table, tableStats);
  186. }
  187. tableStats.updates += item.getCount();
  188. }
  189. else if(sql.startsWith("insert into "))
  190. {
  191. String table = sql.substring("insert into ".length());
  192. int tableEnd = table.indexOf('(');
  193. if(tableEnd == -1)
  194. {
  195. throw new IllegalStateException("Could not find end of the table name: " + sql);
  196. }
  197. table = table.substring(0, tableEnd).trim();
  198. TableStats tableStats = (TableStats) tables.get(table);
  199. if(tableStats == null)
  200. {
  201. tableStats = new TableStats(table);
  202. tables.put(table, tableStats);
  203. }
  204. tableStats.inserts += item.getCount();
  205. }
  206. else if(sql.startsWith("delete from "))
  207. {
  208. String table = sql.substring("delete from ".length());
  209. int tableEnd = table.indexOf(' ');
  210. if(tableEnd == -1)
  211. {
  212. throw new IllegalStateException("Could not find end of the table name: " + sql);
  213. }
  214. table = table.substring(0, tableEnd);
  215. TableStats tableStats = (TableStats) tables.get(table);
  216. if(tableStats == null)
  217. {
  218. tableStats = new TableStats(table);
  219. tables.put(table, tableStats);
  220. }
  221. tableStats.deletes += item.getCount();
  222. }
  223. else
  224. {
  225. throw new IllegalStateException("Unrecognized sql statement: " + sql);
  226. }
  227. }
  228. }
  229. }
  230. // Inner
  231. private static class SqlStats
  232. {
  233. public final String sql;
  234. public int total;
  235. public SqlStats(String sql)
  236. {
  237. this.sql = sql;
  238. }
  239. }
  240. private static class TableStats
  241. {
  242. public final String name;
  243. public int selects;
  244. public int updates;
  245. public int inserts;
  246. public int deletes;
  247. public TableStats(String name)
  248. {
  249. this.name = name;
  250. }
  251. public boolean equals(Object o)
  252. {
  253. if(this == o) return true;
  254. if(!(o instanceof TableStats)) return false;
  255. final TableStats tableStats = (TableStats) o;
  256. if(!name.equals(tableStats.name)) return false;
  257. return true;
  258. }
  259. public int hashCode()
  260. {
  261. return name.hashCode();
  262. }
  263. }
  264. }