1 | package de.ugoe.cs.eventbench.web;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.text.ParseException;
|
---|
8 | import java.text.SimpleDateFormat;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import de.ugoe.cs.eventbench.web.data.WebEvent;
|
---|
16 | import de.ugoe.cs.util.console.Console;
|
---|
17 |
|
---|
18 | public class WeblogParser {
|
---|
19 |
|
---|
20 | private long timeout;
|
---|
21 |
|
---|
22 | private int minLength = 2;
|
---|
23 |
|
---|
24 | private List<List<WebEvent>> sequences;
|
---|
25 |
|
---|
26 | public WeblogParser() {
|
---|
27 | timeout = 3600000; // 1 hour session-timeout as default
|
---|
28 | }
|
---|
29 |
|
---|
30 | public WeblogParser(long timeout) {
|
---|
31 | this.timeout = timeout;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public List<List<WebEvent>> getSequences() {
|
---|
35 | return sequences;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void setTimeout(long timeout) {
|
---|
39 | this.timeout = timeout;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void setMinLength(int minLength) {
|
---|
43 | this.minLength = minLength;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void parseFile(String filename) throws IOException, FileNotFoundException, ParseException {
|
---|
47 | File f = new File(filename);
|
---|
48 | FileReader reader = new FileReader(f);
|
---|
49 | char[] buffer = new char[(int) f.length()];
|
---|
50 | reader.read(buffer);
|
---|
51 | reader.close();
|
---|
52 | String[] lines = (new String(buffer)).split("\n");
|
---|
53 |
|
---|
54 | Map<String, List<Integer>> cookieSessionMap = new HashMap<String, List<Integer>>();
|
---|
55 | int lastId = -1;
|
---|
56 |
|
---|
57 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
58 |
|
---|
59 | sequences = new ArrayList<List<WebEvent>>();
|
---|
60 |
|
---|
61 | for( String line : lines ) {
|
---|
62 | String[] values = line.trim().split(" ");
|
---|
63 |
|
---|
64 | // use cookie as session identifier
|
---|
65 | int cookieStart = values[0].lastIndexOf('.');
|
---|
66 | String cookie = values[0].substring(cookieStart+1);
|
---|
67 | String dateString = values[1].substring(1)+" "+values[2].substring(0, values[2].length()-1);
|
---|
68 | long timestamp = dateFormat.parse(dateString).getTime();
|
---|
69 | String uri = values[3];
|
---|
70 | // String ref = values[4]; // referer is not yet used!
|
---|
71 | List<String> postedVars = new ArrayList<String>();
|
---|
72 | for( int i=5 ; i<values.length ; i++ ) {
|
---|
73 | postedVars.add(values[i]);
|
---|
74 | }
|
---|
75 |
|
---|
76 | WebEvent event = new WebEvent(uri, timestamp, postedVars);
|
---|
77 |
|
---|
78 | // find session and add event
|
---|
79 | List<Integer> sessionIds = cookieSessionMap.get(cookie);
|
---|
80 | if( sessionIds==null ) {
|
---|
81 | sessionIds = new ArrayList<Integer>();
|
---|
82 | // start new session
|
---|
83 | sessionIds.add(++lastId);
|
---|
84 | cookieSessionMap.put(cookie, sessionIds);
|
---|
85 | sequences.add(new LinkedList<WebEvent>());
|
---|
86 | }
|
---|
87 | Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
|
---|
88 | List<WebEvent> lastSession = sequences.get(lastSessionIndex);
|
---|
89 | long lastEventTime = timestamp;
|
---|
90 | if( !lastSession.isEmpty() ) {
|
---|
91 | lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
|
---|
92 | }
|
---|
93 | if( timestamp-lastEventTime>timeout ) {
|
---|
94 | sessionIds.add(++lastId);
|
---|
95 | List<WebEvent> newSession = new LinkedList<WebEvent>();
|
---|
96 | newSession.add(event);
|
---|
97 | sequences.add(newSession);
|
---|
98 | } else {
|
---|
99 | lastSession.add(event);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | Console.traceln(""+sequences.size()+ " user sequences found");
|
---|
103 | // prune sequences shorter than min-length
|
---|
104 | for( int i=0; i<sequences.size(); i++ ) {
|
---|
105 | if( sequences.get(i).size()<minLength ) {
|
---|
106 | sequences.remove(i);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | Console.traceln(""+sequences.size()+ " remaining after pruning of sequences shorter than " + minLength);
|
---|
110 | }
|
---|
111 | }
|
---|