StarViewServlet:add sort support for sql.json and sqlview page

This commit is contained in:
sand
2012-07-25 11:29:38 +08:00
parent af0ca2ebab
commit d7d0a955cb
5 changed files with 151 additions and 25 deletions
@@ -25,6 +25,7 @@ import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.stat.JdbcSqlStat;
import com.alibaba.druid.stat.JdbcStatManager;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.SortUtils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
@@ -41,9 +42,14 @@ public class StatViewServlet extends HttpServlet {
private final static int RESULT_CODE_SUCCESS = 1;
private final static int RESULT_CODE_ERROR = -1;
private final static int DEFAULT_PAGE = 1;
private final static int DEFAULT_PER_PAGE_COUNT = Integer.MAX_VALUE;
private final static String RESOURCE_PATH = "support/http/resources";
private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html";
private static final String DEFAULT_ORDER_TYPE = "asc";
public String templatePage;
public void init() throws ServletException {
@@ -96,7 +102,7 @@ public class StatViewServlet extends HttpServlet {
}
if (path.equals("/sql.json")) {
returnJSONResult(request, response, RESULT_CODE_SUCCESS, getSqlStatDataList());
returnJSONResult(request, response, RESULT_CODE_SUCCESS, getSqlStatDataList(request));
return;
}
@@ -258,14 +264,33 @@ public class StatViewServlet extends HttpServlet {
return null;
}
private List<Object> getSqlStatDataList() {
List<Object> array = new ArrayList<Object>();
private List<Map<String, Object>> getSqlStatDataList(HttpServletRequest request) {
String orderBy = request.getParameter("orderBy");
String orderType = request.getParameter("orderType");
Integer page = StringUtils.stringToInteger(request.getParameter("page"));
Integer perPageCount = StringUtils.stringToInteger(request.getParameter("perPageCount"));
if (!"desc".equals(orderType)) orderType = DEFAULT_ORDER_TYPE;
if (page == null) page = DEFAULT_PAGE;
if (perPageCount == null) perPageCount = DEFAULT_PER_PAGE_COUNT;
List<Map<String, Object>> array = new ArrayList<Map<String, Object>>();
for (DruidDataSource datasource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
for (JdbcSqlStat sqlStat : datasource.getDataSourceStat().getSqlStatMap().values()) {
array.add(getSqlStatData(sqlStat));
}
}
return array;
// orderby
SortUtils.sortMapList(orderBy, array, DEFAULT_ORDER_TYPE.equals(orderType));
// page
int fromIndex = (page - 1) * perPageCount;
int toIndex = page * perPageCount;
if (toIndex > array.size()) toIndex = array.size();
return array.subList(fromIndex, toIndex);
}
private Map<String, Object> dataSourceToMapData(DruidDataSource dataSource) {
@@ -0,0 +1,59 @@
package com.alibaba.druid.util;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author sandzhang<sandzhangtoo@gmail.com>
*/
public class SortUtils {
public static void sortMapList(final String orderByKey, List<Map<String, Object>> targetList, final boolean isDesc) {
if (orderByKey == null || orderByKey.length() == 0) return;
Collections.sort(targetList, new Comparator<Map<String, Object>>() {
private int compare(Number o1, Number o2) {
return (int) (o1.doubleValue() - o2.doubleValue());
}
private int compare(String o1, String o2) {
return Collator.getInstance().compare(o1, o2);
}
private int compare(Date o1, Date o2) {
return (int) (o1.getTime() - o2.getTime());
}
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int result = compare_0(o1, o2);
if (isDesc) result = -result;
return result;
}
public int compare_0(Map<String, Object> o1, Map<String, Object> o2) {
Object v1 = o1.get(orderByKey);
Object v2 = o2.get(orderByKey);
if (v1 == null && v2 == null) return 0;
if (v1 == null) return -1;
if (v2 == null) return 1;
if (v1 instanceof Number) return compare((Number) v1, (Number) v2);
if (v1 instanceof String) return compare((String) v1, (String) v2);
if (v1 instanceof Date) return compare((Date) v1, (Date) v2);
return 0;
}
});
}
}