mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #110 from wenshao/master
sql.html和sql.json支持dataSourceId参数
This commit is contained in:
@@ -46,7 +46,6 @@ import java.util.Properties;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.proxy.config.AbstractDruidFilterConfig;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ClobProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
|
||||
@@ -133,9 +133,13 @@ public class DruidStatManagerFacade {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
public List<Map<String, Object>> getSqlStatDataList(Integer dataSourceId) {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (Object datasource : getDruidDataSourceInstances()) {
|
||||
if (dataSourceId != null && dataSourceId.intValue() != System.identityHashCode(datasource)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Object sqlStat : DruidDataSourceUtils.getSqlStatMap(datasource).values()) {
|
||||
|
||||
Map<String, Object> data = JdbcSqlStatUtils.getData(sqlStat);
|
||||
|
||||
@@ -79,7 +79,7 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
|
||||
public String service(String url) {
|
||||
|
||||
Map<String, String> parameters = StringUtils.getParameters(url);
|
||||
Map<String, String> parameters = getParameters(url);
|
||||
|
||||
if (url.equals("/basic.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, statManagerFacade.returnJSONBasicStat());
|
||||
@@ -138,7 +138,7 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
if (url.startsWith("/webapp.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, getWebAppStatDataList(parameters));
|
||||
}
|
||||
|
||||
|
||||
if (url.startsWith("/websession.json")) {
|
||||
return returnJSONResult(RESULT_CODE_SUCCESS, getWebSessionStatDataList(parameters));
|
||||
}
|
||||
@@ -187,7 +187,7 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
List<Map<String, Object>> array = WebAppStatManager.getInstance().getSessionStatData();
|
||||
return comparatorOrderBy(array, parameters);
|
||||
}
|
||||
|
||||
|
||||
private List<Map<String, Object>> getWebAppStatDataList(Map<String, String> parameters) {
|
||||
List<Map<String, Object>> array = WebAppStatManager.getInstance().getWebAppStatData();
|
||||
return comparatorOrderBy(array, parameters);
|
||||
@@ -199,7 +199,8 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
|
||||
// when parameters is null
|
||||
String orderBy, orderType = null;
|
||||
Integer page, perPageCount = null;
|
||||
Integer page = DEFAULT_PAGE;
|
||||
Integer perPageCount = DEFAULT_PER_PAGE_COUNT;
|
||||
if (parameters == null) {
|
||||
orderBy = DEFAULT_ORDER_TYPE;
|
||||
orderType = DEFAULT_ORDER_TYPE;
|
||||
@@ -208,15 +209,19 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
} else {
|
||||
orderBy = parameters.get("orderBy");
|
||||
orderType = parameters.get("orderType");
|
||||
page = Integer.parseInt(parameters.get("page"));
|
||||
perPageCount = Integer.parseInt(parameters.get("perPageCount"));
|
||||
String pageParam = parameters.get("page");
|
||||
if (pageParam != null && pageParam.length() != 0) {
|
||||
page = Integer.parseInt(pageParam);
|
||||
}
|
||||
String pageCountParam = parameters.get("perPageCount");
|
||||
if (pageCountParam != null && pageCountParam.length() > 0) {
|
||||
perPageCount = Integer.parseInt(pageCountParam);
|
||||
}
|
||||
}
|
||||
|
||||
// others,such as order
|
||||
orderBy = orderBy == null ? DEFAULT_ORDERBY : orderBy;
|
||||
orderType = orderType == null ? DEFAULT_ORDER_TYPE : orderType;
|
||||
page = page == null ? DEFAULT_PAGE : page;
|
||||
perPageCount = perPageCount == null ? DEFAULT_PER_PAGE_COUNT : perPageCount;
|
||||
|
||||
if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE;
|
||||
|
||||
@@ -234,8 +239,16 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> getSqlStatDataList(Map<String, String> parameters) {
|
||||
List<Map<String, Object>> array = statManagerFacade.getSqlStatDataList();
|
||||
return comparatorOrderBy(array, parameters);
|
||||
Integer dataSourceId = null;
|
||||
|
||||
String dataSourceIdParam = parameters.get("dataSourceId");
|
||||
if (dataSourceIdParam != null && dataSourceIdParam.length() > 0) {
|
||||
dataSourceId = Integer.parseInt(dataSourceIdParam);
|
||||
}
|
||||
|
||||
List<Map<String, Object>> array = statManagerFacade.getSqlStatDataList(dataSourceId);
|
||||
List<Map<String, Object>> sortedArray = comparatorOrderBy(array, parameters);
|
||||
return sortedArray;
|
||||
}
|
||||
|
||||
private String getSqlStat(Integer id) {
|
||||
@@ -310,4 +323,27 @@ public class DruidStatService implements DruidStatServiceMBean {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> getParameters(String url) {
|
||||
if (url == null || (url = url.trim()).length() == 0) {
|
||||
return Collections.<String, String> emptyMap();
|
||||
}
|
||||
|
||||
String parametersStr = StringUtils.subString(url, "?", null);
|
||||
if (parametersStr == null || parametersStr.length() == 0) {
|
||||
return Collections.<String, String> emptyMap();
|
||||
}
|
||||
|
||||
String[] parametersArray = parametersStr.split("&");
|
||||
Map<String, String> parameters = new LinkedHashMap<String, String>();
|
||||
|
||||
for (String parameterStr : parametersArray) {
|
||||
int index = parameterStr.indexOf("=");
|
||||
if (index <= 0) continue;
|
||||
|
||||
String name = parameterStr.substring(0, index);
|
||||
String value = parameterStr.substring(index + 1);
|
||||
parameters.put(name, value);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,21 @@ public class DruidDataSourceUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static long getID(Object druidDatasource) {
|
||||
if (druidDatasource.getClass() == DruidDataSource.class) {
|
||||
return ((DruidDataSource) druidDatasource).getID();
|
||||
}
|
||||
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getID");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (Long) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getID error", e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getName(Object druidDatasource) {
|
||||
if (druidDatasource.getClass() == DruidDataSource.class) {
|
||||
return ((DruidDataSource) druidDatasource).getName();
|
||||
|
||||
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
@@ -75,26 +72,6 @@ public class StringUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> getParameters(String url) {
|
||||
if (url == null || (url = url.trim()).length() == 0) return null;
|
||||
|
||||
String parametersStr = subString(url, "?", null);
|
||||
if (parametersStr == null || parametersStr.length() == 0) return null;
|
||||
|
||||
String[] parametersArray = parametersStr.split("&");
|
||||
Map<String, String> parameters = new LinkedHashMap<String, String>();
|
||||
|
||||
for (String parameterStr : parametersArray) {
|
||||
int index = parameterStr.indexOf("=");
|
||||
if (index <= 0) continue;
|
||||
|
||||
String name = parameterStr.substring(0, index);
|
||||
String value = parameterStr.substring(index + 1);
|
||||
parameters.put(name, value);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public static boolean equals(String a, String b) {
|
||||
if (a == null) {
|
||||
return b == null;
|
||||
|
||||
@@ -118,6 +118,8 @@
|
||||
|
||||
html += '<tr><td valign="top" class="td_lable">PollingConnection Info</td><td><a href="connectionInfo.html?datasourceId=' + datasourceId+
|
||||
'">View</a></td><td>Info for polling connection. <a href="connectionInfo-' + datasourceId + '.json" target="_blank">[View JSON API]</a></td></tr>'
|
||||
html += '<tr><td valign="top" class="td_lable">SQL List</td><td><a href="sql.html?datasourceId=' + datasourceId+
|
||||
'">View</a></td><td>Info for SQL. <a href="sql.json?dataSourceId=' + datasourceId + '" target="_blank">[View JSON API]</a></td></tr>'
|
||||
|
||||
html += '</table></div>';
|
||||
return html;
|
||||
|
||||
@@ -75,14 +75,15 @@
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$.namespace("druid.sql");
|
||||
druid.sql = function () {
|
||||
druid.sql = function () {
|
||||
var dataSourceId = druid.common.getUrlVar("dataSourceId");
|
||||
return {
|
||||
init : function() {
|
||||
$("#dataTable th a").click(function(obj) {
|
||||
druid.common.setOrderBy(obj.target.id.substring(3))
|
||||
})
|
||||
druid.common.buildHead(2);
|
||||
druid.common.ajaxuri = 'sql.json?';
|
||||
druid.common.ajaxuri = dataSourceId ? 'sql.json?dataSourceId=' + dataSourceId + '&' : 'sql.json?';
|
||||
druid.common.handleAjaxResult = druid.sql.handleAjaxResult;
|
||||
druid.common.setOrderBy("SQL");
|
||||
druid.common.ajaxRequestForBasicInfo();
|
||||
|
||||
Reference in New Issue
Block a user