add stat service

This commit is contained in:
wenshao
2012-08-03 20:19:00 +08:00
parent 4a39c6331c
commit ffdd81f194
3 changed files with 146 additions and 8 deletions
@@ -51,19 +51,31 @@ public class DruidStatService {
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
DataSourceInfo dataSourceStat = new DataSourceInfo();
dataSourceStat.setId(dataSource.getID());
dataSourceStat.setUrl(dataSource.getUrl());
dataSourceStat.setDbType(dataSource.getDbType());
dataSourceStat.setActiveCount(dataSource.getActiveCount());
dataSourceStat.setActivePeak(dataSource.getActivePeak());
dataSourceStat.setCloseCount((int) dataSource.getCloseCount());
dataSourceStat.setConnectCount((int) dataSource.getConnectCount());
dataSourceStat.setConnectErrorCount((int) dataSource.getConnectErrorCount());
dataSourceStat.setCreateCount((int) dataSource.getCreateCount());
dataSourceStat.setCreateErrorCount((int) dataSource.getCreateErrorCount());
dataSourceStat.setDestoryCount((int) dataSource.getDestroyCount());
dataSourceStat.setExecuteCount((int) dataSource.getExecuteCount());
dataSourceStat.setPoolingCount(dataSource.getPoolingCount());
Collection<JdbcSqlStat> sqlStats = dataSource.getDataSourceStat().getSqlStatMap().values();
List<SqlInfo> sqlStatInfoList = new ArrayList<SqlInfo>(sqlStats.size());
for (JdbcSqlStat sqlStat : dataSource.getDataSourceStat().getSqlStatMap().values()) {
SqlInfo sqlStatInfo = new SqlInfo();
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
continue;
}
sqlStatInfo.setSql(sqlStat.getSql());
sqlStatInfo.setExecuteCount((int) sqlStat.getExecuteCount());
sqlStatInfo.setRunningCount((int) sqlStat.getRunningCount());
@@ -72,12 +84,12 @@ public class DruidStatService {
sqlStatInfo.setInTransactionCount((int) sqlStat.getInTransactionCount());
sqlStatInfo.setFetchRowCount(sqlStat.getFetchRowCount());
sqlStatInfo.setUpdateCount(sqlStat.getUpdateCount());
sqlStatInfoList.add(sqlStatInfo);
}
dataSourceStat.setSqlList(sqlStatInfoList);
dataSourceStatList.add(dataSourceStat);
}
@@ -4,9 +4,26 @@ import java.util.List;
public class DataSourceInfo {
private long id;
private String url;
private String dbType;
private int activeCount;
private int activePeak;
private int poolingCount;
private int connectCount;
private int connectErrorCount;
private int createCount;
private int createErrorCount;
private int executeCount;
private int closeCount;
private int destoryCount;
private List<SqlInfo> sqlList;
public String getDbType() {
@@ -33,4 +50,92 @@ public class DataSourceInfo {
this.sqlList = sqlList;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getActiveCount() {
return activeCount;
}
public void setActiveCount(int activeCount) {
this.activeCount = activeCount;
}
public int getActivePeak() {
return activePeak;
}
public void setActivePeak(int activePeak) {
this.activePeak = activePeak;
}
public int getPoolingCount() {
return poolingCount;
}
public void setPoolingCount(int poolingCount) {
this.poolingCount = poolingCount;
}
public int getConnectCount() {
return connectCount;
}
public void setConnectCount(int connectCount) {
this.connectCount = connectCount;
}
public int getConnectErrorCount() {
return connectErrorCount;
}
public void setConnectErrorCount(int connectErrorCount) {
this.connectErrorCount = connectErrorCount;
}
public int getCreateCount() {
return createCount;
}
public void setCreateCount(int createCount) {
this.createCount = createCount;
}
public int getCreateErrorCount() {
return createErrorCount;
}
public void setCreateErrorCount(int createErrorCount) {
this.createErrorCount = createErrorCount;
}
public int getExecuteCount() {
return executeCount;
}
public void setExecuteCount(int executeCount) {
this.executeCount = executeCount;
}
public int getCloseCount() {
return closeCount;
}
public void setCloseCount(int closeCount) {
this.closeCount = closeCount;
}
public int getDestoryCount() {
return destoryCount;
}
public void setDestoryCount(int destoryCount) {
this.destoryCount = destoryCount;
}
}
@@ -1,16 +1,37 @@
package com.alibaba.druid.stat.service.impl;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import com.alibaba.druid.stat.service.DruidStatStore;
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
public class DruidStatMemoryStore implements DruidStatStore {
private ConcurrentHashMap<Long, DataSourceData> dataSourceDataMap = new ConcurrentHashMap<Long, DataSourceData>();
@Override
public void saveDataSource(long timeMillis, List<DataSourceInfo> dataSourceList) {
for (DataSourceInfo dataSourceInfo : dataSourceList) {
DataSourceData data = dataSourceDataMap.get(dataSourceInfo.getId());
if (data == null) {
dataSourceDataMap.putIfAbsent(dataSourceInfo.getId(), new DataSourceData());
data = dataSourceDataMap.get(dataSourceInfo.getId());
}
data.add(timeMillis, dataSourceInfo);
}
}
public static class DataSourceData {
private ConcurrentSkipListMap<Long, DataSourceInfo> data = new ConcurrentSkipListMap<Long, DataSourceInfo>();
public void add(long timeMillis, DataSourceInfo dataSourceInfo) {
data.put(timeMillis, dataSourceInfo);
}
}
}