1 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 ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 31 36 public class TableReportGenerator 37 extends ReportGenerator 38 { 39 41 protected void content(String reportName, StringBuffer content) throws Exception 42 { 43 StringBuffer reportsTable = new StringBuffer (); 44 reportsTable.append("<table><tr><th>Transaction started by</th><th>Total</th></tr>"); 45 46 Map tables = new HashMap (); 47 Map sqls = new HashMap (); 48 int txTotal = 0; 49 50 Iterator 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 tablesBuf = new StringBuffer (); 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 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 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.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 157 private void generateReport(TxReport report, Map sqls, Map tables) 158 { 159 Map itemMap = (Map ) report.getStats().get(TxReport.SqlStats.NAME); 160 if(itemMap != null) 161 { 162 for(Iterator items = itemMap.values().iterator(); items.hasNext();) 163 { 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 { 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 ("FROM not found in: " + sql); 181 } 182 183 String 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 table = sql.substring("update ".length()); 201 int tableEnd = table.indexOf(' '); 202 if(tableEnd == -1) 203 { 204 throw new IllegalStateException ("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 table = sql.substring("insert into ".length()); 220 int tableEnd = table.indexOf('('); 221 if(tableEnd == -1) 222 { 223 throw new IllegalStateException ("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 table = sql.substring("delete from ".length()); 238 int tableEnd = table.indexOf(' '); 239 if(tableEnd == -1) 240 { 241 throw new IllegalStateException ("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 ("Unrecognized sql statement: " + sql); 256 } 257 } 258 } 259 } 260 261 263 private static class SqlStats 264 { 265 public final String sql; 266 public int total; 267 268 public SqlStats(String sql) 269 { 270 this.sql = sql; 271 } 272 } 273 274 private static class TableStats 275 { 276 public final String name; 277 public int selects; 278 public int updates; 279 public int inserts; 280 public int deletes; 281 282 public TableStats(String name) 283 { 284 this.name = name; 285 } 286 287 public boolean equals(Object 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 |