KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > dbadmin > ShowSqlStats


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet
19  * Contributor(s): ______________________.
20  */

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 /**
28  * This class display statistics of the SQL monitoring module of the controller.
29  *
30  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
31  * @version 1.0
32  */

33 public class ShowSqlStats extends AbstractAdminCommand
34 {
35   private String JavaDoc[] columnNames;
36
37   /**
38    * Creates a new <code>ShowSqlStats</code> object
39    *
40    * @param module virtual database admin module
41    */

42   public ShowSqlStats(VirtualDatabaseAdmin module)
43   {
44     super(module);
45     columnNames = new String JavaDoc[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   /**
58    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
59    */

60   public void parse(String JavaDoc commandText) throws Exception JavaDoc
61   {
62     String JavaDoc[][] 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   /**
71    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
72    */

73   public String JavaDoc getCommandName()
74   {
75     return "show sql statistics"; //$NON-NLS-1$
76
}
77
78   /**
79    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
80    */

81   public String JavaDoc getCommandDescription()
82   {
83     return ConsoleTranslate.get("admin.command.show.sql.stats.description");
84   }
85
86   /**
87    * Format data for text consoles
88    *
89    * @param data to display
90    * @return a formatted string with tabs and end of line
91    */

92   public String JavaDoc displayStats(String JavaDoc[][] data)
93   {
94     if (data == null)
95       return "";
96
97     // constants used for formatting the output
98
final String JavaDoc columnPadding = " ";
99     final String JavaDoc nameValueSeparator = ": ";
100
101     // solve the maximum length for column names
102
// TODO: refactor this into its own method
103
int maxNameLength = 0;
104     for (int i = 0; i < columnNames.length; i++)
105     {
106       maxNameLength = Math.max(maxNameLength, columnNames[i].length());
107     }
108
109     // solve the maximum length for column values
110
// TODO: refactor this into its own method
111
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     // construct a separator line based on maximum column and value lengths
121
// TODO: extract numbers into constants and this block into a new method
122
char[] separator = new char[columnPadding.length() + maxNameLength
123         + nameValueSeparator.length() + maxValueLength + 1]; /*
124                                                                * the newline
125                                                                * character
126                                                                */

127     for (int i = 0; i < separator.length; i++)
128     {
129       separator[i] = '-';
130     }
131     separator[separator.length - 1] = '\n';
132
133     // loop through all the data and print padded lines into the StringBuffer
134
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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         // create the padding needed for this particular column
141
// TODO: extract this into its own method
142
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