| 3487 |
rajveer |
1 |
package in.shop2020.web;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.ComparisonStats;
|
|
|
4 |
import in.shop2020.server.ComparisonStatsRepository;
|
|
|
5 |
|
|
|
6 |
import java.text.SimpleDateFormat;
|
| 3522 |
vikas |
7 |
import java.util.ArrayList;
|
| 3487 |
rajveer |
8 |
import java.util.Calendar;
|
|
|
9 |
import java.util.Date;
|
|
|
10 |
import java.util.HashMap;
|
|
|
11 |
import java.util.List;
|
|
|
12 |
import java.util.Map;
|
|
|
13 |
|
|
|
14 |
import javax.servlet.http.HttpServlet;
|
|
|
15 |
import javax.servlet.http.HttpServletRequest;
|
|
|
16 |
import javax.servlet.http.HttpServletResponse;
|
|
|
17 |
|
| 3504 |
rajveer |
18 |
import org.json.JSONObject;
|
| 3487 |
rajveer |
19 |
|
|
|
20 |
import com.google.appengine.api.datastore.DatastoreService;
|
|
|
21 |
import com.google.appengine.api.datastore.DatastoreServiceFactory;
|
|
|
22 |
import com.google.appengine.api.datastore.Entity;
|
|
|
23 |
import com.google.appengine.api.datastore.PreparedQuery;
|
|
|
24 |
import com.google.appengine.api.datastore.Query;
|
| 3518 |
rajveer |
25 |
import com.google.appengine.api.datastore.Text;
|
| 3487 |
rajveer |
26 |
|
|
|
27 |
public class DailyComparisonStatsGenerator extends HttpServlet {
|
|
|
28 |
private static final long serialVersionUID = -8236918415987438049L;
|
|
|
29 |
|
|
|
30 |
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
31 |
resp.setContentType("application/json");
|
|
|
32 |
|
|
|
33 |
DatastoreService datastore = DatastoreServiceFactory
|
|
|
34 |
.getDatastoreService();
|
|
|
35 |
|
|
|
36 |
Calendar cal = Calendar.getInstance();
|
|
|
37 |
Date toDate = cal.getTime();
|
|
|
38 |
cal.add(Calendar.MONTH, -1);
|
|
|
39 |
Date fromDate = cal.getTime();
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
Query q = new Query("DataLog");
|
|
|
43 |
q.addFilter("eventType", Query.FilterOperator.EQUAL, "PRODUCT_COMPARE");
|
|
|
44 |
q.addFilter("date", Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
|
|
|
45 |
q.addFilter("date", Query.FilterOperator.LESS_THAN_OR_EQUAL, toDate);
|
|
|
46 |
|
|
|
47 |
PreparedQuery pq = datastore.prepare(q);
|
|
|
48 |
Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();
|
|
|
49 |
|
|
|
50 |
|
| 3522 |
vikas |
51 |
List<Entity> results = new ArrayList<Entity>();
|
| 3487 |
rajveer |
52 |
for (Entity result : pq.asIterable()) {
|
| 3522 |
vikas |
53 |
results.add(result);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
for (Entity result : results) {
|
| 3487 |
rajveer |
57 |
List<Long> itemIds;
|
|
|
58 |
try{
|
|
|
59 |
itemIds = (List<Long>) result.getProperty("itemIds");
|
|
|
60 |
}catch (ClassCastException ce) {
|
|
|
61 |
System.out.println("Unable to cast itemIds to List");
|
|
|
62 |
continue;
|
|
|
63 |
}
|
|
|
64 |
if(itemIds != null && !itemIds.isEmpty()){
|
|
|
65 |
for(int i=0; i<itemIds.size(); i++){
|
|
|
66 |
for(int j=0; j<i; j++){
|
|
|
67 |
Long itemOne = itemIds.get(i);
|
|
|
68 |
Long itemTwo = itemIds.get(j);
|
|
|
69 |
Map<Long, Long> comparedItems;
|
|
|
70 |
comparedItems = comparisonStats.get(itemOne);
|
|
|
71 |
if(comparedItems==null){
|
|
|
72 |
comparedItems = new HashMap<Long, Long>();
|
|
|
73 |
comparisonStats.put(itemOne, comparedItems);
|
|
|
74 |
}
|
|
|
75 |
if(comparedItems.containsKey(itemTwo)){
|
|
|
76 |
comparedItems.put(itemTwo, comparedItems.get(itemTwo)+1);
|
|
|
77 |
}else{
|
|
|
78 |
comparedItems.put(itemTwo, 1L);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
comparedItems = comparisonStats.get(itemTwo);
|
|
|
82 |
if(comparedItems==null){
|
|
|
83 |
comparedItems = new HashMap<Long, Long>();
|
|
|
84 |
comparisonStats.put(itemTwo, comparedItems);
|
|
|
85 |
}
|
|
|
86 |
if(comparedItems.containsKey(itemOne)){
|
|
|
87 |
comparedItems.put(itemOne, comparedItems.get(itemOne)+1);
|
|
|
88 |
}else{
|
|
|
89 |
comparedItems.put(itemOne, 1L);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
| 3498 |
rajveer |
97 |
String id = sdf.format(toDate);
|
| 3487 |
rajveer |
98 |
|
|
|
99 |
|
| 3504 |
rajveer |
100 |
JSONObject comparisonStatsJSON = new JSONObject(comparisonStats);
|
| 3487 |
rajveer |
101 |
|
|
|
102 |
ComparisonStats compStats = new ComparisonStats();
|
|
|
103 |
compStats.setId(id);
|
| 3518 |
rajveer |
104 |
compStats.setComparisonStatsJSON(new Text(comparisonStatsJSON.toString()));
|
| 3487 |
rajveer |
105 |
|
|
|
106 |
ComparisonStatsRepository repo = new ComparisonStatsRepository();
|
|
|
107 |
repo.store(compStats);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
111 |
doPost(req, resp);
|
|
|
112 |
}
|
|
|
113 |
}
|