Subversion Repositories SmartDukaan

Rev

Rev 3444 | Rev 3523 | 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
 
48
    private static final String PRODUCT_VIEW_EVENT = "PRODUCT_VIEW";
49
    private static final String ADD_TO_CART_EVENT = "ADD_TO_CART";
50
    private static final String DELETE_FROM_CART_EVENT = "DELETE_FROM_CART";
51
    private static final String RESEARCH_ADD_EVENT = "RESEARCH_ADD";
52
    private static final String RESEARCH_DELETE_EVENT = "RESEARCH_DELETE";
53
    private static final String SHIPPING_ACCESS_EVENT = "SHIPPINIG_ACCESS";
54
    private static final String PAYMENT_SUCCESS_EVENT = "PAYMENT_SUCCESS";
55
    private static final String ORDER_CREATION_EVENT = "ORDER_CREATION";
56
    private static final String PAYMENT_FAILURE_EVENT = "PAYMENT_FAILURE";
57
    private static final String PROCEED_TO_PAY_EVENT = "PROCEED_TO_PAY";
58
    private static final String NEW_SESSION_EVENT = "NEW_SESSION";
59
 
60
    private static final String VIEW_COUNT_KEY = "ViewCount";
61
    private static final String UNIQUE_VIEW_COUNT_KEY = "UniqueViewCount";
62
    private static final String ADD_TO_CART_COUNT_KEY = "AddToCartCount";
63
    private static final String UNIQUE_ADD_TO_CART_COUNT_KEY = "UniqueAddToCartCount";
64
    private static final String DELETE_FROM_CART_COUNT_KEY = "DeleteFromCartCount";
65
    private static final String UNIQUE_DELETE_FROM_CART_COUNT_KEY = "UniqueDeleteFromCartCount";
66
    private static final String ADD_TO_RESEARCH_COUNT_KEY = "AddToResearchCount";
67
    private static final String UNIQUE_ADD_TO_RESEARCH_COUNT_KEY = "UniqueAddToResearchCount";
68
    private static final String DELETE_FROM_RESEARCH_COUNT_KEY = "DeleteFromResearchCount";
69
    private static final String UNIQUE_DELETE_FROM_RESEARCH_COUNT_KEY = "UniqueDeleteFromResearchCount";
70
    private static final String SHIPPING_ACCESS_COUNT_KEY = "ShippingAccessCount";
71
    private static final String UNIQUE_SHIPPING_ACCESS_COUNT_KEY = "UniqueShippingAccessCount";
72
    private static final String PAYMENT_SUCCESS_COUNT_KEY = "PaymentSuccessCount";
73
    private static final String UNIQUE_PAYMENT_SUCCESS_COUNT_KEY = "UniquePaymentSuccessCount";
74
    private static final String ORDER_CREATION_COUNT_KEY = "OrderCreationCount";
75
    private static final String UNIQUE_ORDER_CREATION_COUNT_KEY = "UniqueOrderCreationCount";
76
    private static final String PAYMENT_FAILURE_COUNT_KEY = "PaymentFailureCount";
77
    private static final String UNIQUE_PAYMENT_FAILURE_COUNT_KEY = "UniquePaymentFailureCount";
78
    private static final String PROCEED_TO_PAY_COUNT_KEY = "ProceedToPayCount";
79
    private static final String UNIQUE_PROCEED_TO_PAY_COUNT_KEY = "UniqueProceedToPayCount";
80
 
81
    private static final String FIRST_SOURCE_KEY = "FirstSource";
82
    private static final String SESSION_SOURCE_KEY = "SessionSource";
83
 
3488 vikas 84
    private Map<Long, Item> itemsMap;
85
    private Map<Long, Item> itemsCatalogIdMap;
86
    private Map<String, Item> itemsNameMap;
87
    private Map<String, Map<String, Long>> itemsResultMap;
3444 vikas 88
 
3488 vikas 89
    private Map<String, Map<String, String>> sessionIdSourceMap;
3444 vikas 90
 
91
    private Date fromDate;
92
    private Date toDate;
93
    private DatastoreService datastore;
94
 
95
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
3488 vikas 96
        logger.warning("Running DailyProductWithSourceAggregator");
97
        sessionIdSourceMap = new HashMap<String, Map<String,String>>();
98
        itemsMap = new HashMap<Long, Item>();
99
        itemsCatalogIdMap = new HashMap<Long, Item>();
100
        itemsNameMap = new HashMap<String, Item>();
101
        itemsResultMap = new HashMap<String, Map<String, Long>>();
102
 
3444 vikas 103
        datastore = DatastoreServiceFactory.getDatastoreService();
104
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
105
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
106
        String dateStr = req.getParameter("date");  
107
        if (dateStr != null && !dateStr.isEmpty()) {
108
            try {
109
                cal.setTime(sdf.parse(dateStr));
110
                cal.add(Calendar.DAY_OF_MONTH, 1);
111
            } catch (ParseException e) {
112
                e.printStackTrace();
113
            }
114
        }
115
        cal.set(Calendar.HOUR_OF_DAY, 0);  
116
        cal.set(Calendar.MINUTE, 0);  
117
        cal.set(Calendar.SECOND, 0);  
118
        cal.set(Calendar.MILLISECOND, 0);
119
        toDate = cal.getTime();
120
        cal.add(Calendar.DAY_OF_MONTH, -1);
121
        fromDate = cal.getTime();
122
 
123
        ItemRepository itemRepository = new ItemRepository();
124
        List<Item> items = itemRepository.getAll();
125
        for (Item item : items) {
126
            itemsMap.put(item.getId(), item);
127
            itemsCatalogIdMap.put(item.getCatalogId(), item);
128
            itemsNameMap.put(item.getName().trim(), item);
129
        }
130
 
3488 vikas 131
        logger.warning("DailyProductWithSourceAggregator : Updating View Count Map");
3444 vikas 132
        UpdateViewCount(PRODUCT_VIEW_EVENT, VIEW_COUNT_KEY, UNIQUE_VIEW_COUNT_KEY);
3488 vikas 133
        logger.warning("DailyProductWithSourceAggregator : itemsResultMap size : " + itemsResultMap.size());
134
        logger.warning("DailyProductWithSourceAggregator : sessionIdSourceMap size : " + sessionIdSourceMap.size());
3444 vikas 135
 
3488 vikas 136
        logger.warning("DailyProductWithSourceAggregator : Updating Add To Cart Count Map");
3444 vikas 137
        UpdateItemIdEventCount(ADD_TO_CART_EVENT, ADD_TO_CART_COUNT_KEY, UNIQUE_ADD_TO_CART_COUNT_KEY, false);
3488 vikas 138
        logger.warning("DailyProductWithSourceAggregator : Updating Delete From Cart Count Map");
3444 vikas 139
        UpdateItemIdEventCount(DELETE_FROM_CART_EVENT, DELETE_FROM_CART_COUNT_KEY, UNIQUE_DELETE_FROM_CART_COUNT_KEY, false);
3488 vikas 140
        logger.warning("DailyProductWithSourceAggregator : Updating Add To Research Count Map");
3444 vikas 141
        UpdateItemIdEventCount(RESEARCH_ADD_EVENT, ADD_TO_RESEARCH_COUNT_KEY, UNIQUE_ADD_TO_RESEARCH_COUNT_KEY, true);
3488 vikas 142
        logger.warning("DailyProductWithSourceAggregator : Updating Delete From Research Count Map");
3444 vikas 143
        UpdateItemIdEventCount(RESEARCH_DELETE_EVENT, DELETE_FROM_RESEARCH_COUNT_KEY, UNIQUE_DELETE_FROM_RESEARCH_COUNT_KEY, true);
144
 
3488 vikas 145
        logger.warning("DailyProductWithSourceAggregator : Updating ShippingAccessCount Count Map");
3444 vikas 146
        UpdateItemIdEventCount(SHIPPING_ACCESS_EVENT, SHIPPING_ACCESS_COUNT_KEY, UNIQUE_SHIPPING_ACCESS_COUNT_KEY, false);
3488 vikas 147
        logger.warning("DailyProductWithSourceAggregator : Updating Payment Success Count Map");
3444 vikas 148
        UpdateItemIdEventCount(PAYMENT_SUCCESS_EVENT, PAYMENT_SUCCESS_COUNT_KEY, UNIQUE_PAYMENT_SUCCESS_COUNT_KEY, false);
3488 vikas 149
        logger.warning("DailyProductWithSourceAggregator : Updating Order Creation Count Map");
3444 vikas 150
        UpdateItemIdEventCount(ORDER_CREATION_EVENT, ORDER_CREATION_COUNT_KEY, UNIQUE_ORDER_CREATION_COUNT_KEY, false);
3488 vikas 151
        logger.warning("DailyProductWithSourceAggregator : Updating Payment Failure Count Map");
3444 vikas 152
        UpdateItemIdEventCount(PAYMENT_FAILURE_EVENT, PAYMENT_FAILURE_COUNT_KEY, UNIQUE_PAYMENT_FAILURE_COUNT_KEY, false);
3488 vikas 153
        logger.warning("DailyProductWithSourceAggregator : Updating Proceed To Pay Count Map");
3444 vikas 154
        UpdateItemIdEventCount(PROCEED_TO_PAY_EVENT, PROCEED_TO_PAY_COUNT_KEY, UNIQUE_PROCEED_TO_PAY_COUNT_KEY, false);
155
 
156
        List<ItemActivityWithSource> itemActivities = new ArrayList<ItemActivityWithSource>();
157
        ItemActivityWithSourceRepository itemActivityRepository = new ItemActivityWithSourceRepository();
158
        for (Entry<String, Map<String, Long>> entry : itemsResultMap.entrySet()) {
159
            String key = (String)entry.getKey();
160
            Map<String, Long> itemMap = (Map<String, Long>)entry.getValue();
161
 
162
            String [] keyItems = key.split("_");
163
 
164
            Boolean isPaidFirstSource = keyItems[0].equals("0")? false : true;
165
            String firstSource = keyItems[1];
166
            Boolean isPaidSessionSource = keyItems[2].equals("0")? false : true;
167
            String sessionSource = keyItems[3];
168
            Long catalogItemId = Long.parseLong(keyItems[4]);
169
 
170
            ItemActivityWithSource itemActivity = new ItemActivityWithSource();
171
            itemActivity.setIsPaidFirstSource(isPaidFirstSource);
172
            itemActivity.setFirstSource(firstSource);
173
            itemActivity.setIsPaidSessionSource(isPaidSessionSource);
174
            itemActivity.setSessionSource(sessionSource);
175
            itemActivity.setCatalogId(catalogItemId);
176
            itemActivity.setDate(fromDate);
177
 
178
            itemActivity.setView(itemMap.get(VIEW_COUNT_KEY));
179
            itemActivity.setAddToCart(itemMap.get(ADD_TO_CART_COUNT_KEY));
180
            itemActivity.setAddToResearch(itemMap.get(ADD_TO_RESEARCH_COUNT_KEY));
181
            itemActivity.setDeleteFromCart(itemMap.get(DELETE_FROM_CART_COUNT_KEY));
182
            itemActivity.setDeleteFromResearch(itemMap.get(DELETE_FROM_RESEARCH_COUNT_KEY));
183
            itemActivity.setShippingAccess(itemMap.get(SHIPPING_ACCESS_COUNT_KEY));
184
            itemActivity.setPaymentSuccess(itemMap.get(PAYMENT_SUCCESS_COUNT_KEY));
185
            itemActivity.setOrderCreation(itemMap.get(ORDER_CREATION_COUNT_KEY));
186
            itemActivity.setPaymentFailure(itemMap.get(PAYMENT_FAILURE_COUNT_KEY));
187
            itemActivity.setProceedToPay(itemMap.get(PROCEED_TO_PAY_COUNT_KEY));
188
 
189
            itemActivity.setUniqueView(itemMap.get(UNIQUE_VIEW_COUNT_KEY));
190
            itemActivity.setUniqueAddToCart(itemMap.get(UNIQUE_ADD_TO_CART_COUNT_KEY));
191
            itemActivity.setUniqueAddToResearch(itemMap.get(UNIQUE_ADD_TO_RESEARCH_COUNT_KEY));
192
            itemActivity.setUniqueDeleteFromCart(itemMap.get(UNIQUE_DELETE_FROM_CART_COUNT_KEY));
193
            itemActivity.setUniqueDeleteFromResearch(itemMap.get(UNIQUE_ADD_TO_RESEARCH_COUNT_KEY));
194
            itemActivity.setUniqueShippingAccess(itemMap.get(UNIQUE_SHIPPING_ACCESS_COUNT_KEY));
195
            itemActivity.setUniquePaymentSuccess(itemMap.get(UNIQUE_PAYMENT_SUCCESS_COUNT_KEY));
196
            itemActivity.setUniqueOrderCreation(itemMap.get(UNIQUE_ORDER_CREATION_COUNT_KEY));
197
            itemActivity.setUniquePaymentFailure(itemMap.get(UNIQUE_PAYMENT_FAILURE_COUNT_KEY));
198
            itemActivity.setUniqueProceedToPay(itemMap.get(UNIQUE_PROCEED_TO_PAY_COUNT_KEY));
199
 
200
            itemActivities.add(itemActivity);
3488 vikas 201
 
202
            if (itemActivities.size()>500) {
203
                logger.warning("DailyProductWithSourceAggregator : persisting itemactivities : " + itemActivities.size());
204
                itemActivityRepository.createAll(itemActivities);
205
                itemActivities.clear();
206
            }
3444 vikas 207
        }
3488 vikas 208
        logger.warning("DailyProductWithSourceAggregator : persisting itemactivities : " + itemActivities.size());
209
        logger.warning("DailyProductWithSourceAggregator : itemactivities : " + itemActivities);
3444 vikas 210
        itemActivityRepository.createAll(itemActivities);
211
    }
212
 
213
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
214
        doPost(req, resp);
215
    }
216
 
217
    private void UpdateViewCount(String event, String viewCountKey, String uniqueViewCountKey) {
218
        Query query = new Query(KIND);
3488 vikas 219
        query.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL, event);
220
        query.addFilter(DATE_FIELD, Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
221
        query.addFilter(DATE_FIELD, Query.FilterOperator.LESS_THAN, toDate);
3444 vikas 222
        PreparedQuery pq = datastore.prepare(query);
223
        Set<String> uniqueSet = new HashSet<String>();
3488 vikas 224
        List<Entity> entityResults = new ArrayList<Entity>();
225
 
226
        logger.warning("DailyProductWithSourceAggregator : Getting View Events in list");
227
        for (Entity e : pq.asList(FetchOptions.Builder.withChunkSize(500))) {
228
            entityResults.add(e);
229
        }
230
        logger.warning("DailyProductWithSourceAggregator : Done Getting View Events in list");
231
        for (Entity result : entityResults) {
232
            String sessionId = (String)result.getProperty(SESSION_ID_FIELD);
3444 vikas 233
            String firstSource = getSource(sessionId, FIRST_SOURCE_KEY);
234
            String sessionSource = getSource(sessionId, SESSION_SOURCE_KEY);
235
            String paidFirstSource = "0";
236
            String paidSessionSource = "0";
237
            if (firstSource.startsWith("PAID :")) {
238
                paidFirstSource = "1";
239
            }
240
            if (sessionSource.startsWith("PAID :")) {
241
                paidSessionSource = "1";
242
            }
3488 vikas 243
            firstSource = firstSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
244
            sessionSource = sessionSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
245
            Long catalogItemId = (Long)result.getProperty(ITEMID_FIELD);
3444 vikas 246
            if(catalogItemId == null) {
3488 vikas 247
                String name = (String)result.getProperty(PRODUCTNAME_FIELD);
3444 vikas 248
                if (itemsNameMap.containsKey(name.trim())) {
249
                    catalogItemId = itemsNameMap.get(name.trim()).getCatalogId();
250
                }
251
            }
252
            String key = paidFirstSource + "_" + firstSource + "_"
253
                    + paidSessionSource + "_" + sessionSource + "_"
254
                    + catalogItemId;
255
            if (itemsResultMap.containsKey(key)) {
256
                Map<String, Long> itemMap = itemsResultMap.get(key);
257
                if (itemMap.containsKey(VIEW_COUNT_KEY)) {
258
                    Long count = itemMap.get(VIEW_COUNT_KEY);
259
                    itemMap.put(VIEW_COUNT_KEY, ++count);
260
                }
261
                else {
262
                    itemMap.put(VIEW_COUNT_KEY, 1l);
263
                }
264
            }
265
            else {
266
                Map<String, Long> itemMap = new HashMap<String, Long>();
267
                itemMap.put(VIEW_COUNT_KEY, 1l);
268
                itemsResultMap.put(key, itemMap);
269
            }
270
            String uniqueKey = sessionId + "_" + catalogItemId;
271
            if (!uniqueSet.contains(uniqueKey)) {
272
                Map<String, Long> itemMap = itemsResultMap.get(key);
273
                if (itemMap.containsKey(UNIQUE_VIEW_COUNT_KEY)) {
274
                    Long count = itemMap.get(UNIQUE_VIEW_COUNT_KEY);
275
                    itemMap.put(UNIQUE_VIEW_COUNT_KEY, ++count);
276
                }
277
                else {
278
                    itemMap.put(UNIQUE_VIEW_COUNT_KEY, 1l);
279
                }
280
                uniqueSet.add(uniqueKey);
281
            }
282
        }
283
    }
284
 
285
    private String getSource(String sessionId, String sourceKey) {
3488 vikas 286
        if (sessionIdSourceMap.containsKey(sessionId)) {
287
            logger.warning("DailyProductWithSourceAggregator : sessionIdSourceMap hit : "
288
                    + sessionId);
289
            return sessionIdSourceMap.get(sessionId).get(sourceKey);
290
        }
291
        if (sessionId != null && !sessionId.isEmpty()) {
3444 vikas 292
            Query sessionQuery = new Query(KIND);
3488 vikas 293
            sessionQuery.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL,
3444 vikas 294
                    NEW_SESSION_EVENT);
3488 vikas 295
            sessionQuery.addFilter(SESSION_ID_FIELD,
296
                    Query.FilterOperator.EQUAL, sessionId);
297
 
3444 vikas 298
            PreparedQuery sessionPq = datastore.prepare(sessionQuery);
299
            for (Entity sessionResult : sessionPq.asIterable()) {
300
                String firstSource = "";
301
                String sessionSource = "";
302
                if (sessionResult != null) {
3488 vikas 303
                    if (sessionResult.hasProperty(FIRST_SOURCE_FIELD)) {
304
                        firstSource = sessionResult.getProperty(
305
                                FIRST_SOURCE_FIELD).toString();
3444 vikas 306
                    }
3488 vikas 307
                    if (sessionResult.hasProperty(SESSION_SOURCE_FIELD)) {
308
                        sessionSource = sessionResult.getProperty(
309
                                SESSION_SOURCE_FIELD).toString();
3444 vikas 310
                    }
311
                }
312
                Map<String, String> sessionSourcesMap = new HashMap<String, String>();
313
                sessionSourcesMap.put(FIRST_SOURCE_KEY, firstSource);
314
                sessionSourcesMap.put(SESSION_SOURCE_KEY, sessionSource);
315
                sessionIdSourceMap.put(sessionId, sessionSourcesMap);
316
            }
317
        }
318
        if (sessionIdSourceMap.containsKey(sessionId)) {
319
            return sessionIdSourceMap.get(sessionId).get(sourceKey);
320
        }
321
        return "";
322
    }
323
 
324
    private void UpdateItemIdEventCount(String event, String countKey, String uniqueCountKey, boolean isCatalogIdEvent) {
325
        Query query = new Query(KIND);
3488 vikas 326
        query.addFilter(EVENTTYPE_FIELD, Query.FilterOperator.EQUAL, event);
327
        query.addFilter(DATE_FIELD, Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
328
        query.addFilter(DATE_FIELD, Query.FilterOperator.LESS_THAN, toDate);
3444 vikas 329
        PreparedQuery pq = datastore.prepare(query);
330
        Set<String> uniqueSet = new HashSet<String>();
3488 vikas 331
        List<Entity> results = new ArrayList<Entity>();
332
        for (Entity e : pq.asList(FetchOptions.Builder.withChunkSize(500))) {
333
            results.add(e);
334
        }
335
        for (Entity result : results) {
336
            String sessionId = (String)result.getProperty(SESSION_ID_FIELD);
3444 vikas 337
            String firstSource = getSource(sessionId, FIRST_SOURCE_KEY);
338
            String sessionSource = getSource(sessionId, SESSION_SOURCE_KEY);
339
            String paidFirstSource = "0";
340
            String paidSessionSource = "0";
341
            if (firstSource.startsWith("PAID :")) {
342
                paidFirstSource = "1";
343
            }
344
            if (sessionSource.startsWith("PAID :")) {
345
                paidSessionSource = "1";
346
            }
3488 vikas 347
            firstSource = firstSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
348
            sessionSource = sessionSource.replaceAll(".*http[s]?://", "").replaceAll("/.*", "").replaceAll("www.", "");
3444 vikas 349
            List<Long> itemIds = null;
3488 vikas 350
            if (!result.hasProperty(ITEMIDS_FIELD)) {
3444 vikas 351
                continue;
352
            }
353
            try {
3488 vikas 354
                itemIds = (List<Long>)result.getProperty(ITEMIDS_FIELD);
3444 vikas 355
            } catch (ClassCastException e) {
356
                try {
357
                    itemIds = new ArrayList<Long>();
3488 vikas 358
                    itemIds.add((Long)result.getProperty(ITEMIDS_FIELD));
3444 vikas 359
                }
360
                catch(Exception ex) {
361
                    log(e.getMessage());
362
                }
363
            }
364
 
365
            if (itemIds == null) {
366
                continue;
367
            }
368
            for (Long itemId : itemIds) {
369
                if (itemId == null) {
370
                    continue;
371
                }
372
                if (!isCatalogIdEvent) {
373
                    if (itemsMap.containsKey(itemId)) {
374
                        itemId = itemsMap.get(itemId).getCatalogId();
375
                    }
376
                }
377
                String key = paidFirstSource + "_" + firstSource + "_"
378
                    + paidSessionSource + "_" + sessionSource + "_"
379
                    + itemId;
380
                if (itemsResultMap.containsKey(key)) {
381
                    Map<String, Long> itemMap = itemsResultMap.get(key);
382
                    if (itemMap.containsKey(countKey)) {
383
                        Long count = itemMap.get(countKey);
384
                        itemMap.put(countKey, ++count);
385
                    }
386
                    else {
387
                        itemMap.put(countKey, 1l);
388
                    }
389
                }
390
                else {
391
                    Map<String, Long> itemMap = new HashMap<String, Long>();
392
                    itemMap.put(countKey, 1l);
393
                    itemsResultMap.put(key, itemMap);
394
                }
395
                String uniqueKey = sessionId + itemId;
396
                if (!uniqueSet.contains(uniqueKey)) {
397
                    Map<String, Long> itemMap = itemsResultMap.get(key);
398
                    if (itemMap.containsKey(uniqueCountKey)) {
399
                        Long count = itemMap.get(uniqueCountKey);
400
                        itemMap.put(uniqueCountKey, ++count);
401
                    }
402
                    else {
403
                        itemMap.put(uniqueCountKey, 1l);
404
                    }
405
                    uniqueSet.add(uniqueKey);
406
                }
407
            }
408
        }
409
    }
410
}