Subversion Repositories SmartDukaan

Rev

Rev 2262 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.datalogger;


import in.shop2020.datalogger.event.Event;
import in.shop2020.datalogger.event.ProductCompare;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

public class DataLogParser {
    private static String dataFile = "/var/log/website/data.log";
    private static Map<String, List<Event>> logDataMap;
    private static Map<Long, String> orderSessionMap;
    private static Map<Long, Set<String>> userSessionMap;
    
    private static void parseFile(String dataFile) {
        // TODO Auto-generated method stub
        logDataMap = new HashMap<String, List<Event>>();
        orderSessionMap = new HashMap<Long, String>();
        userSessionMap = new HashMap<Long, Set<String>>();
        File f = new File(dataFile);
        if(!f.exists()) {
            System.out.println(dataFile + " - does not exist");
        }
        
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line;
            while((line = br.readLine()) != null){
                String[] logFields = StringUtils.split(line, ",");
                Event event = Event.createEvent(logFields);
                String sessionId = event.getSessionId();
                if (!logDataMap.containsKey(sessionId)) {
                    List<Event> logEvents = new ArrayList<Event>();
                    logEvents.add(event);
                    logDataMap.put(sessionId, logEvents);
                }
                else {
                    logDataMap.get(sessionId).add(event);
                }
                String eventName = event.getEventType().name();
                if(eventName.equals(EventType.ORDER_CREATION.name())) {
                    Long orderId = Long.parseLong(logFields[5].trim());
                    if (!orderSessionMap.containsKey(orderId)) {
                        orderSessionMap.put(orderId, sessionId);
                    }
                }
                try {
                    Long userId = event.getUserId();
                    if (!userSessionMap.containsKey(userId)) {
                        Set<String> userSessions = new HashSet<String>();
                        userSessions.add(sessionId);
                        userSessionMap.put(userId, userSessions);
                    }
                    else {
                        userSessionMap.get(userId).add(sessionId);
                    }
                } catch (NumberFormatException e) {
                    // e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private static void printHistoryEventsForOrder(long i) {
        String session = orderSessionMap.get(i);
        
        List<Event> events = logDataMap.get(session);
        for (Event event : events) {
            System.out.println(event);
        }
    }
    
    private static List<Event> getCompareEvents(){
        List<Event> events = new ArrayList<Event>();
        
        
        File dir = new File("/var/log/website/");
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.startsWith("data.log");
            }
        };
        String[] children = dir.list(filter);

        for (int i=0; i<children.length; i++) {
                String filename = children[i];
                    File f = new File(filename);
                if(!f.exists()) {
                    System.out.println(filename + " - does not exist");
                }
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(f);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    String line;
                    while((line = br.readLine()) != null){
                        String[] logFields = StringUtils.split(line, ",");
                        Event event = Event.createEvent(logFields);
                        if(event.getEventType() == EventType.PRODUCT_COMPARE){
                                events.add(event);      
                        }
                    }
                }catch (Exception e) {
                                // TODO: handle exception
                        }
        }
        return events;
    }
    

    private static void printCompareStats(){
                class ValueComparator implements Comparator {
        
              Map base;
              public ValueComparator(Map base) {
                  this.base = base;
              }
        
              public int compare(Object a, Object b) {
        
                if((Double)base.get(a) < (Double)base.get(b)) {
                  return 1;
                } else if((Double)base.get(a) == (Double)base.get(b)) {
                  return 0;
                } else {
                  return -1;
                }
              }
            }
        
        StringBuffer sb = new StringBuffer();
        Map<Long, List<Long>> datas = new HashMap<Long, List<Long>>();  
        List<Event> events = getCompareEvents();
        for(Event event: events){
                ProductCompare pevent = (ProductCompare)event;
                List<Long> ids = pevent.getItemIds();
                for(Long id: ids){
                        List<Long> value = datas.get(id);
                        if(value == null){
                                value = new ArrayList<Long>();
                        }
                        value.add(id);
                }
        }
        for(Entry<Long, List<Long>> entry: datas.entrySet()){
                Map<Long, Long> frequencyMap = new HashMap<Long, Long>();
                for(Long entityId: entry.getValue()){
                        if(frequencyMap.containsKey(entityId)){
                                Long count = frequencyMap.get(entityId);
                                count = count + 1;
                        }else{
                                frequencyMap.put(entityId, 1L);
                        }
                }
                
        
              ValueComparator bvc =  new ValueComparator(frequencyMap);
              TreeMap<Long,Long> sorted_map = new TreeMap(bvc);
              sorted_map.putAll(frequencyMap);
              sb.append("\n" + entry.getKey() + ":");
          for (Long key : sorted_map.keySet()) {
                  sb.append(key + ":" + sorted_map.get(key)+":");
                      }
        
                }
          
                File f = new File("/var/log/website/comparestats.txt");
                if(!f.exists()) {
                        try {
                                f.createNewFile();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        
                ObjectOutputStream out = null;
                try {
                        FileOutputStream fos = new FileOutputStream(f);
                        fos.write(sb.toString().getBytes());
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally {
                        if(out != null) {
                                try {
                                        out.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
        
    }
   
    public static void main(String[] args) {
        printCompareStats();
           //parseFile(dataFile);
        
        /*
        for (Map.Entry<String, List<String>> entry : logDataMap.entrySet()) {
            List<String> logs = entry.getValue();
            for (String log : logs) {
                System.out.println(entry.getKey() + " : " + log);
            }
        }
        for (Map.Entry<Long, Set<String>> entry : userSessionMap.entrySet()) {
            Set<String> sessions = entry.getValue();
            for (String session : sessions) {
                System.out.println(entry.getKey() + " : " + session);
            }
        }
        for (Map.Entry<Long, String> entry : orderSessionMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        */
        //System.out.println(orderSessionMap.size());
        //System.out.println(userSessionMap.size());
        //System.out.println(logDataMap.size());
        
        //printHistoryEventsForOrder(500);
    }
}