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

<%@ page import="java.util.*,
				 java.text.*,
				 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
				 com.jivesoftware.forum.database.*,
				 com.jivesoftware.forum.util.*"
%>

<%!	// global variables
	
	// decimal formatter for cache values
	static final DecimalFormat mbFormat = new DecimalFormat("#0.00");
	static final DecimalFormat percentFormat = new DecimalFormat("#0.0");
    
	// cache identifiers, names
	static final int[] CACHE_TYPES = {
		DatabaseCacheManager.FORUM_CACHE,
		DatabaseCacheManager.THREAD_CACHE,
		DatabaseCacheManager.MESSAGE_CACHE,
		DatabaseCacheManager.USER_CACHE,
		DatabaseCacheManager.USER_PERMS_CACHE,
		DatabaseCacheManager.GROUP_CACHE
	};
	static final String[] CACHE_NAMES = {
		"Forum",
		"Thread",
		"Message",
		"User",
		"User Permissions",
		"Group"
	};
    // variable for the VM memory monitor box
    static final int NUM_BLOCKS = 50;
%>
 
<%@ include file="include/global.jsp" %>
 
<%	// get parameters
	boolean cacheEnabled = ParamUtils.getBooleanParameter(request,"cacheEnabled");
	boolean doCache = ParamUtils.getBooleanParameter(request,"doCache");
    boolean clearCache = ParamUtils.getBooleanParameter(request,"clearCache");
	
	// Get the underlying DbForumFactory
	DbForumFactory dbForumFactory = null;
	try {
		dbForumFactory = (DbForumFactory)(((DbForumFactoryProxy)forumFactory).getProxiedForumFactory());
	} catch (Exception e) {}
	
	// Get a cache manager
	DatabaseCacheManager cacheManager = dbForumFactory.getCacheManager();
	
    if (clearCache) {
        for (int i=0; i<CACHE_TYPES.length; i++) {
            cacheManager.clear(CACHE_TYPES[i]);
        }
        response.sendRedirect("cache.jsp");
        return;
    }
    
	// turn the cache on or off
	if (doCache) {
		cacheManager.setCacheEnabled(cacheEnabled);
		response.sendRedirect("cache.jsp");
		return;
	}
	cacheEnabled = cacheManager.isCacheEnabled();
%>

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

<p>

<%  // Title of this page and breadcrumbs
    String title = "Cache Settings";
    String[][] breadcrumbs = {
        {"Main", "main.jsp"},
        {"Cache Settings", "cache.jsp"}
    };
%>
<%@ include file="include/title.jsp" %>

<font size="-1">
Jive Forums relies on its cache to process forum data efficiently. Use this panel
to monitor and modify your cache settings. There are very few circumstances where 
you should entirely disable cache However, you can edit the cache sizes to tune for
minimal memory use or maximum performance.
</font>

<p>

<font size="-1"><b>Cache Status</b></font>
<ul>
    <font size="-1">
    You can enable or disable caching in the Jive system by using the form
    below. Disabling cache will severely degrade performance.
    </font>
    <form action="cache.jsp">
    <input type="hidden" name="doCache" value="true">
    <table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="300">
    <td>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
    <tr bgcolor="#ffffff">
	<td align="center"<%= (cacheEnabled)?" bgcolor=\"#99cc99\"":"" %>>
        <font size="-1">
		<input type="radio" name="cacheEnabled" value="true" id="rb01"
		 <%= (cacheEnabled)?"checked":"" %>>
		<label for="rb01"><%= (cacheEnabled)?"<b>On</b>":"On" %></label>
        </font>
	</td>
	<td align="center"<%= (!cacheEnabled)?" bgcolor=\"#cc6666\"":"" %>>
        <font size="-1">
		<input type="radio" name="cacheEnabled" value="false" id="rb02"
		 <%= (!cacheEnabled)?"checked":"" %>>
		<label for="rb02"><%= (!cacheEnabled)?"<b>Off</b>":"Off" %></label>
        </font>
	</td>
	<td align="center">
		<input type="submit" value="Update">
	</td>
    </tr>
    </table>
    </td>
    </table>
    </form>
    <%  if (cacheEnabled) { %>
    <p>
    <font size="-1">
    Clear all caches by clicking the button below.
    </font>
    <form action="cache.jsp">
    <input type="hidden" name="clearCache" value="true">
    <input type="submit" value="Clear All Caches">
    </form>
    <%  } %>
</ul>

<%  if (cacheEnabled) { %>
<p>

<font size="-1"><b>Cache Performance Summary</b></font>
<ul>
    <font size="-1">
    Below is a summary of all caches. To adjust the
    size of each cache, click the "Edit Cache Sizes" button below.
    <p>
    Effectiveness
    measures how well your cache is working. If the effectiveness is low, that
    usually means that the cache is too small. Caches for which this may be
    the case are flagged below.
    </font>
    <p>
	<table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0">
	<td>
	<table cellpadding="4" cellspacing="1" border="0" width="100%">
	<tr bgcolor="#eeeeee">
	<td align="center"><font size="-2" face="verdana"><b>CACHE TYPE</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>SIZE</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>OBJECTS</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>EFFECTIVENESS</b></font></td>
    </tr>

<%	// cache variables
	double memUsed;
	double totalMem;
	double freeMem;
	double hitPercent;
	long hits;
	long misses;
	
	// Loop through each cache, print out its info
	for( int i=0; i<CACHE_TYPES.length; i++ ) {
		String name = CACHE_NAMES[i];
		int type = CACHE_TYPES[i];
		memUsed = (double)cacheManager.getSize(type)/(1024*1024);
		totalMem = (double)cacheManager.getMaxSize(type)/(1024*1024);
		freeMem = 100 - 100*memUsed/totalMem;
		hits = cacheManager.getHits(type);
		misses = cacheManager.getMisses(type);
		if (hits + misses == 0) {
			hitPercent = 0.0;
		}
		else {
			hitPercent = 100*(double)hits/(hits+misses);
		}
        boolean lowEffec = (hits > 500 && hitPercent < 85.0 && freeMem < 20.0);
%>
	<tr bgcolor="#ffffff">
		<td><font size="-1"><%= name %></font></td>
		<td align="right">
            <font size="-1">
            &nbsp;
			<%= mbFormat.format(totalMem) %> MB,
			<%= percentFormat.format(freeMem)%>% free
            &nbsp;
            </font>
		</td>
		<td align="right">
			<font size="-1">
            &nbsp;
            <%= cacheManager.getNumElements(type) %>
            &nbsp;
            </font>
		</td>
		<td align="right">
            <font size="-1">
            &nbsp;
            <%  if (lowEffec) { %>
			<font color="#ff0000"><b><%= percentFormat.format(hitPercent)%>%</b></font>
            <%  } else { %>
			<b><%= percentFormat.format(hitPercent)%>%</b>
            <%  } %>
			(<%= hits %> hits, <%= misses %> misses)
            &nbsp;
            </font>
		</td> 
	</tr>
<%	} %>
			
	</table>
	</td>
	</table>
    <p>
    <form action="editCache.jsp">
    <input type="submit" value="Edit Cache Sizes">
    </form>

</ul>
<%  } // end if cacheEnabled %>

<p>

<font size="-1"><b>Java VM Memory</b></font>
<ul>

<%	// The java runtime
	Runtime runtime = Runtime.getRuntime();
    
    double freeMemory = (double)runtime.freeMemory()/(1024*1024);
	double totalMemory = (double)runtime.totalMemory()/(1024*1024);
	double usedMemory = totalMemory - freeMemory;
	double percentFree = ((double)freeMemory/(double)totalMemory)*100.0;
    int free = 100-(int)Math.round(percentFree);
%>
	<table border=0>
	<tr><td><font size="-1">Used Memory:</font></td>
        <td><font size="-1"><%= mbFormat.format(usedMemory) %> MB</font></td>
    </tr>
	<tr><td><font size="-1">Total Memory:</font></td>
        <td><font size="-1"><%= mbFormat.format(totalMemory) %> MB</font></td>
    </tr>
	</table>
	<br>
    <table border=0><td>	
    <table bgcolor="#000000" cellpadding="1" cellspacing="0" border="0" width="200" align=left>
    <td>
    <table bgcolor="#000000" cellpadding="1" cellspacing="1" border="0" width="100%">
<%    for (int i=0; i<NUM_BLOCKS; i++) {
        if ((i*(100/NUM_BLOCKS)) < free) {
    %>
    	<td bgcolor="#00ff00" width="<%= (100/NUM_BLOCKS) %>%"><img src="images/blank.gif" width="1" height="15" border="0"></td>
<%		} else { %>
    	<td bgcolor="#006600" width="<%= (100/NUM_BLOCKS) %>%"><img src="images/blank.gif" width="1" height="15" border="0"></td>
<%		}
    }
%>
    </table>
    </td>
    </table></td><td>
        <font size="-1">
        &nbsp;<b><%= percentFormat.format(percentFree) %>% free</b>
        </font>
    </td></table>
</ul>

<%	// Destroy the runtime reference
	runtime = null;
%>

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

