1 21 22 package org.continuent.sequoia.console.text.commands.dbadmin; 23 24 import org.continuent.sequoia.common.i18n.ConsoleTranslate; 25 import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin; 26 27 33 public class ShowSqlStats extends AbstractAdminCommand 34 { 35 private String [] columnNames; 36 37 42 public ShowSqlStats(VirtualDatabaseAdmin module) 43 { 44 super(module); 45 columnNames = new String [9]; 46 columnNames[0] = "SQL statement"; 47 columnNames[1] = "Count"; 48 columnNames[2] = "Error"; 49 columnNames[3] = "Cache hits"; 50 columnNames[4] = "% hits"; 51 columnNames[5] = "Min time (ms)"; 52 columnNames[6] = "Max time (ms)"; 53 columnNames[7] = "Avg time (ms)"; 54 columnNames[8] = "Total time (ms)"; 55 } 56 57 60 public void parse(String commandText) throws Exception  61 { 62 String [][] stats = jmxClient.getDataCollectorProxy().retrieveSQLStats( 63 dbName); 64 if (stats == null) 65 console.printError("No SQL statistics are available on this controller"); 66 else 67 console.println(displayStats(stats)); 68 } 69 70 73 public String getCommandName() 74 { 75 return "show sql statistics"; } 77 78 81 public String getCommandDescription() 82 { 83 return ConsoleTranslate.get("admin.command.show.sql.stats.description"); 84 } 85 86 92 public String displayStats(String [][] data) 93 { 94 if (data == null) 95 return ""; 96 97 final String columnPadding = " "; 99 final String nameValueSeparator = ": "; 100 101 int maxNameLength = 0; 104 for (int i = 0; i < columnNames.length; i++) 105 { 106 maxNameLength = Math.max(maxNameLength, columnNames[i].length()); 107 } 108 109 int maxValueLength = 0; 112 for (int i = 0; i < data.length; i++) 113 { 114 for (int j = 0; j < data[i].length; j++) 115 { 116 maxValueLength = Math.max(maxValueLength, data[i][j].length()); 117 } 118 } 119 120 char[] separator = new char[columnPadding.length() + maxNameLength 123 + nameValueSeparator.length() + maxValueLength + 1]; 127 for (int i = 0; i < separator.length; i++) 128 { 129 separator[i] = '-'; 130 } 131 separator[separator.length - 1] = '\n'; 132 133 StringBuffer sb = new StringBuffer (); 135 for (int i = 0; i < data.length; i++) 136 { 137 sb.append(separator); 138 for (int j = 0; j < data[i].length; j++) 139 { 140 char[] namePadding = new char[maxNameLength - columnNames[j].length()]; 143 for (int x = 0; x < namePadding.length; x++) 144 { 145 namePadding[x] = ' '; 146 } 147 148 sb.append(columnPadding); 149 sb.append(columnNames[j]); 150 sb.append(nameValueSeparator); 151 sb.append(namePadding); 152 sb.append(data[i][j]); 153 sb.append("\n"); 154 } 155 if (i + 1 == data.length) 156 { 157 sb.append(separator); 158 } 159 } 160 return sb.toString(); 161 } 162 163 } | Popular Tags |