Subversion Repositories SmartDukaan

Rev

Rev 2262 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2262 Rev 2392
Line 1... Line 1...
1
package in.shop2020.datalogger;
1
package in.shop2020.datalogger;
2
 
2
 
3
 
3
 
4
import in.shop2020.datalogger.event.Event;
4
import in.shop2020.datalogger.event.Event;
-
 
5
import in.shop2020.datalogger.event.ProductCompare;
5
 
6
 
6
import java.io.BufferedReader;
7
import java.io.BufferedReader;
7
import java.io.File;
8
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.FileInputStream;
-
 
10
import java.io.FileNotFoundException;
-
 
11
import java.io.FileOutputStream;
-
 
12
import java.io.FilenameFilter;
9
import java.io.IOException;
13
import java.io.IOException;
10
import java.io.InputStreamReader;
14
import java.io.InputStreamReader;
-
 
15
import java.io.ObjectOutputStream;
11
import java.util.ArrayList;
16
import java.util.ArrayList;
-
 
17
import java.util.Collections;
-
 
18
import java.util.Comparator;
12
import java.util.HashMap;
19
import java.util.HashMap;
13
import java.util.HashSet;
20
import java.util.HashSet;
-
 
21
import java.util.Iterator;
-
 
22
import java.util.LinkedHashMap;
-
 
23
import java.util.LinkedList;
14
import java.util.List;
24
import java.util.List;
15
import java.util.Map;
25
import java.util.Map;
-
 
26
import java.util.Map.Entry;
16
import java.util.Set;
27
import java.util.Set;
-
 
28
import java.util.TreeMap;
17
 
29
 
18
import org.apache.commons.lang.StringUtils;
30
import org.apache.commons.lang.StringUtils;
19
 
31
 
20
public class DataLogParser {
32
public class DataLogParser {
21
    private static String dataFile = "/var/log/website/data.log";
33
    private static String dataFile = "/var/log/website/data.log";
Line 92... Line 104...
92
        for (Event event : events) {
104
        for (Event event : events) {
93
            System.out.println(event);
105
            System.out.println(event);
94
        }
106
        }
95
    }
107
    }
96
    
108
    
-
 
109
    private static List<Event> getCompareEvents(){
-
 
110
    	List<Event> events = new ArrayList<Event>();
-
 
111
    	
-
 
112
    	
-
 
113
    	File dir = new File("/var/log/website/");
-
 
114
    	FilenameFilter filter = new FilenameFilter() {
-
 
115
    	    public boolean accept(File dir, String name) {
-
 
116
    	        return name.startsWith("data.log");
-
 
117
    	    }
-
 
118
    	};
-
 
119
    	String[] children = dir.list(filter);
-
 
120
 
-
 
121
    	for (int i=0; i<children.length; i++) {
-
 
122
	        String filename = children[i];
-
 
123
		    File f = new File(filename);
-
 
124
	        if(!f.exists()) {
-
 
125
	            System.out.println(filename + " - does not exist");
-
 
126
	        }
-
 
127
	        FileInputStream fis = null;
-
 
128
	        try {
-
 
129
	            fis = new FileInputStream(f);
-
 
130
	            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
-
 
131
	            String line;
-
 
132
	            while((line = br.readLine()) != null){
-
 
133
	                String[] logFields = StringUtils.split(line, ",");
-
 
134
	                Event event = Event.createEvent(logFields);
-
 
135
	                if(event.getEventType() == EventType.PRODUCT_COMPARE){
-
 
136
	                	events.add(event);	
-
 
137
	                }
-
 
138
	            }
-
 
139
	        }catch (Exception e) {
-
 
140
				// TODO: handle exception
-
 
141
			}
-
 
142
    	}
-
 
143
    	return events;
-
 
144
    }
-
 
145
    
-
 
146
 
-
 
147
    private static void printCompareStats(){
-
 
148
	 	class ValueComparator implements Comparator {
-
 
149
	
-
 
150
	      Map base;
-
 
151
	      public ValueComparator(Map base) {
-
 
152
	          this.base = base;
-
 
153
	      }
-
 
154
	
-
 
155
	      public int compare(Object a, Object b) {
-
 
156
	
-
 
157
	        if((Double)base.get(a) < (Double)base.get(b)) {
-
 
158
	          return 1;
-
 
159
	        } else if((Double)base.get(a) == (Double)base.get(b)) {
-
 
160
	          return 0;
-
 
161
	        } else {
-
 
162
	          return -1;
-
 
163
	        }
-
 
164
	      }
-
 
165
	    }
-
 
166
	
-
 
167
	StringBuffer sb = new StringBuffer();
-
 
168
	Map<Long, List<Long>> datas = new HashMap<Long, List<Long>>();  
-
 
169
	List<Event> events = getCompareEvents();
-
 
170
	for(Event event: events){
-
 
171
		ProductCompare pevent = (ProductCompare)event;
-
 
172
		List<Long> ids = pevent.getItemIds();
-
 
173
		for(Long id: ids){
-
 
174
			List<Long> value = datas.get(id);
-
 
175
			if(value == null){
-
 
176
				value = new ArrayList<Long>();
-
 
177
			}
-
 
178
			value.add(id);
-
 
179
		}
-
 
180
	}
-
 
181
	for(Entry<Long, List<Long>> entry: datas.entrySet()){
-
 
182
		Map<Long, Long> frequencyMap = new HashMap<Long, Long>();
-
 
183
		for(Long entityId: entry.getValue()){
-
 
184
			if(frequencyMap.containsKey(entityId)){
-
 
185
				Long count = frequencyMap.get(entityId);
-
 
186
				count = count + 1;
-
 
187
			}else{
-
 
188
				frequencyMap.put(entityId, 1L);
-
 
189
			}
-
 
190
		}
-
 
191
		
-
 
192
	
-
 
193
	      ValueComparator bvc =  new ValueComparator(frequencyMap);
-
 
194
	      TreeMap<Long,Long> sorted_map = new TreeMap(bvc);
-
 
195
	      sorted_map.putAll(frequencyMap);
-
 
196
	      sb.append("\n" + entry.getKey() + ":");
-
 
197
	  for (Long key : sorted_map.keySet()) {
-
 
198
		  sb.append(key + ":" + sorted_map.get(key)+":");
-
 
199
		      }
-
 
200
	
-
 
201
	  	}
-
 
202
	  
-
 
203
		File f = new File("/var/log/website/comparestats.txt");
-
 
204
		if(!f.exists()) {
-
 
205
			try {
-
 
206
				f.createNewFile();
-
 
207
			} catch (IOException e) {
-
 
208
				// TODO Auto-generated catch block
-
 
209
				e.printStackTrace();
-
 
210
			}
-
 
211
		}
-
 
212
	
-
 
213
		ObjectOutputStream out = null;
-
 
214
		try {
-
 
215
			FileOutputStream fos = new FileOutputStream(f);
-
 
216
			fos.write(sb.toString().getBytes());
-
 
217
		} catch (FileNotFoundException e) {
-
 
218
			// TODO Auto-generated catch block
-
 
219
			e.printStackTrace();
-
 
220
		} catch (IOException e) {
-
 
221
			// TODO Auto-generated catch block
-
 
222
			e.printStackTrace();
-
 
223
		}
-
 
224
		finally {
-
 
225
			if(out != null) {
-
 
226
				try {
-
 
227
					out.close();
-
 
228
				} catch (IOException e) {
-
 
229
					// TODO Auto-generated catch block
-
 
230
					e.printStackTrace();
-
 
231
				}
-
 
232
			}
-
 
233
		}
-
 
234
	
-
 
235
    }
-
 
236
   
97
    public static void main(String[] args) {
237
    public static void main(String[] args) {
-
 
238
    	printCompareStats();
98
        parseFile(dataFile);
239
           //parseFile(dataFile);
99
        
240
        
100
        /*
241
        /*
101
        for (Map.Entry<String, List<String>> entry : logDataMap.entrySet()) {
242
        for (Map.Entry<String, List<String>> entry : logDataMap.entrySet()) {
102
            List<String> logs = entry.getValue();
243
            List<String> logs = entry.getValue();
103
            for (String log : logs) {
244
            for (String log : logs) {
Line 112... Line 253...
112
        }
253
        }
113
        for (Map.Entry<Long, String> entry : orderSessionMap.entrySet()) {
254
        for (Map.Entry<Long, String> entry : orderSessionMap.entrySet()) {
114
            System.out.println(entry.getKey() + " : " + entry.getValue());
255
            System.out.println(entry.getKey() + " : " + entry.getValue());
115
        }
256
        }
116
        */
257
        */
117
        System.out.println(orderSessionMap.size());
258
        //System.out.println(orderSessionMap.size());
118
        System.out.println(userSessionMap.size());
259
        //System.out.println(userSessionMap.size());
119
        System.out.println(logDataMap.size());
260
        //System.out.println(logDataMap.size());
120
        
261
        
121
        printHistoryEventsForOrder(500);
262
        //printHistoryEventsForOrder(500);
122
    }
263
    }
123
}
264
}
124
265