* @see org.apache.jetspeed.statistics.PortalStatistics#queryStatistics(org.apache.jetspeed.statistics.StatisticsQueryCriteria)
*/
public AggregateStatistics queryStatistics(StatisticsQueryCriteria criteria)
throws InvalidCriteriaException
{
AggregateStatistics as = new AggregateStatisticsImpl();
String query;
String query2;
String tableName;
String groupColumn;
Date end = new Date();
Date start = getStartDateFromPeriod(criteria.getTimePeriod(), end);
String queryType = criteria.getQueryType();
if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
{
tableName = "USER_STATISTICS";
groupColumn = "USER_NAME";
} else if (PortalStatistics.QUERY_TYPE_PORTLET.equals(queryType))
{
tableName = "PORTLET_STATISTICS";
groupColumn = "PORTLET";
} else if (PortalStatistics.QUERY_TYPE_PAGE.equals(queryType))
{
tableName = "PAGE_STATISTICS";
groupColumn = "PAGE";
} else
{
throw new InvalidCriteriaException(
" invalid queryType passed to queryStatistics");
}
String orderColumn = "itemcount";
String ascDesc = "DESC";
if (!PortalStatistics.QUERY_TYPE_USER.equals(queryType))
{
query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
+ tableName + " where time_stamp > ? and time_stamp < ?";
query2 = "select count(*) as itemcount ,"
+ groupColumn
+ ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
+ "from " + tableName
+ " where time_stamp > ? and time_stamp < ? group by "
+ groupColumn + " order by " + orderColumn + " " + ascDesc;
} else
{
query = "select count(*) as itemcount , MIN(ELAPSED_TIME) as amin,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax from "
+ tableName
+ " where time_stamp > ? and time_stamp < ? and status = 2";
query2 = "select count(*) as itemcount ,"
+ groupColumn
+ ", MIN(ELAPSED_TIME) as amin ,AVG(ELAPSED_TIME) as aavg ,MAX(ELAPSED_TIME) as amax "
+ "from "
+ tableName
+ " where time_stamp > ? and time_stamp < ? and status = 2 group by "
+ groupColumn + " order by " + orderColumn + " " + ascDesc;
}
Connection con = null;
try
{
con = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setTimestamp(1, new Timestamp(start.getTime()));
pstmt.setTimestamp(2, new Timestamp(end.getTime()));
ResultSet rs = pstmt.executeQuery();
float denominator = 1.0f;
if (PortalStatistics.QUERY_TYPE_USER.equals(queryType))
{
denominator = 1000f * 60f; // this should convert from mS to
// minutes
}
if (rs.next())
{
as.setHitCount(rs.getInt("itemcount"));
as.setMinProcessingTime(rs.getFloat("amin") / denominator);
as.setAvgProcessingTime(rs.getFloat("aavg") / denominator);
as.setMaxProcessingTime(rs.getFloat("amax") / denominator);
}
PreparedStatement pstmt2 = con.prepareStatement(query2);
pstmt2.setTimestamp(1, new Timestamp(start.getTime()));
pstmt2.setTimestamp(2, new Timestamp(end.getTime()));
ResultSet rs2 = pstmt2.executeQuery();
int rowCount = 0;
int totalRows = 5;
String listsizeStr = criteria.getListsize();
int temp = -1;
try
{
temp = Integer.parseInt(listsizeStr);
}
catch (NumberFormatException e)
{
}
if(temp != -1) {
totalRows = temp;
}
while ((rs2.next()) && (rowCount < totalRows))
{
Map row = new HashMap();
row.put("count", "" + rs2.getInt("itemcount"));
String col = rs2.getString(groupColumn);
int maxColLen = 35;
if (col != null)
{
if (col.length() > maxColLen)
{
col = col.substring(0, maxColLen);
}
}
row.put("groupColumn", col);
row.put("min", ""
+ floatFormatter(rs2.getFloat("amin") / denominator));
row.put("avg", ""
+ floatFormatter(rs2.getFloat("aavg") / denominator));
row.put("max", ""
+ floatFormatter(rs2.getFloat("amax") / denominator));
as.addRow(row);
rowCount++;
}
}
catch (SQLException e)