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