<%

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0); // Proxies
%>
<%@page import="com.capgemini.framework.common.MemoryHelper" %>
<%@page import="com.capgemini.framework.common.SettingsHelper" %>
<%@page import="com.capgemini.reports.database.javabean.DbMemMonitor"%>
<%@page import="com.capgemini.reports.database.manager.MngMemMonitor"%>
<%@page import="java.util.List" %>
<%
	
	/* Retreive parameter */
	String theMonitoring = MemoryHelper.monitorMemory();
	Integer theMsFrequency = SettingsHelper.getInteger("MngMemMonitor.frequency") * 1000;
	
	/* Browse last memory traces */
	Double theMaxMemory = 0D;
	int theIndexCount = 0;
	StringBuilder theJsArray = new StringBuilder();
	MngMemMonitor theMemMonitor = new MngMemMonitor();
	List<DbMemMonitor> theMonitorList = theMemMonitor.searchMemMonitor();
	for (DbMemMonitor currentMonitor : theMonitorList){
		
		/* Store max memory estimation */
		if (currentMonitor.getMaxMemory() > theMaxMemory){
			theMaxMemory = currentMonitor.getMaxMemory();
		}

        theJsArray.append("\t\tmemArray[").append(theIndexCount).append("] = new Array();\n");
        theJsArray.append("\t\tmemArray[").append(theIndexCount).append("]['freeMem'] = ").append(currentMonitor.getUsedMemory()).append(";\n");
        theJsArray.append("\t\tmemArray[").append(theIndexCount).append("]['monitorDate'] = '").append(currentMonitor.getMonitorDate()).append("';\n");
		theIndexCount ++;
	}
	
%><!DOCTYPE html>
<html>
<head>
	<title>Suivi m&eacute;moire</title>
	<link rel="stylesheet" href="/kurt-web/css/kurt-style-frame.css">
	<script src="/kurt-web/js/kurt-common-submit.js"></script>
</head>
<body>

	<h1>Suivi m&eacute;moire</h1>
			
	<center>
	<div align="center">
		
		<table cellspacing="0" cellpadding="0" border="0"><tr><td valign="bottom" align="left" style="height:<%= theMaxMemory %>px;width:<%= String.valueOf(SettingsHelper.getInteger("MngMemMonitor.maxTraceCount") * 15 + 55) %>px;border-bottom:2px solid #DDDDFF;border-left:1px solid #EEEEFF;">
		<script>
		var memArray = new Array();
<%= theJsArray.toString() %>

		var t = '';
		for (var i=0; i<memArray.length; i++)
		{
			var currentPos = memArray.length - i - 1;
			t += '<DIV style="height:' + memArray[currentPos]['freeMem'] + 'px;width:15px;border:1px solid #EEEEFF;display:inline-block;'
			+ 'background-color:#88FF88;cursor:pointer;" title="' + memArray[currentPos]['monitorDate'] + " = " + memArray[currentPos]['freeMem'] + 'Mo"></DIV>';
		}
		document.write(t);
		
		</script>
		</td></tr></table>
		
		<div style="position:absolute;top:50px;left:10px;width:250px;border:3px double #DDDDFF;color:#8888CC;">
		<%= theMonitoring %>
		</div>
		
	</div></center>

	<script>
	<!-- Auto-refresh of the page: each 5 seconds -->
	setTimeout(function() {document.location.href = 'mem_monitoring.jsp?refresh=' + Math.random();}, <%= String.valueOf(theMsFrequency) %>); 
	</script>
</body>
</html>