source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/web/WeblogParser.java @ 68

Last change on this file since 68 was 68, checked in by sherbold, 13 years ago
  • changed weblog parser such that session without a minimum length are pruned to remove single page visits from the sessions (e.g., through robots)
  • changed loadSessionsFromClickstream such that the session timeout and the minimum session length are optional parameters
  • Property svn:mime-type set to text/plain
File size: 3.4 KB
Line 
1package de.ugoe.cs.eventbench.web;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.text.ParseException;
8import java.text.SimpleDateFormat;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Map;
14
15import de.ugoe.cs.eventbench.web.data.WebEvent;
16import de.ugoe.cs.util.console.Console;
17
18public 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}
Note: See TracBrowser for help on using the repository browser.