mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
StatViewServlet: add activeConnectionStackTrace support, refactor js
This commit is contained in:
@@ -102,6 +102,16 @@ public class StatViewServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.startsWith("/activeConnectionStackTrace-")) {
|
||||
Integer id = StringUtils.subStringToInteger(path, "activeConnectionStackTrace-", ".");
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
|
||||
if (path.endsWith(".json")) returnJSONActiveConnectionStackTrace(datasource, request, response);
|
||||
if (path.endsWith(".html")) returnViewActiveConnectionStackTrace(datasource, request, response);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.equals("/sql.json")) {
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, getSqlStatDataList(request));
|
||||
return;
|
||||
@@ -127,6 +137,30 @@ public class StatViewServlet extends HttpServlet {
|
||||
returnResourceFile(path, response);
|
||||
}
|
||||
|
||||
private void returnJSONActiveConnectionStackTrace(DruidDataSource datasource, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource.isRemoveAbandoned()) {
|
||||
List<String> result = datasource.getActiveConnectionStackTrace();
|
||||
returnJSONResult(request, response, RESULT_CODE_SUCCESS, result);
|
||||
} else {
|
||||
returnJSONResult(request, response, RESULT_CODE_ERROR, "require set removeAbandoned=true");
|
||||
}
|
||||
}
|
||||
|
||||
private void returnViewActiveConnectionStackTrace(DruidDataSource datasource, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
if (datasource == null) return;
|
||||
|
||||
if (!datasource.isRemoveAbandoned()) {
|
||||
response.getWriter().print(mergeTemplatePage("ERROR", "require set removeAbandoned=true"));
|
||||
return;
|
||||
}
|
||||
|
||||
String text = IOUtils.readFromResource(RESOURCE_PATH + "/activeConnectionStackTrace.html");
|
||||
text = text.replaceAll("\\{datasourceId\\}", String.valueOf(System.identityHashCode(datasource)));
|
||||
response.getWriter().print(text);
|
||||
}
|
||||
|
||||
private void returnViewSqlStat(JdbcSqlStat sqlStat, HttpServletResponse response) throws IOException {
|
||||
if (sqlStat == null) return;
|
||||
|
||||
@@ -343,6 +377,9 @@ public class StatViewServlet extends HttpServlet {
|
||||
|
||||
dataMap.put("StartTransactionCount", dataSource.getStartTransactionCount());
|
||||
dataMap.put("TransactionHistogramValues", dataSource.getTransactionHistogramValues());
|
||||
|
||||
dataMap.put("RemoveAbandoned", dataSource.isRemoveAbandoned());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.text.Collator;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
@@ -11,9 +12,9 @@ import java.util.Map;
|
||||
public class MapComparator<K extends Object, V extends Object> implements Comparator<Map<K, V>> {
|
||||
|
||||
private boolean isDesc;
|
||||
private String orderByKey;
|
||||
private K orderByKey;
|
||||
|
||||
public MapComparator(String orderByKey, boolean isDesc) {
|
||||
public MapComparator(K orderByKey, boolean isDesc) {
|
||||
this.orderByKey = orderByKey;
|
||||
this.isDesc = isDesc;
|
||||
}
|
||||
@@ -39,9 +40,24 @@ public class MapComparator<K extends Object, V extends Object> implements Compar
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object getValueByKey(Map<K, V> map, K key) {
|
||||
if (key instanceof String) {
|
||||
String keyStr = (String) key;
|
||||
if (keyStr.matches(".+\\[[0-9]+\\]")) {
|
||||
Object value = map.get(keyStr.substring(0, keyStr.indexOf("[")));
|
||||
Integer index = StringUtils.subStringToInteger(keyStr, "[", "]");
|
||||
if (value.getClass().isArray() && Array.getLength(value) >= index) {
|
||||
return Array.get(value, index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public int compare_0(Map<K, V> o1, Map<K, V> o2) {
|
||||
Object v1 = o1.get(orderByKey);
|
||||
Object v2 = o2.get(orderByKey);
|
||||
Object v1 = getValueByKey(o1, orderByKey);
|
||||
Object v2 = getValueByKey(o2, orderByKey);
|
||||
|
||||
if (v1 == null && v2 == null) return 0;
|
||||
if (v1 == null) return -1;
|
||||
|
||||
Reference in New Issue
Block a user