mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -25,6 +25,7 @@ public class Option {
|
||||
private PrintStyle style = PrintStyle.HORIZONTAL;
|
||||
private int printDataType = 0;
|
||||
private int vmid = -1;
|
||||
private int id = -1;
|
||||
|
||||
public void addPrintDataType(int newValue) {
|
||||
this.printDataType= this.printDataType | newValue;
|
||||
@@ -33,7 +34,7 @@ public class Option {
|
||||
public static boolean isPrintHelp(String[] args) {
|
||||
if (args == null ) return true;
|
||||
for (String arg: args) {
|
||||
if (arg.equals("-h") || arg.equals("--help")) {
|
||||
if (arg.equals("-help") ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -112,6 +113,13 @@ public class Option {
|
||||
option.setStyle(PrintStyle.HORIZONTAL);
|
||||
} else if (args[i].equals("-s2")) {
|
||||
option.setStyle(PrintStyle.VERTICAL);
|
||||
} else if (args[i].equals("-id")) {
|
||||
try {
|
||||
int id = Integer.parseInt(args[i+1]);
|
||||
option.setId(id);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new OptionParseException("vmid argument is not a integer!");
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
@@ -122,7 +130,7 @@ public class Option {
|
||||
}
|
||||
|
||||
public static void printHelp() {
|
||||
System.out.println("Usage: druidStat -help | -sql -ds -act -s1 -s2 vmid");
|
||||
System.out.println("Usage: druidStat -help | -sql -ds -act -s1 -s2 -id vmid");
|
||||
System.out.println();
|
||||
System.out.println("arguments: ");
|
||||
System.out.println(" vmid the process id of jvm that running druid");
|
||||
@@ -131,8 +139,17 @@ public class Option {
|
||||
System.out.println(" -act print active connection stacktrace data");
|
||||
System.out.println(" -s1 print field name in first row");
|
||||
System.out.println(" -s2 print field name in first column");
|
||||
System.out.println(" -id only print the sql data matched with id, use this with -sql argument" );
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,11 +19,26 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
|
||||
public class TabledDataPrinter {
|
||||
|
||||
private static final int SQL_MAX_LEN = 32;
|
||||
|
||||
private static final String[] sqlFields = new String[] {
|
||||
private static final int MAX_COL = 4;
|
||||
|
||||
private static final String[] sqlRowTitle = new String[] {
|
||||
"ID", "SQL", "ExecCount", "ExecTime",
|
||||
"ExecMax","Txn", "Error","Update","FetchRow","Running","Concurrent",
|
||||
"ExecHisto", "ExecRsHisto","FetchRowHisto","UpdateHisto" };
|
||||
|
||||
|
||||
private static final String[] sqlRowField = new String[] {
|
||||
"ID", "SQL", "ExecuteCount", "TotalTime", "MaxTimespan",
|
||||
"InTransactionCount", "ErrorCount", "EffectedRowCount", "FetchRowCount",
|
||||
"RunningCount","ConcurrentMax","Histogram","ExecuteAndResultHoldTimeHistogram",
|
||||
"FetchRowCountHistogram","EffectedRowCountHistogram" };
|
||||
|
||||
private static final String[] sqlColField = new String[] {
|
||||
"ID", "DataSource", "SQL", "ExecuteCount", "ErrorCount",
|
||||
"TotalTime", "LastTime", "MaxTimespan", "LastError", "EffectedRowCount",
|
||||
"FetchRowCount", "MaxTimespanOccurTime", "BatchSizeMax", "BatchSizeTotal", "ConcurrentMax",
|
||||
@@ -33,7 +48,21 @@ public class TabledDataPrinter {
|
||||
"Histogram", "LastSlowParameters", "ResultSetHoldTime", "ExecuteAndResultSetHoldTime", "FetchRowCountHistogram",
|
||||
"EffectedRowCountHistogram", "ExecuteAndResultHoldTimeHistogram", "EffectedRowCountMax", "FetchRowCountMax", "ClobOpenCount" };
|
||||
|
||||
private static final String[] dsFields = new String[] {
|
||||
|
||||
private static final String[] dsRowTitle = new String[] {
|
||||
"Name", "DbType",
|
||||
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
|
||||
"ActiveCount", "ActivePeak", "ActivePeakTime",
|
||||
"ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" };
|
||||
|
||||
private static final String[] dsRowField = new String[] {
|
||||
"Name", "DbType",
|
||||
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
|
||||
"ActiveCount", "ActivePeak", "ActivePeakTime",
|
||||
"ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" };
|
||||
|
||||
|
||||
private static final String[] dsColField = new String[] {
|
||||
"Identity", "Name", "DbType", "DriverClassName", "URL", "UserName", "FilterClassNames",
|
||||
"WaitThreadCount", "NotEmptyWaitCount", "NotEmptyWaitMillis",
|
||||
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
|
||||
@@ -60,29 +89,48 @@ public class TabledDataPrinter {
|
||||
|
||||
public static void printDataSourceData(List<Map<String, Object>> content,Option opt) {
|
||||
if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) {
|
||||
System.out.println(getFormattedOutput(content, dsFields));
|
||||
System.out.println(getFormattedOutput(content, dsRowTitle, dsRowField));
|
||||
} else {
|
||||
System.out.println(getVerticalFormattedOutput(content, dsFields));
|
||||
System.out.println(getVerticalFormattedOutput(content, dsColField));
|
||||
}
|
||||
}
|
||||
|
||||
public static void printSqlData(List<Map<String, Object>> content, Option opt) {
|
||||
if (opt.getId() != -1) {
|
||||
List<Map<String,Object>> matchedContent = new ArrayList<Map<String,Object>>();
|
||||
for (Map<String, Object> sqlStat : content) {
|
||||
Integer idStr = (Integer)sqlStat.get("ID");
|
||||
if (idStr.intValue() == opt.getId()) {
|
||||
matchedContent.add(sqlStat);
|
||||
String dbType = (String) sqlStat.get("DbType");
|
||||
String sql = (String) sqlStat.get("SQL");
|
||||
System.out.println(SQLUtils.format(sql, dbType));
|
||||
System.out.println("===============================\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
content = matchedContent;
|
||||
|
||||
|
||||
}
|
||||
if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) {
|
||||
System.out.println(getFormattedOutput(content, sqlFields));
|
||||
System.out.println(getFormattedOutput(content, sqlRowTitle, sqlRowField));
|
||||
} else {
|
||||
System.out.println(getVerticalFormattedOutput(content, sqlFields));
|
||||
System.out.println(getVerticalFormattedOutput(content, sqlColField));
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFormattedOutput(List<Map<String, Object>> content, String[] titleFields) {
|
||||
public static String getFormattedOutput(List<Map<String, Object>> content,
|
||||
String[] title , String[] rowField) {
|
||||
|
||||
List<String[]> printContents = new ArrayList<String[]>();
|
||||
printContents.add(titleFields);
|
||||
printContents.add(title);
|
||||
|
||||
for (Map<String, Object> sqlStat : content) {
|
||||
String[] row = new String[titleFields.length];
|
||||
for (int i = 0; i < titleFields.length; ++i) {
|
||||
Object value = sqlStat.get(titleFields[i]);
|
||||
row[i] = handleAndConvert(value,titleFields[i]);
|
||||
String[] row = new String[rowField.length];
|
||||
for (int i = 0; i < rowField.length; ++i) {
|
||||
Object value = sqlStat.get(rowField[i]);
|
||||
row[i] = handleAndConvert(value,rowField[i]);
|
||||
}
|
||||
printContents.add(row);
|
||||
}
|
||||
@@ -94,10 +142,12 @@ public class TabledDataPrinter {
|
||||
public static String getVerticalFormattedOutput(List<Map<String, Object>> content, String[] titleFields) {
|
||||
List<String[]> printContents = new ArrayList<String[]>();
|
||||
|
||||
int maxCol = content.size() > MAX_COL ? MAX_COL: content.size();
|
||||
|
||||
for (int i = 0; i < titleFields.length; ++i) {
|
||||
String[] row = new String[content.size()+1];
|
||||
String[] row = new String[maxCol+1];
|
||||
row[0] = titleFields[i];
|
||||
for (int j=0; j<content.size(); j++) {
|
||||
for (int j=0; j< maxCol ; j++) {
|
||||
Map<String,Object> sqlStat = content.get(j);
|
||||
Object value = sqlStat.get(titleFields[i]);
|
||||
row[j+1] = handleAndConvert(value, titleFields[i]);
|
||||
@@ -112,6 +162,8 @@ public class TabledDataPrinter {
|
||||
if (value == null) value = "";
|
||||
if (fieldName.equals("SQL")) {
|
||||
String sql = (String)value;
|
||||
sql = sql.replace("\n", " ");
|
||||
sql = sql.replace("\t", " ");
|
||||
if (sql.length() > SQL_MAX_LEN) {
|
||||
sql = sql.substring(0,SQL_MAX_LEN-3) + "...";
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ druid.common = function () {
|
||||
buildFooter : function() {
|
||||
var html = '<footer class="footer">'+
|
||||
' <div class="container">'+
|
||||
' powered by <a href="https://github.com/AlibabaTech/" target="_blank">AlibabaTech</a> & <a href="http://www.sandzhang.com/" target="_blank">sandzhang</a> & <a href="http://melin.iteye.com/" target="_blank">melin</a>'+
|
||||
' powered by <a href="https://github.com/AlibabaTech/" target="_blank">AlibabaTech</a> & <a href="http://www.sandzhang.com/" target="_blank">sandzhang</a> & <a href="http://melin.iteye.com/" target="_blank">melin</a> & <a href="https://github.com/shrekwang" target="_blank">shrek.wang</a>'+
|
||||
' </div>'+
|
||||
'</footer>';
|
||||
$(document.body).append(html);
|
||||
@@ -169,4 +169,4 @@ druid.common = function () {
|
||||
|
||||
$(document).ready(function() {
|
||||
druid.common.init();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
@echo off
|
||||
|
||||
rem Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
rem
|
||||
rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
rem you may not use this file except in compliance with the License.
|
||||
rem You may obtain a copy of the License at
|
||||
rem
|
||||
rem http://www.apache.org/licenses/LICENSE-2.0
|
||||
rem
|
||||
rem Unless required by applicable law or agreed to in writing, software
|
||||
rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
rem See the License for the specific language governing permissions and
|
||||
rem limitations under the License.
|
||||
|
||||
|
||||
java -classpath "../druid.jar" com.alibaba.druid.support.console.DruidStat
|
||||
@@ -35,6 +35,13 @@ public class OptionTest extends TestCase {
|
||||
Assert.assertFalse(opt.printActiveConn());
|
||||
Assert.assertEquals(opt.getStyle(), Option.PrintStyle.VERTICAL);
|
||||
Assert.assertEquals(opt.getVmid(), 1319);
|
||||
|
||||
cmdArray = new String[] {"-sql","-id","5","200"};
|
||||
opt = Option.parseOptions(cmdArray);
|
||||
Assert.assertNotNull(opt);
|
||||
Assert.assertEquals(opt.getId(), 5);
|
||||
Assert.assertEquals(opt.getVmid(), 200);
|
||||
|
||||
|
||||
|
||||
//not enough arguments
|
||||
|
||||
Reference in New Issue
Block a user