<%
/**
 *	$RCSfile: stats.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:30 $
 */
%>

<%@ page import="java.util.*,
                 com.jivesoftware.forum.*,
                 com.jivesoftware.forum.util.*"
	errorPage="error.jsp"
%>

<%@ include file="include/global.jsp" %>

<%! // Global vars, methods, etc
    
    //
    static float nDays(ResultFilter filter) {
        Date start = filter.getModifiedDateRangeMin();
        Date end = filter.getModifiedDateRangeMax();
        return (float)((float)(end.getTime() - start.getTime())/((float)24.0*3600.0*1000.0));
    }
%>

<%  // Get parameters
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",10);
    
    
    int forumsPerPage = 5;

    
    UserManager userManager = forumFactory.getUserManager();

    Calendar startOfDay = Calendar.getInstance();
        
    startOfDay.set(Calendar.HOUR, 0);
    startOfDay.set(Calendar.MINUTE, 0);
    startOfDay.set(Calendar.SECOND, 0);
    Calendar endOfDay = (Calendar)startOfDay.clone();
    endOfDay.add(Calendar.DATE, 1);
        
    Calendar startOfYesterday = (Calendar)startOfDay.clone();
    startOfYesterday.add(Calendar.DATE, -1);
    Calendar startOfWeek = (Calendar)startOfDay.clone();
    startOfWeek.add(Calendar.DATE, -7);
    Calendar startOfMonth = (Calendar)startOfDay.clone();
    startOfMonth.add(Calendar.MONTH, -1);

    // result filter for today (midnight - present time)
    ResultFilter filterToday = new ResultFilter();
    filterToday.setStartIndex(start);
    filterToday.setNumResults(range);
    filterToday.setModifiedDateRangeMin(startOfDay.getTime());
    filterToday.setModifiedDateRangeMax(endOfDay.getTime());
    float nDaysToday = java.lang.Math.max(nDays(filterToday), (float)0.0001);

    // result filter for yesterday
    ResultFilter filterYesterday = new ResultFilter();
    filterYesterday.setStartIndex(start);
    filterYesterday.setNumResults(range);
    filterYesterday.setModifiedDateRangeMin(startOfYesterday.getTime());
    filterYesterday.setModifiedDateRangeMax(startOfDay.getTime());
    float nDaysYesterday = nDays(filterYesterday);

    // result filter for last week (not including today)
    ResultFilter filterWeek = new ResultFilter();
    filterWeek.setStartIndex(start);
    filterWeek.setNumResults(range);
    filterWeek.setModifiedDateRangeMin(startOfWeek.getTime());
    filterWeek.setModifiedDateRangeMax(startOfDay.getTime());
    float nDaysWeek = nDays(filterWeek);

    // result filter for last month (not including today)
    ResultFilter filterMonth = new ResultFilter();
    filterMonth.setStartIndex(start);
    filterMonth.setNumResults(range);
    filterMonth.setModifiedDateRangeMin(startOfMonth.getTime());
    filterMonth.setModifiedDateRangeMax(startOfDay.getTime());
    float nDaysMonth = nDays(filterMonth);
%>

<%@ include file="include/header.jsp" %>

<p>

<%  // Title of this page and breadcrumbs
    String title = "Basic Statistics";
    String[][] breadcrumbs = {
        {"Main", "main.jsp"},
        {"Basic Stats", "stats.jsp"}
    };
%>
<%@ include file="include/title.jsp" %>

<font size="-1">
Below is a simple summary of your forum activity.
</font>

<p>

<font size="-1">
<%  int userCount = userManager.getUserCount();
    int fCount = forumFactory.getForumCount();
%>
There <%= ((userCount==1)?"is":"are") %> <b><%= userCount %></b> user<%= ((userCount==1)?"":"s") %>
reading <b><%= fCount %></b> forum<%= ((fCount==1)?"":"s") %>.
</font>
<p>

<font size="-1"><b>New Threads &amp; Messages</b></font><p>

<font size="-1">
This is a measurement of new messages per time period.
</font>
<p>

<table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr><td>
<table bgcolor="<%= tableBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#eeeeee">
    <td align="center" rowspan="2"><font size="-2" face="verdana"><b>FORUM</b></font></td>
    <td align="center" colspan="2"><font size="-2" face="verdana"><b>TODAY</b></font></td>
    <td align="center" colspan="2"><font size="-2" face="verdana"><b>YESTERDAY</b></font></td>
    <td align="center" colspan="2"><font size="-2" face="verdana"><b>PAST WEEK</b></font></td>
    <td align="center" colspan="2"><font size="-2" face="verdana"><b>PAST MONTH</b></font></td>
</tr>
<tr bgcolor="#eeeeee">
    <td align="center"><font size="-2" face="verdana">THREADS</font></td>
    <td align="center"><font size="-2" face="verdana">MESSAGES</font></td>
    <td align="center"><font size="-2" face="verdana">THREADS</font></td>
    <td align="center"><font size="-2" face="verdana">MESSAGES</font></td>
    <td align="center"><font size="-2" face="verdana">THREADS</font></td>
    <td align="center"><font size="-2" face="verdana">MESSAGES</font></td>
    <td align="center"><font size="-2" face="verdana">THREADS</font></td>
    <td align="center"><font size="-2" face="verdana">MESSAGES</font></td>
</tr>

    <%
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);
    
        Iterator itForum = forumFactory.forums();
        
        int idx=0;
        while (idx++ < start && itForum.hasNext()) {
            itForum.next();
        }
        // show "range" number of forums
        idx=0;
        
        int forumCount = 0;
        long count;
        float days;
    
        while (idx++ < range && itForum.hasNext()) {
            Forum forum = (Forum)itForum.next();
            ++forumCount;
            int todayMessageCount = forum.getMessageCount(filterToday);
            int yesterdayMessageCount = forum.getMessageCount(filterYesterday);
            int weekMessageCount = forum.getMessageCount(filterWeek);
            int monthMessageCount = forum.getMessageCount(filterMonth);
            
            int todayThreadCount = forum.getThreadCount(filterToday);
            int yesterdayThreadCount = forum.getThreadCount(filterYesterday);
            int weekThreadCount = forum.getThreadCount(filterWeek);
            int monthThreadCount = forum.getThreadCount(filterMonth);
%>
<tr bgcolor="#ffffff">
    <td bgcolor="#ebf1f9"><font size="-1"><b><%= forum.getName() %></b></font></td>
    <td align="center"><font size="-1"><%= todayMessageCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)todayMessageCount/nDaysToday) %>/day</font></td>
    <td align="center"><font size="-1"><%= todayThreadCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)todayThreadCount/nDaysToday) %>/day</font></td>
    <td align="center" bgcolor="#ebf1f9"><font size="-1"><%= yesterdayThreadCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)yesterdayThreadCount/nDaysYesterday) %>/day</font></td>
    <td align="center" bgcolor="#ebf1f9"><font size="-1"><%= yesterdayMessageCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)yesterdayMessageCount/nDaysYesterday) %>/day</font></td>
    <td align="center"><font size="-1"><%= weekThreadCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)weekThreadCount/nDaysWeek) %>/day</font></td>
    <td align="center"><font size="-1"><%= weekMessageCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)weekMessageCount/nDaysWeek) %>/day</font></td>
    <td align="center" bgcolor="#ebf1f9"><font size="-1"><%= monthThreadCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)monthThreadCount/nDaysMonth) %>/day</font></td>
    <td align="center" bgcolor="#ebf1f9"><font size="-1"><%= monthMessageCount %><br></font><font size="-2" color="#666666"><%= nf.format((float)monthMessageCount/nDaysMonth) %>/day</font></td>
</tr>
<%  } %>
</table>
</td></tr>
</table>

<p>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<%  if (start > 0) { %>
	<td width="1%" nowrap><font size="-1"><a href="stats.jsp?start=<%= start-range %>">Stats for previous <%= range %> forums</a></font></td>
<%  } else { %>
	<td width="1%" nowrap><font size="-1">&nbsp;</font></td>
<%  } %>

<%  if (start > 0) { %>
	<td width="98%" align="center"><font size="-1"><a href="stats.jsp?start=0">Front Page</a></font></td>
<%  } else { %>
	<td width="98%" nowrap><font size="-1">&nbsp;</font></td>
<%  } %>

<%  if (start+range < forumFactory.getForumCount()) { %>
	<td width="1%" nowrap><font size="-1"><a href="stats.jsp?start=<%= start+range %>">Stats for next <%= range %> forums</a></font></td>
<%  } else { %>
	<td width="1%" nowrap><font size="-1">&nbsp;</font></td>
<%  } %>
</tr>
</table>

<%@ include file="include/footer.jsp" %>