diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index df5911356b..a0f2f09233 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -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 getSqlStatDataList() { - List array = new ArrayList(); + private List> 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> array = new ArrayList>(); 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 dataSourceToMapData(DruidDataSource dataSource) { diff --git a/src/main/java/com/alibaba/druid/util/SortUtils.java b/src/main/java/com/alibaba/druid/util/SortUtils.java new file mode 100644 index 0000000000..409190d51d --- /dev/null +++ b/src/main/java/com/alibaba/druid/util/SortUtils.java @@ -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 + */ +public class SortUtils { + + public static void sortMapList(final String orderByKey, List> targetList, final boolean isDesc) { + if (orderByKey == null || orderByKey.length() == 0) return; + + Collections.sort(targetList, new Comparator>() { + + 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 o1, Map o2) { + int result = compare_0(o1, o2); + + if (isDesc) result = -result; + + return result; + } + + public int compare_0(Map o1, Map 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; + } + }); + } +} diff --git a/src/main/resources/support/http/resources/api.html b/src/main/resources/support/http/resources/api.html index 315603e33e..1b09db4edb 100644 --- a/src/main/resources/support/http/resources/api.html +++ b/src/main/resources/support/http/resources/api.html @@ -6,7 +6,7 @@ - +