| 2262 |
vikas |
1 |
package in.shop2020.datalogger;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import in.shop2020.datalogger.event.Event;
|
| 2392 |
rajveer |
5 |
import in.shop2020.datalogger.event.ProductCompare;
|
| 2262 |
vikas |
6 |
|
|
|
7 |
import java.io.BufferedReader;
|
|
|
8 |
import java.io.File;
|
|
|
9 |
import java.io.FileInputStream;
|
| 2392 |
rajveer |
10 |
import java.io.FileNotFoundException;
|
|
|
11 |
import java.io.FileOutputStream;
|
|
|
12 |
import java.io.FilenameFilter;
|
| 2262 |
vikas |
13 |
import java.io.IOException;
|
|
|
14 |
import java.io.InputStreamReader;
|
| 2392 |
rajveer |
15 |
import java.io.ObjectOutputStream;
|
| 2262 |
vikas |
16 |
import java.util.ArrayList;
|
| 2392 |
rajveer |
17 |
import java.util.Collections;
|
|
|
18 |
import java.util.Comparator;
|
| 2262 |
vikas |
19 |
import java.util.HashMap;
|
|
|
20 |
import java.util.HashSet;
|
| 2392 |
rajveer |
21 |
import java.util.Iterator;
|
|
|
22 |
import java.util.LinkedHashMap;
|
|
|
23 |
import java.util.LinkedList;
|
| 2262 |
vikas |
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
| 2392 |
rajveer |
26 |
import java.util.Map.Entry;
|
| 2262 |
vikas |
27 |
import java.util.Set;
|
| 2392 |
rajveer |
28 |
import java.util.TreeMap;
|
| 2262 |
vikas |
29 |
|
|
|
30 |
import org.apache.commons.lang.StringUtils;
|
|
|
31 |
|
|
|
32 |
public class DataLogParser {
|
|
|
33 |
private static String dataFile = "/var/log/website/data.log";
|
|
|
34 |
private static Map<String, List<Event>> logDataMap;
|
|
|
35 |
private static Map<Long, String> orderSessionMap;
|
|
|
36 |
private static Map<Long, Set<String>> userSessionMap;
|
|
|
37 |
|
|
|
38 |
private static void parseFile(String dataFile) {
|
|
|
39 |
// TODO Auto-generated method stub
|
|
|
40 |
logDataMap = new HashMap<String, List<Event>>();
|
|
|
41 |
orderSessionMap = new HashMap<Long, String>();
|
|
|
42 |
userSessionMap = new HashMap<Long, Set<String>>();
|
|
|
43 |
File f = new File(dataFile);
|
|
|
44 |
if(!f.exists()) {
|
|
|
45 |
System.out.println(dataFile + " - does not exist");
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
FileInputStream fis = null;
|
|
|
49 |
try {
|
|
|
50 |
fis = new FileInputStream(f);
|
|
|
51 |
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
|
|
|
52 |
String line;
|
|
|
53 |
while((line = br.readLine()) != null){
|
|
|
54 |
String[] logFields = StringUtils.split(line, ",");
|
|
|
55 |
Event event = Event.createEvent(logFields);
|
|
|
56 |
String sessionId = event.getSessionId();
|
|
|
57 |
if (!logDataMap.containsKey(sessionId)) {
|
|
|
58 |
List<Event> logEvents = new ArrayList<Event>();
|
|
|
59 |
logEvents.add(event);
|
|
|
60 |
logDataMap.put(sessionId, logEvents);
|
|
|
61 |
}
|
|
|
62 |
else {
|
|
|
63 |
logDataMap.get(sessionId).add(event);
|
|
|
64 |
}
|
|
|
65 |
String eventName = event.getEventType().name();
|
|
|
66 |
if(eventName.equals(EventType.ORDER_CREATION.name())) {
|
|
|
67 |
Long orderId = Long.parseLong(logFields[5].trim());
|
|
|
68 |
if (!orderSessionMap.containsKey(orderId)) {
|
|
|
69 |
orderSessionMap.put(orderId, sessionId);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
try {
|
|
|
73 |
Long userId = event.getUserId();
|
|
|
74 |
if (!userSessionMap.containsKey(userId)) {
|
|
|
75 |
Set<String> userSessions = new HashSet<String>();
|
|
|
76 |
userSessions.add(sessionId);
|
|
|
77 |
userSessionMap.put(userId, userSessions);
|
|
|
78 |
}
|
|
|
79 |
else {
|
|
|
80 |
userSessionMap.get(userId).add(sessionId);
|
|
|
81 |
}
|
|
|
82 |
} catch (NumberFormatException e) {
|
|
|
83 |
// e.printStackTrace();
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
} catch (IOException e) {
|
|
|
87 |
e.printStackTrace();
|
|
|
88 |
}
|
|
|
89 |
finally {
|
|
|
90 |
if(fis != null) {
|
|
|
91 |
try {
|
|
|
92 |
fis.close();
|
|
|
93 |
} catch (IOException e) {
|
|
|
94 |
e.printStackTrace();
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
private static void printHistoryEventsForOrder(long i) {
|
|
|
101 |
String session = orderSessionMap.get(i);
|
|
|
102 |
|
|
|
103 |
List<Event> events = logDataMap.get(session);
|
|
|
104 |
for (Event event : events) {
|
|
|
105 |
System.out.println(event);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
| 2392 |
rajveer |
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 |
|
| 2262 |
vikas |
237 |
public static void main(String[] args) {
|
| 2392 |
rajveer |
238 |
printCompareStats();
|
|
|
239 |
//parseFile(dataFile);
|
| 2262 |
vikas |
240 |
|
|
|
241 |
/*
|
|
|
242 |
for (Map.Entry<String, List<String>> entry : logDataMap.entrySet()) {
|
|
|
243 |
List<String> logs = entry.getValue();
|
|
|
244 |
for (String log : logs) {
|
|
|
245 |
System.out.println(entry.getKey() + " : " + log);
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
for (Map.Entry<Long, Set<String>> entry : userSessionMap.entrySet()) {
|
|
|
249 |
Set<String> sessions = entry.getValue();
|
|
|
250 |
for (String session : sessions) {
|
|
|
251 |
System.out.println(entry.getKey() + " : " + session);
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
for (Map.Entry<Long, String> entry : orderSessionMap.entrySet()) {
|
|
|
255 |
System.out.println(entry.getKey() + " : " + entry.getValue());
|
|
|
256 |
}
|
|
|
257 |
*/
|
| 2392 |
rajveer |
258 |
//System.out.println(orderSessionMap.size());
|
|
|
259 |
//System.out.println(userSessionMap.size());
|
|
|
260 |
//System.out.println(logDataMap.size());
|
| 2262 |
vikas |
261 |
|
| 2392 |
rajveer |
262 |
//printHistoryEventsForOrder(500);
|
| 2262 |
vikas |
263 |
}
|
|
|
264 |
}
|