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 |
|
---|
17 | public class WeblogParser {
|
---|
18 |
|
---|
19 | private long timeout;
|
---|
20 |
|
---|
21 | private List<List<WebEvent>> sequences;
|
---|
22 |
|
---|
23 | public WeblogParser() {
|
---|
24 | timeout = 3600000; // 1 hour session-timeout as default
|
---|
25 | }
|
---|
26 |
|
---|
27 | public WeblogParser(long timeout) {
|
---|
28 | this.timeout = timeout;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public List<List<WebEvent>> getSequences() {
|
---|
32 | return sequences;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void parseFile(String filename) throws IOException, FileNotFoundException, ParseException {
|
---|
36 | File f = new File(filename);
|
---|
37 | FileReader reader = new FileReader(f);
|
---|
38 | char[] buffer = new char[(int) f.length()];
|
---|
39 | reader.read(buffer);
|
---|
40 | reader.close();
|
---|
41 | String[] lines = (new String(buffer)).split("\n");
|
---|
42 |
|
---|
43 | Map<String, List<Integer>> cookieSessionMap = new HashMap<String, List<Integer>>();
|
---|
44 | int lastId = -1;
|
---|
45 |
|
---|
46 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
47 |
|
---|
48 | sequences = new ArrayList<List<WebEvent>>();
|
---|
49 |
|
---|
50 | for( String line : lines ) {
|
---|
51 | String[] values = line.trim().split(" ");
|
---|
52 |
|
---|
53 | // use cookie as session identifier
|
---|
54 | int cookieStart = values[0].lastIndexOf('.');
|
---|
55 | String cookie = values[0].substring(cookieStart+1);
|
---|
56 | String dateString = values[1].substring(1)+" "+values[2].substring(0, values[2].length()-1);
|
---|
57 | long timestamp = dateFormat.parse(dateString).getTime();
|
---|
58 | String uri = values[3];
|
---|
59 | // String ref = values[4]; // referer is not yet used!
|
---|
60 | List<String> postedVars = new ArrayList<String>();
|
---|
61 | for( int i=5 ; i<values.length ; i++ ) {
|
---|
62 | postedVars.add(values[i]);
|
---|
63 | }
|
---|
64 |
|
---|
65 | WebEvent event = new WebEvent(uri, timestamp, postedVars);
|
---|
66 |
|
---|
67 | // find session and add event
|
---|
68 | List<Integer> sessionIds = cookieSessionMap.get(cookie);
|
---|
69 | if( sessionIds==null ) {
|
---|
70 | sessionIds = new ArrayList<Integer>();
|
---|
71 | // start new session
|
---|
72 | sessionIds.add(++lastId);
|
---|
73 | cookieSessionMap.put(cookie, sessionIds);
|
---|
74 | sequences.add(new LinkedList<WebEvent>());
|
---|
75 | }
|
---|
76 | Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
|
---|
77 | List<WebEvent> lastSession = sequences.get(lastSessionIndex);
|
---|
78 | long lastEventTime = timestamp;
|
---|
79 | if( !lastSession.isEmpty() ) {
|
---|
80 | lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
|
---|
81 | }
|
---|
82 | if( timestamp-lastEventTime>timeout ) {
|
---|
83 | sessionIds.add(++lastId);
|
---|
84 | List<WebEvent> newSession = new LinkedList<WebEvent>();
|
---|
85 | newSession.add(event);
|
---|
86 | sequences.add(newSession);
|
---|
87 | } else {
|
---|
88 | lastSession.add(event);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|