<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld" %>
<%@ include file = "EgMonitorTracker.jsp" %>
<%@ page errorPage = "EgProbsM.jsp" %>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import = "com.egurkha.util.EgSorter" %>
<%@ page import = "com.egurkha.ini.IniUI" %>
<%@ page import = "com.eg.EgInstallInfo"%>
<%@ page import = "com.eg.EgURLEncoder"%>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.egurkha.util.DateUtil"%>
<%@ page import = "com.egurkha.util.DateTimeTokenizer" %>
<%@ page import = "java.text.SimpleDateFormat"%>
<%@ page import = "com.eg.EgDbInfo" %>
<%@ page import = "java.util.*" %>
<%@ page session = "true" %>
<%@ page import = "com.eg.EgSpecsInfo" %>
<%@ page import = "com.egurkha.User"%>
<%@ page import = "com.egurkha.UserRegistry"%>
<%@ page import ="net.sf.json.*" %>
<%@ page import = "com.eg.EgMappingInfo"%>
<%@ page import = "com.eg.EgDateInfo" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%!
	public ArrayList arrayListSort(ArrayList arrList, int cols, int totalCols, boolean flag)
	{
		ArrayList result = new ArrayList();

		if (arrList==null || arrList.size()==0)
			return result;
		String colType = "";
		String order ="";

		ArrayList unsortedList = flag? new ArrayList():new ArrayList(arrList);

		if(flag)		// Jvm Thread Test
		{
			switch(cols)
			{
					/*  1.Thread_name
						2.ThreadID 
						3.Thread_state 
						4.Thread_cpu_time(Secs) 
						5.Pct_thread_cpu_time 
						6.Blocked_count 
						7.Blocked_time(Secs)
						8.Pct_blocked_time
						9.Waited 
						10.Waited_time(Secs)
						11.Pct_waited_time  */
				case 1: colType="String";  order="asc"; break;
				case 3: colType="String";  order="asc"; break;

				case 4:
				case 5: 
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
				case 11:colType="Double"; order="desc"; break;
			}

			for(int k=0;k<arrList.size();k++)
			{
				ArrayList tmpArrList = (ArrayList)arrList.get(k);
				for(int z=0;z<tmpArrList.size();z++)
				{
					String tmpElement = (String)tmpArrList.get(z);
					unsortedList.add(tmpElement);
				}
			}
		}
		else		// Jvm MemoryDetails Test
		{
			switch(cols)
			{
				case 1: colType="String";  order="asc"; break;
				default : colType="Double"; order="desc"; break;	// Other 2, 3, 4, 5
			}
		}
		EgSorter egSort = new EgSorter(unsortedList,totalCols);
		ArrayList sortList = (ArrayList)egSort.sort(cols,colType,order);
		return sortList;
	}
%>

<%!
	String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvxyz";
	int al_len = AlphaNumericString.length();
	int string_size = 20;
	public String getAlphaNumericString(){
		
		StringBuilder sb = new StringBuilder(string_size); 
		try
		{
			for (int i = 0; i < string_size; i++) {
				int index = (int)(al_len * Math.random()); 
				sb.append(AlphaNumericString.charAt(index)); 
			} 
		}
		catch (Exception er)
		{
		}
		return sb.toString();
	}
%>
<%
	String ua = request.getHeader( "User-Agent" );
	boolean isFirefox = ( ua != null && ua.indexOf( "Firefox/" ) != -1 );
	boolean isFirefox2 = ( ua != null && ua.indexOf( "Firefox/2" ) != -1 );
	boolean isMSIE6 = ( ua != null && ua.indexOf( "MSIE 6.0" ) != -1 );
	boolean isMSIE7 = ( ua != null && ua.indexOf( "MSIE 7.0" ) != -1 );
	boolean isChrome = ( ua != null && ua.indexOf( "Chrome/" ) != -1 );
	
try
{
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json");
%>

	<jsp:useBean id="trans" scope="page" class="egurkha.TransactionBean" />
	<jsp:useBean id="dd" scope="page" class="com.egurkha.monitor.EgDiagnosisBean" />

<%
	String isTable="";
	isTable = isFirefox ? "Auto" : "Fixed";
	String s_mes = null;
	String s_st_time = "";
	String s_st_hr = "0";
	String s_en_time = "";
	String s_en_hr = "0";
	String s_st_min = "0";
	String s_end_min = "0";
	String s_info="NONE";
	String s_first_time = request.getParameter("firsttime");
	String s_flag = request.getParameter("flag");
	String s_site=request.getParameter("site");
	String s_host=request.getParameter("host");
	String s_port=request.getParameter("port");
	String s_test=request.getParameter("test");
	String s_date=request.getParameter("sdate");
	String d_time=request.getParameter("time");
	String s_measure=request.getParameter("s_measure");
	String s_msmt_host=request.getParameter("msmt_host");
	String sortOrder=request.getParameter("sortby");
	String jvmCpu = request.getParameter("jvmcputest");
	String filterdate = request.getParameter("filterdate");
	String selectedThreadName  = request.getParameter("selectedThreadName");
	//String fromPage  = request.getParameter("fromPage");
	//fromPage = (fromPage==null||fromPage.trim().length()==0||fromPage.equalsIgnoreCase("null"))?"":fromPage;
	//String fromStackMeasure = request.getParameter("fromStackMeasure");
	//fromStackMeasure = (fromStackMeasure==null||fromStackMeasure.trim().length()==0||fromStackMeasure.equalsIgnoreCase("null"))?"false":fromStackMeasure;
	//boolean isFromStack = false;
	//if((fromPage!=null && fromPage.equalsIgnoreCase("layMod"))||(fromStackMeasure!=null && fromStackMeasure.equalsIgnoreCase("true")))
		//isFromStack = true;
	if(StringUtils.isNull(jvmCpu))
		jvmCpu="false";

	if(s_test.equals("JvmCpuTest"))
	{
		s_test="JvmThreadTest";
		s_measure="Live_threads";
		jvmCpu="true";
	}
	
	EgDbInfo dbInfo = new EgDbInfo("manager");
	trans.createFile("eg_format.ini");
	String strValue=trans. getStrValue("DDD_TESTS", s_test);
	ArrayList header = StringUtils.egTokenizer(strValue,",");
	if(s_test!=null && !s_test.equalsIgnoreCase("JvmThreadTest"))
		Collections.sort(header);
	String [] colNames = dbInfo.getColumnNamesForTest(s_test+"_DDD");

	JSONObject json = new JSONObject();
	JSONArray jsonArray = new JSONArray();
	
	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile egUI = new IniFile(s_iniFile_dir + uiFile);
	String currentDateFormat = egUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	IniUI ui = (IniUI)com.egurkha.ini.IniUI.getInstance();
	UserRegistry registry = UserRegistry.getInstance();
	User user = registry.getUser(session.getId());
	String thisLanguage = ui.getDefaultLanguage();
	EgUserRole userRole = null;
	if(user!=null)
	{
		String userLang = user.getLanguage();
		if(userLang!=null && !userLang.equalsIgnoreCase("NULL"))
		{
			thisLanguage=userLang;
		}
	}
	String dateFormatForDatePick = (String)user.getUserDateFormat();
	//if(s_test.equals("JvmMemoryDetailsTest"))
	{
		s_info = request.getParameter("s_info");
		if(s_info == null){
			s_info ="NONE";
		}
		if(s_measure!=null && s_measure.length()>0)
		{
			String commonDD = trans.getStrValue("COMMON_DD_MAPPING",s_test+":"+s_measure);
			if(commonDD!=null && commonDD.length()>0)
			{
				s_measure = commonDD.substring(commonDD.indexOf(":")+1);
			}
		}
	}
	if(d_time != null && d_time.length() > 0)
	{
		s_st_time=request.getParameter("s_st_time");
		s_en_time=request.getParameter("s_en_time");
		s_st_hr=request.getParameter("s_st_hr");
		s_en_hr=request.getParameter("s_en_hr");
		s_st_min=request.getParameter("startmin");
		s_end_min=request.getParameter("endmin"); 

		if(s_st_min == null || s_st_min.equalsIgnoreCase("null"))
			s_st_min=request.getParameter("s_st_min");

		if(s_end_min == null || s_end_min.equalsIgnoreCase("null"))
			s_end_min=request.getParameter("s_end_min");
		if((s_st_time!=null && s_st_time.trim().length()>0) && (s_en_time!=null && s_en_time.trim().length()>0))
		{
			String comFormat = "";
			String dispDate="";
			EgTimeZoneConverter timeZoneConverterStart=null;
			EgTimeZoneConverter timeZoneConverterEnd=null;
			String startDateForDisplay = null;
			String startHourForDisplay = null;
			String endDateForDisplay = null;
			String endHourForDisplay = null;
			String startMinForDisplay=null;
			String endMinForDisplay=null;

			if(DateUtil.isDate(s_st_time,"MM/dd/yyyy"))
			{
				comFormat = "MM/dd/yyyy";
			}
			else
			{
				comFormat = dateFormatForDatePick;
			}
			dispDate = DateUtil.changeDateFormat(s_st_time,comFormat,dateFormatForDatePick);
			timeZoneConverterStart = new EgTimeZoneConverter(dispDate+" "+s_st_hr+":"+s_st_min+":"+"00",user,true);
			startDateForDisplay = (String) timeZoneConverterStart.getDate(false);
			startHourForDisplay = timeZoneConverterStart.getHour();
			startMinForDisplay = timeZoneConverterStart.getMinute();
			dispDate = DateUtil.changeDateFormat(startDateForDisplay,dateFormatForDatePick,comFormat);
			s_st_time = dispDate;
			s_st_hr = startHourForDisplay;
			s_st_min = startMinForDisplay;
			
			if(DateUtil.isDate(s_en_time,"MM/dd/yyyy"))
			{
				comFormat ="MM/dd/yyyy";
			}
			else
			{
				comFormat = dateFormatForDatePick;
			}
			dispDate = DateUtil.changeDateFormat(s_en_time,comFormat,dateFormatForDatePick);
			timeZoneConverterEnd = new EgTimeZoneConverter(dispDate+" "+s_en_hr+":"+s_end_min+":"+"00",user,true);
		
			endDateForDisplay = (String) timeZoneConverterEnd.getDate(false);
			endHourForDisplay = timeZoneConverterEnd.getHour();
			endMinForDisplay = timeZoneConverterEnd.getMinute();
			dispDate = DateUtil.changeDateFormat(endDateForDisplay,dateFormatForDatePick,comFormat);
			s_en_time = dispDate;
			s_en_hr = endHourForDisplay;
			s_end_min = endMinForDisplay;

		}
		
		if((s_st_time==null || s_st_time.trim().length()==0) || (s_en_time==null || s_en_time.trim().length()==0))
		{
			String tempp = currentDateFormat +" HH:mm";
			String endDateTime ="";
			String startDateTime ="";
			SimpleDateFormat sdf = new SimpleDateFormat(tempp);
			java.util.Date tmpday = new java.util.Date();
			endDateTime = sdf.format(tmpday);
			
			java.util.Date startDate2 = new java.util.Date();
			startDate2.setTime(tmpday.getTime() - (3600 * 1000));
			startDateTime = sdf.format(startDate2);
			
			s_st_time = startDateTime.substring(0,startDateTime.lastIndexOf(" "));
			s_en_time = endDateTime.substring(0,endDateTime.lastIndexOf(" "));

			s_st_hr = startDateTime.substring(startDateTime.lastIndexOf(" ")+1,startDateTime.indexOf(":"));
			s_en_hr = endDateTime.substring(endDateTime.lastIndexOf(" ")+1,endDateTime.indexOf(":"));

			s_st_min = startDateTime.substring(startDateTime.indexOf(":")+1);
			s_end_min = endDateTime.substring(endDateTime.indexOf(":")+1);
		}
	}
	else if(s_date!=null && s_date.length() > 0)
	{
		s_st_time=request.getParameter("s_st_time");
		s_en_time=request.getParameter("s_en_time");
		s_st_hr=request.getParameter("s_st_hr");
		s_en_hr=request.getParameter("s_en_hr");
		s_st_min=request.getParameter("s_st_min");
		s_end_min=request.getParameter("s_end_min"); 
		if((s_st_time!=null && s_st_time.trim().length()>0) && (s_en_time!=null && s_en_time.trim().length()>0))
		{
			String comFormat = "";
			String dispDate="";
			EgTimeZoneConverter timeZoneConverterStart=null;
			EgTimeZoneConverter timeZoneConverterEnd=null;
			String startDateForDisplay = null;
			String startHourForDisplay = null;
			String endDateForDisplay = null;
			String endHourForDisplay = null;
			String startMinForDisplay=null;
			String endMinForDisplay=null;
			if(DateUtil.isDate(s_st_time,"MM/dd/yyyy"))
			{
				comFormat ="MM/dd/yyyy";
			}
			else
			{
				comFormat = dateFormatForDatePick;
			}
			dispDate = DateUtil.changeDateFormat(s_st_time,comFormat,dateFormatForDatePick);
			timeZoneConverterStart = new EgTimeZoneConverter(dispDate+" "+s_st_hr+":"+s_st_min+":"+"00",user,true);
			startDateForDisplay = (String) timeZoneConverterStart.getDate(false);
			startHourForDisplay = timeZoneConverterStart.getHour();
			startMinForDisplay = timeZoneConverterStart.getMinute();
			dispDate = DateUtil.changeDateFormat(startDateForDisplay,dateFormatForDatePick,comFormat);
			s_st_time = dispDate;
			s_st_hr = startHourForDisplay;
			s_st_min = startMinForDisplay;
			if(DateUtil.isDate(s_en_time,"MM/dd/yyyy"))
			{
				comFormat ="MM/dd/yyyy";
			}
			else
			{
				comFormat = dateFormatForDatePick;
			}
			dispDate = DateUtil.changeDateFormat(s_en_time,comFormat,dateFormatForDatePick);
			timeZoneConverterEnd = new EgTimeZoneConverter(dispDate+" "+s_en_hr+":"+s_end_min+":"+"00",user,true);
		
			endDateForDisplay = (String) timeZoneConverterEnd.getDate(false);
			endHourForDisplay = timeZoneConverterEnd.getHour();
			endMinForDisplay = timeZoneConverterEnd.getMinute();
			dispDate = DateUtil.changeDateFormat(endDateForDisplay,dateFormatForDatePick,comFormat);
			s_en_time = dispDate;
			s_en_hr = endHourForDisplay;
			s_end_min = endMinForDisplay;
		}
	}

	Vector v_measures = new Vector();
	
	String comFormat = null;
	String endDateTime ="";
	String startDateTime ="";
	String colNamesForQuery = "";
	String lastMesTime = request.getParameter("time");
	String dummylastMesTime = lastMesTime;
	String s_st_sec="00";

	// Table and cell Width for Browser Compatability
	String wd = "";
	String wdtd = "";
	String divHeight = "";


	ArrayList a_results = new ArrayList();

	//EgMappingInfo mappingInfo = new EgMappingInfo(thisLanguage);
	if((s_st_time == null || (s_st_time != null && s_st_time.equals(""))) || (s_en_time == null || (s_en_time != null && s_en_time.equals(""))))
	{
		String tempp = currentDateFormat +" HH:mm";

		EgDateInfo edi1 = new EgDateInfo();
		SimpleDateFormat sdf = new SimpleDateFormat(tempp);
		//java.util.Date tmpday = new java.util.Date();
		java.util.Date tmpday = edi1.egetRealDate();
		endDateTime = sdf.format(tmpday);
		java.util.Date startDate2 = new java.util.Date();
		startDate2.setTime(tmpday.getTime() - (3600 * 1000));
		startDateTime = sdf.format(startDate2);

		s_st_time = startDateTime.substring(0,startDateTime.lastIndexOf(" "));
		s_en_time = endDateTime.substring(0,endDateTime.lastIndexOf(" "));

		s_st_hr = startDateTime.substring(startDateTime.lastIndexOf(" ")+1,startDateTime.indexOf(":"));
		s_en_hr = endDateTime.substring(endDateTime.lastIndexOf(" ")+1,endDateTime.indexOf(":"));

		s_st_min = startDateTime.substring(startDateTime.indexOf(":")+1);
		s_end_min = endDateTime.substring(endDateTime.indexOf(":")+1);
	}

	boolean filterDateFlag = false;
	boolean filterAllFlag =false;
	String fdate="";
	if(filterdate!=null && filterdate.length()>4)
	{
		String tmpDate="";
		if(filterdate.indexOf(":")>-1)
		{
			fdate=filterdate.substring(0, filterdate.lastIndexOf(DateTimeTokenizer.BLANK_SPACE));
		}

		int shr = Integer.parseInt(filterdate.substring(filterdate.lastIndexOf(DateTimeTokenizer.BLANK_SPACE)+1, filterdate.indexOf(":")));
		int st_min=Integer.parseInt(filterdate.substring(filterdate.indexOf(":")+1,filterdate.lastIndexOf(":")));
		int st_sec=Integer.parseInt(filterdate.substring(filterdate.lastIndexOf(":")+1));
		s_st_time=filterdate.substring(0, filterdate.indexOf(DateTimeTokenizer.BLANK_SPACE));
		s_st_hr = String.valueOf(shr);
		s_st_min = String.valueOf(st_min);
		s_en_hr=String.valueOf(shr);
		s_st_sec=String.valueOf(st_sec);

		if(!DateUtil.isDate(fdate,"MM/dd/yyyy"))
		{
			fdate = DateUtil.changeDateFormat(fdate,currentDateFormat,"MM/dd/yyyy");
		}
		filterDateFlag=true;
	}
	else if(filterdate!=null && filterdate.equals("All"))
	{
		filterAllFlag=true;
	}

	if(DateUtil.isDate(s_st_time,"MM/dd/yyyy"))
			comFormat = "MM/dd/yyyy";
	else
			comFormat = currentDateFormat;

	String ss_st_time = s_st_time;

	if(!DateUtil.isDate(s_st_time,"MM/dd/yyyy"))
			ss_st_time = DateUtil.changeDateFormat(s_st_time,comFormat,"MM/dd/yyyy");
	
	if(DateUtil.isDate(s_en_time,"MM/dd/yyyy"))
			comFormat = "MM/dd/yyyy";
	else
			comFormat = currentDateFormat;

	String se_en_time = s_en_time;

	if(!DateUtil.isDate(s_en_time,"MM/dd/yyyy"))
		se_en_time = DateUtil.changeDateFormat(s_en_time,comFormat,"MM/dd/yyyy");

	if (!strValue.equals(""))
	{
		if(colNames != null && colNames.length > 0)
		{
			for(int r=0; r<colNames.length; r++)
			{
				colNamesForQuery +=colNames[r] + ",";
			}
			if(colNamesForQuery.endsWith(","))
			{
				colNamesForQuery = colNamesForQuery.substring(0, (colNamesForQuery.length()-1));
			}
		}
	}

	if(s_first_time != null && !s_first_time.equals("") && s_first_time.equals("true"))
	{
		if(s_test != null && !s_test.equals(""))
		{
			v_measures=dd.populateMeasures(s_test);
			//Sort measures using Display names
			Vector sortedMeasures = new Vector();
			TreeMap tm = new TreeMap(new Comparator()
			{
				public int compare(Object o1, Object o2)
				{
					String s1 = (String)o1;
					String s2 = (String)o2;
					int sortvalue = s1.compareToIgnoreCase(s2);
					if(sortvalue==0)
					{
						sortvalue = s1.compareTo(s2);
					}
					return sortvalue;
				}
			});
			for(int ij=0; ij<v_measures.size(); ij++)
			{
				String temp_measure = (String)v_measures.get(ij);
				String disp_measure = (String)mappingInfo.getMeasureDescription(s_test,temp_measure);
				tm.put(disp_measure,temp_measure);
			}
			Iterator iterate = tm.entrySet().iterator();
			while(iterate.hasNext())
			{
				Map.Entry e = (Map.Entry)iterate.next();
				sortedMeasures.add((String)e.getValue());
			}
			
			if(sortedMeasures!=null && sortedMeasures.size()>0)
				v_measures = sortedMeasures;
			//Sorting measure Ends
		}
	}

	s_mes = (String) v_measures.get(0);
	String[] Desc = dd.getDescription(s_test,s_mes);

	if(filterAllFlag)
	{
		a_results=new ArrayList();
		a_results=(ArrayList)session.getAttribute("a_results");	
	}
	else
	{
		if(s_measure!=null && s_measure.equals("Total_current_threads"))
		{
			for(int i=0;i<v_measures.size();i++)
			{
				s_mes = (String) v_measures.get(i);
				if(filterDateFlag)
				{
					ArrayList al = dd.getDiagnosisDataForDate(s_host, s_port,s_site, s_test, s_mes, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min,s_end_min,s_st_sec,s_flag,colNamesForQuery,s_msmt_host,fdate);
					if(al != null && al.size() > 0)
						a_results.addAll(al);
				}
				else
				{
					ArrayList al = dd.getDiagnosisData(s_host, s_port,s_site, s_test, s_mes, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min+":"+s_st_sec,s_end_min+":59",s_flag,colNamesForQuery,s_msmt_host);
					if(al != null && al.size() > 0)
						a_results.addAll(al);
				}
			}
		}
		else
		{
			if(jvmCpu.equals("true"))
			{
				s_measure = "JvmCpuTest:Live_threads";
				s_mes = "JvmCpuTest:Live_threads";
			}
			if(s_measure!=null && s_measure.length()>0)
			{
				if(filterDateFlag)
				{
					ArrayList al = dd.getDiagnosisDataForDate(s_host, s_port,s_site, s_test, s_measure, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min,s_end_min,s_st_sec,s_flag,colNamesForQuery,s_msmt_host,fdate);
					if(al != null && al.size() > 0)
						a_results.addAll(al);
				}
				else
				{
					ArrayList al = dd.getDiagnosisData(s_host, s_port,s_site, s_test, s_measure, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min+":"+s_st_sec,s_end_min+":59",s_flag,colNamesForQuery,s_msmt_host);
					if(al != null && al.size() > 0)
						a_results.addAll(al);
				}
			}
			else
			{
				for(int i=0;i<v_measures.size();i++)
				{
					s_mes = (String) v_measures.get(i);
					if(filterDateFlag)
					{
						ArrayList al = dd.getDiagnosisDataForDate(s_host, s_port,s_site, s_test, s_mes, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min,s_end_min,s_st_sec,s_flag,colNamesForQuery,s_msmt_host,fdate);
						if(al != null && al.size() > 0)
						a_results.addAll(al);
					}
					else
					{
						ArrayList al = dd.getDiagnosisData(s_host, s_port,s_site, s_test, s_mes, s_info, ss_st_time, se_en_time, s_st_hr, s_en_hr,s_st_min+":"+s_st_sec,s_end_min+":59",s_flag,colNamesForQuery,s_msmt_host);
						if(al != null && al.size() > 0)
						a_results.addAll(al);
					}
				}
			}
		}
	}
	ArrayList valList = new ArrayList();
	ArrayList measureTimeList = new ArrayList();
	if(a_results.size()>0)
	{
		if(StringUtils.isNull(lastMesTime))
		{
			if(s_date!=null && s_date.length()>0)
			{
				lastMesTime= s_date;
			}
			else
			{
				lastMesTime=(String) a_results.get(0);
				EgTimeZoneConverter egTimeZoneConverter1 = new EgTimeZoneConverter(lastMesTime,user);
				lastMesTime = egTimeZoneConverter1.getDateTime();
			}
		}
		//if(!isFromStack)
		//{
			//EgTimeZoneConverter egTimeZoneConverter1 = new EgTimeZoneConverter(lastMesTime,user);
			//lastMesTime = egTimeZoneConverter1.getDateTime();
		//}
		//lastMesTime = DateUtil.changeDateFormat(lastMesTime,"M/dd/yyyy HH:mm:ss",currentDateFormat + " HH:mm:ss");
	
		for(int k=0;k<a_results.size();k=k+2)
		{
			String mesTime = (String) a_results.get(k);
			EgTimeZoneConverter egTimeZoneConverter = new EgTimeZoneConverter(mesTime,user);
			mesTime = egTimeZoneConverter.getDateTime();
			//mesTime = DateUtil.changeDateFormat(mesTime,"MM/dd/yyyy HH:mm:ss",currentDateFormat + " HH:mm:ss");
			measureTimeList.add(mesTime);
			if(lastMesTime.equals(mesTime))
			{
				ArrayList val = (ArrayList) a_results.get(k+1);
				Set valSet = new HashSet(val);
				List listVal = new ArrayList(valSet);
				for(int j=0;j<listVal.size();j++)
				{
					ArrayList tmpArrList = (ArrayList) listVal.get(j);
//					for(int z =0 ;z < tmpArrList.size();z++)
//					{
//						String tmp2 = (String) tmpArrList.get(0);
						valList.add(tmpArrList);
//					}
				}
			}
		}
	}
	Set tmpSet = new HashSet();
	tmpSet = new HashSet(valList);
	valList = new ArrayList(tmpSet);

	//To avoid duplicate measurement time
	TreeSet ts = new TreeSet(measureTimeList);
	measureTimeList = new ArrayList(ts);
	if(s_test.equals("JvmThreadTest") || s_test.equals("JvmCpuTest"))
	{
		if(s_measure!=null)
		{
			if(StringUtils.isNull(sortOrder))
			{
				if (s_measure.equals("Blocked_threads"))
					sortOrder = "8";
				else if (s_measure.equals("Waiting_threads") || s_measure.equals("Timed_waiting_threads"))
					sortOrder = "4";
				else if(s_measure.equals("Total_current_threads"))
					sortOrder = "1"; 
				else
					sortOrder = "5";
			}
		}
		else
		{
			sortOrder = "1";
		}

		int sortColumn = Integer.parseInt(sortOrder);

		ArrayList threadNames = new ArrayList();

		ArrayList tmpList  = arrayListSort(valList,sortColumn,12,true);
		Hashtable resultTable = new Hashtable();
		for(int j=0;j<tmpList.size();j=j+12)
		{
			String tnames = (String)tmpList.get(j);
			String tId = (String) tmpList.get(j+1);
			threadNames.add(tnames+"*:*"+tId);
			ArrayList tmpArrList = new ArrayList();
			for(int k=0;k<12;k++)
			{
				String tmpString = (String) tmpList.get(j+k);
				tmpArrList.add(tmpString+"#~#");
			}
			resultTable.put(tnames+"*:*"+tId,tmpArrList);
		}
		if(threadNames.size()==0)
		{
				threadNames.add("Nil*:*Nil");
				if(!StringUtils.isNull(dummylastMesTime))
				{
					measureTimeList.add(dummylastMesTime);
				}
				resultTable.put("Nil*:*Nil","Nil");
		}

		//String wd = isFirefox?"657px":"631px";
		//String wdtd = isFirefox?"659px":"633px";

		
		if (isFirefox)
		{
			wd = "657px";
			wdtd = "671px";
			divHeight = "362px";
		}	
		else if (isChrome)
		{
			wd = "657px";
			wdtd = "660px";
			divHeight = "389px";
		}
		else
		{
			wd = "631px";
			wdtd = "633px";
			divHeight = "362px";
		}

		
		if(StringUtils.isNull(lastMesTime))
			lastMesTime = "Not Available";
		if(!lastMesTime.equals("Not Available")||(s_date!=null) && threadNames.size()>=0 && a_results.size()>0)
		{
			String Url="";
			if(s_date==null || s_date.length()==0)
			{
				Url = "../monitor/ThreadDetails.jsp?site=" + EgURLEncoder.encode("NULL","UTF-8") + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+ "&from=0&flag=1&firsttime=true&jvmcputest="+jvmCpu;
			}
			else
			{
				Url = "../monitor/ThreadDetails.jsp?site=" + EgURLEncoder.encode("NULL","UTF-8") + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+ "&from=0&flag=1&firsttime=true&sdate="+com.eg.EgURLEncoder.encode(s_date,"UTF-8")+"&s_st_time="+com.eg.EgURLEncoder.encode(s_st_time,"UTF-8")+"&s_en_time="+com.eg.EgURLEncoder.encode(s_en_time,"UTF-8")+"&s_st_hr="+com.eg.EgURLEncoder.encode(s_st_hr,"UTF-8")+"&s_en_hr="+com.eg.EgURLEncoder.encode(s_en_hr,"UTF-8")+"&s_st_min="+com.eg.EgURLEncoder.encode(s_st_min,"UTF-8")+"&s_end_min="+com.eg.EgURLEncoder.encode(s_end_min,"UTF-8")+"&jvmcputest="+jvmCpu;
			}
			int threadCount = (((String)threadNames.get(0)).equals("Nil"))?0:threadNames.size();
			JSONObject measurementTimeCombo = new JSONObject();
			measurementTimeCombo.put("type","combo");
			measurementTimeCombo.put("name","memtime");
			measurementTimeCombo.put("url",Url);

			JSONArray measureTimeArray = new JSONArray();

			for(int i=measureTimeList.size()-1;i>=0;i--)
			{
				JSONObject measureTimeObject = new JSONObject();
				String tmp = (String)measureTimeList.get(i);
				//String displayDateTime = "";
				EgTimeZoneConverter egTimeZoneConverter = new EgTimeZoneConverter(tmp,user);
				//displayDateTime = egTimeZoneConverter.getDateTime();	
				
				String selected="";
				if (tmp.equals(lastMesTime))
				{
					measureTimeObject.put("name",tmp);
					measureTimeObject.put("value",tmp);
					measurementTimeCombo.put("selectedvalue",tmp);
					selected ="selected";
				}
				else
				{
					measureTimeObject.put("name",tmp);
					measureTimeObject.put("value",tmp);
					selected="";
				}
				measureTimeArray.add(measureTimeObject);
			}
			measurementTimeCombo.put("time",measureTimeArray);
			jsonArray.add(measurementTimeCombo);
			JSONObject measureCombo = new JSONObject();
			measureCombo.put("type","combo");
			measureCombo.put("name","Measurement");
			measureCombo.put("url",Url);
			JSONArray measureArray = new JSONArray();
			JSONObject measureObject = new JSONObject();
			if(jvmCpu.equals("true"))
			{
				String displayValue = mappingInfo.getMeasureDescription(s_test,s_measure);
				measureObject.put("name",displayValue);
				measureObject.put("value",s_measure);
				measureCombo.put("selectedvalue",displayValue);
				measureArray.add(measureObject);
			}
			else if(filterDateFlag || filterAllFlag) 
			{
				String displayValue = mappingInfo.getMeasureDescription(s_test,s_measure);
				measureCombo.put("selectedvalue",displayValue);
				measureObject.put("name",displayValue);
				measureObject.put("value",s_measure);
				//measureArray.add(measureObject);
			}
			else
			{
				for(int k=0;k<v_measures.size();k++)
				{
					measureObject = new JSONObject();
					String tmp = (String) v_measures.get(k);
					String selected="";
					String displayValue = mappingInfo.getMeasureDescription(s_test,tmp);
					if(tmp.equals(s_measure))
					{
						measureCombo.put("selectedvalue",tmp);
						measureObject.put("name",displayValue);
						measureObject.put("value",tmp);
						selected="selected";
					}
					else
					{
						measureObject.put("name",displayValue);
						measureObject.put("value",tmp);
						selected="";
					}
					measureArray.add(measureObject);
				}
			}
			measureCombo.put("measure",measureArray);
			jsonArray.add(measureCombo);

			JSONObject sortByCombo = new JSONObject();
			sortByCombo.put("type","combo");
			sortByCombo.put("name","memsortname");
			sortByCombo.put("url",Url);

			int index=0;
			JSONArray sortByArray = new JSONArray();
			for(int y=0;y<header.size()-1;y++)
			{
				JSONObject sortByObject = new JSONObject();
				String tmpheader = (String)header.get(y);
				index++;
				if(tmpheader.equals("ThreadID") || tmpheader.equals("Stacktrace") )
					continue;
				String selected="";
				if(tmpheader!=null && tmpheader.length()>0)
				{
					tmpheader=tmpheader.replace("_"," ");
				}
				if(index==sortColumn)
				{
					sortByCombo.put("selectedValue",index);
					sortByObject.put("name",mappingInfo.getTextDescription("monitor.dashboard."+tmpheader));
					sortByObject.put("value",index);
					selected="selected";
				}
				else
				{
					sortByObject.put("name",mappingInfo.getTextDescription("monitor.dashboard."+tmpheader));
					sortByObject.put("value",index);
					selected="";
				}
				sortByArray.add(sortByObject);
			}
			sortByCombo.put("sortByOrder",sortByArray);
			jsonArray.add(sortByCombo);

			JSONObject threadName = new JSONObject();
			threadName.put("type","combo");
			threadName.put("name","threadname");
			JSONArray threadNameArray = new JSONArray();

			int count =threadNames.size();
			if(count==1) 
			{
				if(threadNames.contains("Nil*:*Nil"))
					count=0;
			}
			Hashtable threadSession = new Hashtable();
			for(int k=0;k<threadNames.size();k++)
			{
				JSONObject threadNameObject = new JSONObject();
				String rand_st = getAlphaNumericString();
				String tmpName= (String)threadNames.get(k);
				String displayThreadName = tmpName.substring(0,tmpName.indexOf("*:*"));
				String selected="";
				selected = (k==0)?"selected":"";
				Object optionValue = resultTable.get(tmpName);
				if(selectedThreadName==null && k==0)
				{
					threadName.put("selectedValue",rand_st);
				}
				else if(selectedThreadName!=null && selectedThreadName.equals(rand_st))
				{
					threadName.put("selectedValue",rand_st);
				}
					threadNameObject.put("title","No of Threads "+count);
					threadNameObject.put("name",displayThreadName);
					threadNameObject.put("value",rand_st);

					threadSession.put(rand_st,optionValue.toString());
				threadNameArray.add(threadNameObject);
			}

			session.setAttribute("threadSession",threadSession);
			threadName.put("threadname",threadNameArray);
			jsonArray.add(threadName);
			JSONObject hiddenFields = new JSONObject();
			hiddenFields.put("fdate",filterdate);
			hiddenFields.put("jvmcputest",jvmCpu);
			json.put("hiddenFields",hiddenFields);
			json.put("data",jsonArray);
			if(threadNames.size() >0 && threadNames != null)
			{
				String firstKey = (String)threadNames.get(0);
				Object firstOptionValue = resultTable.get(firstKey);
			}
		}
		else
		{
			String msg = "monitor.layermodel.StackTrace not available";
			JSONObject errorMsg = new JSONObject();
			errorMsg.put("name",msg);
			errorMsg.put("value","<img src=\"eg_images/"+monitor_Skin+"/exclamation-icon.gif\" align=\"center\" border=\"0\"/>");
			json.put("errorMsg",errorMsg);
		}
	}
	else	// Jvm Heap Details 
	{
		if(StringUtils.isNull(sortOrder))
		{
			sortOrder = "5";
		}

		int sortColumn = Integer.parseInt(sortOrder);
		ArrayList tmpList  = arrayListSort(valList,sortColumn,5,false);
		String Url="";
		if(s_date==null || s_date.length()==0)
		{
			Url = "../monitor/ThreadDetails.jsp?site=NULL" + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+"&s_info="+s_info+"&from=0&flag=1&firsttime=true";
		}
		else
		{
			Url = "../monitor/ThreadDetails.jsp?site=NULL" + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+"&s_info="+s_info+"&from=0&flag=1&firsttime=true&sdate="+com.eg.EgURLEncoder.encode(s_date,"UTF-8")+"&s_st_time="+com.eg.EgURLEncoder.encode(s_st_time,"UTF-8")+"&s_en_time="+com.eg.EgURLEncoder.encode(s_en_time,"UTF-8")+"&s_st_hr="+com.eg.EgURLEncoder.encode(s_st_hr,"UTF-8")+"&s_en_hr="+com.eg.EgURLEncoder.encode(s_en_hr,"UTF-8")+"&s_st_min="+com.eg.EgURLEncoder.encode(s_st_min,"UTF-8")+"&s_end_min="+com.eg.EgURLEncoder.encode(s_end_min,"UTF-8");
		}

		//String wd = isFirefox?"278px":"252px";
		//String wdtd = isFirefox?"278px":"254px";
		if (isFirefox)
		{
			wd = "278px";
			wdtd = "278px";
			divHeight = "362px";
		}
		else if (isChrome)
		{
			wd = "252px";
			wdtd = "252px";
			divHeight = "400px";
		}
		else
		{
			wd = "252px";
			wdtd = "252px";
			divHeight = "365px";
		}

		if(a_results.size()>0)
		{
			JSONObject measurementTimeCombo = new JSONObject();
			measurementTimeCombo.put("type","combo");
			measurementTimeCombo.put("name","memtime");
			measurementTimeCombo.put("url",Url);
			JSONArray measureTimeArray = new JSONArray();
			for(int i=measureTimeList.size()-1;i>=0;i--)
			{
				JSONObject measureTimeObject = new JSONObject();
				String tmp = (String)measureTimeList.get(i);
				EgTimeZoneConverter egTimeZoneConverter = new EgTimeZoneConverter(tmp,user);
				//String displayDateTime = egTimeZoneConverter.getDateTime();
				String selected="";
				if (tmp.equals(lastMesTime))
				{
					measurementTimeCombo.put("selectedValue",tmp);
					measureTimeObject.put("name",tmp);
					measureTimeObject.put("value",tmp);
					selected ="selected";
				}
				else
				{
					measureTimeObject.put("name",tmp);
					measureTimeObject.put("value",tmp);
					selected="";
				}
				measureTimeArray.add(measureTimeObject);
			}
			measurementTimeCombo.put("time",measureTimeArray);
			jsonArray.add(measurementTimeCombo);

			JSONObject sortByCombo = new JSONObject();
			sortByCombo.put("type","combo");
			sortByCombo.put("name","memsortname");
			sortByCombo.put("url",Url);
			


			int index=0;
			JSONArray sortByArray = new JSONArray();
			for(int y=0;y<header.size();y++)
			{
				JSONObject sortByObject = new JSONObject();
				String tmpheader = (String)header.get(y);
				if(tmpheader!=null && tmpheader.length()>0)
				{
					tmpheader=tmpheader.replace("_"," ");
				}
				index++;
				String selected="";
				if(index==sortColumn)
				{
					sortByCombo.put("selectedValue",index);
					sortByObject.put("name",mappingInfo.getTextDescription("monitor.dashboard."+tmpheader));
					sortByObject.put("value",index);
					selected="selected";
				}
				else
				{
					sortByObject.put("name",mappingInfo.getTextDescription("monitor.dashboard."+tmpheader));
					sortByObject.put("value",index);
					selected="";
				}
				sortByArray.add(sortByObject);
			}
			sortByCombo.put("sortByOrder",sortByArray);
			jsonArray.add(sortByCombo);
			JSONObject hiddenFields = new JSONObject();
			hiddenFields.put("fdate",filterdate);
			
			StringBuffer sb_headers = new StringBuffer();
			//sb_headers.append("<table align=\"left\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\" style=\"table-layout:auto;\" class=\"tableborder_jvm\" width=\"100%\">");
			sb_headers.append("<div class=\"x-table\"><table align=\"left\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\" style=\"table-layout:auto;\" class=\"x-grid-table border-theme\" width=\"100%\"><thead><tr class=\"x-grid-row\">");
			for(int k=0;k<header.size();k++)
			{
				String head = (String)header.get(k);
				if(head!=null && head.length()>0)
				{
					head=head.replace("_"," ");
				}
				sb_headers.append("<td  class=\"x-grid-cell\" style=\"padding:4px 4px 4px 4px !important\" align=\"center\" wrap>"+head+"</td>");
				//sb_headers.append("<td  class=\"eg_75bold tdborder2\" align=\"center\" wrap>"+head+"\'/></td>");
			}
			//sb_headers.append("</tr>");
			sb_headers.append("</tr></thead><tbody>");


			for(int k=0;k<tmpList.size();k++)
			{
				ArrayList dummyList = (ArrayList)tmpList.get(k);
				//sb_headers.append("<tr>");
				sb_headers.append("<tr class=\"x-grid-row\">");

				//String styleClass = k%2==0?"tdborder_jvm":"tdborder2_jvm";
				for(int d=0; d<dummyList.size() ; d++)
				{
					String arrElement = (String) dummyList.get(d);
					arrElement = EgDbInfo.formatValue(arrElement);
					if(d==0)
					{
						if(arrElement.startsWith("<") && arrElement.endsWith(">"))
						{
							arrElement = arrElement.substring(1,arrElement.length()-2);
							arrElement = "&lt;"+arrElement+"&gt;";
						}
					}
					//sb_headers.append("<td align=\"left\" class="+styleClass+"> "+arrElement+" </td>");
					sb_headers.append("<td style=\"padding:3px !important\" class=\"x-grid-cell\" align=\"left\" > "+arrElement+" </td>");

				}
				sb_headers.append("</tr>");

			}
			//sb_headers.append(" </td> </tr> </table> </td> </tr> </table>");
			//jsonArray.add(sb_headers.toString());
			sb_headers.append(" </tbody></table></div>");
			json.put("data",jsonArray);
			json.put("tableData",sb_headers.toString());
			
		}
		else
		{
			String msg = "Heap Details not available";
			JSONObject errorMsg = new JSONObject();
			errorMsg.put("name",msg);
			errorMsg.put("value","<img src=\"eg_images/"+monitor_Skin+"/exclamation-icon.gif\" align=\"center\" border=\"0\"/>");
			json.put("errorMsg",errorMsg);
		}
	}
	response.getWriter().write(json.toString());
}
catch(Exception ee)
{
	ee.printStackTrace();
}
%>
