Subversion Repositories SmartDukaan

Rev

Rev 3523 | Rev 3704 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3444 vikas 1
package in.shop2020.web;
2
 
3
import in.shop2020.model.Item;
4
import in.shop2020.model.ItemActivityWithSource;
5
import in.shop2020.server.ItemActivityWithSourceRepository;
6
import in.shop2020.server.ItemRepository;
7
 
8
import java.text.ParseException;
9
import java.text.SimpleDateFormat;
10
import java.util.ArrayList;
11
import java.util.Calendar;
12
import java.util.Date;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Map.Entry;
18
import java.util.Set;
19
import java.util.TimeZone;
3488 vikas 20
import java.util.logging.Logger;
3444 vikas 21
 
22
import javax.servlet.http.HttpServlet;
23
import javax.servlet.http.HttpServletRequest;
24
import javax.servlet.http.HttpServletResponse;
25
 
26
import com.google.appengine.api.datastore.DatastoreService;
27
import com.google.appengine.api.datastore.DatastoreServiceFactory;
28
import com.google.appengine.api.datastore.Entity;
3488 vikas 29
import com.google.appengine.api.datastore.FetchOptions;
3444 vikas 30
import com.google.appengine.api.datastore.PreparedQuery;
31
import com.google.appengine.api.datastore.Query;
32
 
33
public class DailyProductWithSourceAggregatorServlet extends HttpServlet {
34
    private static final long serialVersionUID = -8236918415987438049L;
3488 vikas 35
    private static Logger logger = Logger.getLogger(DailyProductWithSourceAggregatorServlet.class.getName());
36
 
3444 vikas 37
    private static final String KIND = "DataLog";
3488 vikas 38
    private static final String SESSION_ID_FIELD = "sessionId";
39
    private static final String ITEMID_FIELD = "itemId";
40
    private static final String ITEMIDS_FIELD = "itemIds";
41
    private static final String PRODUCTNAME_FIELD = "productName";
42
    private static final String EVENTTYPE_FIELD = "eventType";
43
    private static final String DATE_FIELD = "date";
44
    private static final String FIRST_SOURCE_FIELD = "firstSource";
45
    private static final String SESSION_SOURCE_FIELD = "source";
3444 vikas 46
 
47
    private static final String PRODUCT_VIEW_EVENT = "PRODUCT_VIEW";
48
    private static final String ADD_TO_CART_EVENT = "ADD_TO_CART";
49
    private static final String DELETE_FROM_CART_EVENT = "DELETE_FROM_CART";
50
    private static final String RESEARCH_ADD_EVENT = "RESEARCH_ADD";
51
    private static final String RESEARCH_DELETE_EVENT = "RESEARCH_DELETE";
52
    private static final String SHIPPING_ACCESS_EVENT = "SHIPPINIG_ACCESS";
53
    private static final String PAYMENT_SUCCESS_EVENT = "PAYMENT_SUCCESS";
54
    private static final String ORDER_CREATION_EVENT = "ORDER_CREATION";
55
    private static final String PAYMENT_FAILURE_EVENT = "PAYMENT_FAILURE";
56
    private static final String PROCEED_TO_PAY_EVENT = "PROCEED_TO_PAY";
57
    private static final String NEW_SESSION_EVENT = "NEW_SESSION";
58
 
59
    private static final String VIEW_COUNT_KEY = "ViewCount";
60
    private static final String UNIQUE_VIEW_COUNT_KEY = "UniqueViewCount";
61
    private static final String ADD_TO_CART_COUNT_KEY = "AddToCartCount";
62
    private static final String UNIQUE_ADD_TO_CART_COUNT_KEY = "UniqueAddToCartCount";
63
    private static final String DELETE_FROM_CART_COUNT_KEY = "DeleteFromCartCount";
64
    private static final String UNIQUE_DELETE_FROM_CART_COUNT_KEY = "UniqueDeleteFromCartCount";
65
    private static final String ADD_TO_RESEARCH_COUNT_KEY = "AddToResearchCount";
66
    private static final String UNIQUE_ADD_TO_RESEARCH_COUNT_KEY = "UniqueAddToResearchCount";
67
    private static final String DELETE_FROM_RESEARCH_COUNT_KEY = "DeleteFromResearchCount";
68
    private static final String UNIQUE_DELETE_FROM_RESEARCH_COUNT_KEY = "UniqueDeleteFromResearchCount";
69
    private static final String SHIPPING_ACCESS_COUNT_KEY = "ShippingAccessCount";
70
    private static final String UNIQUE_SHIPPING_ACCESS_COUNT_KEY = "UniqueShippingAccessCount";
71
    private static final String PAYMENT_SUCCESS_COUNT_KEY = "PaymentSuccessCount";
72
    private static final String UNIQUE_PAYMENT_SUCCESS_COUNT_KEY = "UniquePaymentSuccessCount";
73
    private static final String ORDER_CREATION_COUNT_KEY = "OrderCreationCount";
74
    private static final String UNIQUE_ORDER_CREATION_COUNT_KEY = "UniqueOrderCreationCount";
75
    private static final String PAYMENT_FAILURE_COUNT_KEY = "PaymentFailureCount";
76
    private static final String UNIQUE_PAYMENT_FAILURE_COUNT_KEY = "UniquePaymentFailureCount";
77
    private static final String PROCEED_TO_PAY_COUNT_KEY = "ProceedToPayCount";
78
    private static final String UNIQUE_PROCEED_TO_PAY_COUNT_KEY = "UniqueProceedToPayCount";
79
 
80
    private static final String FIRST_SOURCE_KEY = "FirstSource";
81
    private static final String SESSION_SOURCE_KEY = "SessionSource";
82
 
3488 vikas 83
    private Map<Long, Item> itemsMap;
84
    private Map<Long, Item> itemsCatalogIdMap;
85
    private Map<String, Item> itemsNameMap;
86
    private Map<String, Map<String, Long>> itemsResultMap;
3444 vikas 87
 
3488 vikas 88
    private Map<String, Map<String, String>> sessionIdSourceMap;
3444 vikas 89
 
90
    private Date fromDate;
91
    private Date toDate;
3531 vikas 92
 
93
    private Date queryFromDate;
94
    private Date queryToDate;
95
 
3444 vikas 96
    private DatastoreService datastore;
97
 
98
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
3488 vikas 99
        logger.warning("Running DailyProductWithSourceAggregator");
100
        sessionIdSourceMap = new HashMap<String, Map<String,String>>();
101
        itemsMap = new HashMap<Long, Item>();
102
        itemsCatalogIdMap = new HashMap<Long, Item>();
103
        itemsNameMap = new HashMap<String, Item>();
104
        itemsResultMap = new HashMap<String, Map<String, Long>>();
105
 
3444 vikas 106
        datastore = DatastoreServiceFactory.getDatastoreService();
107
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
108
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
109
        String dateStr = req.getParameter("date");  
110
        if (dateStr != null && !dateStr.isEmpty()) {
111
            try {
112
                cal.setTime(sdf.parse(dateStr));
113
                cal.add(Calendar.DAY_OF_MONTH, 1);
114
            } catch (ParseException e) {
115
                e.printStackTrace();
116
            }
117
        }
118
        cal.set(Calendar.HOUR_OF_DAY, 0);  
119
        cal.set(Calendar.MINUTE, 0);  
120
        cal.set(Calendar.SECOND, 0);  
121
        cal.set(Calendar.MILLISECOND, 0);
122
        toDate = cal.getTime();
123
        cal.add(Calendar.DAY_OF_MONTH, -1);
124
        fromDate = cal.getTime();
125
 
126
        ItemRepository itemRepository = new ItemRepository();
127
        List<Item> items = itemRepository.getAll();
128
        for (Item item : items) {
129
            itemsMap.put(item.getId(), item);
130
            itemsCatalogIdMap.put(item.getCatalogId(), item);
131
            itemsNameMap.put(item.getName().trim(), item);
132
        }
133
 
3531 vikas 134
        for (int i = 0; i < 4; i++) {
135
            cal.setTime(fromDate);
136
            cal.add(Calendar.HOUR_OF_DAY, 6*i);
137
            queryFromDate = cal.getTime();
3444 vikas 138
 
3531 vikas 139
            cal.add(Calendar.HOUR_OF_DAY, 6);
140
            queryToDate = cal.getTime();
3444 vikas 141
 
3531 vikas 142
            logger.warning("From Query Date : " + queryFromDate);
143
            logger.warning("To Query Date : " + queryToDate);
3444 vikas 144
 
3531 vikas 145
            logger.warning("DailyProductWithSourceAggregator : Updating View Count Map");
146
            UpdateViewCount(PRODUCT_VIEW_EVENT, VIEW_COUNT_KEY,
147
                    UNIQUE_VIEW_COUNT_KEY);
148
            logger.warning("DailyProductWithSourceAggregator : itemsResultMap size : "
149
                    + itemsResultMap.size());
150
            logger.warning("DailyProductWithSourceAggregator : sessionIdSourceMap size : "
151
                    + sessionIdSourceMap.size());
152
 
153
            logger.warning("DailyProductWithSourceAggregator : Updating Add To Cart Count Map");
154
            UpdateItemIdEventCount(ADD_TO_CART_EVENT, ADD_TO_CART_COUNT_KEY,
155
                    UNIQUE_ADD_TO_CART_COUNT_KEY, false);
156
            logger.warning("DailyProductWithSourceAggregator : Updating Delete From Cart Count Map");
157
            UpdateItemIdEventCount(DELETE_FROM_CART_EVENT,
158
                    DELETE_FROM_CART_COUNT_KEY,
159
                    UNIQUE_DELETE_FROM_CART_COUNT_KEY, false);
160
            logger.warning("DailyProductWithSourceAggregator : Updating Add To Research Count Map");
161
            UpdateItemIdEventCount(RESEARCH_ADD_EVENT,
162
                    ADD_TO_RESEARCH_COUNT_KEY,
163
                    UNIQUE_ADD_TO_RESEARCH_COUNT_KEY, true);
164
            logger.warning("DailyProductWithSourceAggregator : Updating Delete From Research Count Map");
165
            UpdateItemIdEventCount(RESEARCH_DELETE_EVENT,
166
                    DELETE_FROM_RESEARCH_COUNT_KEY,
167
                    UNIQUE_DELETE_FROM_RESEARCH_COUNT_KEY, true);
168
 
169
            logger.warning("DailyProductWithSourceAggregator : Updating ShippingAccessCount Count Map");
170
            UpdateItemIdEventCount(SHIPPING_ACCESS_EVENT,
171
                    SHIPPING_ACCESS_COUNT_KEY,
172
                    UNIQUE_SHIPPING_ACCESS_COUNT_KEY, false);
173
            logger.warning("DailyProductWithSourceAggregator : Updating Payment Success Count Map");
174
            UpdateItemIdEventCount(PAYMENT_SUCCESS_EVENT,
175
                    PAYMENT_SUCCESS_COUNT_KEY,
176
                    UNIQUE_PAYMENT_SUCCESS_COUNT_KEY, false);
177
            logger.warning("DailyProductWithSourceAggregator : Updating Order Creation Count Map");
178
            UpdateItemIdEventCount(ORDER_CREATION_EVENT,
179
                    ORDER_CREATION_COUNT_KEY, UNIQUE_ORDER_CREATION_COUNT_KEY,
180
                    false);
181
            logger.warning("DailyProductWithSourceAggregator : Updating Payment Failure Count Map");
182
            UpdateItemIdEventCount(PAYMENT_FAILURE_EVENT,
183
                    PAYMENT_FAILURE_COUNT_KEY,
184
                    UNIQUE_PAYMENT_FAILURE_COUNT_KEY, false);
185
            logger.warning("DailyProductWithSourceAggregator : Updating Proceed To Pay Count Map");
186
            UpdateItemIdEventCount(PROCEED_TO_PAY_EVENT,
187
                    PROCEED_TO_PAY_COUNT_KEY, UNIQUE_PROCEED_TO_PAY_COUNT_KEY,
188
                    false);
189
 
190
            List<ItemActivityWithSource> itemActivities = new ArrayList<ItemActivityWithSource>();
191
            ItemActivityWithSourceRepository itemActivityRepository = new ItemActivityWithSourceRepository();
192
            for (Entry<String, Map<String, Long>> entry : itemsResultMap
193
                    .entrySet()) {
194
                String key = (String) entry.getKey();
195
                Map<String, Long> itemMap = (Map<String, Long>) entry
196
                        .getValue();
197
 
198
                String[] keyItems = key.split("_");
199
 
200
                Boolean isPaidFirstSource = keyItems[0].equals("0") ? false
201
                        : true;
202
                String firstSource = keyItems[1];
203
                Boolean isPaidSessionSource = keyItems[2].equals("0") ? false
204
                        : true;
205
                String sessionSource = keyItems[3];
206
                Long catalogItemId = Long.parseLong(keyItems[4]);
207
 
208
                ItemActivityWithSource itemActivity = new ItemActivityWithSource();
209
                itemActivity.setIsPaidFirstSource(isPaidFirstSource);
210
                itemActivity.setFirstSource(firstSource);
211
                itemActivity.setIsPaidSessionSource(isPaidSessionSource);
212
                itemActivity.setSessionSource(sessionSource);
213
                itemActivity.setCatalogId(catalogItemId);
214
                itemActivity.setDate(fromDate);
215
 
216
                itemActivity.setView(itemMap.get(VIEW_COUNT_KEY));
217
                itemActivity.setAddToCart(itemMap.get(ADD_TO_CART_COUNT_KEY));
218
                itemActivity.setAddToResearch(itemMap
219
                        .get(ADD_TO_RESEARCH_COUNT_KEY));
220
                itemActivity.setDeleteFromCart(itemMap
221
                        .get(DELETE_FROM_CART_COUNT_KEY));
222
                itemActivity.setDeleteFromResearch(itemMap
223
                        .get(DELETE_FROM_RESEARCH_COUNT_KEY));
224
                itemActivity.setShippingAccess(itemMap
225
                        .get(SHIPPING_ACCESS_COUNT_KEY));
226
                itemActivity.setPaymentSuccess(itemMap
227
                        .get(PAYMENT_SUCCESS_COUNT_KEY));
228
                itemActivity.setOrderCreation(itemMap
229
                        .get(ORDER_CREATION_COUNT_KEY));
230
                itemActivity.setPaymentFailure(itemMap
231
                        .get(PAYMENT_FAILURE_COUNT_KEY));
232
                itemActivity.setProceedToPay(itemMap
233
                        .get(PROCEED_TO_PAY_COUNT_KEY));
234
 
235
                itemActivity.setUniqueView(itemMap.get(UNIQUE_VIEW_COUNT_KEY));
236
                itemActivity.setUniqueAddToCart(itemMap
237
                        .get(UNIQUE_ADD_TO_CART_COUNT_KEY));
238
                itemActivity.setUniqueAddToResearch(itemMap
239
                        .get(UNIQUE_ADD_TO_RESEARCH_COUNT_KEY));
240
                itemActivity.setUniqueDeleteFromCart(itemMap
241
                        .get(UNIQUE_DELETE_FROM_CART_COUNT_KEY));
242
                itemActivity.setUniqueDeleteFromResearch(itemMap
243
                        .get(UNIQUE_ADD_TO_RESEARCH_COUNT_KEY));
244
                itemActivity.setUniqueShippingAccess(itemMap
245
                        .get(UNIQUE_SHIPPING_ACCESS_COUNT_KEY));
246
                itemActivity.setUniquePaymentSuccess(itemMap
247
                        .get(UNIQUE_PAYMENT_SUCCESS_COUNT_KEY));
248
                itemActivity.setUniqueOrderCreation(itemMap
249
                        .get(UNIQUE_ORDER_CREATION_COUNT_KEY));
250
                itemActivity.setUniquePaymentFailure(itemMap
251
                        .get(UNIQUE_PAYMENT_FAILURE_COUNT_KEY));
252
                itemActivity.setUniqueProceedToPay(itemMap
253
                        .get(UNIQUE_PROCEED_TO_PAY_COUNT_KEY));
254
 
255
                itemActivities.add(itemActivity);
256
 
3488 vikas 257
            }
3531 vikas 258
            logger.warning("DailyProductWithSourceAggregator : persisting itemactivities : "
259
                    + itemActivities.size());
260
            logger.warning("DailyProductWithSourceAggregator : itemactivities : "
261
                    + itemActivities);
262
            itemActivityRepository.createAll(itemActivities);
3444 vikas 263
        }
264
    }
265
 
266
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
267
        doPost(req, resp);
268
    }
269
 
270
    private void UpdateViewCount(String event, String viewCountKey, String uniqueViewCountKey) {
271
        Query query = new Query(KIND);
3488 vikas 272
        query.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL, event);
3531 vikas 273
        query.addFilter(DATE_FIELD, Query.FilterOperator.GREATER_THAN_OR_EQUAL, queryFromDate);
274
        query.addFilter(DATE_FIELD, Query.FilterOperator.LESS_THAN, queryToDate);
3444 vikas 275
        PreparedQuery pq = datastore.prepare(query);
276
        Set<String> uniqueSet = new HashSet<String>();
3488 vikas 277
        List<Entity> entityResults = new ArrayList<Entity>();
278
 
3523 vikas 279
        Set<String> sessionIds = new HashSet<String>();
3488 vikas 280
        logger.warning("DailyProductWithSourceAggregator : Getting View Events in list");
3531 vikas 281
        for (Entity e : pq.asIterable(FetchOptions.Builder.withChunkSize(500))) {
3488 vikas 282
            entityResults.add(e);
3531 vikas 283
        }
284
        for (Entity e : entityResults) {
3523 vikas 285
            String sessionId = (String)e.getProperty(SESSION_ID_FIELD);
286
            if (sessionId != null && !sessionId.isEmpty()) {
287
                sessionIds.add(sessionId);
288
            }
3488 vikas 289
        }
3523 vikas 290
        populateSessionIdSourceMap(sessionIds);
291
        sessionIds = null;
3488 vikas 292
        logger.warning("DailyProductWithSourceAggregator : Done Getting View Events in list");
293
        for (Entity result : entityResults) {
294
            String sessionId = (String)result.getProperty(SESSION_ID_FIELD);
3444 vikas 295
            String firstSource = getSource(sessionId, FIRST_SOURCE_KEY);
296
            String sessionSource = getSource(sessionId, SESSION_SOURCE_KEY);
297
            String paidFirstSource = "0";
298
            String paidSessionSource = "0";
299
            if (firstSource.startsWith("PAID :")) {
300
                paidFirstSource = "1";
301
            }
302
            if (sessionSource.startsWith("PAID :")) {
303
                paidSessionSource = "1";
304
            }
3488 vikas 305
            firstSource = firstSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
306
            sessionSource = sessionSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
307
            Long catalogItemId = (Long)result.getProperty(ITEMID_FIELD);
3444 vikas 308
            if(catalogItemId == null) {
3488 vikas 309
                String name = (String)result.getProperty(PRODUCTNAME_FIELD);
3444 vikas 310
                if (itemsNameMap.containsKey(name.trim())) {
311
                    catalogItemId = itemsNameMap.get(name.trim()).getCatalogId();
312
                }
313
            }
314
            String key = paidFirstSource + "_" + firstSource + "_"
315
                    + paidSessionSource + "_" + sessionSource + "_"
316
                    + catalogItemId;
317
            if (itemsResultMap.containsKey(key)) {
318
                Map<String, Long> itemMap = itemsResultMap.get(key);
319
                if (itemMap.containsKey(VIEW_COUNT_KEY)) {
320
                    Long count = itemMap.get(VIEW_COUNT_KEY);
321
                    itemMap.put(VIEW_COUNT_KEY, ++count);
322
                }
323
                else {
324
                    itemMap.put(VIEW_COUNT_KEY, 1l);
325
                }
326
            }
327
            else {
328
                Map<String, Long> itemMap = new HashMap<String, Long>();
329
                itemMap.put(VIEW_COUNT_KEY, 1l);
330
                itemsResultMap.put(key, itemMap);
331
            }
332
            String uniqueKey = sessionId + "_" + catalogItemId;
333
            if (!uniqueSet.contains(uniqueKey)) {
334
                Map<String, Long> itemMap = itemsResultMap.get(key);
335
                if (itemMap.containsKey(UNIQUE_VIEW_COUNT_KEY)) {
336
                    Long count = itemMap.get(UNIQUE_VIEW_COUNT_KEY);
337
                    itemMap.put(UNIQUE_VIEW_COUNT_KEY, ++count);
338
                }
339
                else {
340
                    itemMap.put(UNIQUE_VIEW_COUNT_KEY, 1l);
341
                }
342
                uniqueSet.add(uniqueKey);
343
            }
344
        }
345
    }
346
 
3523 vikas 347
    private void populateSessionIdSourceMap(Set<String> sessionIds) {
348
 
349
        Query sessionQuery = new Query(KIND);
350
        sessionQuery.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL,
351
                NEW_SESSION_EVENT);
3531 vikas 352
        sessionQuery.addFilter(DATE_FIELD, Query.FilterOperator.GREATER_THAN_OR_EQUAL, queryFromDate);
353
        sessionQuery.addFilter(DATE_FIELD, Query.FilterOperator.LESS_THAN, queryToDate);
3523 vikas 354
 
355
        PreparedQuery sessionPq = datastore.prepare(sessionQuery);
3531 vikas 356
 
357
        List<Entity> entityResults = new ArrayList<Entity>();
358
        for (Entity e : sessionPq.asIterable(FetchOptions.Builder.withChunkSize(500))) {
359
            entityResults.add(e);
360
        }
361
 
362
        for (Entity sessionResult : entityResults) {
3523 vikas 363
            String sessionId = (String)sessionResult.getProperty(SESSION_ID_FIELD);
364
            if (sessionIds.contains(sessionId)) {
365
                String firstSource = "";
366
                String sessionSource = "";
367
                if (sessionResult != null) {
368
                    if (sessionResult.hasProperty(FIRST_SOURCE_FIELD)) {
369
                        firstSource = sessionResult.getProperty(
370
                                FIRST_SOURCE_FIELD).toString();
371
                    }
372
                    if (sessionResult.hasProperty(SESSION_SOURCE_FIELD)) {
373
                        sessionSource = sessionResult.getProperty(
374
                                SESSION_SOURCE_FIELD).toString();
375
                    }
376
                }
377
                Map<String, String> sessionSourcesMap = new HashMap<String, String>();
378
                sessionSourcesMap.put(FIRST_SOURCE_KEY, firstSource);
379
                sessionSourcesMap.put(SESSION_SOURCE_KEY, sessionSource);
380
                sessionIdSourceMap.put(sessionId, sessionSourcesMap);
381
            }
382
        }
383
        logger.warning("DailyProductWithSourceAggregator : polpulateSessionIds : "
384
                + sessionIdSourceMap.size());
385
    }
386
 
3444 vikas 387
    private String getSource(String sessionId, String sourceKey) {
3488 vikas 388
        if (sessionIdSourceMap.containsKey(sessionId)) {
389
            return sessionIdSourceMap.get(sessionId).get(sourceKey);
390
        }
3523 vikas 391
        logger.warning("DailyProductWithSourceAggregator : sessionIdSourceMap miss : "
392
                + sessionId);
3488 vikas 393
        if (sessionId != null && !sessionId.isEmpty()) {
3444 vikas 394
            Query sessionQuery = new Query(KIND);
3488 vikas 395
            sessionQuery.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL,
3444 vikas 396
                    NEW_SESSION_EVENT);
3488 vikas 397
            sessionQuery.addFilter(SESSION_ID_FIELD,
398
                    Query.FilterOperator.EQUAL, sessionId);
399
 
3444 vikas 400
            PreparedQuery sessionPq = datastore.prepare(sessionQuery);
401
            for (Entity sessionResult : sessionPq.asIterable()) {
402
                String firstSource = "";
403
                String sessionSource = "";
404
                if (sessionResult != null) {
3488 vikas 405
                    if (sessionResult.hasProperty(FIRST_SOURCE_FIELD)) {
406
                        firstSource = sessionResult.getProperty(
407
                                FIRST_SOURCE_FIELD).toString();
3444 vikas 408
                    }
3488 vikas 409
                    if (sessionResult.hasProperty(SESSION_SOURCE_FIELD)) {
410
                        sessionSource = sessionResult.getProperty(
411
                                SESSION_SOURCE_FIELD).toString();
3444 vikas 412
                    }
413
                }
414
                Map<String, String> sessionSourcesMap = new HashMap<String, String>();
415
                sessionSourcesMap.put(FIRST_SOURCE_KEY, firstSource);
416
                sessionSourcesMap.put(SESSION_SOURCE_KEY, sessionSource);
417
                sessionIdSourceMap.put(sessionId, sessionSourcesMap);
418
            }
419
        }
420
        if (sessionIdSourceMap.containsKey(sessionId)) {
421
            return sessionIdSourceMap.get(sessionId).get(sourceKey);
422
        }
3523 vikas 423
        logger.warning("DailyProductWithSourceAggregator : sessionIdSourceMap not found : "
424
                + sessionId);
3444 vikas 425
        return "";
426
    }
427
 
428
    private void UpdateItemIdEventCount(String event, String countKey, String uniqueCountKey, boolean isCatalogIdEvent) {
429
        Query query = new Query(KIND);
3488 vikas 430
        query.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL, event);
3531 vikas 431
        query.addFilter(DATE_FIELD, Query.FilterOperator.GREATER_THAN_OR_EQUAL, queryFromDate);
432
        query.addFilter(DATE_FIELD, Query.FilterOperator.LESS_THAN, queryToDate);
3444 vikas 433
        PreparedQuery pq = datastore.prepare(query);
434
        Set<String> uniqueSet = new HashSet<String>();
3488 vikas 435
        List<Entity> results = new ArrayList<Entity>();
436
        for (Entity e : pq.asList(FetchOptions.Builder.withChunkSize(500))) {
437
            results.add(e);
438
        }
439
        for (Entity result : results) {
440
            String sessionId = (String)result.getProperty(SESSION_ID_FIELD);
3444 vikas 441
            String firstSource = getSource(sessionId, FIRST_SOURCE_KEY);
442
            String sessionSource = getSource(sessionId, SESSION_SOURCE_KEY);
443
            String paidFirstSource = "0";
444
            String paidSessionSource = "0";
445
            if (firstSource.startsWith("PAID :")) {
446
                paidFirstSource = "1";
447
            }
448
            if (sessionSource.startsWith("PAID :")) {
449
                paidSessionSource = "1";
450
            }
3488 vikas 451
            firstSource = firstSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
452
            sessionSource = sessionSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
3444 vikas 453
            List<Long> itemIds = null;
3488 vikas 454
            if (!result.hasProperty(ITEMIDS_FIELD)) {
3444 vikas 455
                continue;
456
            }
457
            try {
3488 vikas 458
                itemIds = (List<Long>)result.getProperty(ITEMIDS_FIELD);
3444 vikas 459
            } catch (ClassCastException e) {
460
                try {
461
                    itemIds = new ArrayList<Long>();
3488 vikas 462
                    itemIds.add((Long)result.getProperty(ITEMIDS_FIELD));
3444 vikas 463
                }
464
                catch(Exception ex) {
465
                    log(e.getMessage());
466
                }
467
            }
468
 
469
            if (itemIds == null) {
470
                continue;
471
            }
472
            for (Long itemId : itemIds) {
473
                if (itemId == null) {
474
                    continue;
475
                }
476
                if (!isCatalogIdEvent) {
477
                    if (itemsMap.containsKey(itemId)) {
478
                        itemId = itemsMap.get(itemId).getCatalogId();
479
                    }
480
                }
481
                String key = paidFirstSource + "_" + firstSource + "_"
482
                    + paidSessionSource + "_" + sessionSource + "_"
483
                    + itemId;
484
                if (itemsResultMap.containsKey(key)) {
485
                    Map<String, Long> itemMap = itemsResultMap.get(key);
486
                    if (itemMap.containsKey(countKey)) {
487
                        Long count = itemMap.get(countKey);
488
                        itemMap.put(countKey, ++count);
489
                    }
490
                    else {
491
                        itemMap.put(countKey, 1l);
492
                    }
493
                }
494
                else {
495
                    Map<String, Long> itemMap = new HashMap<String, Long>();
496
                    itemMap.put(countKey, 1l);
497
                    itemsResultMap.put(key, itemMap);
498
                }
499
                String uniqueKey = sessionId + itemId;
500
                if (!uniqueSet.contains(uniqueKey)) {
501
                    Map<String, Long> itemMap = itemsResultMap.get(key);
502
                    if (itemMap.containsKey(uniqueCountKey)) {
503
                        Long count = itemMap.get(uniqueCountKey);
504
                        itemMap.put(uniqueCountKey, ++count);
505
                    }
506
                    else {
507
                        itemMap.put(uniqueCountKey, 1l);
508
                    }
509
                    uniqueSet.add(uniqueKey);
510
                }
511
            }
512
        }
513
    }
514
}