Subversion Repositories SmartDukaan

Rev

Rev 5552 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
<%@     page import="com.google.appengine.api.datastore.Query.SortDirection"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@     page import="java.util.Map.Entry"%>
<%@     page import="java.text.ParseException"%>
<%@     page import="java.util.Calendar"%>
<%@     page import="java.util.TimeZone"%>
<%@     page import="java.util.Date"%>
<%@     page import="java.util.List"%>
<%@     page import="java.util.Calendar"%>
<%@     page import="java.text.SimpleDateFormat"%>
<%@     page import="java.util.Arrays"%>

<html>
<head>
<title>Comparison Statistics</title>
</head>
<body>
<%
Date toDate = new Date();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
cal.add(Calendar.DAY_OF_MONTH, -1);
Date fromDate = cal.getTime();
SimpleDateFormat iSdf = new SimpleDateFormat("yyyyMMdd");
String toDateStr  = request.getParameter("toDate");
String fromDateStr = request.getParameter("fromDate");
if (fromDateStr == null || fromDateStr.isEmpty()) {
    fromDateStr = iSdf.format(fromDate);
}
if (toDateStr == null || toDateStr.isEmpty()) {
    toDateStr = iSdf.format(toDate);
}
%>

<form id="searchfilter" name="searchfilter" method="post" action="/jsp/comparison-stats.jsp">
<label>From Date(YYYYMMDD) : </label> <input type="text" name="fromDate" id="fromDate" value="<%= fromDateStr %>"/>
<label>To Date(YYYYMMDD) : </label> <input type="text" name="toDate" id="toDate" value="<%= toDateStr %>"/>
<input type="Submit" id="Submit" name="Submit" value="Submit" />
</form>

<%
    try {
        if (fromDateStr != null) {
            fromDate = iSdf.parse(fromDateStr);
        }
        if (toDateStr != null) {
            toDate = iSdf.parse(toDateStr);
        }
    }
    catch (ParseException pe) {
%>
        <span>Invalid Date Format</span>
<%
    }
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query = new Query("DataLog");
    query.addFilter("eventType", Query.FilterOperator.IN, Arrays.asList("PRODUCT_COMPARE","RESEARCH_ADD","RESEARCH_DELETE"));
    query.addFilter("date", Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
    query.addFilter("date", Query.FilterOperator.LESS_THAN_OR_EQUAL, toDate);
    PreparedQuery pq = datastore.prepare(query);
    
    Map<String, Long>product_compare_map = new HashMap<String, Long>();
    Map<String, Long>compare_add_map = new HashMap<String, Long>();
    long totalComparisons = 0;
    long totalProductsCompared = 0;
    long totalProductsDeleted = 0;
    long totaldays = toDate.compareTo(fromDate);
    int i = 0;
    String err = "";
    for (Entity result : pq.asIterable()) {
        try{
                if(result.getProperty("eventType").equals("PRODUCT_COMPARE")){
                        totalComparisons++;
                        String source = (String)result.getProperty("source");
                        List<Long>itemIds = (List<Long>)result.getProperty("itemIds");
                        totalProductsCompared = totalProductsCompared + itemIds.size();
                        if(product_compare_map.containsKey(source)) {
                                Long source_count = product_compare_map.get(source);
                                source_count = source_count + 1;
                                product_compare_map.put(source, source_count);
                                } else {
                                        product_compare_map.put(source, 1L);
                                }
                        }
                else if(result.getProperty("eventType").equals("RESEARCH_ADD")){
                                String source = (String)result.getProperty("source");
                        if(compare_add_map.containsKey(source)) {
                                Long source_count = compare_add_map.get(source);
                                source_count = source_count + 1;
                                compare_add_map.put(source, source_count);
                                } else {
                                        compare_add_map.put(source, 1L);
                                }
                        }
                else if(result.getProperty("eventType").equals("RESEARCH_DELETE")){
                        totalProductsDeleted++;
                        }
        } catch (Exception e) {
                i++;
                err = err.concat(e.getStackTrace().toString());
                System.out.print(e);
        }
    }
    
    double avgComparisons = 0.0;
    double avgProductsCompared = 0.0;
    if(totalComparisons!=0){
        avgProductsCompared = (double)(totalProductsCompared/totalComparisons);
        }
        if(totaldays!=0){
                avgComparisons = (double)(totalComparisons/totaldays);
        }
%>
<h7 style = "display:none"><%=i%></h7>
<h7 style = "display:none"><%=err%></h7>
<h2> Comparison Stats </h2>
<table cellpadding="5" cellspacing="0" border="1">
        <thead>
                <tr>
                        <th>Source</th>
                        <th>Number of Comparisons</th>
                </tr>
        </thead>
        <tbody>
        <%
        for(Entry<String, Long> comparison: product_compare_map.entrySet()){
                String source = comparison.getKey();
                long numComparisons = comparison.getValue();
        %>
                        <tr>
                <td><%=source%></td>
                                <td><%=numComparisons%></td>
            </tr>    
        <%}%>           
        </tbody>
</table>
<h3> Number of Comparisons per Day : <%=avgComparisons%></h3>
<h3> Average Size of Comparisons : <%=avgProductsCompared%></h3>
<h2> Add to Compare Stats </h2>
<table cellpadding="5" cellspacing="0" border="1">
        <thead>
                <tr>
                        <th>Source</th>
                        <th>Number of Comparisons</th>
                </tr>
        </thead>
        <tbody>
        <%
        for(Entry<String, Long> comparison: compare_add_map.entrySet()){
                String source = comparison.getKey();
                long numComparisons = comparison.getValue();
        %>
                        <tr>
                <td><%=source%></td>
                                <td><%=numComparisons%></td>
            </tr>    
        <%}%>           
        </tbody>
</table>
<h2> Comparisons Deleted Stats </h2>
<h3> Number of Deleted Comparisons : <%=totalProductsDeleted%></h3>
</body>
</html>